perm filename BOOK.XGP[LSP,JRA]5 blob
sn#333843 filedate 1978-01-18 generic text, type T, neo UTF8
/LMAR=0/XLINE=4/FONT#0=BASL30/FONT#1=BASB30/FONT#5=ASI30[LSP,JRA]/FONT#2=ASI30[LSP,JRA]/FONT#3=SUB/FONT#4=SET1/FONT#6=GRK30/FONT#7=SUP/FONT#8=SPEC[LSP,JRA]/FONT#9=BUCK75/FONT#10=GRFX25[LSP,JRA]/FONT#11=METSB/FONT#12=NGB30/FONT#13=GERM35/FONT#14=MG[LSP,JRA]/FONT#15=GRFX35
␈↓ α←␈↓␈↓␈↓ DPreface i␈↓
␈↓"β␈↓ α←␈↓␈↓ Preface␈↓
␈↓"β␈↓ α←␈↓¬␈↓ β'"...␈α∩it␈α⊃is␈α∩important␈α⊃not␈α∩to␈α⊃lose␈α∩sight␈α⊃of␈α∩the␈α⊃fact␈α∩that␈α⊃there␈α∩is␈α⊃a
␈↓ α←␈↓¬␈↓ β'difference␈αbetween␈αtraining␈αand␈αeducation.␈α If␈αcomputer␈αscience␈αis␈α
a
␈↓ α←␈↓¬␈↓ β'fundamental␈α∃discipline,␈α∃then␈α∃university␈α∃education␈α∃in␈α∃this␈α∀field
␈↓ α←␈↓¬␈↓ β'should␈α∀emphasize␈α∪enduring␈α∀fundamental␈α∪principles␈α∀rather␈α∪than
␈↓ α←␈↓¬␈↓ β'transient current technology."
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ ∧zPeter Wegner, ␈↓αThree Computer Cultures␈↓ [Weg 70]
␈↓"λ␈↓ α←␈↓This␈α⊃text␈α⊃is␈α⊃nominally␈α⊃about␈α∩LISP␈α⊃and␈α⊃data␈α⊃structures.␈α⊃However,␈α∩in␈α⊃the
␈↓ α←␈↓process␈α∞it␈α
covers␈α∞much␈α
broader␈α∞areas␈α
of␈α∞computer␈α
science.␈α∞ The␈α∞author␈α
has
␈↓ α←␈↓long␈α
felt␈α∞that␈α
the␈α∞beginning␈α
student␈α
of␈α∞computer␈α
science␈α∞has␈α
been␈α∞getting␈α
a
␈↓ α←␈↓distorted␈α
and␈α
disjointed␈α
picture␈α
of␈α
the␈α
field.␈α
In␈α
some␈α
ways␈α
this␈α∞confusion␈α
is
␈↓ α←␈↓natural;␈α∪the␈α∪field␈α∀has␈α∪been␈α∪growing␈α∀at␈α∪such␈α∪a␈α∀rapid␈α∪rate␈α∪that␈α∀few␈α∪are
␈↓ α←␈↓prepared␈α∂to␈α∂be␈α⊂judged␈α∂experts␈α∂in␈α∂all␈α⊂areas␈α∂of␈α∂the␈α∂discipline.␈α⊂ The␈α∂current
␈↓ α←␈↓alternative␈α∞seems␈α∞to␈α∂be␈α∞to␈α∞give␈α∞a␈α∂few␈α∞introductory␈α∞courses␈α∂in␈α∞programming
␈↓ α←␈↓and␈α
machine␈α
organization␈α
followed␈αby␈α
relatively␈α
specialized␈α
courses␈α
in␈αmore
␈↓ α←␈↓technical␈α∀areas.␈α∀The␈α∀difficulty␈α∀with␈α∀this␈α∀approach␈α∀is␈α∀that␈α∀much␈α∀of␈α∪the
␈↓ α←␈↓technical␈α≤material␈α≠never␈α≤gets␈α≤related.␈α≠The␈α≤student's␈α≤perspective␈α≠and
␈↓ α←␈↓motivation␈α∩suffers␈α∩in␈α∩the␈α∩process.␈α∩This␈α∩book␈α∩uses␈α∩LISP␈α∩as␈α∩a␈α∪means␈α∩for
␈↓ α←␈↓relating␈α∂topics␈α∂which␈α∂normally␈α∂get␈α∂treated␈α∂in␈α∂several␈α∂separate␈α∂courses.␈α∂The
␈↓ α←␈↓point␈αis␈αnot␈α
that␈αwe␈α␈↓¬can␈↓␈αdo␈α
this␈αin␈αLISP,␈α
but␈αrather␈αthat␈αit␈α
is␈α␈↓¬natural␈↓␈αto␈αdo␈α
it
␈↓ α←␈↓in␈α
LISP.␈α
The␈α
high-level␈α
notation␈α
for␈α
algorithms␈α
is␈α
beneficial␈α
in␈αexplaining
␈↓ α←␈↓␈↓ii Preface␈↓
O␈↓
␈↓"β␈↓ α←␈↓and␈α
understanding␈α
complex␈α
algorithms.␈α The␈α
use␈α
of␈α
abstract␈α
data␈αstructures
␈↓ α←␈↓and␈α
abstract␈α
LISP␈α
programs␈α
shows␈α
the␈α
intent␈α
of␈α
structured␈αprogramming␈α
and
␈↓ α←␈↓step-wise␈α
refinement.␈α
Much␈α
of␈α
the␈α
current␈α
work␈α
in␈α
mathematical␈α
theories␈α
of
␈↓ α←␈↓computation␈αis␈αbased␈αon␈αLISP-like␈αlanguages.␈αThus␈αLISP␈αis␈αa␈αformalism␈αfor
␈↓ α←␈↓describing␈α∂algorithms,␈α∂for␈α∂writing␈α∂programs,␈α∂and␈α∂for␈α∂proving␈α∂properties␈α∞of
␈↓ α←␈↓algorithms.␈α∂ We␈α∂use␈α∂data␈α∂structures␈α⊂as␈α∂the␈α∂main␈α∂thread␈α∂in␈α⊂our␈α∂discussions
␈↓ α←␈↓because␈α∩a␈α∩proper␈α⊃appreciation␈α∩of␈α∩data␈α∩structures␈α⊃as␈α∩abstract␈α∩objects␈α∩is␈α⊃a
␈↓ α←␈↓necessary prerequisite to an understanding of modern computer science.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊂importance␈α⊂of␈α⊂abstraction␈α⊂obviously␈α⊂goes␈α⊂much␈α⊂further␈α⊂than␈α⊂its
␈↓ α←␈↓appearance␈αin␈αLISP.␈αAbstraction␈αhas␈αoften␈αbeen␈αused␈αin␈αother␈α
disciplines␈αas
␈↓ α←␈↓a␈αmeans␈αfor␈αcontrolling␈αcomplexity.␈α
In␈αmathematics,␈αwe␈αfrequently␈αare␈αable␈α
to
␈↓ α←␈↓gain␈αnew␈αinsights␈αby␈αrecasting␈αa␈αparticularly␈αintransigent␈αproblem␈αin␈αa␈αmore
␈↓ α←␈↓general␈α↔setting.␈α↔ Similarly,␈α↔the␈α_intent␈α↔of␈α↔an␈α↔algorithm␈α↔expressed␈α_in␈α↔a
␈↓ α←␈↓high-level␈α∞language␈α∞like␈α∞Fortran␈α∞or␈α∞PL/1␈α∞is␈α∞more␈α∞readily␈α∞apparent␈α∞than␈α∞its
␈↓ α←␈↓machine-language␈α⊗equivalent.␈α⊗ These␈α⊗are␈α⊗both␈α⊗examples␈α⊗of␈α⊗the␈α⊗use␈α∃of
␈↓ α←␈↓abstraction.␈α∂Our␈α∞use␈α∂of␈α∂abstraction␈α∞will␈α∂impinge␈α∞on␈α∂both␈α∂the␈α∞mathematical
␈↓ α←␈↓and␈αthe␈αprogramming␈αaspects.␈α Initially,␈αwe␈αwill␈αtalk␈αabout␈αdata␈αstructures␈αas
␈↓ α←␈↓abstract␈α∩objects␈α∩just␈α∪as␈α∩the␈α∩mathematician␈α∪takes␈α∩the␈α∩natural␈α∪numbers␈α∩as
␈↓ α←␈↓abstract␈α⊂entities.␈α⊂We␈α⊂will␈α⊂attempt␈α⊂to␈α⊂categorize␈α⊂properties␈α⊂common␈α⊃to␈α⊂data
␈↓ α←␈↓structures␈α∞and␈α∞introduce␈α∞notation␈α∂for␈α∞describing␈α∞functions␈α∞defined␈α∂on␈α∞these
␈↓ α←␈↓abstractions.␈α⊂At␈α⊂this␈α⊃level␈α⊂of␈α⊂discussion␈α⊂we␈α⊃are␈α⊂thinking␈α⊂of␈α⊃our␈α⊂LISP-like
␈↓ α←␈↓language␈α
primarily␈αas␈α
a␈αnotational␈α
convenience␈αrather␈α
than␈α
a␈αcomputational
␈↓ α←␈↓device.␈α∃ However,␈α∃after␈α∃a␈α∃certain␈α∃familiarity␈α∃has␈α∃been␈α∃established␈α∃it␈α∀is
␈↓ α←␈↓important␈αto␈αlook␈αat␈αour␈αwork␈αfrom␈αthe␈αviewpoint␈αof␈αcomputer␈αscience.␈αHere
␈↓ α←␈↓we␈α⊂must␈α⊂think␈α⊂of␈α⊂the␈α⊂computational␈α⊂aspects␈α⊂of␈α⊂our␈α⊂notation.␈α⊂We␈α⊂must␈α∂be
␈↓ α←␈↓concerned␈α∩with␈α∩the␈α∩representational␈α∩problems:␈α∩implementation␈α∩on␈α∩realistic
␈↓ α←␈↓machines,␈α∀and␈α∀efficiency␈α∪of␈α∀algorithms␈α∀and␈α∪data␈α∀structures.␈α∀However,␈α∪it
␈↓ α←␈↓cannot␈αbe␈αover-emphasized␈αthat␈αour␈αneed␈αfor␈αunderstanding␈αis␈αbest␈αserved␈αat
␈↓ α←␈↓the␈α⊂higher␈α⊂level␈α⊂of␈α⊂abstraction;␈α∂the␈α⊂advantage␈α⊂of␈α⊂a␈α⊂high-level␈α⊂language␈α∂is
␈↓ α←␈↓notational␈α∩rather␈α∩than␈α∩computational.␈α∩That␈α⊃is,␈α∩it␈α∩allows␈α∩us␈α∩to␈α∩think␈α⊃and
␈↓ α←␈↓represent␈α
our␈α
algorithms␈α∞in␈α
mathematical␈α
terms␈α
rather␈α∞than␈α
in␈α
terms␈α∞of␈α
the
␈↓ α←␈↓machine.␈α It␈αis␈αafter␈αa␈αclear␈αunderstanding␈αof␈αthe␈αproblem␈αis␈αattained␈αthat␈α
we
␈↓ α←␈↓should begin thinking about representation.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αcan␈αexploit␈αthe␈α
analogy␈αwith␈αtraditional␈αmathematics␈αa␈α
bit␈αfurther.
␈↓ α←␈↓When␈α⊗we␈α⊗write␈α⊗␈↓αsqrt(x)␈↓␈α⊗in␈α∃Fortran,␈α⊗for␈α⊗example,␈α⊗we␈α⊗are␈α⊗initially␈α∃only
␈↓ α←␈↓concerned␈α≤with␈α≤␈↓αsqrt␈↓␈α≤as␈α≤a␈α≤mathematical␈α≤function␈α≤defined␈α≤such␈α≠that
␈↓ α←␈↓␈↓αx = sqrt(x)*sqrt(x)␈↓.␈α∂We␈α∂are␈α∂not␈α⊂interested␈α∂in␈α∂the␈α∂specific␈α∂algorithm␈α⊂used␈α∂to
␈↓ α←␈↓approximate␈α
the␈α
function␈α∞intended␈α
in␈α
the␈α
notation.␈α∞Indeed,␈α
thought␈α
of␈α∞as␈α
a
␈↓ α←␈↓mathematical␈α⊃notation,␈α⊃it␈α⊂doesn't␈α⊃matter␈α⊃how␈α⊂␈↓αsqrt␈↓␈α⊃is␈α⊃computed.␈α⊃We␈α⊂might
␈↓ α←␈↓wish␈α
to␈α
prove␈α
some␈α
properties␈α
of␈αthe␈α
algorithm␈α
which␈α
we␈α
are␈α
encoding.␈α If␈α
so,
␈↓ α←␈↓we␈α
would␈α
only␈α∞use␈α
the␈α
mathematical␈α
properties␈α∞of␈α
the␈α
idealized␈α∞square␈α
root
␈↓ α←␈↓function.␈α
Only␈α
later,␈α
after␈α
we␈α
had␈α
convinced␈α
ourselves␈α
of␈α
the␈αcorrect␈α
encoding
␈↓ α←␈↓of␈α⊗our␈α⊗intention␈α⊗in␈α⊗the␈α↔Fortran␈α⊗program,␈α⊗would␈α⊗we␈α⊗worry␈α↔about␈α⊗the
␈↓ α←␈↓computational␈α∂aspects␈α∞of␈α∂the␈α∞Fortran␈α∂implementation␈α∞␈↓αsqrt␈↓.␈α∂The␈α∂typical␈α∞user
␈↓ α←␈↓␈↓␈↓ 2Preface iii␈↓
␈↓"β␈↓ α←␈↓will␈α∪never␈α∪proceed␈α∪deeper␈α∪into␈α∪the␈α∪representation␈α∪than␈α∪this;␈α∪only␈α∀if␈α∪his
␈↓ α←␈↓computation␈α≠is␈α≤lethargic␈α≠due␈α≠to␈α≤inefficiencies,␈α≠or␈α≠inaccurate␈α≤due␈α≠to
␈↓ α←␈↓uncooperative␈α∞approximations,␈α∞will␈α
he␈α∞look␈α∞at␈α
the␈α∞actual␈α∞implementation␈α
of
␈↓ α←␈↓␈↓αsqrt␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Just␈αas␈α
it␈αis␈α
unnecessary␈αto␈α
learn␈αmachine␈α
language␈αto␈α
study␈αnumerical
␈↓ α←␈↓algorithms,␈α∂it␈α∞is␈α∂also␈α∂unnecessary␈α∞to␈α∂learn␈α∞machine␈α∂language␈α∂to␈α∞understand
␈↓ α←␈↓non-numerical␈α∂or␈α∂data␈α∂structure␈α∞processes.␈α∂ We␈α∂make␈α∂a␈α∂distinction␈α∞between
␈↓ α←␈↓data␈α⊗structures␈α⊗and␈α↔storage␈α⊗structures.␈α⊗Data␈α⊗structures␈α↔are␈α⊗abstractions,
␈↓ α←␈↓independent␈αof␈α␈↓¬how␈↓␈αthey␈αare␈αimplemented␈αon␈αa␈αmachine.␈α Data␈αstructures␈αare
␈↓ α←␈↓representations␈α_of␈α↔information␈α_chosen␈α↔to␈α_exhibit␈α↔certain␈α_ordering␈α↔and
␈↓ α←␈↓accessibility␈α≤relationships␈α≤between␈α≤data␈α≤items.␈α≤ Storage␈α≤structures␈α≠are
␈↓ α←␈↓particular␈αimplementations␈αof␈αthe␈αabstract␈αideas.␈α Certainly␈αwe␈αcannot␈αignore
␈↓ α←␈↓storage␈αstructures␈αwhen␈αwe␈αare␈α
deciding␈αupon␈αthe␈αdata␈αstructures␈α
which␈αwill
␈↓ α←␈↓encode␈α⊃the␈α⊃algorithm,␈α⊃but␈α⊃the␈α⊃interesting␈α⊃aspects␈α⊃of␈α⊃the␈α∩representation␈α⊃of
␈↓ α←␈↓information␈α
can␈α∞be␈α
discussed␈α∞at␈α
the␈α∞level␈α
of␈α∞data␈α
structures␈α∞with␈α
no␈α∞loss␈α
of
␈↓ α←␈↓generality.␈α∂ The␈α∂mapping␈α∞of␈α∂data␈α∂structures␈α∞to␈α∂storage␈α∂structures␈α∂is␈α∞usually
␈↓ α←␈↓quite␈α∞machine␈α∞dependent␈α∂and␈α∞we␈α∞are␈α∞more␈α∂interested␈α∞in␈α∞ideas␈α∂than␈α∞coding
␈↓ α←␈↓tricks.␈α∞ We␈α∞will␈α∞see␈α∞that␈α∞it␈α∞is␈α∞possible,␈α∞and␈α∞most␈α∞beneficial,␈α∞to␈α∞structure␈α∞our
␈↓ α←␈↓programs␈α∩such␈α∪that␈α∩there␈α∪is␈α∩a␈α∪very␈α∩clean␈α∪interface␈α∩between␈α∪the␈α∩abstract
␈↓ α←␈↓algorithm␈α⊃and␈α⊃the␈α⊃chosen␈α⊃representation.␈α⊃ That␈α⊃is,␈α⊃there␈α⊃will␈α⊃be␈α⊃a␈α⊃set␈α⊃of
␈↓ α←␈↓representation-manipulating␈α∞programs␈α∞to␈α∞test,␈α∞select␈α∞or␈α∞construct␈α∞elements␈α
of
␈↓ α←␈↓the␈αdomain;␈α
and␈αthere␈α
will␈αbe␈αa␈α
program␈αencoding␈α
the␈αalgorithm.␈αChanges␈α
of
␈↓ α←␈↓representations␈α∃only␈α∃require␈α∃changes␈α∃to␈α∃the␈α∃programs␈α∃which␈α⊗access␈α∃the
␈↓ α←␈↓representation, not to the basic program.
␈↓"β␈↓ α←␈↓␈↓ β'One␈αimportant␈αinsight␈αwhich␈αshould␈αbe␈αcultivated␈αin␈αthis␈αprocess␈αis␈αthe
␈↓ α←␈↓distinction␈α⊃between␈α⊃the␈α⊃concepts␈α⊃of␈α⊂function␈α⊃and␈α⊃algorithm.␈α⊃ The␈α⊃idea␈α⊂of
␈↓ α←␈↓function␈α∞is␈α∞mathematical␈α
and␈α∞is␈α∞independent␈α
of␈α∞any␈α∞notion␈α∞of␈α
computation;
␈↓ α←␈↓the␈α
meaning␈αof␈α
"algorithm"␈α
is␈αcomputational,␈α
the␈α
effect␈αof␈α
an␈αalgorithm␈α
being
␈↓ α←␈↓to␈α
compute␈αa␈α
function.␈α
Thus␈αthere␈α
are␈α
typically␈αmany␈α
algorithms␈α
which␈αwill
␈↓ α←␈↓compute a specific function.
␈↓"β␈↓ α←␈↓␈↓ β'This␈α⊃text␈α⊃is␈α⊃␈↓¬not␈↓␈α⊃meant␈α⊃to␈α⊃be␈α⊃a␈α⊃programming␈α⊃manual␈α⊃for␈α⊃LISP.␈α⊃ A
␈↓ α←␈↓certain␈α∞amount␈α∂of␈α∞time␈α∂is␈α∞spent␈α∂giving␈α∞insights␈α∂into␈α∞techniques␈α∂for␈α∞writing
␈↓ α←␈↓LISP␈α⊃functions.␈α⊃ There␈α⊃are␈α⊃two␈α⊃reasons␈α⊃for␈α⊃this.␈α⊃First,␈α⊃the␈α⊃style␈α⊃of␈α⊂LISP
␈↓ α←␈↓programming␈α∞is␈α∞quite␈α∞different␈α∞from␈α∞that␈α∞of␈α∞"normal"␈α∞programming.␈α
LISP
␈↓ α←␈↓was␈α~one␈α≠of␈α~the␈α~first␈α≠languages␈α~to␈α~exploit␈α≠the␈α~virtues␈α≠of␈α~recursive
␈↓ α←␈↓programming␈αand␈αexplore␈αthe␈αpower␈αof␈αprocedure-valued␈αvariables.␈α Second,
␈↓ α←␈↓we␈α
will␈α
spend␈α
a␈α
great␈α
deal␈α
of␈α
time␈α
discussing␈α
various␈α
levels␈α
of␈α
implementation
␈↓ α←␈↓of␈α∪the␈α∪language.␈α∩LISP␈α∪is␈α∪an␈α∩excellent␈α∪medium␈α∪for␈α∪introducing␈α∩standard
␈↓ α←␈↓techniques␈αin␈αdata␈αstructure␈αmanipulation.␈αTechniques␈αfor␈αimplementation␈α
of
␈↓ α←␈↓recursion,␈α⊂implementation␈α⊂of␈α∂complex␈α⊂data␈α⊂structures,␈α⊂storage␈α∂management,
␈↓ α←␈↓and␈α∃symbol␈α⊗table␈α∃manipulation␈α⊗are␈α∃easily␈α∃motivated␈α⊗in␈α∃the␈α⊗context␈α∃of
␈↓ α←␈↓language␈α∞implementation.␈α∂Many␈α∞of␈α∂these␈α∞standard␈α∂techniques␈α∞first␈α∂arose␈α∞in
␈↓ α←␈↓the␈α∂implementation␈α∂of␈α⊂LISP.␈α∂But␈α∂it␈α∂is␈α⊂pointless␈α∂to␈α∂attempt␈α∂a␈α⊂discussion␈α∂of
␈↓ α←␈↓implementation unless the reader has a thorough grasp of the language.
␈↓ α←␈↓␈↓iv Preface␈↓
O␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Granting␈αthe␈αefficacy␈αof␈αour␈αendeavor␈αin␈αabstraction,␈αwhy␈αstudy␈αLISP?
␈↓ α←␈↓LISP␈α∪is␈α∀at␈α∪least␈α∀fifteen␈α∪years␈α∪old␈α∀and␈α∪many␈α∀new␈α∪languages␈α∀have␈α∪been
␈↓ α←␈↓proposed.␈α∃ The␈α⊗difficulty␈α∃is␈α⊗that␈α∃the␈α⊗appropriate␈α∃combination␈α⊗of␈α∃these
␈↓ α←␈↓features␈α
is␈α
not␈α
present␈α
in␈α
any␈α
other␈α
language.␈α
LISP␈α
unifies␈α∞and␈α
rationalizes
␈↓ α←␈↓many␈α
divergent␈α
formulations␈α
of␈αlanguage␈α
constructs.␈α
One␈α
might␈αsurmise␈α
that
␈↓ α←␈↓a␈α∩new␈α∪language,␈α∩profiting␈α∪from␈α∩LISP's␈α∩experience,␈α∪would␈α∩make␈α∪a␈α∩better
␈↓ α←␈↓pedagogical␈α
tool.␈α A␈α
strong␈αsuccessor␈α
has␈αnot␈α
arrived,␈αand␈α
toy␈α
languages␈αare
␈↓ α←␈↓suspect␈αfor␈αseveral␈αreasons.␈αThe␈αstudent␈αmay␈αsuspect␈αthat␈αhe␈αis␈αa␈αsubject␈αin␈α
a
␈↓ α←␈↓not␈α∪too␈α∪clever␈α∪experiment␈α∪being␈α∪performed␈α∪upon␈α∪him␈α∪by␈α∪his␈α∩instructor.
␈↓ α←␈↓Having␈α∩a␈α∪backlog␈α∩of␈α∪fifteen␈α∩years␈α∩of␈α∪experience␈α∩and␈α∪example␈α∩programs
␈↓ α←␈↓should␈αdo␈αmuch␈αto␈αalleviate␈αthis␈αdiscomfort.␈α The␈αdevelopment␈αof␈αLISP␈αalso
␈↓ α←␈↓shows␈α⊂many␈α⊂of␈α⊂the␈α⊂mistakes␈α∂that␈α⊂the␈α⊂original␈α⊂implementors␈α⊂and␈α∂designers
␈↓ α←␈↓made.␈α
We␈α
will␈α
point␈α
out␈α
the␈α
flaws␈α
and␈α
pitfalls␈α
awaiting␈α
the␈αunwary␈α
language
␈↓ α←␈↓designer.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αclaim␈α
the␈αmore␈αinteresting␈α
aspects␈αof␈αLISP␈α
for␈αstudents␈αof␈α
computer
␈↓ α←␈↓science␈αlie␈αnot␈αin␈αits␈αfeatures␈αas␈αa␈αprogramming␈αlanguage,␈αbut␈αin␈αwhat␈αit␈αcan
␈↓ α←␈↓show␈αabout␈αthe␈α␈↓¬structure␈↓␈αof␈αcomputer␈αscience.␈α There␈αis␈αa␈αrapidly␈αexpanding
␈↓ α←␈↓body␈α⊃of␈α⊃knowledge␈α⊃unique␈α⊃to␈α⊃computer␈α⊃science,␈α⊃neither␈α⊃mathematical␈α⊂nor
␈↓ α←␈↓engineering␈αper␈α
se.␈α Much␈α
of␈αthis␈αarea␈α
is␈αpresented␈α
most␈αclearly␈α
by␈αstudying
␈↓ α←␈↓LISP.
␈↓"β␈↓ α←␈↓␈↓ β'Again␈α∪there␈α∪are␈α∪two␈α∪ways␈α∪to␈α∪look␈α∪at␈α∪a␈α∪high␈α∪level␈α∪language:␈α∪as␈α∪a
␈↓ α←␈↓mathematical␈α
formalism,␈α
and␈α
as␈α
a␈α
programming␈α
language.␈α
LISP␈α
is␈α
a␈α
better
␈↓ α←␈↓formalism␈α⊂than␈α⊂most␈α⊂of␈α⊂its␈α⊂mathematical␈α⊂rivals␈α⊂because␈α⊂there␈α⊃is␈α⊂sufficient
␈↓ α←␈↓organizational␈α
complexity␈α
present␈αin␈α
LISP␈α
so␈α
as␈αto␈α
make␈α
its␈αimplementation␈α
a
␈↓ α←␈↓realistic␈α∀computer␈α∀science␈α∀task␈α∪and␈α∀not␈α∀just␈α∀an␈α∀interesting␈α∪mathematical
␈↓ α←␈↓curiosity.␈α∪ Much␈α∪of␈α∪the␈α∪power␈α∪of␈α∩LISP␈α∪lies␈α∪in␈α∪its␈α∪simplicity.␈α∪ The␈α∩data
␈↓ α←␈↓structures␈αare␈αrich␈αenough␈αto␈αeasily␈αdescribe␈αsophisticated␈αalgorithms␈αbut␈αnot
␈↓ α←␈↓so␈α
rich␈α
as␈α
to␈α
become␈αobfuscatory.␈α
Most␈α
every␈α
aspect␈α
of␈α
the␈αimplementation␈α
of
␈↓ α←␈↓LISP␈αand␈αits␈α
translators␈αhas␈αimmediate␈α
implications␈αto␈αthe␈αimplementation␈α
of
␈↓ α←␈↓other languages and to the design of programming languages in general.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α⊃will␈α⊂describe␈α⊃language␈α⊂translators␈α⊃(interpreters␈α⊂and␈α⊃compilers)␈α⊂as
␈↓ α←␈↓LISP␈α∂functions.␈α∂ The␈α∂structure␈α∂of␈α∂these␈α∂translators␈α∂when␈α∂exposed␈α∂as␈α∂LISP
␈↓ α←␈↓functions␈α⊂aids␈α⊂immensely␈α⊂in␈α⊂understanding␈α⊂the␈α⊂essential␈α⊂character␈α⊃of␈α⊂such
␈↓ α←␈↓translators.␈α This␈αis␈αpartly␈αdue␈αto␈αthe␈αsimplicity␈αof␈αthe␈αlanguage,␈αbut␈αperhaps
␈↓ α←␈↓more␈α⊃due␈α⊂to␈α⊃our␈α⊂ability␈α⊃to␈α⊃go␈α⊂right␈α⊃to␈α⊂the␈α⊃essential␈α⊃translating␈α⊂algorithm
␈↓ α←␈↓without becoming bogged down in details of syntax.
␈↓"β␈↓ α←␈↓␈↓ β'LISP␈α∩has␈α∩very␈α∩important␈α∩implications␈α∩in␈α∩the␈α∩field␈α∩of␈α⊃programming
␈↓ α←␈↓language␈αsemantics,␈α
and␈αis␈αthe␈α
dominant␈αlanguage␈αin␈α
the␈αclosely␈αrelated␈α
study
␈↓ α←␈↓of␈αprovability␈αof␈αproperties␈αof␈αprograms.␈α The␈αidea␈αof␈αproving␈α
properties␈αof
␈↓ α←␈↓programs␈α⊗has␈α⊗been␈α⊗around␈α⊗for␈α↔a␈α⊗very␈α⊗long␈α⊗time.␈α⊗Goldstein␈α↔and␈α⊗von
␈↓ α←␈↓Neumann␈α↔were␈α↔aware␈α↔of␈α↔the␈α↔practical␈α↔benefits␈α↔of␈α↔such␈α↔endeavors.␈α↔J.
␈↓ α←␈↓McCarthy's␈α∃work␈α⊗in␈α∃LISP␈α⊗and␈α∃the␈α∃Theory␈α⊗of␈α∃Computation␈α⊗sought␈α∃to
␈↓ α←␈↓establish␈α⊂formalisms␈α⊂and␈α⊂rules␈α∂of␈α⊂inference␈α⊂for␈α⊂reasoning␈α⊂about␈α∂programs.
␈↓ α←␈↓However,␈α∂the␈α∂working␈α∂programmers␈α∞recognized␈α∂debugging␈α∂as␈α∂the␈α∂only␈α∞tool
␈↓ α←␈↓␈↓␈↓ <Preface v␈↓
␈↓"β␈↓ α←␈↓with␈α which␈α to␈α generate␈α!a␈α "correct"␈α program,␈α though␈α!clearly␈α the
␈↓ α←␈↓non-occurrence␈α∂of␈α∂bugs␈α∂is␈α∂no␈α∂guarantee␈α∂of␈α∂correctness.␈α∂ Until␈α⊂very␈α∂recently
␈↓ α←␈↓techniques␈α∞for␈α
establishing␈α∞correctness␈α
of␈α∞practical␈α
programs␈α∞simply␈α∞did␈α
not
␈↓ α←␈↓exist.
␈↓"β␈↓ α←␈↓␈↓ β'A recent set of events is beginning to change this.
␈↓"λ␈↓ α←␈↓␈↓↓1.␈↓␈α
Programs␈α
are␈αbecoming␈α
so␈α
large␈αand␈α
complex␈α
that,␈αeven␈α
though␈α
we␈αwrite
␈↓ α←␈↓␈↓ β∂in␈α∂a␈α∂high-level␈α∂language,␈α∂our␈α∞intuitions␈α∂are␈α∂not␈α∂sufficient␈α∂to␈α∂sustain␈α∞us
␈↓ α←␈↓␈↓ β∂when␈α∞we␈α
try␈α∞to␈α
find␈α∞bugs.␈α
We␈α∞are␈α
literally␈α∞being␈α
forced␈α∞to␈α∞look␈α
beyond
␈↓ α←␈↓␈↓ β∂debugging.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓␈α∂The␈α∞formalisms␈α∂are␈α∂maturing.␈α∞We␈α∂know␈α∞a␈α∂lot␈α∂more␈α∞about␈α∂how␈α∂to␈α∞write
␈↓ α←␈↓␈↓ β∂"structured␈α
programs";␈α
we␈α
know␈α
how␈α
to␈α
design␈α
languages␈α
whose␈α
constructs
␈↓ α←␈↓␈↓ β∂are␈αmore␈αamenable␈αto␈αproof␈αtechniques.␈α And␈αmost␈αimportantly,␈αthe␈αtools
␈↓ α←␈↓␈↓ β∂we␈α→need␈α→for␈α→expressing␈α→properties␈α→of␈α→programs␈α→are␈α→finally␈α→being
␈↓ α←␈↓␈↓ β∂developed.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓␈α∀The␈α∀development␈α∀of␈α∀on-line␈α∪techniques.␈α∀The␈α∀on-line␈α∀system,␈α∀with␈α∪its
␈↓ α←␈↓␈↓ β∂sophisticated␈α∩display␈α∩editors,␈α∩debuggers␈α∩and␈α∩file␈α∩handlers,␈α∩is␈α∩the␈α⊃only
␈↓ α←␈↓␈↓ β∂reason␈α⊃that␈α⊃the␈α⊃traditional␈α⊃means␈α⊃of␈α⊃construction␈α⊃and␈α⊃modification␈α⊃of
␈↓ α←␈↓␈↓ β∂complex␈α∞programs␈α∞and␈α∞systems␈α∞has␈α
been␈α∞able␈α∞to␈α∞survive␈α∞this␈α∞long.␈α
The
␈↓ α←␈↓␈↓ β∂interactive␈α∪experience␈α∪can␈α∪now␈α∩be␈α∪adapted␈α∪to␈α∪program␈α∪verifiers␈α∩and
␈↓ α←␈↓␈↓ β∂synthesizers.
␈↓"β␈↓ α←␈↓␈↓ β'This␈α∪view␈α∩of␈α∪the␈α∩programming␈α∪process␈α∩blends␈α∪well␈α∩with␈α∪the␈α∩LISP
␈↓ α←␈↓philosophy.␈α
We␈αwill␈α
show␈αthat␈α
the␈α
most␈αnatural␈α
way␈αto␈α
write␈αLISP␈α
programs
␈↓ α←␈↓is␈α"structured"␈αin␈αthe␈αbest␈αsense␈αof␈αthe␈αword,␈αbeing␈αclean␈αin␈αcontrol␈αstructure,
␈↓ α←␈↓concise␈α∞by␈α
not␈α∞attempting␈α
to␈α∞do␈α∞too␈α
much,␈α∞and␈α
independent␈α∞of␈α∞a␈α
particular
␈↓ α←␈↓data representation.
␈↓"β␈↓ α←␈↓␈↓ β'Many␈α
of␈αthe␈α
existing␈α
techniques␈αfor␈α
establishing␈α
correctness␈αoriginated
␈↓ α←␈↓in␈α⊗McCarthy's␈α⊗investigations␈α⊗of␈α⊗LISP;␈α∃and␈α⊗some␈α⊗very␈α⊗recent␈α⊗work␈α∃on
␈↓ α←␈↓mathematical␈α
models␈α
for␈α
programming␈αlanguages␈α
is␈α
easily␈α
motivated␈α
from␈αa
␈↓ α←␈↓discussion of LISP.
␈↓"β␈↓ α←␈↓␈↓ β'LISP␈αis␈αthe␈αstarting␈α
point␈αfor␈αthose␈αinterested␈αin␈α
Artificial␈αIntelligence.
␈↓ α←␈↓It␈α∃is␈α∃no␈α∃longer␈α∃the␈α∃"research"␈α∃language,␈α∃but␈α∃has␈α∃become␈α∃the␈α∃"systems"
␈↓ α←␈↓language␈αfor␈αA.I.␈αToday's␈αresearch␈αlanguages␈αare␈αbuilt␈αon␈αLISP,␈αusing␈αLISP
␈↓ α←␈↓as a machine language.
␈↓"β␈↓ α←␈↓␈↓ β'Finally␈αthere␈αare␈αcertain␈αproperties␈αof␈αLISP-like␈αlanguages␈αwhich␈αmake
␈↓ α←␈↓them␈α∩the␈α∩natural␈α∩candidate␈α∩for␈α∩interactive␈α∩program␈α∩specification.␈α∩ In␈α⊃the
␈↓ α←␈↓chapter␈α∞on␈α∞implications␈α∞of␈α∞LISP␈α∞we␈α∞will␈α∞characterize␈α∞"LISP-like"␈α∞and␈α
show
␈↓ α←␈↓how interactive methods can be developed.
␈↓"β␈↓ α←␈↓␈↓ β'This␈α⊂text␈α∂is␈α⊂primarily␈α∂designed␈α⊂for␈α∂undergraduates␈α⊂and␈α⊂therefore␈α∂an
␈↓ α←␈↓attempt␈α
is␈α∞made␈α
to␈α∞make␈α
it␈α
self-contained.␈α∞There␈α
are␈α∞basically␈α
five␈α∞areas␈α
in
␈↓ α←␈↓which␈α
to␈α
partition␈α
the␈α
topics:␈α
the␈α
mechanics␈α
of␈α
the␈α
language,␈α
the␈αevaluation
␈↓ α←␈↓of␈αexpressions␈αin␈αLISP,␈αthe␈αstatic␈αstructure␈αof␈αLISP,␈αthe␈αdynamic␈αstructure␈αof
␈↓ α←␈↓␈↓vi Preface␈↓
O␈↓
␈↓"β␈↓ α←␈↓LISP,␈α∩and␈α∩the␈α∩efficient␈α∩representation␈α∩of␈α∩data␈α∩structures␈α∪and␈α∩algorithms.
␈↓ α←␈↓Each␈α
area␈α∞builds␈α
on␈α∞the␈α
previous.␈α
Taken␈α∞as␈α
a␈α∞group␈α
these␈α∞topics␈α
introduce
␈↓ α←␈↓much of what is interesting computer science.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
first␈αarea␈α
develops␈α
the␈αprogramming␈α
philosophy␈αof␈α
LISP:␈α
the␈αuse
␈↓ α←␈↓of␈α∂data␈α∂structures␈α∞in␈α∂programming;␈α∂the␈α∞language␈α∂primitives,␈α∂recursion,␈α∞and
␈↓ α←␈↓other␈α
control␈α
structures.␈α The␈α
second␈α
area,␈α
involveing␈αa␈α
careful␈α
study␈α
of␈αthe
␈↓ α←␈↓meaning␈α∞of␈α∞evaluation␈α∂in␈α∞LISP,␈α∞gives␈α∞insights␈α∂into␈α∞other␈α∞languages␈α∂and␈α∞to
␈↓ α←␈↓the␈α⊂general␈α⊂question␈α⊂of␈α⊃implementation.␈α⊂The␈α⊂next␈α⊂two␈α⊂areas␈α⊃are␈α⊂involved
␈↓ α←␈↓with␈α⊃implementation.␈α⊃The␈α⊃section␈α∩on␈α⊃static␈α⊃structure␈α⊃deals␈α⊃with␈α∩the␈α⊃basic
␈↓ α←␈↓organization␈α
of␈α
memory␈α
for␈α
a␈α
LISP␈α
machine -- be␈α
it␈α
hardware␈α∞or␈α
simulated
␈↓ α←␈↓in␈αsoftware.␈αThe␈αdynamics␈αof␈αLISP␈αdiscusses␈αthe␈αprimitive␈αcontrol␈αstructures
␈↓ α←␈↓necessary␈α∂for␈α∞implementation␈α∂of␈α∞the␈α∂LISP␈α∞control␈α∂structures␈α∂and␈α∞procedure
␈↓ α←␈↓calls.␈α∀LISP␈α∃compilers␈α∀are␈α∃discussed␈α∀here.␈α∀ The␈α∃final␈α∀section␈α∃relates␈α∀our
␈↓ α←␈↓discussion␈α
of␈αLISP␈α
and␈α
its␈αimplementation␈α
to␈α
the␈αmore␈α
traditional␈αmaterial␈α
of
␈↓ α←␈↓a␈α
data␈α
structures␈αcourse.␈α
We␈α
discuss␈αthe␈α
problems␈α
of␈α
efficient␈αrepresentation
␈↓ α←␈↓of␈α→data␈α→structures.␈α→By␈α→this␈α~point␈α→the␈α→student␈α→should␈α→have␈α~a␈α→better
␈↓ α←␈↓understanding␈α⊃of␈α⊂the␈α⊃uses␈α⊂of␈α⊃data␈α⊂structures␈α⊃and␈α⊂should␈α⊃be␈α⊃motivated␈α⊂to
␈↓ α←␈↓examine these issues with a better understanding.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α
large␈αcollection␈α
of␈αproblems␈α
has␈αbeen␈α
included.␈αThe␈α
reader␈α
is␈αurged
␈↓ α←␈↓to␈αdo␈αas␈α
many␈αas␈αpossible.␈α
The␈αproblems␈αare␈α
mostly␈αnon-trivial;␈αthey␈α
attempt
␈↓ α←␈↓to␈α∞be␈α∞realistic,␈α
introducing␈α∞some␈α∞new␈α
information␈α∞which␈α∞the␈α∞readers␈α
should
␈↓ α←␈↓be␈α∪able␈α∪to␈α∪discover␈α∪themselves.␈α∩There␈α∪are␈α∪also␈α∪a␈α∪few␈α∪rather␈α∩substantial
␈↓ α←␈↓projects.␈α At␈αleast␈αone␈αshould␈αbe␈αattempted.␈α There␈αis␈αa␈αsignificant␈αdifference
␈↓ α←␈↓between␈α⊂being␈α⊂able␈α⊃to␈α⊂program␈α⊂small␈α⊂problems␈α⊃and␈α⊂being␈α⊂able␈α⊃to␈α⊂handle
␈↓ α←␈↓large␈α∞projects.␈α
Small␈α∞programming␈α∞projects␈α
can␈α∞be␈α
accomplished␈α∞in␈α∞spite␈α
of
␈↓ α←␈↓any␈α⊃admonitions␈α⊃about␈α∩"good␈α⊃programming␈α⊃style".␈α∩ A␈α⊃large␈α⊃project␈α∩is␈α⊃an
␈↓ α←␈↓effective␈α∩demonstration␈α∩of␈α∪the␈α∩need␈α∩for␈α∩elegant␈α∪programming␈α∩techniques.
␈↓ α←␈↓The␈α∃text␈α∃is␈α∃large␈α∃and␈α∃covers␈α∀much␈α∃more␈α∃than␈α∃is␈α∃recommended␈α∃for␈α∀a
␈↓ α←␈↓one-semester course. A typical one semester course on data structures covers:
␈↓"∀␈↓ α←␈↓␈↓ βWChapter 1: all
␈↓" ␈↓ α←␈↓␈↓ βWChapter 2: without 2.4, 2.5, and 2.10.
␈↓" ␈↓ α←␈↓␈↓ βWChapter 3: without the mathematical aspects of 3.13
␈↓" ␈↓ α←␈↓␈↓ βWChapter 4: without 4.7, 4.8, and the mathematical aspects of 4.11
␈↓" ␈↓ α←␈↓␈↓ βWChapter 5: without 5.8, 5.19, and 5.20
␈↓" ␈↓ α←␈↓␈↓ βWChapter 6: without 6.8, and 6.12 through 6.20
␈↓" ␈↓ α←␈↓␈↓ βWChapter 7: without 7.5, 7.6, and 7.10 through 7.14
␈↓" ␈↓ α←␈↓␈↓ βWChapter 8 is also optional.
␈↓"∀␈↓ α←␈↓␈↓ β'If␈α∞a␈α∞good␈α∞interactive␈α∞LISP␈α
implementation␈α∞is␈α∞available,␈α∞then␈α∞the␈α
pace
␈↓ α←␈↓can␈α⊂be␈α⊂quickened␈α⊂and␈α⊂the␈α⊂projects␈α⊂enlarged.␈α⊂ However,␈α⊂if␈α⊂only␈α⊂a␈α⊂poor␈α∂or
␈↓ α←␈↓mediocre␈α⊂implementation␈α∂is␈α⊂accessible,␈α∂then␈α⊂the␈α∂course␈α⊂time␈α∂is␈α⊂better␈α∂spent
␈↓ α←␈↓without␈α∩␈↓¬any␈↓␈α∩actual␈α∩programming,␈α∩or␈α∩the␈α∩course␈α∩should␈α∩be␈α∪augmented␈α∩to
␈↓ α←␈↓include␈α∀an␈α∀implementation␈α∀laboratory.␈α∀ LISP␈α∀is␈α∀an␈α∀interactive␈α∀language;
␈↓ α←␈↓␈↓␈↓ *Preface vii␈↓
␈↓"β␈↓ α←␈↓attempts␈α∞at␈α∂other␈α∞modes␈α∂of␈α∞operation␈α∞do␈α∂a␈α∞disservice␈α∂to␈α∞both␈α∂the␈α∞language
␈↓ α←␈↓and the user.
␈↓"β␈↓ α←␈↓␈↓ β'Finally␈α
a␈α
note␈αon␈α
the␈α
structure␈αof␈α
the␈α
text.␈αThe␈α
emphasis␈α
flows␈αfrom␈α
the
␈↓ α←␈↓abstract␈αto␈αthe␈αspecific,␈αbeginning␈αwith␈αa␈αdescription␈αof␈αthe␈αdomain␈α
of␈αLISP
␈↓ α←␈↓functions␈α⊃and␈α∩the␈α⊃operations␈α∩defined␈α⊃over␈α∩that␈α⊃domain,␈α∩and␈α⊃moves␈α∩to␈α⊃a
␈↓ α←␈↓discussion␈α
of␈α∞the␈α
details␈α∞of␈α
efficient␈α∞implementation␈α
of␈α∞LISP-like␈α
languages.
␈↓ α←␈↓The␈α∃practical-minded␈α∃programmer␈α∃might␈α∃be␈α∃put-off␈α∃by␈α∃the␈α∃"irrelevant"
␈↓ α←␈↓theory␈α⊂and␈α⊂the␈α⊂theoretical-minded␈α⊂mathematician␈α⊂might␈α⊂be␈α⊂put-off␈α⊃by␈α⊂the
␈↓ α←␈↓"irrelevant"␈α⊂details␈α⊂of␈α⊂implementation.␈α⊂If␈α⊂you␈α⊂lie␈α⊂somewhere␈α⊂between␈α∂these
␈↓ α←␈↓two extremes, then welcome.
␈↓ α←␈↓␈↓␈↓ λzCONTENTS 447␈↓
␈↓"β␈↓ α←␈↓↓␈↓ ∧aT A B L E O F C O N T E N T S
␈↓ α←␈↓↓PREFACE␈↓
Ei
␈↓ α←␈↓↓CHAPTER␈↓ |PAGE␈↓
␈↓ α←␈↓␈↓ ββ1␈↓ β3SYMBOLIC EXPRESSIONS␈↓
@1
␈↓ α←␈↓␈↓ βc1.1␈↓ ∧+Introduction␈↓ ¬← . . . . . . . . . . . . . . . . . . . . . . . ␈↓
6 1
␈↓ α←␈↓␈↓ βc1.2␈↓ ∧+Symbolic Expressions: Abstract Data Structures␈↓ λ← . . . . . . . . ␈↓
6 5
␈↓ α←␈↓␈↓ βc1.3␈↓ ∧+Trees: Representations of Symbolic expressions␈↓ λ← . . . . . . . . ␈↓
6 8
␈↓ α←␈↓␈↓ βc1.4␈↓ ∧+Primitive Functions␈↓ ε' . . . . . . . . . . . . . . . . . . . ␈↓
' 10
␈↓ α←␈↓␈↓ βc1.5␈↓ ∧+Predicates and Conditional Expressions␈↓ λG . . . . . . . . ␈↓
' 18
␈↓ α←␈↓␈↓ βc1.6␈↓ ∧+Sequences: Abstract Data Structures␈↓ π␈ . . . . . . . . . . . ␈↓
' 26
␈↓ α←␈↓␈↓ βc1.7␈↓ ∧+Lists: Representations of Sequences␈↓ π␈ . . . . . . . . . . . ␈↓
' 31
␈↓ α←␈↓␈↓ βc1.8␈↓ ∧+A Respite␈↓ ¬; . . . . . . . . . . . . . . . . . . . . . . . ␈↓
' 38
␈↓ α←␈↓␈↓ βc1.9␈↓ ∧+Becoming an Expert␈↓ εK . . . . . . . . . . . . . . . . . . ␈↓
' 43
␈↓ α←␈↓␈↓ ββ2␈↓ β3APPLICATIONS OF LISP␈↓
153
␈↓ α←␈↓␈↓ βc2.1␈↓ ∧+Introduction␈↓ ¬← . . . . . . . . . . . . . . . . . . . . . . ␈↓
' 53
␈↓ α←␈↓␈↓ βc2.2␈↓ ∧+Examples of LISP Applications␈↓ π[ . . . . . . . . . . . . ␈↓
' 56
␈↓ α←␈↓␈↓ βc2.3␈↓ ∧+Differentiation␈↓ εβ . . . . . . . . . . . . . . . . . . . . . ␈↓
' 57
␈↓ α←␈↓␈↓ βc2.4␈↓ ∧+Tree Searching␈↓ εβ . . . . . . . . . . . . . . . . . . . . . ␈↓
' 66
␈↓ α←␈↓␈↓ βc2.5␈↓ ∧+Data Bases␈↓ ¬; . . . . . . . . . . . . . . . . . . . . . . . ␈↓
' 69
␈↓ α←␈↓␈↓ βc2.6␈↓ ∧+Algebra of Polynomials␈↓ εo . . . . . . . . . . . . . . . . ␈↓
' 75
␈↓ α←␈↓␈↓ βc2.7␈↓ ∧+Evaluation of Polynomials␈↓ π∪ . . . . . . . . . . . . . . . ␈↓
' 79
␈↓ α←␈↓␈↓ βc2.8␈↓ ∧+The Great Progenitors␈↓ εK . . . . . . . . . . . . . . . . . . ␈↓
' 91
␈↓ α←␈↓␈↓ βc2.9␈↓ ∧+Another Respite␈↓ εβ . . . . . . . . . . . . . . . . . . . . . ␈↓
' 92
␈↓ α←␈↓␈↓ βc2.10␈↓ ∧+Proving Properties of Programs␈↓ π[ . . . . . . . . . . . . ␈↓
' 94
␈↓ α←␈↓␈↓ ββ3␈↓ β3EVALUATION OF LISP EXPRESSIONS␈↓
197
␈↓ α←␈↓␈↓ βc3.1␈↓ ∧+Introduction␈↓ ¬← . . . . . . . . . . . . . . . . . . . . . . ␈↓
' 97
␈↓ α←␈↓␈↓ βc3.2␈↓ ∧+S-expr Translation of LISP Expressions␈↓ λG . . . . . . . . ␈↓
_ 104
␈↓ α←␈↓␈↓ βc3.3␈↓ ∧+Symbol Tables␈↓ εβ . . . . . . . . . . . . . . . . . . . . ␈↓
_ 107
␈↓ α←␈↓␈↓ βc3.4␈↓ ∧+␈↓αλ␈↓-notation␈↓ ¬; . . . . . . . . . . . . . . . . . . . . . . . ␈↓
_ 110
␈↓ α←␈↓␈↓ βc3.5␈↓ ∧+Mechanization of Evaluation␈↓ π7 . . . . . . . . . . . . . ␈↓
_ 114
␈↓ α←␈↓␈↓ βc3.6␈↓ ∧+Examples of ␈↓αeval␈↓␈↓ εβ . . . . . . . . . . . . . . . . . . . . ␈↓
_ 118
␈↓ α←␈↓␈↓ βc3.7␈↓ ∧+Variables␈↓ ¬; . . . . . . . . . . . . . . . . . . . . . . . ␈↓
_ 128
␈↓ α←␈↓␈↓448 CONTENTS␈↓
O␈↓
␈↓"β␈↓ α←␈↓␈↓ βc3.8␈↓ ∧+Environments and Bindings␈↓ π∪ . . . . . . . . . . . . . . ␈↓
_ 131
␈↓ α←␈↓␈↓ βc3.9␈↓ ∧+␈↓αlabel␈↓␈↓ ∧s . . . . . . . . . . . . . . . . . . . . . . . . . . ␈↓
_ 136
␈↓ α←␈↓␈↓ βc3.10␈↓ ∧+Functional Arguments and Functional Values␈↓ λ/ . . . . . . . . ␈↓
_ 137
␈↓ α←␈↓␈↓ βc3.11␈↓ ∧+Binding Strategies and Implementations␈↓ λG . . . . . . . . ␈↓
_ 149
␈↓ α←␈↓␈↓ βc3.12␈↓ ∧+Special Forms and Macros␈↓ π∪ . . . . . . . . . . . . . . ␈↓
_ 154
␈↓ α←␈↓␈↓ βc3.13␈↓ ∧+Review and Reflection␈↓ εK . . . . . . . . . . . . . . . . . ␈↓
_ 157
␈↓ α←␈↓␈↓ ββ4␈↓ β3IMPERATIVE CONSTRUCTS IN LISP␈↓
"184
␈↓ α←␈↓␈↓ βc4.1␈↓ ∧+Introduction␈↓ ¬← . . . . . . . . . . . . . . . . . . . . . ␈↓
_ 184
␈↓ α←␈↓␈↓ βc4.2␈↓ ∧+The ␈↓αprog␈↓-feature␈↓ εβ . . . . . . . . . . . . . . . . . . . . ␈↓
_ 186
␈↓ α←␈↓␈↓ βc4.3␈↓ ∧+Alternatives to ␈↓αprog␈↓␈↓ ε' . . . . . . . . . . . . . . . . . . . ␈↓
_ 194
␈↓ α←␈↓␈↓ βc4.4␈↓ ∧+Extensions to ␈↓αeval␈↓␈↓ ε' . . . . . . . . . . . . . . . . . . . ␈↓
_ 197
␈↓ α←␈↓␈↓ βc4.5␈↓ ∧+Non-recursive Control Structures␈↓ π[ . . . . . . . . . . . . ␈↓
_ 198
␈↓ α←␈↓␈↓ βc4.6␈↓ ∧+␈↓αeval␈↓ with Explicit Access␈↓ εo . . . . . . . . . . . . . . . . ␈↓
_ 199
␈↓ α←␈↓␈↓ βc4.7␈↓ ∧+␈↓αeval␈↓ with Explicit Control␈↓ π∪ . . . . . . . . . . . . . . ␈↓
_ 207
␈↓ α←␈↓␈↓ βc4.8␈↓ ∧+An Evaluator for ␈↓αprog␈↓␈↓ εK . . . . . . . . . . . . . . . . . ␈↓
_ 211
␈↓ α←␈↓␈↓ βc4.9␈↓ ∧+Alternatives to ␈↓αeval␈↓␈↓ ε' . . . . . . . . . . . . . . . . . . . ␈↓
_ 220
␈↓ α←␈↓␈↓ βc4.10␈↓ ∧+Function Definitions␈↓ εK . . . . . . . . . . . . . . . . . ␈↓
_ 224
␈↓ α←␈↓␈↓ βc4.11␈↓ ∧+Rapprochement: In Retrospect␈↓ π7 . . . . . . . . . . . . . ␈↓
_ 227
␈↓ α←␈↓␈↓ βc4.12␈↓ ∧+LISP Machines␈↓ εβ . . . . . . . . . . . . . . . . . . . . ␈↓
_ 235
␈↓ α←␈↓␈↓ ββ5␈↓ β3THE STATIC STRUCTURE OF LISP␈↓
"244
␈↓ α←␈↓␈↓ βc5.1␈↓ ∧+Introduction␈↓ ¬← . . . . . . . . . . . . . . . . . . . . . ␈↓
_ 244
␈↓ α←␈↓␈↓ βc5.2␈↓ ∧+Representation of S-expressions␈↓ π[ . . . . . . . . . . . . ␈↓
_ 245
␈↓ α←␈↓␈↓ βc5.3␈↓ ∧+Representation of LISP Primitives␈↓ π␈ . . . . . . . . . . ␈↓
_ 249
␈↓ α←␈↓␈↓ βc5.4␈↓ ∧+AMBIT/G␈↓ ¬; . . . . . . . . . . . . . . . . . . . . . . . ␈↓
_ 253
␈↓ α←␈↓␈↓ βc5.5␈↓ ∧+A Few Programming Techniques␈↓ π[ . . . . . . . . . . . . ␈↓
_ 254
␈↓ α←␈↓␈↓ βc5.6␈↓ ∧+Symbol Tables and Property-lists␈↓ π[ . . . . . . . . . . . . ␈↓
_ 256
␈↓ α←␈↓␈↓ βc5.7␈↓ ∧+Property-list Functions␈↓ εK . . . . . . . . . . . . . . . . . ␈↓
_ 261
␈↓ α←␈↓␈↓ βc5.8␈↓ ∧+An ␈↓αeval␈↓ for Property-lists␈↓ εo . . . . . . . . . . . . . . . . ␈↓
_ 262
␈↓ α←␈↓␈↓ βc5.9␈↓ ∧+Representation of Property-lists␈↓ π7 . . . . . . . . . . . . . ␈↓
_ 265
␈↓ α←␈↓␈↓ βc5.10␈↓ ∧+A Picture of the Atom ␈↓αNIL␈↓␈↓ π∪ . . . . . . . . . . . . . . ␈↓
_ 270
␈↓ α←␈↓␈↓ βc5.11␈↓ ∧+Input/Output: ␈↓αread␈↓ and ␈↓αprint␈↓␈↓ π7 . . . . . . . . . . . . . ␈↓
_ 271
␈↓ α←␈↓␈↓ βc5.12␈↓ ∧+Table Searching: Hashing␈↓ π∪ . . . . . . . . . . . . . . ␈↓
_ 275
␈↓ α←␈↓␈↓ βc5.13␈↓ ∧+A First Look At ␈↓αcons␈↓␈↓ εK . . . . . . . . . . . . . . . . . ␈↓
_ 281
␈↓ α←␈↓␈↓ βc5.14␈↓ ∧+Storage Management: Garbage Collection␈↓ λG . . . . . . . . ␈↓
_ 282
␈↓ α←␈↓␈↓ βc5.15␈↓ ∧+A Simple LISP Garbage Collector␈↓ π␈ . . . . . . . . . . ␈↓
_ 284
␈↓ α←␈↓␈↓ βc5.16␈↓ ∧+A Review of the Structure of the LISP Machine␈↓ λS . . . . . . . ␈↓
_ 289
␈↓ α←␈↓␈↓ βc5.17␈↓ ∧+Implementations of Binding␈↓ π∪ . . . . . . . . . . . . . . ␈↓
_ 289
␈↓ α←␈↓␈↓ βc5.18␈↓ ∧+Stack Implementation of a LISP subset: Deep
␈↓ α←␈↓␈↓ ¬GBound␈↓ ε' . . . . . . . . . . . . . . . . . . . ␈↓
_ 292
␈↓ α←␈↓␈↓␈↓ λzCONTENTS 449␈↓
␈↓"β␈↓ α←␈↓␈↓ βc5.19␈↓ ∧+Stack Implementation of a LISP Subset: Shallow
␈↓ α←␈↓␈↓ ¬GBound␈↓ ε' . . . . . . . . . . . . . . . . . . . ␈↓
_ 293
␈↓ α←␈↓␈↓ βc5.20␈↓ ∧+Strategies for Full LISP Implementation␈↓ λG . . . . . . . . ␈↓
_ 300
␈↓ α←␈↓␈↓ βc5.21␈↓ ∧+Epilogue␈↓ ¬↔ . . . . . . . . . . . . . . . . . . . . . . . . ␈↓
_ 302
␈↓ α←␈↓␈↓ ββ6␈↓ β3THE DYNAMIC STRUCTURE OF LISP␈↓
"304
␈↓ α←␈↓␈↓ βc6.1␈↓ ∧+Introduction␈↓ ¬← . . . . . . . . . . . . . . . . . . . . . ␈↓
_ 304
␈↓ α←␈↓␈↓ βc6.2␈↓ ∧+Primitives for LISP␈↓ ε' . . . . . . . . . . . . . . . . . . . ␈↓
_ 309
␈↓ α←␈↓␈↓ βc6.3␈↓ ∧+␈↓↓SM␈↓: A Simple Machine␈↓ εo . . . . . . . . . . . . . . . . ␈↓
_ 312
␈↓ α←␈↓␈↓ βc6.4␈↓ ∧+Implementation of the Primitives␈↓ π[ . . . . . . . . . . . . ␈↓
_ 316
␈↓ α←␈↓␈↓ βc6.5␈↓ ∧+Assemblers␈↓ ¬; . . . . . . . . . . . . . . . . . . . . . . . ␈↓
_ 320
␈↓ α←␈↓␈↓ βc6.6␈↓ ∧+Compilers for Subsets of LISP␈↓ π7 . . . . . . . . . . . . . ␈↓
_ 323
␈↓ α←␈↓␈↓ βc6.7␈↓ ∧+Compilation of Conditional Expressions␈↓ λG . . . . . . . . ␈↓
_ 325
␈↓ α←␈↓␈↓ βc6.8␈↓ ∧+One-pass Assemblers and Fixups␈↓ π[ . . . . . . . . . . . . ␈↓
_ 329
␈↓ α←␈↓␈↓ βc6.9␈↓ ∧+A compiler for Simple ␈↓αeval␈↓: The Value Stack␈↓ λ; . . . . . . . . ␈↓
_ 332
␈↓ α←␈↓␈↓ βc6.10␈↓ ∧+A Compiler for Simple ␈↓αeval␈↓␈↓ π∪ . . . . . . . . . . . . . . ␈↓
_ 335
␈↓ α←␈↓␈↓ βc6.11␈↓ ∧+Efficient Compilation␈↓ εK . . . . . . . . . . . . . . . . . ␈↓
_ 340
␈↓ α←␈↓␈↓ βc6.12␈↓ ∧+Efficiency: Primitive Operations␈↓ π[ . . . . . . . . . . . . ␈↓
_ 341
␈↓ α←␈↓␈↓ βc6.13␈↓ ∧+Efficiency: Calling Sequences␈↓ π7 . . . . . . . . . . . . . ␈↓
_ 343
␈↓ α←␈↓␈↓ βc6.14␈↓ ∧+Efficiency: Predicates␈↓ εK . . . . . . . . . . . . . . . . . ␈↓
_ 348
␈↓ α←␈↓␈↓ βc6.15␈↓ ∧+A Compiler for ␈↓αprogs␈↓␈↓ εK . . . . . . . . . . . . . . . . . ␈↓
_ 350
␈↓ α←␈↓␈↓ βc6.16␈↓ ∧+Further Optimizations␈↓ εK . . . . . . . . . . . . . . . . . ␈↓
_ 352
␈↓ α←␈↓␈↓ βc6.17␈↓ ∧+Functional Arguments␈↓ εK . . . . . . . . . . . . . . . . . ␈↓
_ 354
␈↓ α←␈↓␈↓ βc6.18␈↓ ∧+Macros and Special Forms␈↓ π∪ . . . . . . . . . . . . . . ␈↓
_ 355
␈↓ α←␈↓␈↓ βc6.19␈↓ ∧+Compilation and Variables␈↓ π∪ . . . . . . . . . . . . . . ␈↓
_ 356
␈↓ α←␈↓␈↓ βc6.20␈↓ ∧+Compiling and Interpreting␈↓ π∪ . . . . . . . . . . . . . . ␈↓
_ 358
␈↓ α←␈↓␈↓ βc6.21␈↓ ∧+Interactive Programming␈↓ εo . . . . . . . . . . . . . . . . ␈↓
_ 362
␈↓ α←␈↓␈↓ βc6.22␈↓ ∧+LISP Editors␈↓ ¬← . . . . . . . . . . . . . . . . . . . . . ␈↓
_ 365
␈↓ α←␈↓␈↓ βc6.23␈↓ ∧+Debugging in LISP␈↓ ε' . . . . . . . . . . . . . . . . . . . ␈↓
_ 367
␈↓ α←␈↓␈↓ ββ7␈↓ β3STORAGE STRUCTURES AND EFFICIENCY␈↓
"370
␈↓ α←␈↓␈↓ βc7.1␈↓ ∧+Introduction␈↓ ¬← . . . . . . . . . . . . . . . . . . . . . ␈↓
_ 370
␈↓ α←␈↓␈↓ βc7.2␈↓ ∧+Vectors and Arrays␈↓ ε' . . . . . . . . . . . . . . . . . . . ␈↓
_ 371
␈↓ α←␈↓␈↓ βc7.3␈↓ ∧+Strings and Linear LISP␈↓ εo . . . . . . . . . . . . . . . . ␈↓
_ 374
␈↓ α←␈↓␈↓ βc7.4␈↓ ∧+A Compacting Collector for LISP␈↓ π[ . . . . . . . . . . . . ␈↓
_ 377
␈↓ α←␈↓␈↓ βc7.5␈↓ ∧+Bit-tables␈↓ ¬; . . . . . . . . . . . . . . . . . . . . . . . ␈↓
_ 380
␈↓ α←␈↓␈↓ βc7.6␈↓ ∧+Representations of Complex Data Structures␈↓ λ/ . . . . . . . . ␈↓
_ 381
␈↓ α←␈↓␈↓ βc7.7␈↓ ∧+␈↓αrplaca␈↓ and ␈↓αrplacd␈↓␈↓ ε' . . . . . . . . . . . . . . . . . . . ␈↓
_ 382
␈↓ α←␈↓␈↓ βc7.8␈↓ ∧+Applications of ␈↓αrplaca␈↓ and ␈↓αrplacd␈↓␈↓ π[ . . . . . . . . . . . . ␈↓
_ 385
␈↓ α←␈↓␈↓ βc7.9␈↓ ∧+Numbers␈↓ ¬↔ . . . . . . . . . . . . . . . . . . . . . . . . ␈↓
_ 389
␈↓ α←␈↓␈↓ βc7.10␈↓ ∧+Stacks and Threading␈↓ εK . . . . . . . . . . . . . . . . . ␈↓
_ 392
␈↓ α←␈↓␈↓450 CONTENTS␈↓
O␈↓
␈↓"β␈↓ α←␈↓␈↓ βc7.11␈↓ ∧+A Non-recursive ␈↓αread␈↓␈↓ εK . . . . . . . . . . . . . . . . . ␈↓
_ 394
␈↓ α←␈↓␈↓ βc7.12␈↓ ∧+More Applications of Threading␈↓ π[ . . . . . . . . . . . . ␈↓
_ 397
␈↓ α←␈↓␈↓ βc7.13␈↓ ∧+Storage Management and LISP␈↓ π[ . . . . . . . . . . . . ␈↓
_ 398
␈↓ α←␈↓␈↓ βc7.14␈↓ ∧+Hash Techniques␈↓ ε' . . . . . . . . . . . . . . . . . . . ␈↓
_ 402
␈↓ α←␈↓␈↓ ββ8␈↓ β3IMPLICATIONS OF LISP␈↓
"404
␈↓ α←␈↓␈↓ ββ9␈↓ β3PROJECTS␈↓
"412
␈↓ α←␈↓␈↓ βc9.1␈↓ ∧+Extensions to ␈↓αeval␈↓␈↓ ε' . . . . . . . . . . . . . . . . . . . ␈↓
_ 412
␈↓ α←␈↓␈↓ βc9.2␈↓ ∧+Pretty-printing␈↓ ¬← . . . . . . . . . . . . . . . . . . . . . ␈↓
_ 414
␈↓ α←␈↓␈↓ βc9.3␈↓ ∧+Syntax-directed Processes␈↓ εo . . . . . . . . . . . . . . . . ␈↓
_ 417
␈↓ α←␈↓␈↓ βc9.4␈↓ ∧+Syntax-directed I/O␈↓ ε' . . . . . . . . . . . . . . . . . . . ␈↓
_ 421
␈↓ α←␈↓␈↓↓BIBLIOGRAPHY␈↓
"426␈↓
␈↓ α←␈↓␈↓↓INDEX␈↓
∨443␈↓
␈↓ α←␈↓␈↓1.␈↓ λ⊃Symbolic expressions 1␈↓
␈↓"β␈↓ α←␈↓␈↓ "␈↓↓CHAPTER 1␈↓
␈↓"β␈↓ α←␈↓␈↓ Symbolic Expressions␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬h␈↓↓1.1 Introduction␈↓
␈↓"β␈↓ α←␈↓This␈α∩book␈α⊃is␈α∩a␈α∩study␈α⊃of␈α∩data␈α∩structures␈α⊃and␈α∩programming␈α∩languages;␈α⊃in
␈↓ α←␈↓particular␈α∃it␈α∀is␈α∃a␈α∃study␈α∀of␈α∃data␈α∀structures␈α∃and␈α∃programming␈α∀languages
␈↓ α←␈↓centered␈α
around␈α
the␈α∞language␈α
LISP.␈α
However,␈α
this␈α∞is␈α
not␈α
a␈α
manual␈α∞to␈α
help
␈↓ α←␈↓you␈αbecome␈αa␈αproficient␈αLISP␈αcoder.␈α We␈αwill␈αstudy␈αmany␈αof␈αthe␈αformal␈αand
␈↓ α←␈↓theoretical␈αaspects␈αof␈αlanguages␈αand␈αdata␈αstructures␈αas␈αwell␈αas␈αexamining␈αthe
␈↓ α←␈↓practical␈α∩applications␈α∩of␈α∩data␈α∩structures.␈α∪We␈α∩will␈α∩show␈α∩that␈α∩this␈α∪area␈α∩of
␈↓ α←␈↓computer␈αscience␈αis␈αa␈αdiscipline␈αof␈αimportance␈αand␈αbeauty,␈αworthy␈α
of␈αcareful
␈↓ α←␈↓study.␈α
How␈α
are␈α
we␈αto␈α
proceed?␈α
How␈α
do␈α
we␈αintroduce␈α
rigor␈α
into␈α
a␈αfield␈α
whose
␈↓ α←␈↓countenance␈α
is␈α
as␈α␈↓αad␈α
hoc␈↓␈α
and␈αdiverse␈α
as␈α
that␈αof␈α
programming?␈α
We␈αmust␈α
bear
␈↓ α←␈↓in␈α
mind␈αthat␈α
the␈αresults␈α
of␈αour␈α
studies␈αare␈α
to␈αhave␈α
practical␈αapplications.␈α
We
␈↓ α←␈↓must␈α∂not␈α∞pursue␈α∂theory␈α∞and␈α∂rigor␈α∞without␈α∂proper␈α∞regard␈α∂for␈α∂practice.␈α∞Our
␈↓ α←␈↓study␈α∞is␈α∞not␈α
that␈α∞of␈α∞pure␈α
mathematics;␈α∞our␈α∞results␈α
will␈α∞have␈α∞applications␈α
in
␈↓ α←␈↓everyday␈α→programming␈α~practice.␈α→ However,␈α→for␈α~guidance␈α→let's␈α~look␈α→at
␈↓ α←␈↓mathematics.␈α
Here␈αis␈α
a␈α
well-established␈αdiscipline␈α
rich␈α
in␈αhistory␈α
and␈α
full␈αof
␈↓ α←␈↓results of both practical and theoretical importance.
␈↓"β␈↓ α←␈↓␈↓ β'One␈α
of␈α∞the␈α
more␈α∞fertile,␈α
yet␈α∞easily␈α
introduced␈α∞areas␈α
of␈α∞mathematics,␈α
is
␈↓ α←␈↓that␈α∞of␈α∞elementary␈α∞number␈α∞theory.␈α∂It␈α∞is␈α∞easy␈α∞to␈α∞introduce␈α∂because␈α∞everyone
␈↓ α←␈↓knows␈α_something␈α↔about␈α_the␈α_natural␈α↔numbers.␈α_ Number␈α_theory␈α↔studies
␈↓ α←␈↓properties␈α∀of␈α∀a␈α∀certain␈α∀class␈α∀of␈α∀operations␈α∀definable␈α∀over␈α∀the␈α∀set␈α∀␈↓N␈↓␈α∪of
␈↓ α←␈↓␈↓2 Symbolic expressions␈↓
(1.1␈↓
␈↓"β␈↓ α←␈↓non-negative␈α≡integers␈α≥also␈α≡called␈α≥natural␈α≡numbers.␈α≥ A␈α≡very␈α≥formal
␈↓ α←␈↓presentation␈α∪might␈α∩begin␈α∪with␈α∪a␈α∩construction␈α∪of␈α∩␈↓N␈↓␈α∪from␈α∪more␈α∩primitive
␈↓ α←␈↓notions,␈α∀but␈α∀it␈α∀is␈α∪usually␈α∀assumed␈α∀that␈α∀the␈α∪reader␈α∀is␈α∀familiar␈α∀with␈α∪the
␈↓ α←␈↓fundamental␈αproperties␈αof␈α␈↓N␈↓.␈α In␈αeither␈αcase␈αthe␈αnext␈αstep␈αwould␈αbe␈αto␈αdefine
␈↓ α←␈↓the class of operations which we would allow on our domain.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α⊗shall␈α⊗begin␈α⊗our␈α⊗study␈α⊗of␈α⊗LISP␈α⊗in␈α⊗a␈α⊗similar␈α⊗manner,␈α⊗as␈α⊗an
␈↓ α←␈↓investigation␈α⊃of␈α⊃a␈α⊂certain␈α⊃class␈α⊃of␈α⊃operations␈α⊂definable␈α⊃over␈α⊃a␈α⊃domain␈α⊂of
␈↓ α←␈↓objects,␈α∞called␈α
Symbolic␈α∞Expressions.␈α
Though␈α∞most␈α
people␈α∞know␈α
something
␈↓ α←␈↓about␈αthe␈αnatural␈αnumbers,␈αthe␈αterm␈α"symbolic␈αexpresssions"␈αhas␈αno␈α
standard
␈↓ α←␈↓interpretation.␈α∞ We␈α∞must␈α∞define␈α∞what␈α∞we␈α∞mean␈α∞by␈α∞"symbolic␈α∞expression".␈α
If
␈↓ α←␈↓we␈α
asked␈α
someone␈α∞to␈α
define␈α
the␈α
domain␈α∞␈↓N␈↓,␈α
the␈α
definition␈α
we␈α∞would␈α
receive
␈↓ α←␈↓would␈αdepend␈αon␈αhow␈αfamilar␈αthat␈αindividual␈αwas␈αwith␈αthe␈αproperties␈αof␈αthe
␈↓ α←␈↓natural numbers.␈↓π 1␈↓
␈↓"β␈↓ α←␈↓␈↓ β'For␈αmost␈αpeople␈αand␈αmost␈αpurposes,␈αthe␈αfollowing␈αcharacterization␈αof␈αa
␈↓ α←␈↓natural number is satisfactory:
␈↓"β␈↓ α←␈↓␈↓↓I␈↓␈↓ β∂A natural number is a sequence of decimal digits.
␈↓"β␈↓ α←␈↓The␈α
definition␈α
assumes␈α
the␈α
terminology␈α
of␈α
"sequence",␈α
"decimal"␈α∞and␈α
"digit"
␈↓ α←␈↓are␈α∂known.␈α⊂If␈α∂any␈α⊂of␈α∂these␈α⊂terms␈α∂are␈α⊂␈↓¬not␈↓␈α∂understood,␈α⊂they␈α∂can␈α⊂be␈α∂further
␈↓ α←␈↓elaborated.␈α⊗However,␈α↔this␈α⊗process␈α⊗of␈α↔explanation␈α⊗and␈α↔description␈α⊗must
␈↓ α←␈↓terminate.␈α≤We␈α≠must␈α≤assume␈α≠that␈α≤some␈α≠concepts␈α≤require␈α≤no␈α≠further
␈↓ α←␈↓elaboration.␈α→The␈α→current␈α→definition␈α→suffers␈α→from␈α→a␈α→different␈α→kind␈α→of
␈↓ α←␈↓inadequacy.␈αIt␈α
fails␈αto␈α
illuminate␈αthe␈α
relationships␈αbetween␈α
natural␈αnumbers.
␈↓ α←␈↓The␈α"meaning"␈α
of␈αthe␈α
natural␈αnumbers␈α
is␈αmissing.␈α
It␈αis␈α
like␈αgiving␈α
a␈αperson
␈↓ α←␈↓an␈α⊗alphabet␈α∃and␈α⊗rules␈α∃for␈α⊗forming␈α∃syntactically␈α⊗correct␈α∃words␈α⊗but␈α∃not
␈↓ α←␈↓supplying a dictionary which relates these words to the person's vocabulary.
␈↓"β␈↓ α←␈↓␈↓ β'If␈α!pressed␈α!for␈α!details␈α"we␈α!might␈α!attempt␈α!a␈α"more␈α!elaborate
␈↓ α←␈↓characterization like the following:
␈↓"β␈↓ α←␈↓␈↓ β≠␈↓↓1.␈↓ ␈↓αzero␈↓ is an element of ␈↓N␈↓.
␈↓"β␈↓ α←␈↓␈↓↓II␈↓ ␈↓↓2.␈↓ If ␈↓αn␈↓ is in ␈↓N␈↓ then the ␈↓αsuccessor␈↓ of ␈↓αn␈↓ is in ␈↓N␈↓.
␈↓"β␈↓ α←␈↓␈↓ β≠␈↓↓3.␈↓␈α
The␈α
only␈α
elements␈α
of␈α␈↓N␈↓␈α
are␈α
those␈α
created␈α
by␈α
finitely␈αmany␈α
applications
␈↓ α←␈↓␈↓ β≠of rules ␈↓↓1␈↓ and ␈↓↓2␈↓.
␈↓"β␈↓ α←␈↓Definition␈α∞␈↓↓II␈↓␈α∞appears␈α∞to␈α∞be␈α∞completely␈α
at␈α∞the␈α∞other␈α∞end␈α∞of␈α∞the␈α∞spectrum;␈α
it
␈↓ α←␈↓tells␈αus␈αvery␈αlittle␈αabout␈αthe␈αappearance␈αof␈αthe␈αintegers.␈αIt␈αgives␈αus␈αan␈αinitial
␈↓ α←␈↓element␈α⊂␈↓αzero␈↓␈α⊂and␈α⊂an␈α∂operation␈α⊂called␈α⊂␈↓αsuccessor␈↓,␈α⊂which␈α∂is␈α⊂to␈α⊂exhibit␈α⊂a␈α∂new
␈↓ α←␈↓element,␈α⊃given␈α⊃an␈α⊃old␈α⊃one.␈α∩ Unless␈α⊃we␈α⊃are␈α⊃careful␈α⊃about␈α⊃the␈α∩meaning␈α⊃of
␈↓ α←␈↓␈↓αsuccessor␈↓,␈α⊃definition␈α⊃␈↓↓II␈↓␈α⊃will␈α⊃be␈α∩inadequate.␈α⊃For␈α⊃example␈α⊃if␈α⊃we␈α∩define␈α⊃the
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 1␈↓We␈αwill␈αnot␈αattempt␈αto␈αarrive␈αat␈αa␈αcompletely␈αself-contained␈αdefinition
␈↓ α←␈↓of␈α
"natural␈α
number".␈α
That␈α
is␈α
a␈α
difficult␈α
undertaking.␈α
See␈α
[Goo 57].␈α
We␈αwill
␈↓ α←␈↓be satisfied with discussing ␈↓¬some␈↓ of their characteristics.
␈↓ α←␈↓␈↓1.1␈↓ λ|Introduction 3␈↓
␈↓"β␈↓ α←␈↓successor␈α
of␈α
a␈α
natural␈α
number␈α
to␈α
be␈α
that␈α
same␈α
number␈α
then␈α
␈↓↓II␈↓␈α
is␈αsatisfied␈α
but
␈↓ α←␈↓unsatisfactory.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∂can␈α∂define␈α∂␈↓αsuccessor␈↓␈α∂as␈α∂a␈α∂specific␈α∂mapping,␈α∂␈↓↓S␈↓,␈α∂which␈α∂creates␈α∂new
␈↓ α←␈↓elements␈αsubject␈αto␈αthe␈αrules␈αthat␈αtwo␈αelements,␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓␈αare␈αequal␈αjust␈αin␈αthe
␈↓ α←␈↓case␈α∞that␈α∞␈↓↓S␈↓α(x)␈↓␈α∞equals␈α
␈↓↓S␈↓α(y)␈↓;␈α∞and␈α∞␈↓↓S␈↓α(x)␈↓␈α∞is␈α∞different␈α
from␈α∞␈↓αx␈↓,␈α∞for␈α∞any␈α∞element␈α
␈↓αx␈↓.
␈↓ α←␈↓We␈α
select␈α
a␈α∞distinguished␈α
element,␈α
␈↓α0␈↓,␈α
as␈α∞a␈α
notation␈α
for␈α
␈↓αzero␈↓;␈α∞and␈α
abbreviate
␈↓ α←␈↓␈↓↓S␈↓α(0)␈↓ as ␈↓α1␈↓, and abbreviate ␈↓↓S␈↓α(␈↓↓S␈↓α(0))␈↓ as ␈↓α2␈↓ etc. in the usual manner.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∩characterization␈α⊃of␈α∩decimal␈α⊃digits␈α∩given␈α⊃in␈α∩␈↓↓I␈↓␈α⊃is␈α∩syntactic.␈α⊃ The
␈↓ α←␈↓notation␈α∀itself␈α∃tells␈α∀us␈α∃nothing␈α∀about␈α∃the␈α∀interrelationships␈α∃between␈α∀the
␈↓ α←␈↓numbers,␈α
but␈α
it␈αdoes␈α
give␈α
us␈αa␈α
notation␈α
for␈αrepresenting␈α
them.␈α
Thus␈α
␈↓α2␈↓␈αcan
␈↓ α←␈↓be␈α
used␈α
to␈α∞represent␈α
␈↓αtwo␈↓.␈α
One␈α∞benefit␈α
of␈α
the␈α∞␈↓↓S␈↓-notation␈α
is␈α
that␈α∞it␈α
explicitly
␈↓ α←␈↓shows␈αthe␈αmeans␈αof␈αconstruction.␈αThat␈αis,␈αit␈αshows␈αmore␈αof␈αthe␈αproperties␈αof
␈↓ α←␈↓these␈α∀numbers␈α∀than␈α∀just␈α∀distinguishability.␈α∀ We␈α∀shall␈α∀refer␈α∀to␈α∀the␈α∪digit
␈↓ α←␈↓representation␈α∂as␈α∂␈↓↓numerals␈↓␈α∂and␈α∂reserve␈α∞the␈α∂term,␈α∂␈↓↓natural␈α∂number␈↓,␈α∂for␈α∞the
␈↓ α←␈↓abstract␈α∞object.␈α∞ Thus␈α∞numerals␈α∂denote,␈α∞stand␈α∞for,␈α∞or␈α∞represent␈α∂the␈α∞abstract
␈↓ α←␈↓objects␈αcalled␈αnatural␈αnumbers;␈αand␈αdefinition␈α␈↓↓I␈↓␈αis␈αbetter␈αstated␈αas:␈α"a␈α
natural
␈↓ α←␈↓number can be represented as a finite sequence of digits".
␈↓"β␈↓ α←␈↓␈↓ β'But␈α⊂notation␈α∂and␈α⊂syntax␈α∂are␈α⊂necessary␈α⊂and␈α∂we␈α⊂must␈α∂be␈α⊂able␈α⊂to␈α∂give
␈↓ α←␈↓precise␈α⊂descriptions␈α⊃of␈α⊂syntactic␈α⊃notions.␈α⊂ Given␈α⊂a␈α⊃choice␈α⊂between␈α⊃the␈α⊂two
␈↓ α←␈↓previous␈αdefinitions,␈α␈↓↓I␈↓␈αand␈α
␈↓↓II␈↓,␈αit␈αappears␈αthat␈α
␈↓↓II␈↓␈αis␈αmore␈αprecise.␈α
Much␈αless
␈↓ α←␈↓is␈α∀left␈α∀to␈α∪the␈α∀imagination;␈α∀given␈α∪␈↓αzero␈↓␈α∀and␈α∀a␈α∪definition␈α∀of␈α∀␈↓αsuccessor␈↓␈α∪the
␈↓ α←␈↓definition␈α⊂will␈α⊂act␈α⊂as␈α⊃a␈α⊂recipe␈α⊂for␈α⊂producing␈α⊃elements␈α⊂of␈α⊂␈↓N␈↓.␈α⊂This␈α⊃style␈α⊂of
␈↓ α←␈↓definition is called an inductive definition or generative definition.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αbasic␈αcontent␈αof␈αan␈αinductive␈αdefinition␈αof␈αa␈αset␈αof␈αobjects␈αconsists
␈↓ α←␈↓of three parts:
␈↓"β␈↓ α←␈↓␈↓ βW(1)␈αA␈αdescription␈αof␈αan␈αinitial␈αset␈αof␈αobjects;␈αthe␈αelements␈αof␈αthis␈αset
␈↓ α←␈↓␈↓ βWare␈αthe␈αinitial␈αelements␈αof␈αthe␈αset␈αwe␈αare␈αdescribing␈αin␈αthe␈αinductive
␈↓ α←␈↓␈↓ βWdefinition.
␈↓"β␈↓ α←␈↓␈↓↓IND␈↓
␈↓"β␈↓ α←␈↓␈↓ βW(2)␈α∞Given␈α∂the␈α∞description␈α∞of␈α∂some␈α∞existing␈α∞elements␈α∂in␈α∞the␈α∂set,␈α∞we
␈↓ α←␈↓␈↓ βWare given a means of constructing more elements.
␈↓" ␈↓ α←␈↓␈↓ βW(3)␈αA␈α
termination␈αclause,␈α
saying␈αthat␈αthe␈α
only␈αelements␈α
in␈αthe␈αset␈α
are
␈↓ α←␈↓␈↓ βWthose which gained admittance by either (1) or (2).
␈↓"β␈↓ α←␈↓Notice␈αthat␈αour␈αdefinition␈αof␈α␈↓N␈↓,␈αin␈αterms␈αof␈α␈↓αzero␈↓␈αand␈α␈↓αsuccessor␈↓,␈αis␈αan␈αinstance
␈↓ α←␈↓of␈α␈↓↓IND␈↓:␈αwe␈αare␈αdefining␈αthe␈αset␈αof␈αnatural␈αnumbers:␈α␈↓αzero␈↓␈αis␈αinitially␈αincluded
␈↓ α←␈↓in␈αthe␈αset;␈αthen␈αapplying␈αthe␈αsecond␈αphrase␈αof␈αthe␈αdefinition␈αwe␈αcan␈αsay␈αthat
␈↓ α←␈↓␈↓αone␈↓ is in the set since ␈↓αone␈↓ is the successor of ␈↓αzero␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∃can␈α∃recast␈α∀the␈α∃positional␈α∃notation␈α∀description␈α∃as␈α∃an␈α∀inductive
␈↓ α←␈↓definition.
␈↓ α←␈↓␈↓4 Symbolic expressions␈↓
(1.1␈↓
␈↓"λ␈↓ α←␈↓␈↓↓1.␈↓ A digit is a numeral.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ If ␈↓αn␈↓ is a numeral then ␈↓αn␈↓ followed by a digit is a numeral.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ The␈α∂only␈α∂numerals␈α∞are␈α∂those␈α∂created␈α∂by␈α∞finitely␈α∂many␈α∂applications␈α∂of␈α∞␈↓↓1␈↓
␈↓ α←␈↓␈↓ β∂and ␈↓↓2␈↓.
␈↓"β␈↓ α←␈↓In words, "a numeral is a digit, or a numeral followed by a digit".
␈↓"β␈↓ α←␈↓␈↓ β'In␈α∞this␈α
application␈α∞of␈α
␈↓↓IND␈↓,␈α∞the␈α∞initial␈α
set␈α∞has␈α
more␈α∞than␈α∞one␈α
element;
␈↓ α←␈↓namely␈α
the␈αten␈α
decimal␈α
digits.␈α Again,␈α
we␈α
assume␈αthat␈α
the␈α
questioner␈αknows
␈↓ α←␈↓what␈α
"digit"␈α
means.␈α
This␈α
is␈α
a␈α
characteristic␈α
of␈α
all␈α
definitions:␈α
we␈α∞must␈α
stop
␈↓ α←␈↓␈↓¬somewhere␈↓␈α∞in␈α∞our␈α∞explication.␈α∞Notice␈α∞too␈α∞that␈α∞we␈α∞assume␈α∞that␈α∞"followed␈α
by"
␈↓ α←␈↓means juxtaposition.
␈↓"β␈↓ α←␈↓␈↓ β'Inductive␈αdefinitions␈αhave␈αbeen␈αthe␈αprovince␈αof␈αmathematics␈α
for␈αmany
␈↓ α←␈↓years;␈α
however,␈αcomputer␈α
science␈αhas␈α
developed␈α
a␈αstyle␈α
of␈αsyntax␈α
specification
␈↓ α←␈↓called␈α
BNF␈α(Backus-Naur␈α
Form)␈αequations␈α
which␈αhas␈α
the␈αsame␈α
intent␈αas␈α
that
␈↓ α←␈↓of␈α↔inductive␈α⊗definitions.␈α↔ Here␈α⊗is␈α↔the␈α⊗previous␈α↔inductive␈α↔definition␈α⊗of
␈↓ α←␈↓"numeral" as a set of BNF equations:
␈↓"∀␈↓ α←␈↓<numeral>␈↓ ∧π::= <digit>
␈↓" ␈↓ α←␈↓<numeral>␈↓ ∧π::= <numeral><digit>
␈↓"β␈↓ α←␈↓As an abbreviation, the two BNF equations may also be written:
␈↓"β␈↓ α←␈↓<numeral>␈↓ ∧π::= <digit> | <numeral><digit>.
␈↓"∀␈↓ α←␈↓A␈αcomparison␈αbetween␈αthe␈αBNF␈αand␈αthe␈αinductive␈αdescriptions␈αof␈α"numeral"
␈↓ α←␈↓should␈α∀clarify␈α∪much␈α∀of␈α∪the␈α∀notation,␈α∀but␈α∪we␈α∀will␈α∪give␈α∀a␈α∀more␈α∪detailed
␈↓ α←␈↓analysis.␈α∞ The␈α∞symbol␈α∞"::="␈α∂may␈α∞be␈α∞read␈α∞"is␈α∞a",␈α∂the␈α∞symbol␈α∞"|"␈α∞may␈α∂be␈α∞read
␈↓ α←␈↓"or".␈α↔ The␈α↔character␈α↔strings␈α↔beginning␈α↔with␈α↔"<"␈α↔and␈α↔ending␈α_with␈α↔">"
␈↓ α←␈↓correspond␈α
to␈α"numeral"␈α
and␈α
"digit"␈αin␈α
␈↓↓1␈↓␈α
and␈α␈↓↓2␈↓;␈α
by␈α
convention,␈αcomponents␈α
of
␈↓ α←␈↓BNF␈α⊃equations␈α⊃which␈α⊃␈↓¬describe␈↓␈α⊃elements␈α⊃are␈α⊃enclosed␈α⊃in␈α⊃"<"␈α⊃and␈α⊃">";␈α⊃and
␈↓ α←␈↓elements␈α
which␈α
are␈αgiven␈α
␈↓¬explicitly␈↓␈α
are␈α
written␈αwithout␈α
the␈α
"<␈α
>"␈αfence.␈α
Thus
␈↓ α←␈↓"<digit>"␈α⊂is␈α⊂not␈α∂a␈α⊂numeral␈α⊂but␈α∂is␈α⊂a␈α⊂description;␈α∂to␈α⊂make␈α⊂the␈α⊂definition␈α∂of
␈↓ α←␈↓<numeral> complete we should include an equation like:
␈↓"∀␈↓ α←␈↓<digit>␈↓ ∧π:: = ␈↓α0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9␈↓
␈↓"∀␈↓ α←␈↓Juxtaposition␈α
of␈α
objects␈α
implies␈α
concatenation␈α
of␈α
the␈α
syntactic␈α
objects.␈αThus
␈↓ α←␈↓"89" is an instance of "<numeral><digit>".
␈↓"β␈↓ α←␈↓␈↓ β'It␈α
will␈α
be␈α
convenient␈α
to␈α
have␈α
notations␈α
for␈α
the␈α
abstract␈α
objects␈α
as␈αwell
␈↓ α←␈↓as␈α∂notations␈α∂for␈α∂the␈α∂syntactic␈α∂representations.␈α∂The␈α∂BNF␈α∂equations␈α∂describe
␈↓ α←␈↓syntactic␈αclasses;␈αfor␈αexample,␈αthe␈αset␈αdescribed␈αby␈α<numeral>␈αis␈αthe␈αsyntactic
␈↓ α←␈↓class␈αof␈α
numerals.␈↓π 2␈↓␈αWhen␈αwe␈α
are␈αtalking␈α
about␈αa␈αsyntactic␈α
class␈αof␈αobjects␈α
we
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 2␈↓Note␈α-we␈α.could␈α-have␈α-written␈α.<numeral> ::= <digit>␈α-and
␈↓ α←␈↓<numeral> ::= <digit><numeral>,␈αgenerating␈αthe␈αsame␈αclass,␈αbut␈αin␈αa␈αdifferent
␈↓ α←␈↓order.␈α∞Questions␈α∞of␈α
syntax␈α∞and␈α∞grammars␈α
will␈α∞not␈α∞be␈α
stressed␈α∞in␈α∞this␈α
book.
␈↓ α←␈↓See [Aho 72].
␈↓ α←␈↓␈↓1.1␈↓ λ|Introduction 5␈↓
␈↓"β␈↓ α←␈↓will␈α∂write␈α∂<object>;␈α∂when␈α∂we␈α∂are␈α∂talking␈α∂about␈α∂the␈α∂abstract␈α∂object␈α⊂we␈α∂will
␈↓ α←␈↓write ␈↓<object>␈↓. For example ␈↓<numeral>␈↓ is the class of natural numbers.
␈↓"β␈↓ α←␈↓␈↓ β'What␈α
should␈α∞be␈α
remembered␈α∞from␈α
the␈α
discussion␈α∞in␈α
this␈α∞section?␈α
We
␈↓ α←␈↓need␈αprecise␈αways␈αof␈αdescribing␈αthe␈αelements␈αof␈αour␈αstudy␈αon␈αdata␈αstructures.
␈↓ α←␈↓We␈α∂have␈α∂seen␈α∞that␈α∂inductive␈α∂definitions␈α∂are␈α∞a␈α∂powerful␈α∂way␈α∂of␈α∞describing
␈↓ α←␈↓sets␈α∀of␈α∀objects.␈α∀We␈α∀have␈α∪seen␈α∀a␈α∀variant␈α∀of␈α∀inductive␈α∀definitions␈α∪called
␈↓ α←␈↓Backus-Naur␈α∂Form␈α⊂equations.␈α∂We␈α⊂will␈α∂use␈α∂BNF␈α⊂equations␈α∂to␈α⊂describe␈α∂the
␈↓ α←␈↓syntax of our data structures and our language.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αhave␈αalso␈αintroduced␈αthe␈αdifference␈αbetween␈αan␈αabstract␈αobject␈αand
␈↓ α←␈↓apα∂representation␈α∞for␈α∂that␈α∂object.␈α∞ This␈α∂distinction␈α∂has␈α∞been␈α∂well␈α∂studied␈α∞in
␈↓ α←␈↓philosophy␈α∪and␈α∪mathematics,␈α∪and␈α∪we␈α∀will␈α∪see␈α∪that␈α∪this␈α∪idea␈α∀has␈α∪strong
␈↓ α←␈↓consequences␈α∞for␈α∞the␈α
field␈α∞of␈α∞programming␈α
and␈α∞computer␈α∞science.␈α
Abstract
␈↓ α←␈↓objects and their representations will play crucial roles in this text.
␈↓"β␈↓ α←␈↓␈↓ βy␈↓↓1.2 Symbolic Expressions: Abstract Data Structures␈↓
␈↓"β␈↓ α←␈↓We␈α∞wish␈α∞to␈α
show␈α∞that␈α∞the␈α∞use␈α
of␈α∞abstraction␈α∞will␈α
benefit␈α∞the␈α∞study␈α∞of␈α
data
␈↓ α←␈↓structures␈α∞and␈α∞LISP.␈α∞ To␈α∂begin␈α∞our␈α∞study␈α∞we␈α∞should␈α∂therefore␈α∞characterize
␈↓ α←␈↓the␈αdomain␈αof␈αLISP␈αdata␈αstructures␈αin␈αa␈αmanner␈αsimilar␈αto␈αwhat␈αwe␈α
did␈αfor
␈↓ α←␈↓numbers.
␈↓"β␈↓ α←␈↓␈↓ β'Our␈α→objects␈α→are␈α→called␈α→␈↓↓Symbolic␈α→Expressions␈↓.␈α→ Our␈α→domain␈α→of
␈↓ α←␈↓Symbolic␈α∩Expressions␈α∩is␈α∩named␈α∩␈↓<sexpr>␈↓.␈α∩ Symbolic␈α∩expressions␈α∩are␈α⊃also
␈↓ α←␈↓known as ␈↓↓S-expressions␈↓ or ␈↓↓S-exprs␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
set␈α∞of␈α
symbolic␈α
expressions␈α∞is␈α
defined␈α
inductively␈α∞over␈α
a␈α∞base␈α
set
␈↓ α←␈↓named␈α
␈↓<atom>␈↓.␈α
The␈α
set␈α
␈↓<atom>␈↓␈α
can␈α
itself␈α
be␈α
defined␈α
inductively.␈α
We␈α
give␈α
a
␈↓ α←␈↓set␈α∃of␈α∀BNF␈α∃equations␈α∀for␈α∃elements␈α∀of␈α∃<atom>␈α∀below,␈α∃but␈α∃the␈α∀essential
␈↓ α←␈↓character␈αof␈αthe␈αdomain␈αis␈αthat␈αit␈αrepresents␈αtwo␈αkinds␈αof␈αobjects:␈αthe␈α␈↓↓literal
␈↓ α←␈↓↓atoms␈↓ and the integers. The elements of ␈↓<atom>␈↓ are called ␈↓↓atoms␈↓.
␈↓"∀␈↓ α←␈↓<atom>␈↓ ∧7:: = <literal atom> | <numeral> | -<numeral>
␈↓" ␈↓ α←␈↓<literal atom>␈↓ ∧7:: = <atom letter>
␈↓"β␈↓ α←␈↓␈↓ ∧7:: = <literal atom><atom letter>
␈↓"β␈↓ α←␈↓␈↓ ∧7:: = <literal atom><digit>
␈↓" ␈↓ α←␈↓<numeral>␈↓ ∧7:: = <digit> | <numeral><digit>
␈↓" ␈↓ α←␈↓<atom letter>␈↓ ∧7:: =␈↓α A | B | C ... | Z␈↓ ␈↓π 3␈↓
␈↓" ␈↓ α←␈↓<digit>␈↓ ∧7:: = ␈↓α0 | 1 | 2 ... | 9␈↓
␈↓"∀␈↓ α←␈↓A␈α<literal␈α
atom>␈αis␈αtherefore␈α
a␈αstring␈αof␈α
uppercase␈αletters␈αand␈α
digits,␈αsubject
␈↓ α←␈↓to the provision that the ␈↓¬first␈↓ character in the atom be a letter.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 3␈↓We use ellipses here as a convienient abbreviation.
␈↓ α←␈↓␈↓6 Symbolic expressions␈↓
(1.2␈↓
␈↓"∀␈↓ α←␈↓For example:␈↓ ∧Catoms␈↓ ¬wnon atoms
␈↓"∀␈↓ α←␈↓␈↓α␈↓ ∧CABC123␈↓ ¬w2A
␈↓" ␈↓ α←␈↓α␈↓ ∧C12␈↓ ¬wa
␈↓" ␈↓ α←␈↓α␈↓ ∧CA4D6␈↓ ¬w$$g
␈↓" ␈↓ α←␈↓α␈↓ ∧CNIL␈↓ ¬wABD.
␈↓" ␈↓ α←␈↓α␈↓ ∧CT␈↓ ¬w(A . B)
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α≠characteristics␈α≠of␈α≠atoms␈α≠which␈α≠most␈α≠interest␈α≠us␈α≤are␈α≠their
␈↓ α←␈↓distinguishability:␈α
the␈αatom␈α
␈↓αABC␈↓␈α
is␈αdistinguishable␈α
from␈αthe␈α
atom␈α
␈↓αAB␈↓.␈αThat
␈↓ α←␈↓␈↓α"AB"␈↓␈αis␈αa␈α
part␈αof␈α␈↓α"ABC"␈↓␈α
is␈αnot␈αgermane␈α
to␈αour␈αcurrent␈αdiscussion.␈↓π 4␈↓␈α
Similarly,
␈↓ α←␈↓we␈α⊗will␈α⊗seldom␈α⊗need␈α⊗to␈α⊗exploit␈α⊗numerical␈α⊗relationships␈α⊗underlying␈α⊗the
␈↓ α←␈↓numerals;␈αat␈αmost␈αwe␈αwill␈αuse␈αsimple␈αcounting␈αproperties.␈α Therefore␈αmost␈αof
␈↓ α←␈↓our␈α
discussions␈α
will␈α
deal␈α
with␈α
non-numeric␈α
atoms.␈α
Most␈α
implementations␈αof
␈↓ α←␈↓LISP␈α#do␈α#however␈α#contain␈α#a␈α#large␈α#arithmetic␈α#entourage.␈α# Many
␈↓ α←␈↓implementations␈α
also␈αgive␈α
a␈αwider␈α
class␈αof␈α
literal␈αatoms,␈α
allowing␈αsome␈α
special
␈↓ α←␈↓characters␈α⊃to␈α⊃appear;␈α⊃for␈α⊃most␈α⊃of␈α⊃our␈α⊃discussion␈α⊃the␈α⊃above␈α⊃class␈α∩is␈α⊃quite
␈↓ α←␈↓sufficient.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊗domain␈α⊗of␈α↔Symbolic␈α⊗expressions,␈α⊗called␈α⊗␈↓<sexpr>␈↓␈α↔is␈α⊗defined
␈↓ α←␈↓inductively over the domain ␈↓<atom>␈↓.␈↓π 5␈↓
␈↓"λ␈↓ α←␈↓␈↓↓1.␈↓ Any element of ␈↓<atom>␈↓ is an element of ␈↓<sexpr>␈↓.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ If␈α␈↓λα␈↓β1␈↓␈αand␈α␈↓λα␈↓β2␈↓␈αare␈αelements␈αof␈α
␈↓<sexpr>␈↓,␈αthen␈αthe␈αpair␈αof␈α␈↓λα␈↓β1␈↓␈αand␈α␈↓λα␈↓β2␈↓␈α
is␈αin
␈↓ α←␈↓␈↓ β∂␈↓<sexpr>␈↓.␈α~Pairs␈α~are␈α~also␈α~called␈α~␈↓↓dotted-pairs␈↓␈α~since␈α~their␈α~standard
␈↓ α←␈↓␈↓ β∂representation in LISP is ␈↓α(␈↓λα␈↓β1␈↓α.␈↓λα␈↓β2␈↓α)␈↓.
␈↓"β␈↓ α←␈↓Thus␈α␈↓<sexpr>␈↓␈αincludes␈α␈↓<atom>␈↓␈αas␈αa␈αproper␈αsubset.␈αThe␈αnotation␈αwe␈αchose
␈↓ α←␈↓for the dotted-pairs is the following:
␈↓"∀␈↓ α←␈↓␈↓ βWA␈α
dotted-pair␈αconsists␈α
of␈αa␈α
left-parenthesis␈αfollowed␈α
by␈αan
␈↓ α←␈↓␈↓ βWS-expr,␈α⊗followed␈α∃by␈α⊗a␈α∃period,␈α⊗followed␈α∃by␈α⊗an␈α∃S-expr,
␈↓ α←␈↓␈↓ βWfollowed by a right-parenthesis.
␈↓"∀␈↓ α←␈↓For␈α_example,␈α_let␈α_␈↓λα␈↓β1␈↓␈α↔be␈α_␈↓α(A . B)␈↓␈α_and␈α_␈↓λα␈↓β2␈↓␈α↔be␈α_␈↓α(1 . T)␈↓;␈α_then␈α_␈↓α(␈↓λα␈↓β1␈↓ . ␈↓λα␈↓β2␈↓)␈α↔is
␈↓ α←␈↓␈↓α((A . B) . (1 . T))␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Greek␈α⊂letters␈α⊃␈↓λα␈↓␈α⊂and␈α⊃␈↓λβ␈↓␈α⊂will␈α⊃be␈α⊂used␈α⊃in␈α⊂the␈α⊃text␈α⊂to␈α⊃designate␈α⊂pattern
␈↓ α←␈↓matches.␈α≡In␈α≡the␈α≡current␈α∨context␈α≡the␈α≡pattern␈α≡matches␈α∨will␈α≡involve
␈↓ α←␈↓S-expressions;␈α∞they␈α∞can␈α∞match␈α∂any␈α∞well-formed␈α∞S-expression.␈α∞ For␈α∂a␈α∞further
␈↓ α←␈↓example,␈α⊂let␈α⊂␈↓α(A . (B . C))␈↓␈α⊂be␈α⊃␈↓α(␈↓λα␈↓α . ␈↓λβ␈↓α)␈↓␈α⊂then␈α⊂␈↓λα␈↓␈α⊂is␈α⊃␈↓αA␈↓␈α⊂and␈α⊂␈↓λβ␈↓␈α⊂is␈α⊃␈↓α(B . C)␈↓.␈α⊂ These
␈↓ α←␈↓variables are called ␈↓↓match-variables␈↓ or ␈↓↓meta-variables␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Finally here's a BNF description of the full set of S-expressions.
␈↓"∀␈↓ α←␈↓␈↓ ∧L<sexpr> :: = <atom> | ␈↓α(␈↓<sexpr> . <sexpr>␈↓α)
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 4␈↓However,␈α→we␈α→will␈α→discuss␈α→such␈α→topics␈α→in␈α→Section 7.3␈α→on␈α→string
␈↓ α←␈↓processing.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 5␈↓We will not give the termination clause, but it is assumed to hold.
␈↓ α←␈↓␈↓1.2␈↓ ¬USymbolic Expressions: Abstract Data Structures 7␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Notice␈α∞that␈α∂if␈α∞we␈α∂allow␈α∞real␈α∞numbers␈α∂as␈α∞atoms␈α∂then␈α∞some␈α∂care␈α∞would
␈↓ α←␈↓need␈αto␈αbe␈αexercised␈αwhen␈αwriting␈αS-expressions.␈αFor␈αexample,␈αshould␈α␈↓α(3.1.2)␈↓
␈↓ α←␈↓be␈αinterpreted␈αas␈αthe␈αdotted␈αpair␈α␈↓α(3␈α.␈α1.2)␈↓,␈αas␈αthe␈αdotted␈αpair␈α␈↓α(3.1 . 2)␈↓,␈αor␈αis␈αit
␈↓ α←␈↓just␈α∂an␈α∂ill-formed␈α∂expression?␈α∂ Interpretation␈α∂of␈α∂such␈α∂ambiguous␈α∞constructs
␈↓ α←␈↓will depend on the implementation; such details are discussed later.
␈↓"∀␈↓ α←␈↓Examples:␈↓ ∧∨S-exprs␈↓ ε{non S-exprs
␈↓"∀␈↓ α←␈↓α␈↓ ∧∨A␈↓ ε{A . B
␈↓" ␈↓ α←␈↓α␈↓ ∧∨(A . B)␈↓ ε{(A . B . C)
␈↓" ␈↓ α←␈↓α␈↓ ∧∨(((A . B) . C) . (A . B))␈↓ ε{((A . B))
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α
set␈α
described␈α
by␈α
<sexpr>␈α
is␈α
a␈α
␈↓¬specific␈↓␈α
syntactic␈α
representation␈α
of␈α
the
␈↓ α←␈↓domain␈α∞␈↓<sexpr>␈↓.␈α∞ However,␈α∞the␈α∞set␈α∞<sexpr>␈α∞will␈α∞be␈α∞a␈α∞convenient␈α
notation
␈↓ α←␈↓since␈α∩it␈α⊃makes␈α∩explicit␈α⊃the␈α∩construction␈α⊃of␈α∩the␈α⊃composite␈α∩S-expr␈α∩from␈α⊃its
␈↓ α←␈↓components,␈↓π 6␈↓ and the notation is also consistent with LISP history.
␈↓"β␈↓ α←␈↓␈↓ β'However␈α∂there␈α∂is␈α∂more␈α∂to␈α∂the␈α∂domain␈α∂␈↓<sexpr>␈↓␈α∂than␈α∂syntax,␈α∂just␈α∂as
␈↓ α←␈↓there␈αis␈αmore␈αto␈α␈↓N␈↓␈αthan␈αpositional␈αnotation.␈↓π 7␈↓␈αWhat␈α␈↓¬are␈↓␈αthe␈αessential␈αfeatures
␈↓ α←␈↓of␈α∂S-expressions?␈α∂Symbolic␈α∂expressions␈α∂are␈α∂either␈α∂atomic␈α∂or␈α∂they␈α∂have␈α∂two
␈↓ α←␈↓components.␈α∂If␈α∂we␈α∂are␈α⊂confronted␈α∂with␈α∂a␈α∂non-atomic␈α∂S-expression␈α⊂then␈α∂we
␈↓ α←␈↓want␈α↔a␈α↔means␈α↔of␈α↔distinguishing␈α↔between␈α↔the␈α↔"first"␈α↔and␈α↔the␈α⊗"second"
␈↓ α←␈↓component.␈αThe␈α"dot␈αnotation"␈αdoes␈αthis␈αfor␈αus,␈αbut␈αobviously␈α"(",␈α")",␈αand␈α"."
␈↓ α←␈↓of␈α
the␈α
dotted-pairs␈αare␈α
simply␈α
notation␈αor␈α
syntax.␈α
We␈αcould␈α
have␈α
just␈αas␈α
well
␈↓ α←␈↓represented␈α⊃the␈α⊃dotted-pair␈α⊃of␈α⊃␈↓αA␈↓␈α⊃and␈α⊃␈↓αB␈↓␈α⊃as␈α⊃the␈α⊃set-theoretic␈α∩ordered␈α⊃pair,
␈↓ α←␈↓␈↓α<A,B>␈↓␈α∂or␈α⊂any␈α∂other␈α∂notation␈α⊂which␈α∂preserves␈α∂the␈α⊂essentials␈α∂of␈α⊂the␈α∂domain
␈↓ α←␈↓␈↓<sexpr>␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∞distinctions␈α∞between␈α∂abstract␈α∞objects␈α∞and␈α∞their␈α∂representation␈α∞are
␈↓ α←␈↓quite␈α
important.␈α
As␈α
we␈α
continue␈α
our␈α
study␈α
of␈α
more␈α
and␈α
more␈α
complex␈αdata
␈↓ α←␈↓structures␈α⊗the␈α⊗use␈α⊗of␈α∃an␈α⊗abstract␈α⊗data␈α⊗structure␈α∃instead␈α⊗of␈α⊗one␈α⊗of␈α∃its
␈↓ α←␈↓representations␈α
can␈α
mean␈α
the␈α∞difference␈α
between␈α
a␈α
clear␈α
and␈α∞clean␈α
program
␈↓ α←␈↓and␈α∂a␈α∂confusing␈α∂and␈α∂complicated␈α∂program.␈α∂There␈α∂are␈α∂similar␈α∂gains␈α∂for␈α∞us
␈↓ α←␈↓when␈α∞we␈α∂study␈α∞algorithms␈α∂defined␈α∞over␈α∞these␈α∂abstract␈α∞data␈α∂structures.␈α∞The
␈↓ α←␈↓less␈α
the␈α
algorithm␈α
knows␈α
about␈α∞the␈α
representation␈α
of␈α
the␈α
data␈α∞structure,␈α
the
␈↓ α←␈↓easier␈α⊂it␈α∂will␈α⊂be␈α∂to␈α⊂modify␈α∂or␈α⊂understand␈α∂that␈α⊂algorithm.␈α∂Indeed␈α⊂you␈α∂may
␈↓ α←␈↓have␈α⊃already␈α⊃experienced␈α⊃this␈α⊃phenomenon␈α⊃if␈α⊃you␈α⊃have␈α⊃programmed.␈α⊂ A
␈↓ α←␈↓program␈α≡written␈α∨in␈α≡a␈α∨high-level␈α≡language␈α∨is␈α≡almost␈α∨always␈α≡more
␈↓ α←␈↓understandable␈α_than␈α_its␈α_machine-language␈α_counterpart.␈α_ The␈α_high-level
␈↓ α←␈↓program␈α∂is␈α∂more␈α∂abstract␈α∂whereas␈α∂the␈α∂machine-language␈α∂program␈α∂knows␈α∞a
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 6␈↓Just␈αas␈αthe␈α
"successor"␈αnotation␈αshows␈α
the␈αconstruction␈αof␈αthe␈α
numbers
␈↓ α←␈↓from␈α
␈↓α0␈↓.␈αThis␈α
kind␈α
of␈αnotation␈α
will␈α
be␈αmuch␈α
more␈α
useful␈αin␈α
LISP,␈α
since␈αour
␈↓ α←␈↓interest␈α⊃in␈α∩data␈α⊃structures␈α⊃will␈α∩focus␈α⊃on␈α⊃the␈α∩construction␈α⊃process␈α∩and␈α⊃the
␈↓ α←␈↓interrelationships between components of an S-expr.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 7␈↓␈↓α2␈↓,␈α∞II␈α∞in␈α∞Roman␈α∞numerals,␈α∞10␈α∂in␈α∞binary,␈α∞"zwei"␈α∞in␈α∞German␈α∞...␈α∂are␈α∞all
␈↓ α←␈↓representations of the same number.
␈↓ α←␈↓␈↓8 Symbolic expressions␈↓
(1.2␈↓
␈↓"β␈↓ α←␈↓great␈α∨deal␈α∨about␈α∨representations.␈α≡ Finally,␈α∨if␈α∨you␈α∨still␈α∨doubt␈α≡that
␈↓ α←␈↓representations␈α∀make␈α∀a␈α∀difference␈α∀in␈α∀clarity,␈α∀try␈α∀doing␈α∀long␈α∀division␈α∀in
␈↓ α←␈↓Roman␈α≤numerals.␈α≤ We␈α≤will␈α≥say␈α≤much␈α≤more␈α≤about␈α≥abstraction␈α≤and
␈↓ α←␈↓representation in algorithms and data structures as we proceed.
␈↓"β␈↓ α←␈↓␈↓ β{␈↓↓1.3 Trees: Representations of Symbolic expressions␈↓
␈↓"β␈↓ α←␈↓Besides␈α∩the␈α∩more␈α⊃conventional␈α∩typographical␈α∩notations,␈α∩S-expressions␈α⊃also
␈↓ α←␈↓have␈α∨interesting␈α∨␈↓¬graphical␈↓␈α≡representations.␈α∨ S-exprs␈α∨have␈α∨a␈α≡natural
␈↓ α←␈↓interpretation as a structure which we call a LISP-tree or L-tree.
␈↓"β␈↓ α←␈↓Here are some L-trees:
␈↓"⊃␈↓ α←␈↓∂ /\ /\
␈↓"␈↓ α←␈↓∂ / \ / \
␈↓"␈↓ α←␈↓∂ / \ / /\
␈↓"␈↓ α←␈↓∂ /\ /\ / / \
␈↓"␈↓ α←␈↓∂ / \ / \ ␈↓αA␈↓∂ ␈↓αB␈↓∂ ␈↓αNIL␈↓∂
␈↓"␈↓ α←␈↓∂ ␈↓α1␈↓∂ ␈↓α2␈↓∂ ␈↓αA␈↓∂ /\
␈↓"␈↓ α←␈↓∂ ␈↓αD␈↓∂ ␈↓αE␈↓∂
␈↓"∀␈↓ α←␈↓We can give an inductive definition:
␈↓"λ␈↓ α←␈↓␈↓↓1.␈↓ Any element of <atom> is an L-tree.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ If ␈↓αn␈↓β1␈↓ and ␈↓αn␈↓β2␈↓ are L-trees then
␈↓"␈↓ α←␈↓∂ /\
␈↓"␈↓ α←␈↓∂ / \
␈↓"␈↓ α←␈↓∂ / \
␈↓"␈↓ α←␈↓∂ ␈↓αn␈↓β1␈↓∂ ␈↓αn␈↓β2␈↓∂
␈↓"∀␈↓ α←␈↓also␈α⊂forms␈α⊂an␈α⊂L-tree.␈α⊂ Most␈α⊂important:␈α⊂there␈α⊂are␈α⊂no␈α⊃intersecting␈α⊂branches.
␈↓ α←␈↓Later we will talk about more general structures called list-structures.
␈↓"β␈↓ α←␈↓␈↓ β'You␈α∀can␈α∀see␈α∀how␈α∀to␈α∀interpret␈α∀S-exprs␈α∀as␈α∀L-trees.␈α∀ The␈α∀atoms␈α∪are
␈↓ α←␈↓interpreted␈αas␈αterminal␈αnodes;␈αand␈αsince␈αnon-atomic␈αS-exprs␈αalways␈αhave␈αtwo
␈↓ α←␈↓sub-expressions␈αwe␈αcan␈αwrite␈α
the␈αfirst␈αsub-expression␈αas␈α
the␈αleft␈αbranch␈αof␈α
an
␈↓ α←␈↓L-tree and the second sub-expression as the right branch.
␈↓ α←␈↓␈↓1.3␈↓ ¬TTrees: Representations of Symbolic expressions 9␈↓
␈↓"β␈↓ α←␈↓For example:
␈↓"⊃␈↓ α←␈↓∂␈↓α(A . B)␈↓∂␈↓ ∧␈␈↓α(A . (B . C))␈↓∂␈↓ π+␈↓α((A . B) . C)␈↓∂
␈↓"␈↓ α←␈↓∂ /\␈↓ ∧␈ /\␈↓ π∨ /\
␈↓"␈↓ α←␈↓∂ / \␈↓ ∧␈ / \␈↓ π∨ / \
␈↓"␈↓ α←␈↓∂␈↓αA␈↓∂ ␈↓αB␈↓∂␈↓ ∧␈ / /\␈↓ π∨ /\ \
␈↓"␈↓ α←␈↓∂␈↓ ∧␈ / / \␈↓ π∨ / \ \
␈↓"␈↓ α←␈↓∂␈↓ ∧␈ ␈↓αA␈↓∂ ␈↓αB␈↓∂ ␈↓αC␈↓∂␈↓ π+ ␈↓αA␈↓∂ ␈↓αB␈↓∂ ␈↓αC␈↓∂
␈↓"∀␈↓ α←␈↓␈↓ β'Other␈α≡representations␈α≡of␈α∨LISP-trees␈α≡are␈α≡possible;␈α∨for␈α≡example
␈↓ α←␈↓␈↓α(A . (B . C))␈↓ can be expressed as: ␈↓α
␈↓"⊃␈↓ α←␈↓
⊂αααπααα⊃ ⊂αααπααα⊃
␈↓"␈↓ α←␈↓
ααααα→~ # ~ #αβααααααααααα→~ # ~ # ~
␈↓"␈↓ α←␈↓
%αβα∀ααα$ %αβα∀αβα$
␈↓"␈↓ α←␈↓
↓ ↓ ↓
␈↓"␈↓ α←␈↓
␈↓αA␈↓
␈↓αB␈↓
␈↓αC␈↓
␈↓"∀␈↓ α←␈↓or:
␈↓"⊃␈↓ α←␈↓
⊂αααπααα⊃ ⊂αααπααα⊃
␈↓"␈↓ α←␈↓
ααααα→~ A ~ #αβααααααααααα→~ B ~ C ~
␈↓"␈↓ α←␈↓
%ααα∀ααα$ %ααα∀ααα$
␈↓"∀␈↓ α←␈↓These last two representations are called ␈↓↓box-notation␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Please␈α
keep␈α
in␈αmind␈α
the␈α
distinction␈α
between␈αthe␈α
abstract␈α
S-expr␈αand␈α
the
␈↓ α←␈↓several␈α!representations␈α!which␈α!we␈α!have␈α!shown.␈α! The␈α"question␈α!of
␈↓ α←␈↓representation␈αis␈αso␈αimportant␈αand␈αwill␈αoccur␈αso␈αfrequently␈αthat␈αwe␈αintroduce
␈↓ α←␈↓notation␈α∩for␈α∪a␈α∩representational␈α∪mapping,␈α∩␈↓λr␈↓.␈α∩ To␈α∪represent␈α∩domain␈α∪␈↓D␈↓␈α∩in
␈↓ α←␈↓domain␈α∂␈↓E␈↓,␈α∂we␈α⊂will␈α∂define␈α∂a␈α∂function␈α⊂␈↓λr␈↓βD→E␈↓␈α∂which␈α∂usually␈α∂will␈α⊂be␈α∂specified
␈↓ α←␈↓inductively, and will express the desired mapping.
␈↓"β␈↓ α←␈↓␈↓ β'For example a representational mapping ␈↓λr␈↓β<sexpr>→L-tree␈↓ can be given:
␈↓"β␈↓ α←␈↓␈↓ ¬A␈↓λr␈↓∞(␈↓<atom>␈↓∞)␈↓ = <atom>
␈↓" ␈↓ α←␈↓and for ␈↓λα␈↓ and ␈↓λβ␈↓ in <sexpr>:
␈↓"␈↓ α←␈↓∂␈↓ ∧␈␈↓λr␈↓∞(␈↓α(␈↓λα␈↓α . ␈↓λβ␈↓α)␈↓∞)␈↓ =␈↓∂␈↓ ε≠ /\
␈↓"␈↓ α←␈↓∂␈↓ ∧␈␈↓ ε∂ / \
␈↓"␈↓ α←␈↓∂␈↓ ∧␈␈↓ ε∂ / \
␈↓"␈↓ α←␈↓∂␈↓ ∧␈␈↓ ε∂␈↓λr␈↓∞(␈↓λα␈↓∞)␈↓∂ ␈↓λr␈↓∞(␈↓λβ␈↓∞)␈↓∂
␈↓"β␈↓ α←␈↓␈↓ β'Typically␈α⊗context␈α∃will␈α⊗determine␈α⊗the␈α∃appropriate␈α⊗subscript␈α⊗on␈α∃the
␈↓ α←␈↓␈↓λr␈↓-mapping; thus we will omit it.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"∀␈↓ α←␈↓1. Which of the following are dotted-pairs?
␈↓" ␈↓ α←␈↓␈↓ ββ␈↓↓a.␈↓α (X . Y)␈↓ ∧+␈↓↓b.␈↓α ((A . (B . C))␈↓ ε3 ␈↓↓c.␈↓α A2␈↓ πs ␈↓↓d.␈↓α (X . Y2 . Z)
␈↓"∀␈↓ α←␈↓2. Write the following as LISP trees:
␈↓" ␈↓ α←␈↓␈↓ ββ␈↓↓a.␈↓α ((A . B) . (B . (C . D)))␈↓ ε3␈↓ πs␈↓↓b.␈↓α (((A . B) . C) . E)
␈↓" ␈↓ α←␈↓α␈↓ ββ␈↓↓c.␈↓α ((X . NIL) . (Y . (Z . NIL)))␈↓ ε3␈↓ πs␈↓↓d.␈↓α (NIL . NIL)
␈↓ α←␈↓␈↓10 Symbolic expressions␈↓
(1.3␈↓
␈↓"β␈↓ α←␈↓␈↓ β'3. Write the following LISP trees as S-exprs:
␈↓"ε␈↓ α←␈↓∂␈↓ β' ␈↓↓a.␈↓∂␈↓ ¬; ␈↓↓b.␈↓∂␈↓ π+ ␈↓↓c.␈↓∂
␈↓"␈↓ α←␈↓∂␈↓ β' /\␈↓ ¬; /\␈↓ π+ /\
␈↓"␈↓ α←␈↓∂␈↓ β' / \␈↓ ¬; / \␈↓ π+ / \
␈↓"␈↓ α←␈↓∂␈↓ β' / /\␈↓ ¬; A /\␈↓ π+ A /\
␈↓"␈↓ α←␈↓∂␈↓ β' / / \␈↓ ¬; / \␈↓ π+ / \
␈↓"␈↓ α←␈↓∂␈↓ β'A B C␈↓ ¬; / \␈↓ π+ B /\
␈↓"␈↓ α←␈↓∂␈↓ ¬; /\ /\␈↓ π+ / \
␈↓"␈↓ α←␈↓∂␈↓ ¬; / \ D E␈↓ π+ C /\
␈↓"␈↓ α←␈↓∂␈↓ ¬; A NIL␈↓ π+ / \
␈↓"␈↓ α←␈↓∂␈↓ ¬;␈↓ π+ D NIL
␈↓"␈↓ α←␈↓
␈↓↓d.␈↓
␈↓↓e.␈↓
␈↓"␈↓ α←␈↓
⊂αααπααα⊃ ⊂αααπααααα⊃ ⊂ααααααπααα⊃ ⊂αααπααα⊃ ⊂αααπααααα⊃
␈↓"␈↓ α←␈↓
~ # ~ #αβα→~ # ~ # ~ ~ CONS ~ #αβα→~ X ~ #αβα→~ Y ~ NIL ~
␈↓"␈↓ α←␈↓
%αβα∀ααα$ %αβα∀ααβαα$ %αααααα∀ααα$ %ααα∀ααα$ %ααα∀ααααα$
␈↓"␈↓ α←␈↓
↓ ~ ↓
␈↓"␈↓ α←␈↓
CAR ~ NIL
␈↓"␈↓ α←␈↓
~ ⊂αααααπααα⊃ ⊂αααπααααα⊃
␈↓"␈↓ α←␈↓
%α→~ # ~ #αβα→~ # ~ # ~
␈↓"␈↓ α←␈↓
%ααβαα∀ααα$ %αβα∀ααβαα$
␈↓"␈↓ α←␈↓
↓ ↓ ↓
␈↓"␈↓ α←␈↓
QUOTE A NIL
␈↓"β␈↓ α←␈↓␈↓ ¬3␈↓↓1.4 Primitive Functions␈↓
␈↓"β␈↓ α←␈↓So␈α
far␈αwe␈α
have␈αdescribed␈α
the␈α
domain␈αof␈α
abstract␈αobjects␈α
called␈α
S-exprs␈αand
␈↓ α←␈↓have␈α∃exhibited␈α⊗several␈α∃representations␈α∃for␈α⊗these␈α∃objects.␈α∃ We␈α⊗will␈α∃now
␈↓ α←␈↓describe␈α
some␈αfunctions␈α
or␈α
operations␈αto␈α
be␈α
performed␈αon␈α
this␈α
domain.␈α We
␈↓ α←␈↓need␈α
to␈α
be␈α
a␈α
bit␈α
careful␈α
here.␈α
We␈α
are␈α
about␈α
to␈α
see␈α
one␈α
of␈α
the␈αmain␈α
differences
␈↓ α←␈↓between␈αmathematics␈αand␈αcomputer␈αscience:␈αmathematics␈αemphasizes␈αthe␈αidea
␈↓ α←␈↓of␈α∞function;␈α∂computer␈α∞science␈α∂emphasizes␈α∞the␈α∞idea␈α∂of␈α∞algorithm,␈α∂process,␈α∞or
␈↓ α←␈↓procedure.
␈↓"β␈↓ α←␈↓␈↓ β'Mathematically␈αa␈αfunction␈αis␈αsimply␈αa␈αmapping␈αsuch␈αthat␈αfor␈αany␈α
given
␈↓ α←␈↓argument␈αin␈αthe␈αdomain␈αof␈αthe␈αfunction␈αthere␈αexists␈αa␈αunique␈αcorresponding
␈↓ α←␈↓value.␈α
In␈α
elementary␈α
set␈α
theory,␈α
a␈α
definition␈α
of␈α
function␈α
␈↓αf␈↓␈α
involves␈αsaying␈α
that
␈↓ α←␈↓␈↓αf␈↓␈α
is␈α
a␈α
set␈α∞of␈α
ordered␈α
pairs␈α
␈↓αf = { <x␈↓β1␈↓α, y␈↓β1␈↓α>, ...}␈↓;␈α
the␈α∞␈↓αx␈↓βi␈↓'s␈α
are␈α
all␈α
distinct␈α∞and␈α
the
␈↓ α←␈↓value␈αof␈α
the␈αfunction␈α
␈↓αf␈↓␈αfor␈αan␈α
argument␈α␈↓αx␈↓βi␈↓␈α
is␈αdefined␈α
to␈αbe␈αthe␈α
corresponding
␈↓ α←␈↓␈↓αy␈↓βi␈↓.␈α No␈αrule␈αof␈αcomputation␈αis␈αgiven␈αto␈αlocate␈αvalues;␈αwith␈αthe␈αfirst␈αdefinition
␈↓ α←␈↓it␈αis␈αimplicit␈αthat␈αthe␈αinternal␈αstructure␈αof␈αthe␈αmapping␈αdoesn't␈αmatter;␈αin␈αthe
␈↓ α←␈↓set-theoretic definition, the correspondence is explicitly given.
␈↓"β␈↓ α←␈↓␈↓ β'An␈α⊃algorithm␈α⊂or␈α⊃procedure␈α⊂is␈α⊃a␈α⊂process␈α⊃for␈α⊂computing␈α⊃values␈α⊃for␈α⊂a
␈↓ α←␈↓function.␈α∩The␈α∩factorial␈α⊃function,␈α∩␈↓αn!␈↓,␈α∩can␈α⊃be␈α∩computed␈α∩by␈α∩many␈α⊃different
␈↓ α←␈↓algorithms; but as a function it is a set
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ∧`␈↓α{<0,1>, <1,1>, <2,2>, <3,6>, ...<n,n!>, ...}␈↓.
␈↓"∀␈↓ α←␈↓The␈α
␈↓↓domain␈↓␈α
of␈αa␈α
function␈α
is␈α
the␈αset␈α
of␈α
all␈α
values␈αfor␈α
which␈α
the␈α
function␈αis
␈↓ α←␈↓␈↓1.4␈↓ λ∀Primitive Functions 11␈↓
␈↓"β␈↓ α←␈↓defined;␈α
the␈α
␈↓↓range␈↓␈α∞of␈α
a␈α
function␈α∞is␈α
the␈α
set␈α
of␈α∞all␈α
values␈α
which␈α∞the␈α
function
␈↓ α←␈↓takes␈α∪on.␈α∪ A␈α∩careful␈α∪definition␈α∪of␈α∪a␈α∩function␈α∪requires␈α∪specification␈α∪of␈α∩a
␈↓ α←␈↓further␈αset␈αcalled␈αthe␈α␈↓↓domain␈αof␈αdiscourse␈↓.␈αThe␈αdomain␈αof␈αdiscourse,␈αnamed
␈↓ α←␈↓␈↓D␈↓,␈α⊃consists␈α⊃of␈α⊃all␈α⊃possible␈α⊃values␈α⊃which␈α⊃may␈α⊃occur␈α⊃as␈α⊃the␈α⊃argument␈α⊃to␈α⊃a
␈↓ α←␈↓function.␈α If␈αthe␈αdomain␈αof␈αa␈αparticular␈αfunction␈α␈↓αf␈↓␈αcoincides␈αwith␈α␈↓D␈↓␈αthen␈α␈↓αf␈↓␈αis
␈↓ α←␈↓said␈αto␈αbe␈αa␈α␈↓↓total␈αfunction␈↓␈αover␈α␈↓D␈↓;␈αif␈αthere␈αare␈αelements␈αof␈α␈↓D␈↓␈αwhich␈αare␈αnot
␈↓ α←␈↓in␈α
the␈αdomain␈α
of␈α␈↓αf␈↓␈α
then␈α␈↓αf␈↓␈α
is␈αa␈α
␈↓↓partial␈αfunction␈↓␈α
over␈α␈↓D␈↓,␈α
and␈α␈↓αf␈↓␈α
is␈αsaid␈α
to␈αbe
␈↓ α←␈↓␈↓↓undefined␈↓␈α∞for␈α
those␈α∞values.␈α
For␈α∞example,␈α
the␈α∞factorial␈α
function␈α∞is␈α
typically
␈↓ α←␈↓considered␈αto␈αbe␈αpartial␈αover␈αthe␈αintegers:␈αtotal␈αfor␈αthe␈αnatural␈αnumbers,␈αbut
␈↓ α←␈↓undefined␈α∞for␈α∞negative␈α∞integers.␈α∞ Thus␈α∞the␈α∞concept␈α∞of␈α∞"total"␈α∞or␈α∞"partial"␈α∞is
␈↓ α←␈↓relative␈αto␈αa␈αspecified␈αdomain␈αof␈αdiscourse.␈α However,␈αa␈αfunction␈α␈↓αf␈↓␈αtotal␈αover
␈↓ α←␈↓a␈αdomain␈α␈↓D␈↓β1␈↓␈αcan␈αbe␈αextended␈αto␈αbe␈αtotal␈αover␈αa␈αdomain␈α␈↓D␈↓β1␈↓∪␈↓D␈↓β2␈↓␈αby␈αassigning
␈↓ α←␈↓values␈α∀to␈α∀␈↓αf(d)␈↓␈α∃for␈α∀␈↓αd␈↓λε␈↓D␈↓β2␈↓-␈↓D␈↓β1␈↓.␈α∀In␈α∀this␈α∃way,␈α∀for␈α∀example,␈α∀factorial␈α∃can␈α∀be
␈↓ α←␈↓extended␈αto␈αbe␈αtotal␈αover␈αthe␈αintegers␈αby␈αdefining␈α␈↓αn!␈↓␈αto␈αbe␈α␈↓α0␈↓␈αfor␈α␈↓αn␈↓␈αless␈αthan
␈↓ α←␈↓␈↓α0␈↓.␈α
We␈αmay␈α
extend␈α
the␈αrange␈α
of␈α
a␈αfunction␈α
when␈α
we␈αextend␈α
the␈αdomain;␈α
thus
␈↓ α←␈↓␈↓αf(d)␈↓␈αneed␈αnot␈αbe␈αin␈αthe␈αrange␈αof␈αthe␈αoriginal␈α␈↓αf␈↓.␈α For␈αexample,␈αwe␈αadded␈α␈↓α0␈↓␈αto
␈↓ α←␈↓the␈α⊂range␈α⊂when␈α⊃we␈α⊂extended␈α⊂the␈α⊂factorial␈α⊃function.␈α⊂When␈α⊂we␈α⊃extend␈α⊂the
␈↓ α←␈↓range we must specify what additions have been made.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α⊃substantive␈α⊂decision␈α⊃needs␈α⊂to␈α⊃be␈α⊂made␈α⊃on␈α⊂how␈α⊃we␈α⊂are␈α⊃to␈α⊂handle
␈↓ α←␈↓partial␈α∞functions.␈↓π 8␈↓␈α∞Since␈α
we␈α∞are␈α∞attempting␈α
to␈α∞be␈α∞reasonably␈α∞realistic␈α
about
␈↓ α←␈↓our␈α⊂modelling␈α⊂of␈α⊂computation␈α⊂we␈α⊂should␈α⊂be␈α⊂as␈α⊂precise␈α⊂as␈α⊂possible␈α⊃in␈α⊂our
␈↓ α←␈↓formalism.␈α
We␈α
could␈α
introduce␈α
a␈α
class␈αof␈α
error␈α
values␈α
and␈α
include␈α
them␈αin
␈↓ α←␈↓the␈αrange␈αof␈α␈↓αf␈↓;␈αthese␈αvalues␈αwould␈αbe␈αgiven␈αas␈αthe␈αresult␈αof␈αapplying␈α␈↓αf␈↓␈αto␈αan
␈↓ α←␈↓argument␈α∪not␈α∀in␈α∪its␈α∀domain;␈α∪or␈α∀we␈α∪could␈α∀simply␈α∪say␈α∀that␈α∪the␈α∀result␈α∪is
␈↓ α←␈↓"unspecified".␈↓π 9␈↓␈α⊂We␈α∂shall␈α⊂pick␈α∂an␈α⊂intermediate␈α∂position;␈α⊂we␈α⊂shall␈α∂introduce
␈↓ α←␈↓␈↓¬one␈↓␈α
new␈α∞element,␈α
␈↓λB␈↓,␈α∞called␈α
"unspecified"␈α
or␈α∞"undefined",␈α
or␈α∞"bottom".␈↓π 10␈↓␈α
We
␈↓ α←␈↓will␈αdefine␈αall␈αour␈αfunctions␈αover␈αdomains␈αaugmented␈αwith␈αthis␈αelement;␈αthus
␈↓ α←␈↓constructs␈αlike␈α␈↓αf(␈↓λB␈↓α) = a␈↓␈αare␈αallowed.␈αFor␈αthe␈αmoment,␈αthink␈αof␈α␈↓λB␈↓␈αas␈αcovering
␈↓ α←␈↓all␈α∀anomalous␈α∪conditions␈α∀which␈α∀could␈α∪be␈α∀detected␈α∪and␈α∀printed␈α∀as␈α∪error
␈↓ α←␈↓messages; later we will refine this interpretation.
␈↓"β␈↓ α←␈↓␈↓ β'As␈αwe␈αdefine␈αnew␈αdata␈αstructures␈α
we␈αwill␈αfrequently␈αwant␈αto␈αextend␈α
our
␈↓ α←␈↓functions␈α
to␈α
larger␈α
domains.␈αFor␈α
most␈α
of␈α
our␈αpurposes,␈α
a␈α
function␈α
␈↓αf␈↓␈αdefined
␈↓ α←␈↓on␈α(an␈αaugmented␈αdomain)␈α␈↓D␈↓␈αwill␈αbe␈αextended␈αto␈αa␈αlarger␈αdomain,␈α␈↓D␈↓∪␈↓D␈↓β1␈↓,␈αby
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 8␈↓Partial␈α
functions␈α
occur␈α
naturally␈α
in␈α
computation.␈α
Most␈α
programs␈α
will
␈↓ α←␈↓fail␈α∀to␈α∀give␈α∪results␈α∀under␈α∀some␈α∪circumstances.␈α∀The␈α∀function␈α∀which␈α∪that
␈↓ α←␈↓program␈α∀is␈α∀computing␈α∀is␈α∃a␈α∀partial␈α∀function.␈α∀Some␈α∀error␈α∃conditions␈α∀can
␈↓ α←␈↓produce␈α⊃error␈α⊃messages;␈α⊃some␈α⊃error␈α⊃conditions␈α⊃may␈α⊃cause␈α⊃the␈α⊃program␈α⊂to
␈↓ α←␈↓loop. We will analyze both situations.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 9␈↓How␈α∞"unspecified"␈α∞manifests␈α∞itself␈α∞on␈α
a␈α∞machine␈α∞will␈α∞depend␈α∞on␈α
the
␈↓ α←␈↓implementation. Sometimes error messages are given; sometimes not.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 10␈↓"bottom" is sometimes written ␈↓λW␈↓.
␈↓ α←␈↓␈↓12 Symbolic expressions␈↓
(1.4␈↓
␈↓"β␈↓ α←␈↓defining␈α→␈↓αf(d␈↓β1␈↓α) = f(␈↓λB␈↓α)␈↓␈α→for␈α→␈↓αd␈↓β1␈↓λε␈↓D␈↓β1␈↓-␈↓D␈↓,␈↓π 11␈↓␈α→therefore␈α→␈↓αf(␈↓λB␈↓α)␈↓␈α→need␈α→not␈α→be␈α_␈↓λB␈↓.
␈↓ α←␈↓However␈αmany␈α
of␈αthe␈α
functions␈αwhich␈α
we␈αwill␈α
examine␈αare␈α
defined␈αsuch␈α
that
␈↓ α←␈↓␈↓αf(..., ␈↓λB␈↓α, ...) = ␈↓λB␈↓.␈α↔Functions␈α↔which␈α↔possess␈α↔this␈α↔property␈α↔are␈α↔called␈α⊗␈↓↓strict
␈↓ α←␈↓↓functions␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'To␈α∞apply␈α∂this␈α∞discussion␈α∂of␈α∞␈↓λB␈↓␈α∞to␈α∂S-exprs␈α∞we␈α∂will␈α∞define␈α∂an␈α∞extended
␈↓ α←␈↓domain ␈↓S␈↓ to be:
␈↓"∀␈↓ α←␈↓␈↓ ¬Q␈↓S␈↓ = ␈↓<sexpr>␈↓ ∪ {␈↓λB␈↓}
␈↓"∀␈↓ α←␈↓Then␈αwe␈αcan␈αtalk␈αabout␈αfunctions␈αwhich␈αare␈αtotal␈αover␈α␈↓S␈↓␈αor␈α
over␈α␈↓<sexpr>␈↓,
␈↓ α←␈↓and␈αwe␈αwill␈α
talk␈αabout␈αfunctions␈α
which␈αare␈αpartial␈α
over␈α␈↓<sexpr>␈↓.␈α When␈α
we
␈↓ α←␈↓ask␈αif␈αan␈αS-expr␈αfunction␈αis␈αpartial␈αor␈αtotal␈αwithout␈αspecifying␈αa␈αdomain,␈αwe
␈↓ α←␈↓are asking the question over the natural, unextended domain, ␈↓<sexpr>␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∂will␈α∞now␈α∂move␈α∂towards␈α∞a␈α∂more␈α∞algorithmic␈α∂presentation.␈α∂We␈α∞will
␈↓ α←␈↓return␈αto␈αthe␈αmathematical␈αaspects␈αoccasionally,␈αbut␈αour␈αmain␈αconcern␈αin␈αthis
␈↓ α←␈↓text␈αis␈αa␈αtreatment␈αof␈αalgorithms␈αexpressed␈αin␈αLISP.␈αWe␈αwill␈αcontinue␈αto␈αsay
␈↓ α←␈↓"LISP function"␈αor␈αjust␈α"function",␈αbut␈αwhat␈αwe␈αare␈αexpressing␈αor␈αdescribing
␈↓ α←␈↓is␈α∂a␈α∞particular␈α∂algorithm␈α∂or␈α∞procedure,␈α∂not␈α∂a␈α∞function␈α∂in␈α∂the␈α∞mathematical
␈↓ α←␈↓sense.␈α⊃When␈α⊃we␈α⊃wish␈α⊃to␈α⊃stress␈α⊂the␈α⊃distinction␈α⊃we␈α⊃will␈α⊃use␈α⊃"procedure"␈α⊂or
␈↓ α←␈↓"algorithm".
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∂first␈α∂LISP␈α∂function␈α∂we␈α∂consider␈α∂is␈α∂␈↓αcons␈↓.␈α∂This␈α∂binary␈α∂function␈α∂is
␈↓ α←␈↓used␈α∩to␈α∩generate␈α⊃S-exprs␈α∩from␈α∩less␈α∩complicated␈α⊃S-exprs.␈α∩ ␈↓αcons␈↓␈α∩is␈α∩called␈α⊃a
␈↓ α←␈↓␈↓↓constructor␈↓-function␈αand␈αis␈αa␈αstrict␈αfunction;␈↓π 12␈↓␈αit␈αis␈αa␈αtotal␈αfunction␈αover␈αthe
␈↓ α←␈↓domain␈α␈↓S␈↓.␈αMore␈αprecisely,␈αsince␈α␈↓αcons␈↓␈αis␈αa␈α␈↓↓binary␈↓␈αfunction,␈αeach␈α
argument␈αof
␈↓ α←␈↓␈↓αcons␈↓␈α
is␈αfree␈α
to␈αtake␈α
on␈αvalues␈α
from␈α
␈↓S␈↓.␈↓π 13␈↓␈αWhenever␈α
␈↓αcons␈↓␈αis␈α
presented␈αwith␈α
two
␈↓ α←␈↓elements␈α⊂␈↓λα␈↓␈α∂and␈α⊂␈↓λβ␈↓␈α⊂from␈α∂␈↓<sexpr>␈↓,␈α⊂␈↓αcons[␈↓λα␈↓α;␈↓λβ␈↓α]␈↓␈α⊂returns␈α∂a␈α⊂new␈α⊂S-expr␈α∂␈↓α(␈↓λα␈↓α . ␈↓λβ␈↓α)␈↓.
␈↓ α←␈↓Interpreted␈αas␈αa␈αLISP-tree,␈α␈↓αcons[␈↓λα␈↓α;␈↓λβ␈↓α]␈↓␈αforms␈αa␈αnew␈αLISP␈αtree␈αwhich␈αhas␈αa␈αleft
␈↓ α←␈↓branch ␈↓λα␈↓ and has a right branch ␈↓λβ␈↓.
␈↓""␈↓ α←␈↓For example:␈↓ ¬G␈↓αcons[A; B] = (A . B)
␈↓""␈↓ α←␈↓α␈↓ ¬Gcons[(A . B); C] = ((A . B) . C)␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Expressions␈α⊂which␈α⊂can␈α⊂have␈α⊃a␈α⊂value,␈α⊂are␈α⊂called␈α⊂␈↓↓forms␈↓.␈α⊃ S-exprs␈α⊂are
␈↓ α←␈↓forms␈αsince␈αthey␈αare␈αthe␈αconstants␈αof␈αour␈αlanguage:␈αthe␈αvalue␈αof␈αa␈αconstant␈αis
␈↓ α←␈↓that␈α⊃constant.␈α⊂ Function␈α⊃applications␈α⊃are␈α⊂forms:␈α⊃the␈α⊃value␈α⊂is␈α⊃the␈α⊃result␈α⊂of
␈↓ α←␈↓performing the designated function on the designated arguments.
␈↓"β␈↓ α←␈↓␈↓ β'Notice␈α↔that␈α↔we␈α↔are␈α↔designating␈α↔function␈α↔application␈α↔in␈α↔LISP␈α↔by
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 11␈↓The␈α
exception␈αto␈α
this␈αextension␈α
convention␈αinvolves␈α
the␈αdefinition␈α
of
␈↓ α←␈↓predicates␈α
which␈α
can␈α
tell␈α
whether␈α
or␈α
not␈α
an␈α
arbitrary␈α
element␈α
is␈α
in␈αa␈α
specified
␈↓ α←␈↓domain.␈α⊃These␈α⊃predicates␈α∩always␈α⊃give␈α⊃true␈α⊃or␈α∩false␈α⊃when␈α⊃applied␈α∩to␈α⊃any
␈↓ α←␈↓element other than ␈↓λB␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 12␈↓For an alternative interpretation of ␈↓αcons␈↓ see [Fri 76a].
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 13␈↓We could also say that ␈↓αcons␈↓ is total over the Cartesian product ␈↓SxS␈↓.
␈↓ α←␈↓␈↓1.4␈↓ λ∀Primitive Functions 13␈↓
␈↓"β␈↓ α←␈↓"function␈α∞name,␈α∂followed␈α∞by␈α∂a␈α∞list␈α∞of␈α∂arguments␈α∞delimited␈α∂by␈α∞`['␈α∂and␈α∞`]'."␈↓π 14␈↓
␈↓ α←␈↓The␈α→`[...]'-notation␈α~is␈α→part␈α~of␈α→the␈α~LISP␈α→syntax␈α~and␈α→we␈α~will␈α→reserve
␈↓ α←␈↓`(...)'-notation␈αfor␈α
the␈αfunction␈α
application␈αof␈α
mathematics.␈αIn␈α
a␈αfew␈α
places␈αin
␈↓ α←␈↓our␈α∞discussions␈α∞the␈α∞distinction␈α∞will␈α∞be␈α∞important.␈α∞ Typically␈α∞the␈α∞distinctions
␈↓ α←␈↓will␈αoccur␈αwhen␈αwe␈αwish␈αto␈αdistinguish␈αbetween␈αthe␈αLISP␈αalgorithm␈αand␈αthe
␈↓ α←␈↓mathematical function computed by that algorithm.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α↔critical␈α⊗distinction␈α↔has␈α⊗already␈α↔arisen␈α⊗in␈α↔discussing␈α↔forms␈α⊗like
␈↓ α←␈↓␈↓αcons[A;B]␈↓.␈α∀The␈α∀constructor␈α∀␈↓αcons␈↓␈α∀is␈α∃actually␈α∀an␈α∀algorithm.␈α∀ Since␈α∀it␈α∃is␈α∀a
␈↓ α←␈↓primitive␈α∂algorithm␈α∂it␈α⊂will␈α∂be␈α∂represented␈α∂on␈α⊂a␈α∂machine␈α∂by␈α∂a␈α⊂sequence␈α∂of
␈↓ α←␈↓operations␈α∞which␈α
depend␈α∞on␈α
the␈α∞implementation␈α
of␈α∞S-exprs␈α
and␈α∞depend␈α
on
␈↓ α←␈↓the␈αprimitive␈α
operations␈αof␈αthe␈α
hardware␈αmachine.␈α
The␈α␈↓¬process␈↓␈αof␈α
extracting
␈↓ α←␈↓a␈α∩value␈α∩from␈α∩the␈α∩form␈α∩␈↓αcons[A; B]␈↓␈α∩is␈α∩called␈α∩␈↓↓evaluation␈↓.␈α∩Evaluation␈α∩is␈α⊃an
␈↓ α←␈↓algorithmic␈αidea;␈αthere␈αis␈αno␈αidea␈αof␈αevaluation␈αinvolved␈αwith␈αthe␈αconcept␈αof
␈↓ α←␈↓"function".␈α⊂ To␈α⊂reinforce␈α⊂this␈α⊂algorithmic␈α⊂interpretation␈α⊂we␈α⊂will␈α⊂say␈α∂things
␈↓ α←␈↓like␈α
" a function␈α
returns␈α
as␈α
value ..."␈α
meaning␈α
the␈α∞algorithmic␈α
representation
␈↓ α←␈↓of a function computes and produces a value.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∃have␈α⊗two␈α∃strict,␈α⊗unary␈α∃␈↓↓selector␈↓␈α∃functions,␈α⊗␈↓αcar␈↓␈α∃and␈α⊗␈↓αcdr␈↓,␈↓π 15␈↓␈α∃for
␈↓ α←␈↓traversing␈α⊂LISP-trees.␈α⊃ We␈α⊂already␈α⊃know␈α⊂the␈α⊂meaning␈α⊃of␈α⊂"strict";␈α⊃a␈α⊂unary
␈↓ α←␈↓function␈α⊂expects␈α⊂␈↓¬one␈↓␈α∂argument;␈α⊂and␈α⊂a␈α⊂selector␈α∂function␈α⊂is␈α⊂a␈α⊂data␈α∂structure
␈↓ α←␈↓manipulating␈α⊂function␈α⊂which␈α⊃will␈α⊂select␈α⊂a␈α⊃component␈α⊂of␈α⊂a␈α⊃composite␈α⊂data
␈↓ α←␈↓structure.␈α∪ Such␈α∩LISP␈α∪functions␈α∩are␈α∪called␈α∩selectors␈α∪since␈α∩they␈α∪will␈α∩␈↓¬select␈↓
␈↓ α←␈↓components␈α∩of␈α∩non-atomic␈α∩elements␈α∪of␈α∩␈↓<sexpr>␈↓.␈α∩ Thus␈α∩␈↓αcar␈↓␈α∩and␈α∪␈↓αcdr␈↓␈α∩are
␈↓ α←␈↓partial␈α∪functions␈α∪over␈α∪␈↓<sexpr>␈↓:␈α∪they␈α∪give␈α∪values␈α∪in␈α∪␈↓<sexpr>␈↓␈α∪only␈α∩for
␈↓ α←␈↓non-atomic␈α⊃arguments;␈α⊂they␈α⊃give␈α⊃␈↓λB␈↓␈α⊂whenever␈α⊃they␈α⊂are␈α⊃presented␈α⊃with␈α⊂an
␈↓ α←␈↓atomic argument.
␈↓"β␈↓ α←␈↓␈↓ β'When␈α∞given␈α∞a␈α∞non-atomic␈α
argument,␈α∞␈↓α(␈↓λα␈↓α␈α∞.␈α∞␈↓λβ␈↓α)␈↓,␈α
␈↓αcar␈↓␈α∞returns␈α∞as␈α∞value␈α
the
␈↓ α←␈↓first␈αsubexpression,␈α␈↓λα␈↓;␈α␈↓αcdr␈↓␈α(pronounced␈αcould-er)␈αreturns␈αas␈αvalue␈α
the␈αsecond
␈↓ α←␈↓sub-expression ␈↓λβ␈↓.
␈↓""␈↓ α←␈↓For example:␈↓ ¬←␈↓αcar[(A . B)] = A
␈↓"⊃␈↓ α←␈↓α␈↓ ¬←car[A] = ␈↓λB␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬←cdr[(A . B)] = B
␈↓"⊃␈↓ α←␈↓α␈↓ ¬←cdr[(A . (B . C))] = (B . C)
␈↓"⊃␈↓ α←␈↓α␈↓ ¬←car[((A . B) . C)] = (A . B)
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 14␈↓The syntax equations for forms are given on page 17.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 15␈↓These␈α∩names␈α∩are␈α∩hold-overs␈α⊃from␈α∩the␈α∩original␈α∩implementation␈α⊃of
␈↓ α←␈↓LISP␈α∃on␈α∃an␈α∃IBM␈α∃704.␈α∃That␈α∃machine␈α∃had␈α∃partial-word␈α∃instructions␈α∀to
␈↓ α←␈↓reference␈α
the␈α∞␈↓a␈↓ddress␈α
and␈α
␈↓d␈↓ecrement␈α∞parts␈α
of␈α
a␈α∞machine␈α
location.␈α
The␈α∞␈↓αa␈↓␈α
of
␈↓ α←␈↓␈↓αcar␈↓␈αcomes␈αfrom␈α"address",␈αthe␈α␈↓αd␈↓␈αof␈α␈↓αcdr␈↓␈αcomes␈αfrom␈α"decrement".␈αThe␈α␈↓αc␈↓␈αand␈α␈↓αr␈↓
␈↓ α←␈↓come␈α
from␈α
"contents␈α
of"␈α
and␈α
"register".␈α
Thus␈α
␈↓αcar␈↓␈α
could␈α
be␈α
read␈α∞"contents␈α
of
␈↓ α←␈↓address part of register".
␈↓ α←␈↓␈↓14 Symbolic expressions␈↓
(1.4␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'We␈α⊃will␈α⊃include␈α⊂functional␈α⊃composition␈α⊃as␈α⊂a␈α⊃notation␈α⊃for␈α⊂combining
␈↓ α←␈↓LISP␈α∪expressions.␈α∪ The␈α∪composition␈α∪of␈α∪two␈α∪unary␈α∪functions␈α∪␈↓αf␈↓␈α∪and␈α∀␈↓αg␈↓␈α∪is
␈↓ α←␈↓another␈α→function,␈α→denoted␈α→in␈α→mathematics␈α→by␈α→␈↓αf␈↓∞o␈↓αg␈↓.␈α→The␈α→value␈α→of␈α→an
␈↓ α←␈↓expression,␈α␈↓αf␈↓∞o␈↓αg[x]␈↓,␈αis␈αthe␈αvalue␈αof␈α␈↓αf[g[x]]␈↓.␈α That␈αis,␈αthe␈αvalue␈αof␈α␈↓αf␈↓∞o␈↓αg[x]␈↓␈αis␈αa␈α␈↓αz␈↓
␈↓ α←␈↓such␈α⊃that␈α⊃␈↓αy␈↓␈α⊃is␈α⊃the␈α⊂value␈α⊃of␈α⊃␈↓αg[x]␈↓␈α⊃and␈α⊃␈↓αz␈↓␈α⊃is␈α⊂the␈α⊃value␈α⊃of␈α⊃␈↓αf[y]␈↓.␈α⊃␈↓αf␈↓∞o␈↓αg␈↓␈α⊃may␈α⊂be
␈↓ α←␈↓undefined␈α
for␈αseveral␈α
reasons:␈α
␈↓αg[x]␈↓␈αmay␈α
be␈αundefined␈α
and␈α
␈↓αf␈↓␈αis␈α
strict,␈α
or␈α␈↓αf[y]␈↓
␈↓ α←␈↓may be undefined.
␈↓"β␈↓ α←␈↓␈↓ β'Here are some examples of composition:
␈↓"∀␈↓ α←␈↓α␈↓ β:car␈↓∞o␈↓αcdr[(A . (B . C))] = car[cdr[(A . (B . C))]] = car[(B . C)] = B
␈↓"∀␈↓ α←␈↓α␈↓ β8cdr␈↓∞o␈↓αcdr[(A . (C . B))] = cdr[cdr[(A . (C . B))]] = cdr[(C . B)] = B
␈↓"∀␈↓ α←␈↓α␈↓ ¬scdr[cdr[A]] = ␈↓λB␈↓α
␈↓"∀␈↓ α←␈↓α␈↓ ¬Qcar[cdr[(A . B)]] = ␈↓λB␈↓α
␈↓"∀␈↓ α←␈↓α␈↓ ∧Kcar[cons[X;A]] = X cdr[cons[Y;X]] = X
␈↓"∀␈↓ α←␈↓All␈αthe␈αfunctions␈αin␈αthese␈αexamples␈αare␈αstrict;␈αfor␈αthat␈αreason,␈αif␈α␈↓αg[x]␈↓␈αgives␈α␈↓λB␈↓
␈↓ α←␈↓then␈α
the␈α
composition␈α∞␈↓αf␈↓∞o␈↓αg[x]␈↓␈α
also␈α
gives␈α
␈↓λB␈↓.␈α∞That␈α
need␈α
not␈α
be␈α∞the␈α
case␈α
if␈α∞␈↓αf␈↓␈α
is
␈↓ α←␈↓non-strict.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∂composition␈α∂of␈α⊂many␈α∂␈↓αcar␈↓␈α∂and␈α⊂␈↓αcdr␈↓␈α∂functions␈α∂occurs␈α⊂so␈α∂frequently
␈↓ α←␈↓that␈α⊂an␈α⊃abbreviation␈α⊂has␈α⊃been␈α⊂developed.␈α⊂ Given␈α⊃such␈α⊂a␈α⊃composition,␈α⊂we
␈↓ α←␈↓select␈αin␈αleft-to-right␈αorder,␈αthe␈αrelevant␈α␈↓αa␈↓'s␈αand␈α␈↓αd␈↓'s␈αin␈αthe␈α␈↓αcar␈↓'s␈αand␈α␈↓αcdr␈↓'s.␈αWe
␈↓ α←␈↓sandwich␈αthis␈αstring␈αof␈α␈↓αa␈↓'s␈αand␈α␈↓αd␈↓'s␈αbetween␈αa␈αleft-hand␈α␈↓αc␈↓␈αand␈αa␈αright-hand␈α␈↓αr␈↓
␈↓ α←␈↓and give the composition this name.
␈↓"∀␈↓ α←␈↓For example:␈↓α␈↓ ¬Ncadr[x] <= car[cdr[x]]
␈↓"∀␈↓ α←␈↓α␈↓ ¬(caddr[x] <= car[cdr[cdr[x]]]
␈↓"∀␈↓ α←␈↓α␈↓ ¬Ncdar[x] <= cdr[car[x]]
␈↓"∀␈↓ α←␈↓These␈α→compositions␈α→are␈α→also␈α→called␈α→␈↓↓␈↓αcar-cdr-␈↓↓chains␈↓,␈α→and␈α→are␈α→useful␈α→in
␈↓ α←␈↓traversing␈α
LISP-trees.␈α
The␈α
notation␈α
"<="␈α
is␈α
to␈α
be␈α
read␈α
"is␈α
defined␈α
to␈α∞be␈α
the
␈↓ α←␈↓function␈α...".␈α This␈αnotation␈αis␈αonly␈αa␈αtemporary␈αconvenience␈αand␈αnot␈α
part␈αof
␈↓ α←␈↓LISP.␈α
Soon␈α
we␈α
will␈αstudy␈α
what␈α
is␈α
involved␈αin␈α
giving␈α
and␈α
using␈αdefinitions
␈↓ α←␈↓in LISP (Section 3.4). For the moment intuition will suffice.
␈↓"β␈↓ α←␈↓␈↓ β'It␈α⊃is␈α∩useful␈α⊃to␈α⊃introduce␈α∩some␈α⊃terminology␈α⊃for␈α∩the␈α⊃components␈α∩of␈α⊃a
␈↓ α←␈↓function definition. Let
␈↓"∀␈↓ α←␈↓α␈↓ ¬nf[x␈↓β1␈↓α; ...; x␈↓βn␈↓α] <= ␈↓λx␈↓α
␈↓"∀␈↓ α←␈↓represent␈αa␈αtypical␈αdefinition.␈α
The␈α␈↓↓name␈↓␈αof␈αthe␈αfunction␈α
is␈α␈↓αf␈↓;␈αthe␈α␈↓↓body␈↓␈αof␈α
the
␈↓ α←␈↓function␈αis␈α
the␈αexpression␈α␈↓λx␈↓.␈α
The␈αlist␈α
␈↓α[x␈↓β1␈↓α;␈α...;␈αx␈↓βn␈↓α]␈↓␈α
appearing␈αafter␈αthe␈α
function
␈↓ α←␈↓name␈α∪is␈α∪called␈α∀the␈α∪␈↓↓formal␈α∪parameter␈↓␈α∪list.␈α∀ The␈α∪elements␈α∪of␈α∀the␈α∪formal
␈↓ α←␈↓parameter␈α∂list␈α∂are␈α∂called␈α∂formal␈α∂parameters␈α∞and␈α∂will␈α∂play␈α∂a␈α∂role␈α∂similar␈α∞to
␈↓ α←␈↓␈↓1.4␈↓ λ∀Primitive Functions 15␈↓
␈↓"β␈↓ α←␈↓that␈α∂of␈α∞variables␈α∂in␈α∞mathematics.␈↓π 16␈↓␈α∂Therefore␈α∞we␈α∂will␈α∞also␈α∂refer␈α∂to␈α∞formal
␈↓ α←␈↓parameters␈α∞as␈α∞variables.␈α∞ Lower-case␈α∞identifiers␈↓π 17␈↓␈α∞will␈α∞be␈α∞used␈α∞as␈α∞variables
␈↓ α←␈↓and␈αfunction␈αnames.␈α So␈αfor␈αexample␈α␈↓αY␈↓␈αand␈α␈↓αCAR␈↓␈αare␈αatoms;␈α␈↓αy␈↓␈αand␈α␈↓αcar␈↓␈αcould
␈↓ α←␈↓be␈αused␈α
as␈αvariables.␈α
Be␈αclear␈α
on␈αthe␈α
distinction␈αbetween␈α
LISP␈αvariables␈α
like
␈↓ α←␈↓␈↓αx,␈α⊃y␈↓␈α⊃or␈α⊃␈↓αfoo␈↓,␈α⊃and␈α⊂the␈α⊃match␈α⊃variables␈↓π 18␈↓␈α⊃like␈α⊃␈↓λα␈↓␈α⊂or␈α⊃␈↓λβ␈↓.␈α⊃ If␈α⊃␈↓λα␈↓␈α⊃and␈α⊃␈↓λβ␈↓␈α⊂denote
␈↓ α←␈↓S-expressions␈α∂then␈α∞␈↓α(␈↓λα␈↓α . ␈↓λβ␈↓α)␈↓␈α∂denotes␈α∂a␈α∞well-formed␈α∂S-expr.␈α∂ The␈α∞construction,
␈↓ α←␈↓␈↓α(x . y)␈↓, is ␈↓¬not␈↓ well-formed, but ␈↓αcons[x;y]␈↓ is correct.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α→function␈α_is␈α→␈↓↓applied␈↓␈α_using␈α→the␈α_common␈α→notation␈α→of␈α_␈↓↓function
␈↓ α←␈↓↓application␈↓:
␈↓"∀␈↓ α←␈↓α␈↓ ε⊂f[a␈↓β1␈↓α; ...; a␈↓βn␈↓α]
␈↓"∀␈↓ α←␈↓The␈α
␈↓αa␈↓βi␈↓'s␈α
are␈α
called␈α
␈↓↓actual␈α
parameters␈↓;␈αfor␈α
an␈α
application␈α
to␈α
be␈α
well␈αformed,
␈↓ α←␈↓the␈α
actual␈α∞parameters␈α
must␈α
agree␈α∞in␈α
number␈α
with␈α∞the␈α
formal␈α∞parameters␈α
of
␈↓ α←␈↓the␈αdefinition␈αand␈αthey␈αare␈αto␈αbe␈αassociated␈αin␈αa␈αone-for-one␈αorder,␈α␈↓αa␈↓βi␈↓␈αwith␈α
␈↓αx␈↓βi␈↓.
␈↓ α←␈↓Thus␈α∪in␈α∩the␈α∪expression␈α∩␈↓αcar[cdr[(A . B)]]␈↓␈α∪the␈α∩actual␈α∪parameter␈α∩to␈α∪the␈α∩␈↓αcar␈↓
␈↓ α←␈↓function␈α⊃is␈α∩␈↓αcdr[(A . B)]␈↓,␈α⊃and␈α∩the␈α⊃actual␈α∩parameter␈α⊃to␈α∩␈↓αcdr␈↓␈α⊃is␈α∩␈↓α(A . B)␈↓.␈α⊃ The
␈↓ α←␈↓process␈α∩of␈α∩associating␈α∩formal␈α⊃parameters␈α∩with␈α∩actual␈α∩parameters␈α∩is␈α⊃called
␈↓ α←␈↓␈↓↓binding␈↓.␈α∪A␈α∪large␈α∪part␈α∪of␈α∪our␈α∩study␈α∪will␈α∪involve␈α∪various␈α∪aspects␈α∪of␈α∩the
␈↓ α←␈↓binding process.
␈↓"β␈↓ α←␈↓␈↓ β'It␈α∞is␈α∂convenient␈α∞to␈α∞introduce␈α∂some␈α∞terminology␈α∞to␈α∂distinguish␈α∞between
␈↓ α←␈↓an␈α≥algorithmic␈α≥idea␈α≥and␈α≥its␈α≥mathematical␈α≥counterpart.␈α≡The␈α≥phrase
␈↓ α←␈↓"function call"␈α&is␈α&used␈α&to␈α&name␈α&the␈α&procedural␈α'counterpart␈α&to
␈↓ α←␈↓"function application".␈α⊃ LISP␈α⊃is␈α∩called␈α⊃an␈α⊃␈↓↓applicative language␈↓␈α⊃since␈α∩it␈α⊃is
␈↓ α←␈↓based␈α∃on␈α∀the␈α∃idea␈α∀of␈α∃function␈α∀application.␈α∃ Mathematically␈α∃speaking,␈α∀a
␈↓ α←␈↓composition␈α
of␈α
functions␈α
is␈α
simply␈α
another␈α
function␈α
-- i.e., a␈α∞mapping --␈α
and
␈↓ α←␈↓therefore␈α∞nothing␈α∂need␈α∞be␈α∂said␈α∞about␈α∞how␈α∂to␈α∞compute␈α∂composed␈α∞functions.
␈↓ α←␈↓From␈α∪a␈α∪computational␈α∪point␈α∪of␈α∪view,␈α∪we␈α∪want␈α∪to␈α∪express␈α∪evaluation␈α∩of
␈↓ α←␈↓expressions␈α∪involving␈α∀composed␈α∪functions␈α∪in␈α∀terms␈α∪of␈α∪the␈α∀evaluation␈α∪of
␈↓ α←␈↓subexpressions.␈α∞This␈α∞would␈α∂allow␈α∞us␈α∞to␈α∂describe␈α∞a␈α∞complex␈α∂computation␈α∞in
␈↓ α←␈↓terms␈α∂of␈α∂an␈α∂appropriate␈α∂sequence␈α∂of␈α∂subsidiary␈α∂computations.␈α∂ One␈α⊂of␈α∂the
␈↓ α←␈↓more␈α∀natural␈α∀ways␈α∀to␈α∀evaluate␈α∀expressions␈α∀involving␈α∀compositions␈α∃is␈α∀to
␈↓ α←␈↓evaluate␈α⊗the␈α⊗inner-most␈α⊗expressions␈α⊗first,␈α⊗then␈α⊗work␈α↔outwards.␈α⊗Assume
␈↓ α←␈↓arguments␈α⊂to␈α∂multi-argument␈α⊂functions␈α∂are␈α⊂evaluated␈α∂in␈α⊂left-to-right␈α∂order.
␈↓ α←␈↓Thus:
␈↓"∀␈↓ α←␈↓α␈↓ α←cons[car[(A . B)];cdr[(A . (1 . 2))]]␈↓ ε3␈↓reduces to␈↓α cons[A;cdr[(A . (1 . 2))]]
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε3␈↓reduces to␈↓α cons[A;(1 . 2)]
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε3␈↓reduces to␈↓α (A . (1 . 2))
␈↓"∀␈↓ α←␈↓This␈α⊂may␈α∂seem␈α⊂to␈α∂be␈α⊂a␈α⊂simple␈α∂operation␈α⊂but␈α∂in␈α⊂fact␈α∂evaluation␈α⊂is␈α⊂a␈α∂very
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 16␈↓The␈α⊂behavior␈α∂of␈α⊂formal␈α∂parameters␈α⊂and␈α∂variables␈α⊂is␈α⊂␈↓¬not␈↓␈α∂identical.
␈↓ α←␈↓We will say more about the distinction in Section 4.1.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 17␈↓See page 17 for the BNF equations for <identifier>.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 18␈↓also called meta-variables
␈↓ α←␈↓␈↓16 Symbolic expressions␈↓
(1.4␈↓
␈↓"β␈↓ α←␈↓complex␈α∞process.␈α∞ The␈α∞value␈α∞of␈α∞an␈α∞expression␈α∞may␈α∞depend␈α∞on␈α∞the␈α∂␈↓¬order␈↓␈α∞in
␈↓ α←␈↓which␈α#we␈α#do␈α#things.␈α#For␈α#example,␈α#consider␈α#the␈α#evaluation␈α"of
␈↓ α←␈↓␈↓αsecond[car[A]; B]␈↓␈α∂where␈α∂␈↓αsecond[x;y] <= y␈↓.␈α∞ If␈α∂we␈α∂expect␈α∞␈↓αsecond␈↓␈α∂to␈α∂be␈α∂a␈α∞strict
␈↓ α←␈↓function,␈α
then␈α
␈↓αsecond[car[A]; B]␈↓␈α
must␈α
return␈α
␈↓λB␈↓␈α
even␈α
though␈α
it␈α
is␈α
reasonable
␈↓ α←␈↓to␈α
believe␈α∞that␈α
the␈α
value␈α∞of␈α
the␈α∞computation␈α
should␈α
be␈α∞␈↓αB␈↓␈α
since␈α∞␈↓αsecond␈↓␈α
does
␈↓ α←␈↓not␈αvisibly␈αdepend␈αon␈αthe␈αvalue␈αof␈αits␈αfirst␈αparameter.␈α It␈αappears␈αthat␈αif␈αwe
␈↓ α←␈↓postponed␈α∞the␈α∞evaluation␈α∞of␈α∞the␈α∞arguments␈α∞until␈α∞those␈α∞values␈α∞were␈α
actually
␈↓ α←␈↓needed,␈α~then␈α→at␈α~least␈α→this␈α~problem␈α→would␈α~be␈α→solved.␈α~ However,␈α→the
␈↓ α←␈↓consequences␈α∂of␈α∞defining␈α∂a␈α∞function␈α∂to␈α∞be␈α∂strict␈α∞are␈α∂severe;␈α∞they␈α∂cannot␈α∞be
␈↓ α←␈↓sidestepped␈α∃by␈α∃resorting␈α∃to␈α∃different␈α∃schemes␈α∃for␈α∃evaluating␈α∀arguments.
␈↓ α←␈↓There␈αis␈αan␈αalternative,␈αbut␈αnot␈αparticularly␈αattractive,␈αstrategy␈α
for␈αassigning
␈↓ α←␈↓strictness:␈αwe␈αcould␈αexamine␈αthe␈αbody␈αof␈αthe␈αfunction;␈αif␈αthe␈αfunction␈αuses␈αall
␈↓ α←␈↓its␈αparameters,␈αthen␈αit's␈αstrict.␈αIf␈αthe␈αfunction␈αdoesn't␈αdepend␈αon␈αone␈αor␈αmore
␈↓ α←␈↓parameters,␈α∃then␈α∃it's␈α∃non-strict.␈α∀Thus␈α∃with␈α∃this␈α∃interpretation,␈α∃␈↓αsecond␈↓␈α∀␈↓¬is␈↓
␈↓ α←␈↓non-strict.␈α
We␈α
prefer␈α
the␈α
initial␈α
interpretation,␈α
reasoning␈α
that,␈α
if␈α
a␈αfunction
␈↓ α←␈↓is␈α∩passed␈α∩bad␈α∩information,␈α∩then␈α∩we␈α∪wish␈α∩to␈α∩know␈α∩about␈α∩it,␈α∩even␈α∪if␈α∩the
␈↓ α←␈↓function does not use that specious result.
␈↓"β␈↓ α←␈↓␈↓ β'Strictness␈αis␈αclosely␈αrelated␈αto␈αevaluation␈αschemes␈αfor␈αparameter␈α
passing.
␈↓ α←␈↓Here are two common techniques:
␈↓"∀␈↓ α←␈↓␈↓↓CBV␈↓ Evaluate␈α≤the␈α≤arguments␈α≤to␈α≠a␈α≤function;␈α≤pass␈α≤those␈α≠evaluated
␈↓ α←␈↓␈↓ βKarguments to the function.
␈↓"∀␈↓ α←␈↓This␈α⊃scheme,␈α∩called␈α⊃␈↓↓C␈↓all␈α⊃␈↓↓B␈↓y␈α∩␈↓↓V␈↓alue,␈α⊃is␈α⊃what␈α∩we␈α⊃were␈α⊃informally␈α∩using␈α⊃to
␈↓ α←␈↓evaluate the previous examples.
␈↓"β␈↓ α←␈↓␈↓ β'An alternative evaluation process is ␈↓↓C␈↓all ␈↓↓B␈↓y ␈↓↓N␈↓ame:
␈↓"∀␈↓ α←␈↓␈↓↓CBN␈↓␈↓ β?Pass the unevaluated arguments into the body of the function.
␈↓"∀␈↓ α←␈↓Assuming␈α
␈↓αsecond␈↓␈αis␈α
defined␈αto␈α
be␈αstrict,␈α
then␈α␈↓αsecond[car[A]; B]␈↓␈α
yields␈α␈↓λB␈↓␈α
under
␈↓ α←␈↓either␈α␈↓↓CBV␈↓␈αor␈α␈↓↓CBN␈↓.␈αHowever␈αif␈αwe␈αdefine␈α␈↓αsecond␈↓␈αto␈αbe␈αnon-strict␈αthen␈α␈↓↓CBV␈↓
␈↓ α←␈↓and␈α
␈↓↓CBN␈↓␈α
will␈α
both␈α
give␈α
value␈α
␈↓αB␈↓.␈α∞ With␈α
␈↓↓CBV␈↓,␈α
␈↓αx␈↓␈α
is␈α
bound␈α
to␈α
␈↓λB␈↓;␈α∞while␈α
with
␈↓ α←␈↓␈↓↓CBN␈↓ ␈↓αx␈↓ is bound to ␈↓αcar[A]␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Further␈αrelationships␈αbetween␈αevaluation␈αschemes␈αand␈αstrictness␈αwill␈αbe
␈↓ α←␈↓investigated.␈α⊗ On␈α⊗page 21␈α∃we␈α⊗discuss␈α⊗non-terminating␈α⊗computations.␈α∃ In
␈↓ α←␈↓Chapter 3␈α∪we␈α∪will␈α∩discuss␈α∪evaluation␈α∪techniques␈α∩and␈α∪will␈α∪give␈α∪a␈α∩precise
␈↓ α←␈↓characterization␈αof␈αthe␈α
evaluation␈αof␈αLISP␈α
expressions.␈α On␈αpage 19␈α
we␈αwill
␈↓ α←␈↓introduce␈α∃a␈α⊗non-strict␈α∃language␈α⊗construct␈α∃but,␈α⊗until␈α∃that␈α⊗time,␈α∃intuitive
␈↓ α←␈↓application of ␈↓↓CBV␈↓ will suffice.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∞must␈α∞exercise␈α∂care␈α∞when␈α∞discussing␈α∂the␈α∞process␈α∞of␈α∂evaluation;␈α∞the
␈↓ α←␈↓function␈αwe␈αare␈αcharacterizing␈αby␈αcomputing␈αits␈αvalues␈αwill␈αoften␈α
depend␈αon
␈↓ α←␈↓our choice of evaluation scheme.
␈↓ α←␈↓␈↓1.4␈↓ λ∀Primitive Functions 17␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Before␈α∞introducing␈α∞a␈α∞further␈α∞class␈α∞of␈α∞LISP␈α∞expressions␈α∞we␈α
summarize
␈↓ α←␈↓the syntax of the LISP expressions allowed so far:
␈↓"∀␈↓ α←␈↓<form>␈↓ ∧C::= <constant> | <application> | <variable>
␈↓" ␈↓ α←␈↓<constant>␈↓ ∧C::= <sexpr> (where <sexpr> is given on page 6)
␈↓" ␈↓ α←␈↓<application>␈↓ ∧C::= <function-part>[<arg>; ...;<arg>]
␈↓" ␈↓ α←␈↓<function-part>␈↓ ∧C::= <identifier>
␈↓" ␈↓ α←␈↓<arg>␈↓ ∧C::= <form>
␈↓" ␈↓ α←␈↓<variable>␈↓ ∧C::= <identifier>
␈↓" ␈↓ α←␈↓<identifier>␈↓ ∧C::= <letter> | <identifier><letter> | <identifier><digit>
␈↓" ␈↓ α←␈↓<letter>␈↓ ∧C::= ␈↓αa | b | c ... | z ␈↓
␈↓" ␈↓ α←␈↓<digit>␈↓ ∧C::= ␈↓α1 | 2 | ... | 9
␈↓"∀␈↓ α←␈↓The␈α⊂use␈α⊂of␈α⊂ellipses␈α⊂in␈α⊂the␈α⊂last␈α⊂equation␈α⊂is␈α⊂an␈α⊂abbreviation␈α⊂we␈α⊂have␈α⊂seen
␈↓ α←␈↓before.␈α The␈α
use␈αof␈α
ellipses␈αin␈α
the␈α<application>␈α
equation␈αis␈α
different.␈αIt␈αis␈α
an
␈↓ α←␈↓abbreviation␈αmeaning␈α"zero␈αor␈αmore␈αoccurrences".␈α Thus␈αthe␈αequation␈αmeans
␈↓ α←␈↓an␈α<application>␈αis␈αa␈α<function-part>␈αfollowed␈αby␈αthe␈αsymbol␈α"["␈αfollowed␈αby
␈↓ α←␈↓zero␈α∂or␈α∂more␈α∂<arg>'s␈α∂followed␈α∂by␈α∂the␈α∂symbol␈α∂"]".␈α∂ This␈α∂use␈α∂of␈α∂ellipses␈α∂can
␈↓ α←␈↓always␈α∪be␈α∪replaced␈α∪by␈α∪a␈α∀sequence␈α∪of␈α∪BNF␈α∪equations.␈α∪for␈α∀example,␈α∪this
␈↓ α←␈↓instance can be replaced by:
␈↓"∀␈↓ α←␈↓<application>␈↓ ∧∨::= <function-part>[<arg-list>] | <function-part>[ ]
␈↓" ␈↓ α←␈↓<arg-list>␈↓ ∧∨::= <arg> | <arg-list>;<arg>
␈↓"∀␈↓ α←␈↓To␈α⊃improve␈α⊃readability␈α⊃we␈α⊃will␈α⊃frequently␈α⊃violate␈α⊃these␈α⊃syntax␈α⊂equations,
␈↓ α←␈↓allowing␈αfunction␈αnames␈αcontaining␈αspecial␈αcharacters,␈αe.g.␈α␈↓αfact*␈↓,␈α␈↓αfib␈↓λ'␈↓␈αor␈α+␈α;␈αor
␈↓ α←␈↓writing␈α
␈↓αx+y␈↓␈αinstead␈α
of␈α␈↓α+[x;y]␈↓.␈α
No␈α
attempt␈αwill␈α
be␈αmade␈α
to␈α
characterize␈αthese
␈↓ α←␈↓violations; occurrences of them should be clear from context.
␈↓"β␈↓ α←␈↓␈↓ β'Notice␈α
that␈α
the␈α
class␈α
<form>␈αis␈α
a␈α
collection␈α
of␈α
LISP␈α
expressions␈αwhich
␈↓ α←␈↓can be evaluated. A <form> is either:
␈↓"λ␈↓ α←␈↓␈↓↓1.␈↓ a constant: the value is that constant.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ an␈α∀application:␈α∀we've␈α∀said␈α∃a␈α∀bit␈α∀about␈α∀evaluation␈α∀schemes␈α∃for␈α∀these
␈↓ α←␈↓␈↓ β∂constructs.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ a␈α
variable:␈α
a␈α
variable␈α
in␈α
LISP␈α
will␈α
typically␈α
have␈α
an␈α
associated␈α
value␈αin
␈↓ α←␈↓␈↓ β∂some environment.
␈↓"β␈↓ α←␈↓We will wait to Section 3.4 for a precise description.
␈↓"β␈↓ α←␈↓␈↓ β'An␈α∂important␈α∞constraint␈α∂on␈α∞LISP␈α∂forms␈α∞which␈α∂is␈α∞not␈α∂covered␈α∂by␈α∞the
␈↓ α←␈↓syntax␈αequations␈α
is␈αthe␈α
requirement␈αthat␈α
functions␈αare␈α
defined␈αas␈αbeing␈α
n-ary
␈↓ α←␈↓for␈α
some␈α
␈↓¬fixed␈↓␈α
n.␈α
Any␈α
n-ary␈αLISP␈α
function␈α
must␈α
have␈α
␈↓¬exactly␈↓␈α
n␈αarguments
␈↓ α←␈↓presented␈α∀to␈α∀it␈α∀whenever␈α∀it␈α∀is␈α∀applied.␈α∀ Thus␈α∀␈↓αcons[A],␈α∀cons[A;B;C],␈α∪␈↓and
␈↓ α←␈↓␈↓αcar[A;B]␈↓ are all ill-formed expressions and therefore denote ␈↓λB␈↓.
␈↓ α←␈↓␈↓18 Symbolic expressions␈↓
(1.4␈↓
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Discuss ␈↓αcons[car[x];cdr[x]] = x␈↓.
␈↓" ␈↓ α←␈↓2. Discuss ␈↓αcons[car[␈↓λα␈↓α];cdr[␈↓λα␈↓α]] = ␈↓λα␈↓.
␈↓"β␈↓ α←␈↓␈↓ ∧1␈↓↓1.5 Predicates and Conditional Expressions␈↓
␈↓"β␈↓ α←␈↓We␈α
cannot␈α
generate␈α
a␈α
very␈α
exciting␈α
theory␈α
based␈α
simply␈α
on␈α
␈↓αcar,␈α
cdr,␈↓␈α
and␈α␈↓αcons␈α
␈↓
␈↓ α←␈↓with␈α∀functional␈α∃composition.␈α∀Before␈α∀we␈α∃can␈α∀write␈α∃reasonably␈α∀interesting
␈↓ α←␈↓algorithms␈α
we␈α
must␈αhave␈α
some␈α
way␈α
of␈αperforming␈α
conditional␈α
actions.␈α To␈α
do
␈↓ α←␈↓this␈α⊂we␈α⊂first␈α⊂need␈α⊂predicates.␈α⊃A␈α⊂LISP␈α⊂predicate␈α⊂is␈α⊂a␈α⊂function␈α⊃returning␈α⊂a
␈↓ α←␈↓value␈α∂representing␈α∂truth␈α∂or␈α∂falsity.␈α∂ We␈α∂will␈α∂represent␈α∂the␈α∂concepts␈α∂of␈α∂true
␈↓ α←␈↓and␈α
false␈α
by␈α
␈↓
t␈↓␈α∞and␈α
␈↓
f␈↓␈α
respectively.␈α
Since␈α∞these␈α
truth␈α
values␈α
are␈α∞distinct␈α
from
␈↓ α←␈↓elements␈α∂of␈α⊂␈↓S␈↓,␈α∂we␈α⊂will␈α∂set␈α∂up␈α⊂a␈α∂new␈α⊂domain␈α∂␈↓Tr␈↓␈α∂which␈α⊂will␈α∂consist␈α⊂of␈α∂the
␈↓ α←␈↓elements,␈α
␈↓
t␈↓␈α
and␈α
␈↓
f␈↓.␈α
As␈α
usual␈α
the␈αextra␈α
element␈α
␈↓λB␈↓␈α
is␈α
included␈α
so␈α
that␈α
we␈αmay
␈↓ α←␈↓talk␈α⊂about␈α⊃partial␈α⊂predicates␈α⊂just␈α⊃as␈α⊂we␈α⊂talked␈α⊃about␈α⊂partial␈α⊃functions␈α⊂on
␈↓ α←␈↓␈↓<sexpr>␈↓.␈↓π 19␈↓
␈↓"β␈↓ α←␈↓␈↓ β'LISP␈αhas␈αtwo␈αprimitive␈αpredicates.␈α The␈αfirst␈αis␈αa␈αstrict␈αunary␈αpredicate
␈↓ α←␈↓named␈α
␈↓αatom␈↓;␈α
␈↓αatom␈↓␈αis␈α
total␈α
over␈α␈↓<sexpr>␈↓,␈α
and␈α
is␈αa␈α
special␈α
kind␈α
of␈αpredicate
␈↓ α←␈↓called␈α∞a␈α∞␈↓↓recognizer␈↓␈α∂or␈α∞a␈α∞␈↓↓discriminator␈↓.␈α∂ Recognizers␈α∞are␈α∞used␈α∂to␈α∞determine
␈↓ α←␈↓the␈α∞type␈α
of␈α∞an␈α∞instance␈α
of␈α∞a␈α
data␈α∞structure.␈α∞ Thus␈α
␈↓αatom␈↓␈α∞will␈α
return␈α∞␈↓
t␈↓␈α∞if␈α
the
␈↓ α←␈↓argument␈αdenotes␈αan␈αatom,␈αand␈αwill␈αreturn␈α␈↓
f␈↓␈αif␈αthe␈αargument␈αis␈αa␈αnon-atomic
␈↓ α←␈↓S-expr.
␈↓""␈↓ α←␈↓␈↓ ¬#␈↓αatom[A] = atom[NIL] = ␈↓
t␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬#atom[(A . B)] = ␈↓
f␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬#atom[car[(A . B)]] = ␈↓
t␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬#atom[␈↓λB␈↓α] = ␈↓λB␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'What␈α
should␈αwe␈α
do␈αabout␈α
the␈α
value␈αof␈α
constructs␈αlike:␈α
␈↓αcons[atom[A]; A]␈↓?
␈↓ α←␈↓The␈αevaluation␈α
of␈α␈↓αatom[A]␈↓␈α
gives␈α␈↓
t␈↓,␈α
but␈α␈↓
t␈↓␈αis␈α
not␈αan␈α
element␈αof␈α
␈↓S␈↓␈αand␈α
thus␈αis
␈↓ α←␈↓not␈αappropriate␈αas␈αan␈αargument␈αto␈α␈↓αcons␈↓.␈αUsing␈αour␈αdiscussion␈αof␈αpage 11,␈αwe
␈↓ α←␈↓extend the domains of the S-expr primitives to
␈↓"∀␈↓ α←␈↓␈↓ ε∪␈↓S␈↓β1␈↓ = ␈↓S␈↓∪␈↓Tr␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'For example, for ␈↓αs␈↓λε␈↓Tr␈↓:␈↓αcar[s] = car[␈↓λB␈↓α], ␈↓and␈↓α cons[s; A] = cons[␈↓λB␈↓α; A]␈↓
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 19␈↓A␈α∞word␈α∞for␈α∞the␈α
previous␈α∞LISP␈α∞user:␈α∞our␈α∞use␈α
of␈α∞␈↓
t␈↓␈α∞and␈α∞␈↓
f␈↓␈α∞marks␈α
our
␈↓ α←␈↓first␈α∞major␈α
break␈α∞from␈α
current␈α∞LISP␈α
folklore.␈α∞ The␈α
typical␈α∞LISP␈α
trick␈α∞is␈α
to
␈↓ α←␈↓use␈α∪the␈α∀atoms␈α∪␈↓αT␈↓␈α∀and␈α∪␈↓αNIL␈↓␈α∀rather␈α∪than␈α∪␈↓
t␈↓␈α∀and␈α∪␈↓
f␈↓␈α∀as␈α∪truth␈α∀values.␈α∪ Our
␈↓ α←␈↓convention␈α⊃will␈α⊂disallow␈α⊃some␈α⊂mixed␈α⊃compositions␈α⊂of␈α⊃LISP␈α⊃functions␈α⊂and
␈↓ α←␈↓predicates. We will relax this restriction when we write LISP programs.
␈↓ α←␈↓␈↓1.5␈↓ ε%Predicates and Conditional Expressions 19␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Since those primitives are strict with respect to undefined we have:
␈↓""␈↓ α←␈↓α␈↓ ¬watom[␈↓
t␈↓α] = ␈↓λB␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬wcons[␈↓λB␈↓α; A] = ␈↓λB␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬wcons[A; ␈↓λB␈↓α] = ␈↓λB␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Notice␈αthat␈α
we␈αnow␈αhave␈α
␈↓¬two␈↓␈αseparate␈α
domains:␈αS-expressions␈αand␈α
truth
␈↓ α←␈↓values.␈α Since␈αwe␈αwill␈α
be␈αwriting␈αfunctions␈αover␈α
several␈αdomains␈αwe␈αwill␈α
need
␈↓ α←␈↓a␈α
general␈α
recognizer␈α
for␈α
each␈α
domain␈α
to␈α
assure␈α
that␈α
the␈α
operations␈α
defined␈α
on
␈↓ α←␈↓each␈α⊃abstract␈α⊃data␈α⊃structure␈α⊃are␈α⊂properly␈α⊃applied.␈α⊃Thus␈α⊃we␈α⊃introduce␈α⊂the
␈↓ α←␈↓recognizer␈α␈↓αissexpr␈↓␈αwhich␈αwill␈αgive␈α␈↓
t␈↓␈αon␈αthe␈αdomain␈αof␈αS-exprs,␈α␈↓
f␈↓␈αfor␈αfor␈αany
␈↓ α←␈↓element not in ␈↓<sexpr>␈↓ and will give ␈↓λB␈↓ for ␈↓λB␈↓.
␈↓""␈↓ α←␈↓α␈↓ ∧sissexpr[(A . B)] = issexpr[A] = ␈↓
t␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ∧sissexpr[␈↓
t␈↓α] = ␈↓
f␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ∧sissexpr[␈↓λB␈↓α] = ␈↓λB␈↓α
␈↓"∀␈↓ α←␈↓␈↓ β'Another␈α
primitive␈αpredicate␈α
we␈αneed␈α
is␈αnamed␈α
␈↓αeq␈↓.␈α It␈α
is␈αa␈α
strict␈αbinary
␈↓ α←␈↓predicate,␈αpartial␈αover␈αthe␈αset␈α␈↓<sexpr>␈↓;␈α
it␈αwill␈αgive␈αa␈αtruth␈αvalue␈αonly␈α
if␈αits
␈↓ α←␈↓arguments␈α∂are␈α∞both␈α∂atomic.␈α∞ It␈α∂returns␈α∞␈↓
t␈↓␈α∂if␈α∞the␈α∂arguments␈α∞denote␈α∂the␈α∞same
␈↓ α←␈↓atom;␈α
it␈α
returns␈α
␈↓
f␈↓␈αif␈α
the␈α
arguments␈α
represent␈αdifferent␈α
atoms.␈α
␈↓αeq␈↓␈α
yields␈α
␈↓λB␈↓␈αif
␈↓ α←␈↓either argument to ␈↓αeq␈↓ denotes an element not in the set ␈↓<atom>␈↓.
␈↓"∀␈↓ α←␈↓α␈↓ ∧∨eq[A;A] = ␈↓
t␈↓α␈↓ εWeq[A;B] = ␈↓
f␈↓α
␈↓"∀␈↓ α←␈↓α␈↓ ∧∨eq[(A . B); A] = ␈↓λB␈↓α␈↓ εWeq[(A . B);(A . B)] = ␈↓λB␈↓α
␈↓"∀␈↓ α←␈↓α␈↓ ∧∨eq[eq[A;B];D] = ␈↓λB␈↓α␈↓ εWeq[␈↓λB␈↓α;x] = ␈↓λB␈↓α
␈↓"∀␈↓ α←␈↓α␈↓ ∧∨eq[car[(A . B)];car[cdr[(A . (B . C))]]] = ␈↓
f␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Rather␈α
than␈αdefine␈α
a␈α
version␈αof␈α
␈↓αeq␈↓,␈αsay␈α
␈↓αeq␈↓βTr␈↓,␈α
which␈αis␈α
defined␈α
over␈α␈↓Tr␈↓
␈↓ α←␈↓and␈α∞acts␈α
like␈α∞␈↓αeq␈↓,␈α∞we␈α
will␈α∞simply␈α∞extend␈α
the␈α∞definition␈α
of␈α∞␈↓αeq␈↓␈α∞to␈α
␈↓S␈↓β1␈↓␈α∞so␈α∞that␈α
it
␈↓ α←␈↓may compare two elements of ␈↓Tr␈↓.
␈↓"∀␈↓ α←␈↓α␈↓ ∧geq[␈↓
t␈↓α;␈↓
t␈↓α] = ␈↓
t␈↓α␈↓ ε3eq[␈↓
f␈↓α;␈↓λB␈↓α] = ␈↓λB␈↓α
␈↓"∀␈↓ α←␈↓α␈↓ ∧geq[␈↓
f␈↓α;␈↓
f␈↓α] = ␈↓
t␈↓α␈↓ ε3eq[␈↓
t␈↓α;␈↓
f␈↓α] = ␈↓
f␈↓α
␈↓"∀␈↓ α←␈↓α␈↓ ∧geq[A;␈↓
t␈↓α] = ␈↓λB␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'We␈α≠need␈α~to␈α≠include␈α≠a␈α~construct␈α≠in␈α≠our␈α~language␈α≠to␈α≠effect␈α~a
␈↓ α←␈↓test-and-branch␈α⊗operation.␈α⊗ In␈α⊗LISP␈α∃this␈α⊗operation␈α⊗is␈α⊗indicated␈α⊗by␈α∃the
␈↓ α←␈↓␈↓↓conditional expression␈↓. It is written:
␈↓"β␈↓ α←␈↓α␈↓ ¬∂[␈↓↓p␈↓β1␈↓α → ␈↓↓e␈↓β1␈↓α; ␈↓↓p␈↓β2␈↓α → ␈↓↓e␈↓β2␈↓α; ... ; ␈↓↓p␈↓βn␈↓α → ␈↓↓e␈↓βn␈↓α]
␈↓"β␈↓ α←␈↓Each␈α
␈↓↓p␈↓βi␈↓␈α
is␈αan␈α
expression␈α
which␈αtakes␈α
on␈α
values␈αin␈α
the␈α
set␈α␈↓Tr␈↓␈α
or␈α
gives␈α␈↓λB␈↓;␈α
each
␈↓ α←␈↓␈↓↓e␈↓βi␈↓␈α∪is␈α∩an␈α∪expression␈α∩which␈α∪will␈α∩give␈α∪a␈α∩value␈α∪in␈α∩␈↓S␈↓β1␈↓.␈α∪We␈α∩will␈α∪restrict␈α∩the
␈↓ α←␈↓␈↓20 Symbolic expressions␈↓
(1.5␈↓
␈↓"β␈↓ α←␈↓conditional␈α⊃expression␈α⊃such␈α⊂that␈α⊃all␈α⊃the␈α⊂␈↓↓e␈↓βi␈↓␈α⊃must␈α⊃have␈α⊂values␈α⊃in␈α⊃the␈α⊂␈↓¬same␈↓
␈↓ α←␈↓domain or be ␈↓λB␈↓; i.e. all be in ␈↓<sexpr>␈↓ or all be in ␈↓Tr␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Assuming␈α∃that␈α∃an␈α∀instance␈α∃of␈α∃a␈α∀conditional␈α∃expression␈α∃meets␈α∀this
␈↓ α←␈↓restriction, the rule for evaluation is given by the following:
␈↓"∀␈↓ α←␈↓␈↓ βWWe␈α∂evaluate␈α∂the␈α∂␈↓↓p␈↓βi␈↓'s␈α∂from␈α∂left␈α∂to␈α∂right,␈α∂finding␈α⊂the␈α∂␈↓¬first␈↓
␈↓ α←␈↓␈↓ βWwhich␈α∀returns␈α∀value␈α∀␈↓
t␈↓.␈α∀ When␈α∀we␈α∀find␈α∀such␈α∀a␈α∀␈↓↓p␈↓βi␈↓,␈α∪we
␈↓ α←␈↓␈↓ βWevaluate␈α∨the␈α∨corresponding␈α≡␈↓↓e␈↓βi␈↓.␈α∨ The␈α∨value␈α∨of␈α≡the
␈↓ α←␈↓␈↓ βWconditional␈αexpression␈αis␈αthe␈α
value␈αcomputed␈αby␈αthat␈α
␈↓↓e␈↓βi␈↓;␈αif
␈↓ α←␈↓␈↓ βWall␈αof␈αthe␈α␈↓↓p␈↓βi␈↓'s␈αevaluate␈αto␈α␈↓
f␈↓␈αthen␈αthe␈αconditional␈αexpression
␈↓ α←␈↓␈↓ βWgives␈α∂␈↓λB␈↓.␈α⊂ The␈α∂conditional␈α⊂expression␈α∂also␈α⊂gives␈α∂␈↓λB␈↓␈α⊂if␈α∂we
␈↓ α←␈↓␈↓ βWcome␈α
across␈αa␈α
␈↓↓p␈↓βi␈↓␈αwhich␈α
has␈α
value␈α␈↓λB␈↓␈α
before␈αwe␈α
reach␈α
a␈α␈↓↓p␈↓βi␈↓
␈↓ α←␈↓␈↓ βWwith value ␈↓
t␈↓.
␈↓"∀␈↓ α←␈↓For example:
␈↓"∀␈↓ α←␈↓α␈↓ ∧←[atom [A] → B; eq [A;(A . B)] → C] = B
␈↓"∀␈↓ α←␈↓Notice␈αthat␈αthe␈α␈↓↓p␈↓β2␈↓␈αexpression␈αis␈αundefined,␈αbut␈αthe␈αconditional␈αgives␈αvalue␈α
␈↓αB␈↓
␈↓ α←␈↓since ␈↓↓p␈↓β1␈↓ gives value ␈↓
t␈↓; this means that conditional expressions are non-strict.
␈↓"∀␈↓ α←␈↓α␈↓ ∧7[eq [A;(A . B)] → C; atom [A] → B] = ␈↓λB␈↓α
␈↓"∀␈↓ α←␈↓Here a reordering makes the evaluation return ␈↓λB␈↓.
␈↓"∀␈↓ α←␈↓α␈↓ ∧7[atom [(A . B)] → B;
␈↓"β␈↓ α←␈↓α␈↓ ∧7 eq [A ; B] → C;
␈↓"β␈↓ α←␈↓α␈↓ ∧7 eq [car[(A . B)]; cdr[(B . A)]] → E] = E
␈↓"∀␈↓ α←␈↓This␈α∀example␈α∀is␈α∀more␈α∀complex␈α∀so,␈α∀to␈α∀improve␈α∀readibility,␈α∀we␈α∃split␈α∀the
␈↓ α←␈↓conditional␈α∪clauses␈α∩across␈α∪several␈α∩lines.␈α∪This␈α∩stylistic␈α∪formatting␈α∪is␈α∩called
␈↓ α←␈↓␈↓↓pretty printing␈↓.
␈↓"∀␈↓ α←␈↓α␈↓ ∧7[eq [A; A] → ␈↓
t␈↓α; atom [A] → ␈↓
f␈↓α] = ␈↓
t␈↓α
␈↓" ␈↓ α←␈↓α␈↓ ∧7[eq [A; A] → ␈↓
t␈↓α; atom [A] → B] = ␈↓λB␈↓α
␈↓"∀␈↓ α←␈↓Note␈αthat␈α
non-strictness␈αis␈αrelative␈α
to␈αa␈αsingle␈α
domain;␈αthus␈αthe␈α
last␈αexample
␈↓ α←␈↓above gives ␈↓λB␈↓ since it contains ␈↓↓e␈↓βi␈↓'s of differing domains.
␈↓"β␈↓ α←␈↓␈↓ β'Frequently␈α⊃it␈α⊃is␈α⊃convenient␈α⊃to␈α⊂use␈α⊃a␈α⊃special␈α⊃form␈α⊃of␈α⊃the␈α⊂conditional
␈↓ α←␈↓expression␈α⊃where␈α⊃the␈α⊃final␈α⊃␈↓↓p␈↓βn␈↓␈α⊃is␈α⊃guaranteed␈α⊃to␈α⊃be␈α⊃true.␈α⊃There␈α⊃are␈α⊃many
␈↓ α←␈↓expressions␈α↔which␈α↔always␈α⊗evaluate␈α↔to␈α↔␈↓
t␈↓;␈α⊗ ␈↓αeq[1;1]␈↓␈α↔is␈α↔one.␈α↔ The␈α⊗simplest
␈↓ α←␈↓expression is the constant ␈↓
t␈↓.
␈↓"∞␈↓ α←␈↓␈↓ β'Consider the special form: ␈↓α[␈↓↓p␈↓β1␈↓α → ␈↓↓e␈↓β1␈↓α; ...; ␈↓
t␈↓α → ␈↓↓e␈↓βn␈↓α]
␈↓"∞␈↓ α←␈↓If␈α∪we␈α∪know␈α∀that␈α∪the␈α∪previous␈α∀␈↓↓p␈↓βi␈↓'s␈α∪are␈α∪either␈α∀true␈α∪or␈α∪false,␈↓π 20␈↓␈α∀the␈α∪final
␈↓ α←␈↓␈↓↓p␈↓βn␈↓ → ␈↓↓e␈↓βn␈↓-case␈αis␈αa␈αcatch-all␈αor␈αotherwise-case␈αwhich␈αwill␈αbe␈αexecuted␈αif␈αnone␈αof
␈↓ α←␈↓the␈α⊃previous␈α⊃␈↓↓p␈↓βi␈↓'s␈α⊃give␈α⊃␈↓
t␈↓.␈α⊃ Thus␈α⊃the␈α⊂use␈α⊃of␈α⊃␈↓
t␈↓␈α⊃in␈α⊃this␈α⊃context␈α⊃can␈α⊃be␈α⊂read
␈↓ α←␈↓"otherwise"; and the conditional can be read:
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 20␈↓We must also know that all the ␈↓↓e␈↓βi␈↓'s are elements of the same domain.
␈↓ α←␈↓␈↓1.5␈↓ ε%Predicates and Conditional Expressions 21␈↓
␈↓"β␈↓ α←␈↓␈↓ βW"If ␈↓↓p␈↓β1␈↓ is true then ␈↓↓e␈↓β1␈↓, else if ␈↓↓p␈↓β2␈↓ is true then ..., otherwise ␈↓↓e␈↓βn␈↓"
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α⊂introduction␈α⊂of␈α⊂conditional␈α∂expressions␈α⊂has␈α⊂further␈α⊂widened␈α∂the
␈↓ α←␈↓gap␈α⊂between␈α⊃traditional␈α⊂mathematical␈α⊃theories␈α⊂and␈α⊃computational␈α⊂theories.
␈↓ α←␈↓Previously␈α
we␈α
could␈α
almost␈α
side-step␈αthe␈α
issue␈α
of␈α
order␈α
of␈α
evaluation;␈αit␈α
didn't
␈↓ α←␈↓really␈αmatter␈αunless␈α␈↓λB␈↓␈αwas␈αinvolved.␈α
But␈αnow␈αthe␈αvery␈αdefinition␈αof␈α
meaning
␈↓ α←␈↓of conditionals involves an order of evaluation.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
order␈αof␈α
evaluation␈αis␈α
important␈αfrom␈α
a␈α
computational␈αviewpoint:
␈↓ α←␈↓if␈αwe␈αare␈αgoing␈αto␈αgive␈αas␈α
value␈αthe␈αleftmost␈α␈↓↓e␈↓βi␈↓␈αwhose␈α␈↓↓p␈↓βi␈↓␈αevaluates␈αto␈α
␈↓
t␈↓,␈αthen
␈↓ α←␈↓there␈αis␈αno␈α
need␈αto␈αcompute␈αany␈α
of␈αthe␈αother␈α␈↓↓e␈↓βj␈↓'s;␈α
those␈αvalues␈αwill␈α
never␈αbe
␈↓ α←␈↓used.␈α∞ A␈α∞more␈α
pressing␈α∞difficulty␈α∞is␈α
that␈α∞of␈α∞partial␈α
functions.␈α∞If␈α∞we␈α∞did␈α
not
␈↓ α←␈↓impose␈α⊃an␈α⊃order␈α⊃of␈α⊃evaluation␈α⊃on␈α⊃the␈α⊃components␈α⊃of␈α⊃a␈α∩conditional,␈α⊃then
␈↓ α←␈↓frequently␈α⊂we␈α⊂would␈α⊂attempt␈α⊂to␈α⊂evaluate␈α⊂expressions␈α⊂which␈α⊂would␈α⊂lead␈α∂to
␈↓ α←␈↓undefined␈α∀results:␈α∀␈↓α[eq[0;0] → 1; ␈↓
t␈↓α → car[A]]␈↓␈α∃gives␈α∀␈↓α1␈↓␈α∀using␈α∀the␈α∃meaning␈α∀of
␈↓ α←␈↓conditionals,␈αwhereas␈αthe␈αexpression␈αwould␈α
be␈αundefined␈αif␈αwe␈αwere␈α
required
␈↓ α←␈↓to␈α∞evaluate␈α∞␈↓αcar[A]␈↓.␈α∞If␈α∞we␈α∞think␈α∞of␈α∞an␈α∞occurrence␈α∞of␈α∞␈↓λB␈↓␈α∞being␈α∞mapped␈α∞to␈α
an
␈↓ α←␈↓error␈αmessage,␈αevaluating␈α␈↓αcar[A]␈↓␈α
would␈αcause␈αtermination␈αof␈αthe␈α
computation.
␈↓ α←␈↓But,␈α∩if␈α∪we␈α∩continue␈α∪to␈α∩allow␈α∪␈↓λB␈↓␈α∩as␈α∩an␈α∪argument␈α∩or␈α∪value,␈α∩then␈α∪we␈α∩can
␈↓ α←␈↓characterize␈α∞the␈α∞effect␈α∂of␈α∞a␈α∞conditional␈α∂expression␈α∞as␈α∞a␈α∂␈↓↓non-strict␈↓␈α∞function.
␈↓ α←␈↓Recall,␈αa␈αnon-strict␈αfunction␈αis␈αallowed␈αto␈αreturn␈αa␈αvalue␈αother␈αthan␈α
␈↓λB␈↓␈αwhen
␈↓ α←␈↓one␈α∪of␈α∪its␈α∪arguments␈α∪is␈α∪␈↓λB␈↓;␈α∪or,␈α∪put␈α∪another␈α∪way,␈α∪we␈α∪don't␈α∀examine␈α∪the
␈↓ α←␈↓definedness of arguments before applying the function.
␈↓"β␈↓ α←␈↓␈↓ β'For␈αexample,␈αlet␈α␈↓αif(x;y;z)␈↓␈αbe␈αthe␈α␈↓↓conditional␈αfunction␈↓␈α␈↓π 21␈↓␈αcomputed␈αby:
␈↓ α←␈↓␈↓α[x → y; ␈↓
t␈↓α → z]␈↓. We can define ␈↓αif␈↓ as a non-strict function such that:
␈↓"∀␈↓ α←␈↓␈↓ ∧C␈↓ ¬w␈↓αy␈↓ if ␈↓αx␈↓ is ␈↓
t␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧C␈↓αif(x;y;z) =␈↓ ¬wz␈↓ if ␈↓αx␈↓ is ␈↓
f␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧C␈↓ ¬w␈↓λB␈↓ if ␈↓αx␈↓ is ␈↓λB␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'However␈α⊗there␈α⊗is␈α⊗more␈α↔to␈α⊗the␈α⊗"strictness"␈α⊗implied␈α↔by␈α⊗conditional
␈↓ α←␈↓expressions␈α∩than␈α∩just␈α∩making␈α∩sure␈α∩that␈α∩proper␈α∩arguments␈α∩are␈α∩passed␈α∩on
␈↓ α←␈↓function calls.
␈↓"β␈↓ α←␈↓␈↓ β'Consider the following algorithm:
␈↓"β␈↓ α←␈↓α␈↓ ¬αone[x] <= [x=0 → 1; ␈↓
t␈↓α → one[x-1]]
␈↓"β␈↓ α←␈↓Assume␈α⊂that␈α⊃␈↓αone␈↓␈α⊂is␈α⊂non-strict␈α⊃and␈α⊂assume␈α⊂the␈α⊃domain␈α⊂of␈α⊂discourse␈α⊃is␈α⊂the
␈↓ α←␈↓integers.␈α That␈αmeans,␈α␈↓αone␈↓␈αwill␈αtry␈αto␈αcompute␈αwith␈α␈↓¬any␈↓␈α(integer)␈αargument␈αit
␈↓ α←␈↓is␈α∃given.␈α∃ The␈α∃algorithm␈α∃for␈α∃␈↓αone␈↓␈α∃defines␈α∃a␈α∃function␈α∃giving␈α∃␈↓α1␈↓␈α∃for␈α∃any
␈↓ α←␈↓non-negative␈α∃integer␈α∃and␈α⊗is␈α∃undefined␈α∃for␈α⊗any␈α∃other␈α∃number.␈α⊗From␈α∃a
␈↓ α←␈↓computational␈α∀point␈α∪of␈α∀view,␈α∪however,␈α∀␈↓αone[-1]␈↓␈α∪appears␈α∀"undefined"␈α∀in␈α∪a
␈↓ α←␈↓different␈αsense␈αfrom␈α␈↓αcar[A]␈↓␈α
being␈α"undefined".␈αThe␈αcomputation␈α
␈↓αone[-1]␈↓␈αdoes
␈↓ α←␈↓not␈αterminate␈αand␈αis␈αsaid␈αto␈α␈↓↓diverge␈↓.␈α For␈αa␈αpartial␈αfunction␈αlike␈α␈↓αcar␈↓,␈α
we␈αcan
␈↓ α←␈↓give␈α⊃an␈α⊃error␈α⊃message␈α⊃whenever␈α⊃we␈α⊃attempt␈α⊃to␈α⊃apply␈α⊃the␈α⊃function␈α⊃to␈α⊃an
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 21␈↓Notice␈α
we␈α
are␈α
writing␈α
`(...)'␈αrather␈α
than␈α
`[...]'␈α
since␈α
we␈α
are␈αtalking␈α
about
␈↓ α←␈↓the function and not the algorithm. See page 12.
␈↓ α←␈↓␈↓22 Symbolic expressions␈↓
(1.5␈↓
␈↓"β␈↓ α←␈↓atomic␈α_argument,␈α_but␈α_we␈α_cannot␈α→expect␈α_to␈α_include␈α_tests␈α_like␈α→"if␈α_the
␈↓ α←␈↓computation␈α⊂␈↓αf[a]␈↓␈α⊂does␈α⊂not␈α⊂terminate␈α∂then␈α⊂give␈α⊂error␈α⊂No.␈α⊂15."␈↓π 22␈↓␈α⊂From␈α∂the
␈↓ α←␈↓purely␈αfunctional␈αpoint␈αof␈αview,␈α␈↓αone␈↓␈αstill␈αdefines␈αthe␈αpartial␈αfunction␈αwhich␈α
is
␈↓ α←␈↓␈↓α1␈↓␈α∩for␈α∩the␈α⊃non-negative␈α∩integers,␈α∩but␈α⊃computationally␈α∩there's␈α∩an␈α⊃important
␈↓ α←␈↓distinction to be made.
␈↓"β␈↓ α←␈↓␈↓ β'So␈α∞we␈α∞see␈α∂that␈α∞a␈α∞computation␈α∞may␈α∂be␈α∞"undefined"␈α∞for␈α∞two␈α∂reasons:␈α∞it
␈↓ α←␈↓involves␈α⊃a␈α∩non-terminating␈α⊃computation␈α⊃or␈α∩it␈α⊃involves␈α⊃applying␈α∩a␈α⊃partial
␈↓ α←␈↓function␈α∞to␈α∞a␈α∞value␈α∞not␈α∂in␈α∞its␈α∞domain.␈↓π 23␈↓␈α∞Note␈α∞that␈α∞the␈α∂distinction␈α∞between
␈↓ α←␈↓"undefined"␈α
and␈α
"diverges"␈α
is␈α∞fuzzy.␈α
If␈α
we␈α
restrict␈α∞the␈α
domain␈α
of␈α
␈↓αone␈↓␈α∞to␈α
the
␈↓ α←␈↓natural␈α∀numbers,␈α∀then␈α∀␈↓αone[-1]␈↓␈α∃denotes␈α∀␈↓λB␈↓␈α∀rather␈α∀than␈α∀diverges.␈α∃Or,␈α∀put
␈↓ α←␈↓another␈α
way,␈α
"undefined␈α
strictness"␈α
is␈α
a␈α
special␈α
case␈α
of␈α∞"divergent␈α
strictness"
␈↓ α←␈↓where␈α∞we␈α
are␈α∞able␈α
to␈α∞predict␈α
which␈α∞computations␈α
will␈α∞not␈α∞terminate.␈α
Those
␈↓ α←␈↓cases␈α∂can␈α⊂be␈α∂checked␈α∂by␈α⊂defining␈α∂the␈α⊂function␈α∂to␈α∂be␈α⊂strict␈α∂over␈α⊂a␈α∂domain
␈↓ α←␈↓which␈α∞rules␈α
out␈α∞those␈α
anomalies.␈α∞ Thus␈α∞a␈α
case␈α∞can␈α
be␈α∞made␈α∞for␈α
identifying
␈↓ α←␈↓divergent␈α≠computations␈α≠with␈α≠␈↓λB␈↓;␈α~however␈α≠there␈α≠is␈α≠typically␈α≠more␈α~to
␈↓ α←␈↓non-termination that just "wrong kind of arguments".
␈↓"β␈↓ α←␈↓␈↓ β'We␈α
want␈αto␈α
extend␈αour␈α
discussion␈α
of␈αstrictness␈α
to␈αencompass␈α
divergence.
␈↓ α←␈↓Recall␈α
the␈αdiscussion␈α
on␈α
page 15␈αof␈α
␈↓αsecond[x; y] <= y␈↓.␈α Defining␈α
␈↓αsecond␈↓␈α
to␈αbe
␈↓ α←␈↓strict␈α∪required␈α∪that␈α∪each␈α∀application␈α∪of␈α∪␈↓αsecond␈↓␈α∪determine␈α∀whether␈α∪either
␈↓ α←␈↓argument␈α⊗denoted␈α⊗␈↓λB␈↓.␈α⊗ If␈α⊗we␈α⊗want␈α⊗␈↓αsecond␈↓␈α⊗to␈α⊗be␈α⊗strict␈α⊗with␈α⊗respect␈α∃to
␈↓ α←␈↓divergence,␈α∂then␈α∂we␈α∂must␈α⊂test␈α∂each␈α∂argument␈α∂for␈α∂divergence.␈α⊂That␈α∂implies
␈↓ α←␈↓evaluation␈α∃of␈α∃each␈α∀of␈α∃the␈α∃arguments,␈α∀which␈α∃in␈α∃turn␈α∀implies␈α∃that␈α∃if␈α∀a
␈↓ α←␈↓computation␈α
of␈α
an␈αargument␈α
diverges,␈α
then␈αthe␈α
computation␈α
of␈α
the␈αfunction
␈↓ α←␈↓application␈α⊂must␈α⊂also␈α⊂diverge.␈α⊂ This␈α⊂implies␈α⊂that␈α⊂it␈α⊂is␈α⊂natural␈α⊂to␈α∂associate
␈↓ α←␈↓"strict␈αwith␈αrespect␈αto␈αdivergence"␈αwith␈α␈↓↓CBV␈↓,␈αsince␈αin␈αthe␈αprocess␈αof␈αchecking
␈↓ α←␈↓for␈αtermination,␈αwe␈αmust␈α
compute␈αvalues.␈αHowever␈αif␈α
a␈αfunction␈αis␈αstrict␈α
then
␈↓ α←␈↓calling␈α
style␈αdoesn't␈α
matter.␈α
In␈αcontrast,␈α
a␈α
non-strict␈αfunction␈α
does␈α
not␈αcheck
␈↓ α←␈↓arguments␈α
for␈α
divergence,␈αand␈α
indeed␈α
the␈αdivergence␈α
of␈α
a␈α
computation␈αmay
␈↓ α←␈↓depend␈α⊃on␈α⊂the␈α⊃calling␈α⊂style.␈α⊃ Consider␈α⊂the␈α⊃evaluation␈α⊃of␈α⊂␈↓αsecond[one[-1]; B]␈↓
␈↓ α←␈↓where␈α␈↓αone␈↓␈αis␈αtotal␈αover␈αthe␈αintegers.␈αThis␈αevaluation␈αwill␈αdiverge␈αunder␈α␈↓↓CBV␈↓
␈↓ α←␈↓while it converges to ␈↓αB␈↓ using ␈↓↓CBN␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αcannot␈αrequire␈αall␈αour␈αfunctions␈αto␈αbe␈αstrict␈αif␈αwe␈αexpect␈αto␈αdo␈αany
␈↓ α←␈↓non-trivial␈α
computation.␈α
That␈α
is,␈α
we␈α
need␈α
a␈α
function␈α
which␈α
can␈αdetermine␈α
its
␈↓ α←␈↓value␈α∂without␈α∂computing␈α∂the␈α∂values␈α∂of␈α∂all␈α∂of␈α∂its␈α∂arguments␈α∂--a␈α∂"don't␈α∞care
␈↓ α←␈↓condition"--.␈α The␈αconditional␈αfunction␈αis␈αsuch␈αa␈αnon-strict␈αfunction.␈α That␈αis
␈↓ α←␈↓␈↓αif(␈↓
t␈↓α;q;r)␈↓␈αhas␈αvalue␈α␈↓αq␈↓␈αwithout␈αknowing␈αanything␈αabout␈αwhat␈αhappens␈αto␈α␈↓αr␈↓.␈α In
␈↓ α←␈↓particular,␈α∞␈↓αif(␈↓
t␈↓α;q;␈↓λB␈↓α) = q␈↓␈α∞and␈α∞␈↓αif(␈↓
f␈↓α;␈↓λB␈↓α;r) = r␈↓.␈α∞ Now␈α∞since␈α∞␈↓αif␈↓␈α∞is␈α∞to␈α∞be␈α∞a␈α∞function
␈↓ α←␈↓and␈α↔therefore␈α↔single-valued,␈α⊗if␈α↔␈↓αif(␈↓
t␈↓α;q;␈↓λB␈↓α) = q␈↓␈α↔then␈α⊗for␈α↔any␈α↔argument␈α⊗␈↓αx␈↓,
␈↓ α←␈↓␈↓αif(␈↓
t␈↓α;q;x) = q␈↓.␈α⊗ Notice␈α⊗that␈α∃␈↓λB␈↓␈α⊗is␈α⊗now␈α∃carrying␈α⊗an␈α⊗additional␈α∃"don't-care"
␈↓ α←␈↓interpretation;␈αthis␈αis␈αconsistent␈αwith␈αits␈αprevious␈αmeaning␈αwhen␈αwe␈αthink␈αof
␈↓ α←␈↓the function being computed by the algorithm.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 22␈↓A␈α⊂discussion␈α⊂of␈α⊂such␈α⊂topics␈α⊂involves␈α⊂a␈α⊂description␈α⊂of␈α⊃the␈α⊂"halting
␈↓ α←␈↓problem" for computational devices. See [Rog 67] for details.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 23␈↓Compare ␈↓εw␈↓-undefined and E-undefined in [Mor 68].
␈↓ α←␈↓␈↓1.5␈↓ ε%Predicates and Conditional Expressions 23␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Even␈α
given␈α∞that␈α
a␈α∞computational␈α
definition␈α
is␈α∞desired,␈α
there␈α∞are␈α
other
␈↓ α←␈↓plausible␈α%interpretations␈α%of␈α%conditionals.␈α% Consider␈α%the␈α%definition:
␈↓ α←␈↓␈↓αg[x;y] <= [lic[x] → 1;␈↓
t␈↓α → 1]␈↓.␈α∂Assuming␈α∂that␈α∞␈↓αlic␈↓␈α∂is␈α∂a␈α∞total␈α∂predicate,␈α∂any␈α∞value
␈↓ α←␈↓computed␈αby␈α␈↓αg␈↓␈αwill␈αbe␈α␈↓α1␈↓.␈αBut␈αrequiring␈αleft-to-right␈αevaluation␈αcould␈αspend␈αa
␈↓ α←␈↓great␈α∞deal␈α∂of␈α∞unnecessary␈α∂computation␈α∞if␈α∂␈↓αlic␈↓␈α∞is␈α∂a␈α∞␈↓αl␈↓ong␈α∂␈↓αi␈↓nvolved␈α∞␈↓αc␈↓alculation.
␈↓ α←␈↓One␈α
might␈α∞further␈α
request␈α∞that␈α
␈↓αg[x;y]␈↓␈α
give␈α∞␈↓α1␈↓␈α
even␈α∞if␈α
␈↓αlic␈↓␈α∞is␈α
non-terminating.
␈↓ α←␈↓Questions␈α_of␈α_evaluation␈α_are␈α↔non-trivial.␈α_We␈α_will␈α_spend␈α_two␈α↔chapters,
␈↓ α←␈↓Chapter 3␈α↔and␈α⊗Chapter 4,␈α↔discussing␈α⊗LISP␈α↔evaluation␈α⊗and␈α↔its␈α⊗possible
␈↓ α←␈↓alternatives.
␈↓"β␈↓ α←␈↓␈↓ β'What␈αbenefits␈αhave␈αresulted␈αfrom␈αour␈αstudy␈αof␈α␈↓λB␈↓␈αand␈αdivergence?␈α We
␈↓ α←␈↓should␈αhave␈αa␈αclearer␈αunderstanding␈αof␈αthe␈αdifference␈αbetween␈αfunction␈αand
␈↓ α←␈↓algorithm␈α
and␈α
a␈α
better␈α
grasp␈α
of␈α
the␈α
kinds␈α
of␈α
difficulties␈α
which␈α
can␈α
befall␈α
a
␈↓ α←␈↓computation.␈α⊂ We␈α⊂have␈α⊂uncovered␈α⊂an␈α⊂important␈α⊂class␈α⊂of␈α⊂detectable␈α⊂errors.
␈↓ α←␈↓The␈α∀character␈α∀of␈α∪these␈α∀miscreants␈α∀is␈α∪that␈α∀they␈α∀occur␈α∪in␈α∀the␈α∀context␈α∪of
␈↓ α←␈↓supplying␈α
the␈α∞wrong␈α
␈↓¬kind␈↓␈α∞of␈α
argument␈α∞to␈α
a␈α∞function.␈α
This␈α∞kind␈α
of␈α∞error␈α
is
␈↓ α←␈↓called␈α
a␈α␈↓↓type␈α
fault␈↓,␈α
meaning␈αthat␈α
we␈αexpected␈α
an␈α
argument␈αof␈α
a␈αspecific␈α
type,
␈↓ α←␈↓that␈αis␈α
from␈αa␈α
specific␈αdomain,␈αand␈α
since␈αit␈α
was␈αnot␈α
forthcoming,␈αwe␈αrefuse␈α
to
␈↓ α←␈↓perform␈α∞any␈α∞kind␈α∞of␈α∞calculation.␈α∞Thus␈α∞␈↓αatom[␈↓
f␈↓α]␈↓␈α∞and␈α∞␈↓αcons[␈↓
t␈↓α;A]␈↓␈α∞are␈α
undefined
␈↓ α←␈↓since␈α∞both␈α∞expect␈α∞elements␈α∞of␈α∞␈↓S␈↓␈α∞as␈α∞arguments.␈α∞ Divergent␈α∞computations␈α∞are
␈↓ α←␈↓equally␈α⊂repugnant␈α⊂but␈α⊂there␈α⊂is␈α⊃no␈α⊂general␈α⊂method␈α⊂for␈α⊂testing␈α⊃whether␈α⊂an
␈↓ α←␈↓arbitrary calculation will terminate.
␈↓"β␈↓ α←␈↓␈↓ β'This␈α
discussion␈αconcludes␈α
the␈αapplicative␈α
portion␈αof␈α
LISP␈αconstructs.␈α
It
␈↓ α←␈↓may␈αnot␈αseem␈αlike␈α
you␈αcan␈αdo␈αmuch␈α
useful␈αcomputation␈αwith␈αsuch␈α
a␈αlimited
␈↓ α←␈↓collection␈α⊃of␈α∩operations␈α⊃as␈α∩those␈α⊃proposed␈α⊃so␈α∩far␈α⊃for␈α∩LISP;␈α⊃there␈α∩are␈α⊃no
␈↓ α←␈↓assignment␈αstatements␈αor␈αexplicit␈αcontrol␈αconstructs.␈α Things␈αare␈αnot␈α
quite␈αas
␈↓ α←␈↓trivial␈αas␈α
they␈αmight␈α
seem.␈α In␈α
elementary␈αnumber␈α
theory␈αall␈α
you␈αhave␈αis␈α
zero
␈↓ α←␈↓and␈α∀some␈α∀simple␈α∀functions,␈α∃and␈α∀elementary␈α∀number␈α∀theory␈α∀is␈α∃far␈α∀from
␈↓ α←␈↓"elementary."␈α≤Manipulation␈α≤of␈α≠our␈α≤primitives,␈α≤with␈α≤composition,␈α≠and
␈↓ α←␈↓conditional␈α∃expressions,␈α∀coupled␈α∃with␈α∃techniques␈α∀for␈α∃definition␈α∃can␈α∀␈↓¬also␈↓
␈↓ α←␈↓become complicated.
␈↓"β␈↓ α←␈↓␈↓ β'Let's␈αapply␈αthe␈αLISP␈αconstructs␈αwhich␈αwe␈αnow␈αhave,␈αand␈αdefine␈αa␈αnew
␈↓ α←␈↓LISP␈α⊂function.␈α⊃ For␈α⊂example:␈α⊂our␈α⊃predicate␈α⊂␈↓αeq␈↓␈α⊂is␈α⊃defined␈α⊂only␈α⊃for␈α⊂atomic
␈↓ α←␈↓arguments.␈α∞ We␈α∂would␈α∞like␈α∂to␈α∞test␈α∂for␈α∞equality␈α∂of␈α∞arbitrary␈α∂S-exprs.␈α∞ What
␈↓ α←␈↓should␈αthis␈αmore␈αcomplex␈αequality␈αmean?␈α By␈αequality␈αwe␈αmean:␈αas␈αtrees,␈αthe
␈↓ α←␈↓S-exprs␈α
have␈α
the␈α
same␈α
branching␈α
structure;␈α
and␈α
the␈α∞corresponding␈α
terminal
␈↓ α←␈↓nodes␈α∩are␈α⊃labeled␈α∩by␈α⊃the␈α∩same␈α⊃atoms.␈α∩ Thus,␈α⊃we␈α∩would␈α⊃like␈α∩to␈α∩define␈α⊃a
␈↓ α←␈↓predicate, ␈↓αequal␈↓, such that:
␈↓""␈↓ α←␈↓α␈↓ ¬#equal[(A . B);(A . B)] = ␈↓
t␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬#equal[A;A] = ␈↓
t␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬#equal[(A . B);(B . A)] = ␈↓
f␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬#equal[(A . (B . C));(A . (B . C))] = ␈↓
t␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬#equal[(A . (B . C));((A . B) . C)] = ␈↓
f␈↓α
␈↓"∀␈↓ α←␈↓Here's an informal description of the ␈↓αequal␈↓ predicate.
␈↓ α←␈↓␈↓24 Symbolic expressions␈↓
(1.5␈↓
␈↓"β␈↓ α←␈↓␈↓↓1.␈↓ If␈αboth␈αarguments␈αare␈αatomic␈αthen␈αsee␈αwhat␈α␈↓αeq␈↓␈αsays␈αabout␈αthem.␈α
We␈αcan
␈↓ α←␈↓␈↓ β∂test if they are both atomic by using ␈↓αatom␈↓ and a conditional expression.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ If one is atomic and the other is not they can't be equal S-exprs.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ Otherwise␈α
both␈α
are␈α
non-atomic␈α
S-exprs.␈α
Both␈α
have␈α
two␈α
sub-expressions.
␈↓ α←␈↓␈↓ β∂Look␈αat␈αboth␈αfirst␈αsubexpressions.␈α If␈αthese␈αsub-expressions␈αare␈αnot␈αequal
␈↓ α←␈↓␈↓ β∂then␈α↔the␈α↔original␈α↔expressions␈α↔cannot␈α⊗be␈α↔equal␈α↔either.␈α↔ If␈α↔the␈α⊗first
␈↓ α←␈↓␈↓ β∂subexpressions␈α␈↓¬are␈↓␈α
equal␈αthen␈αthe␈α
question␈αof␈αwhether␈α
or␈αnot␈αthe␈α
original
␈↓ α←␈↓␈↓ β∂expressions␈α≡are␈α∨equal␈α≡depends␈α∨on␈α≡the␈α∨equality␈α≡of␈α∨the␈α≡second
␈↓ α←␈↓␈↓ β∂subexpressions. Thus the following definition:
␈↓"∀␈↓ α←␈↓α equal[x;y] <=␈↓ ∧C[atom[x] → [atom[y] → eq [x;y]; ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ ∧C atom[y] → ␈↓
f␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ∧C equal [car[x];car[y]] → equal[cdr[x];cdr[y]];
␈↓"β␈↓ α←␈↓α␈↓ ∧C ␈↓
t␈↓α → ␈↓
f␈↓α]
␈↓"∀␈↓ α←␈↓Notice␈α
that␈α
the␈α
third␈α
informal␈α
clause␈α
translates␈α
into␈α
a␈α
LISP␈αconditional␈α
clause
␈↓ α←␈↓which␈α
involves␈αapplications␈α
of␈α
the␈α␈↓αequal␈↓␈α
predicate␈α
itself.␈αThe␈α
use␈αof␈α
␈↓↓recursive␈↓
␈↓ α←␈↓definitions is an important and powerful programming tool.
␈↓"β␈↓ α←␈↓␈↓ β'Notice␈α⊂too␈α⊂that␈α⊂we␈α⊂use␈α⊂nested␈α⊂conditional␈α⊂expressions␈α⊂in␈α⊂␈↓αequal␈↓:␈α⊂␈↓↓e␈↓β1␈↓␈α⊂is
␈↓ α←␈↓itself␈α
a␈αconditional.␈α
Also␈αwe␈α
have␈αused␈α
predicates␈α
in␈αthe␈α
␈↓↓e␈↓βi␈↓␈αpositions␈α
at␈α␈↓↓e␈↓β3␈↓␈α
and
␈↓ α←␈↓␈↓↓e␈↓β11␈↓; this is allowable, and in fact expected, since ␈↓αequal␈↓ is a predicate.
␈↓"β␈↓ α←␈↓␈↓ β'Let's␈α⊃show␈α⊃that␈α⊃␈↓αequal␈↓␈α⊃does␈α⊂perform␈α⊃correctly␈α⊃for␈α⊃a␈α⊃specific␈α⊂example.
␈↓ α←␈↓This␈α⊂will␈α⊂also␈α⊂show␈α⊂a␈α⊂complicated␈α⊂evaluation␈α⊂of␈α⊂a␈α⊃conditional␈α⊂expression.
␈↓ α←␈↓We␈α∩will␈α∩use␈α∩the␈α∩call-by-value␈α∩rules.␈α∩ We␈α∩will␈α∩perform␈α∩the␈α∩evaluation␈α∩by
␈↓ α←␈↓substituting␈αthe␈αevaluated␈α
actual␈αparameters␈αfor␈α
the␈αformal␈αparameters␈αin␈α
the
␈↓ α←␈↓body of the definition. Then we will simplify the resulting expression.␈↓π 24␈↓
␈↓"∀␈↓ α←␈↓α␈↓ ¬πequal[(A . B);(A . C)] ␈↓reduces to:␈↓α
␈↓"∀␈↓ α←␈↓α␈↓ βK[atom[(A . B)] → [atom[(A . C)] → eq[(A . B);(A . C)]; ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ βK atom[(A . C)] → ␈↓
f␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ βK equal[car[(A . B)];car[(A . C)]] → equal[cdr[(A . B)];cdr[(A . C)]];
␈↓"β␈↓ α←␈↓α␈↓ βK ␈↓
t␈↓α → ␈↓
f␈↓α]
␈↓"∀␈↓ α←␈↓We␈αfind␈αthat␈α␈↓↓p␈↓β1␈↓␈α(i.e., ␈↓αatom[(A . B)]␈↓ )␈αand␈α␈↓↓p␈↓β2␈↓␈α( ␈↓αatom[(A . C)]␈↓ )␈αwhen␈αevaluated
␈↓ α←␈↓(in␈α)order)␈α)give␈α)␈↓
f␈↓.␈α) We␈α(must␈α)now␈α)evaluate␈α)␈↓↓p␈↓β3␈↓,␈α)which␈α(is:
␈↓ α←␈↓␈↓αequal[car[(A . B)];car[(A . C)]]␈↓. This reduces to ␈↓αequal[A;A]␈↓, and:
␈↓"∀␈↓ α←␈↓α␈↓ β' equal[A;A] =␈↓ ∧s[atom[A] → [atom[A] → eq[A;A]; ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧s atom[A] → ␈↓
f␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧s equal[car[A];car[A]] → equal[cdr[A];cdr[A]];
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧s ␈↓
t␈↓α → ␈↓
f␈↓α]
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 24␈↓This␈α
is␈α
␈↓¬not␈↓␈αthe␈α
method␈α
LISP␈αuses␈α
to␈α
perform␈αcall-by␈α
value,␈α
but␈αit␈α
has
␈↓ α←␈↓the␈αsame␈αcomputational␈αeffect␈αin␈αmost␈αcases.␈αThe␈αanomalous␈αcases␈αinvolve␈α
an
␈↓ α←␈↓important␈α∪area␈α∪in␈α∀language␈α∪design.␈α∪For␈α∀example,␈α∪how␈α∪should␈α∀␈↓αf[2;3]␈↓␈α∪be
␈↓ α←␈↓evaluated when ␈↓αf[x;y] <= +[x;[y;z]]␈↓ ?
␈↓ α←␈↓␈↓1.5␈↓ ε%Predicates and Conditional Expressions 25␈↓
␈↓"β␈↓ α←␈↓This␈α∞conditional␈α
expression␈α∞will␈α∞evaluate␈α
to␈α∞␈↓
t␈↓.␈α
So␈α∞␈↓↓p␈↓β3␈↓␈α∞in␈α
the␈α∞original␈α∞call␈α
of
␈↓ α←␈↓␈↓αequal[(A . B);(A . C)]␈↓␈αis␈α
true␈αand␈αwe␈α
must␈αevaluate␈αthe␈α
␈↓↓e␈↓β3␈↓␈αexpression␈αwhich␈α
is
␈↓ α←␈↓␈↓αequal[cdr[(A . B)];cdr[(A . C)]]␈↓.␈α∞That␈α∞expression␈α∞simplifies␈α∞to␈α∂␈↓αequal[B;C]␈↓␈α∞and
␈↓ α←␈↓we␈α
call␈α
␈↓αequal␈↓.␈α
After␈α∞substitution␈α
and␈α
simplification␈α
␈↓αequal␈↓␈α
will␈α∞finally␈α
return
␈↓ α←␈↓value␈α~␈↓
f␈↓.␈α→That␈α~means␈α→that␈α~␈↓αequal[(A . B);(A . C)]␈↓␈α→gives␈α~␈↓
f␈↓.␈α~ Notice␈α→that
␈↓ α←␈↓␈↓αeq[(A . B);(A . C)]␈↓␈α
appeared␈α
but␈α
was␈α
never␈α
evaluated␈α
because␈α
of␈α
left-to-right
␈↓ α←␈↓evaluation scheme of conditional expressions.
␈↓"β␈↓ α←␈↓␈↓ β'Clearly,␈α
evaluation␈α
of␈α
LISP␈α
expressions␈α
in␈α
this␈α
amount␈α
of␈α
detail␈α
is␈α
not␈α
a
␈↓ α←␈↓process␈αwhich␈αwe␈αwish␈αto␈αdo␈αvery␈αoften␈αby␈αhand.␈αFortunately␈αthe␈αprocess␈αcan
␈↓ α←␈↓be executed by a machine.
␈↓"β␈↓ α←␈↓␈↓ β'Finally,␈α∃to␈α∃include␈α∃conditional␈α∃expressions␈α∃in␈α∃our␈α∃syntax␈α∃of␈α∀LISP
␈↓ α←␈↓expressions, we should add:
␈↓"∀␈↓ α←␈↓<form> ::= <conditional expression>
␈↓" ␈↓ α←␈↓and <conditional expression> ::= [<form> → <form>; ...; <form> → <form>]
␈↓" ␈↓ α←␈↓where <form> was defined on page 17.
␈↓"∀␈↓ α←␈↓These␈α⊃syntax␈α⊃equations␈α⊃fail␈α⊃to␈α⊃capture␈α⊃all␈α⊃of␈α⊃our␈α⊃intended␈α∩meaning.␈α⊃For
␈↓ α←␈↓example,␈αthe␈α
<form>s␈αappearing␈αin␈α
the␈α␈↓↓p␈↓βi␈↓-position␈αare␈α
restricted␈αto␈α
be␈αforms
␈↓ α←␈↓taking␈α∞values␈α∞in␈α∞␈↓Tr␈↓,␈α∞the␈α∞truth␈α∞domain.␈α∞That␈α∞restriction␈α∞is␈α∞not␈α∞expressed␈α∞in
␈↓ α←␈↓the␈α⊂equations,␈α⊂and␈α⊂indeed,␈α⊂is␈α⊂difficult␈α⊂to␈α⊂express␈α⊂naturally␈α⊂in␈α⊂such␈α∂syntax
␈↓ α←␈↓equations. See [Hop 69] for a discussion of expressibility and grammars.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"∀␈↓ α←␈↓1. Evaluate the following:
␈↓"β␈↓ α←␈↓␈↓ ββ␈↓↓a.␈↓α eq[X;Y]␈↓ εK␈↓↓b.␈↓α cons[X;Y]
␈↓" ␈↓ α←␈↓α␈↓ ββ␈↓↓c.␈↓α car[(X . Y)]␈↓ εK␈↓↓d.␈↓α car[cons[X;Y]]
␈↓" ␈↓ α←␈↓α␈↓ ββ␈↓↓e.␈↓α cadr[(X .(Y . NIL))]␈↓ εK␈↓↓f.␈↓α cdar[(X .(Y . NIL))]
␈↓" ␈↓ α←␈↓α␈↓ ββ␈↓↓g.␈↓α eq[cdr[(A . B)];cdr[(C . B)]]␈↓ εK␈↓↓h.␈↓α atom[cons[(A . B);(C . D)]]
␈↓" ␈↓ α←␈↓α␈↓ ββ␈↓↓i.␈↓α cons[atom[A];atom[(A . B)]]␈↓ εK␈↓↓j.␈↓α eq[atom[ATOM];atom[EQ]]
␈↓" ␈↓ α←␈↓α␈↓ ββ␈↓↓k.␈↓α [␈↓
t␈↓α → A; ␈↓
t␈↓α → B]␈↓ εK␈↓↓l.␈↓α [␈↓
f␈↓α → A; ␈↓
t␈↓α → B] ␈↓↓m.␈↓α [eq[A;B] → 4]
␈↓" ␈↓ α←␈↓α␈↓ ββ␈↓↓n.␈↓α [atom[X] → atom[X]; ␈↓
t␈↓α → FOO]
␈↓" ␈↓ α←␈↓α␈↓ ββ␈↓↓o.␈↓α [eq[EQ; X] → A; eq[A; B] → B; ␈↓
t␈↓α → C]
␈↓" ␈↓ α←␈↓α␈↓ ββ␈↓↓p.␈↓α cons[[eq[A; B] → 1; ␈↓
t␈↓α → FOO]; cons[A; cadr[(A . (B . C))]]]
␈↓" ␈↓ α←␈↓α␈↓ ββ␈↓↓q.␈↓α equal[(A . B);(A . B)]␈↓ εK␈↓↓r.␈↓α eq[(A . B);(A . B)]
␈↓"β␈↓ α←␈↓2. Consider the following definition:
␈↓"∀␈↓ α←␈↓α␈↓ βo twist[s] <=␈↓ ¬#[atom[s] → s;
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ ¬# ␈↓
t␈↓α → cons[twist[cdr[s]];twist[car[s]]]]
␈↓"∀␈↓ α←␈↓α␈↓↓a␈↓. Is the function partial or is it total? Now evaluate:
␈↓" ␈↓ α←␈↓␈↓↓b.␈↓α twist[A] ␈↓↓c.␈↓α twist[(A . B)] ␈↓↓d.␈↓α twist[((A . B) . C)]
␈↓ α←␈↓␈↓26 Symbolic expressions␈↓
(1.5␈↓
␈↓"β␈↓ α←␈↓3. Now try:
␈↓"∀␈↓ α←␈↓α␈↓ βKfindem[x;y] <=␈↓ ¬[atom[x] → [eq[x;y] → T; ␈↓
t␈↓α → NIL];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ¬ ␈↓
t␈↓α → cons[findem[car[x];y];findem[cdr[x];y]]]
␈↓"∀␈↓ α←␈↓α␈↓↓a␈↓. Is this function total? Now evaluate:
␈↓" ␈↓ α←␈↓␈↓ ββ␈↓↓b.␈↓α findem[(A . B);A]␈↓ ¬w␈↓↓c.␈↓α findem[(B .(A . C));A]
␈↓" ␈↓ α←␈↓α␈↓ ββ␈↓↓d.␈↓α findem[(B .(A . C));C]␈↓ ¬w␈↓↓e.␈↓α findem[(A . B);(A . B)]
␈↓"β␈↓ α←␈↓␈↓ ∧J␈↓↓1.6 Sequences: Abstract Data Structures␈↓
␈↓"β␈↓ α←␈↓In␈α∂several␈α∂areas␈α∂of␈α∂mathematics␈α∂it␈α∂is␈α∂convenient␈α∂to␈α∂deal␈α∂with␈α∂sequences␈α∞of
␈↓ α←␈↓information.␈α↔For␈α_example,␈α↔a␈α_problem␈α↔domain␈α_may␈α↔be␈α_more␈α↔naturally
␈↓ α←␈↓described␈αas␈αordered␈αcollections␈αof␈αnumbers␈αrather␈αthan␈αindividual␈αnumbers.
␈↓ α←␈↓This␈α∪may␈α∪either␈α∪simplify␈α∪understanding␈α∪of␈α∪the␈α∪problem␈α∪or␈α∪simplify␈α∩the
␈↓ α←␈↓formulation␈α
of␈α∞the␈α
functions␈α
defined␈α∞on␈α
the␈α
domain.␈α∞ Several␈α
programming
␈↓ α←␈↓languages␈αinclude␈αarrays␈αas␈αrepresentations␈αof␈αthese␈αmathematical␈αideas.␈α We
␈↓ α←␈↓should␈α∂notice␈α∂that␈α∂sequences␈α∂are␈α∂data␈α∂structures.␈α∂We␈α∂will␈α∂have␈α⊂to␈α∂describe
␈↓ α←␈↓constructors,␈αselectors,␈αand␈α
recognizers␈αfor␈αthem.␈α
Subsequently␈αwe␈αwill␈α
explore
␈↓ α←␈↓applications of sequences as data structures.
␈↓"β␈↓ α←␈↓␈↓ β'After␈α∂a␈α∂certain␈α⊂familiarity␈α∂is␈α∂gained␈α⊂in␈α∂the␈α∂application␈α⊂of␈α∂algorithms
␈↓ α←␈↓which␈α∞manipulate␈α∞sequences,␈α
we␈α∞will␈α∞discuss␈α
the␈α∞problems␈α∞of␈α
representation
␈↓ α←␈↓and␈α≥implementation␈α≤of␈α≥this␈α≤data␈α≥structure.␈α≤We␈α≥will␈α≤first␈α≥give␈α≤an
␈↓ α←␈↓implementation␈α∪of␈α∪sequences␈α∪in␈α∪terms␈α∪of␈α∪S-expressions.␈α∪That␈α∪is,␈α∪we␈α∩will
␈↓ α←␈↓describe␈α∃an␈α∀␈↓λr␈↓-mapping␈α∃giving␈α∀a␈α∃representation␈α∀of␈α∃sequences␈α∃and␈α∀their
␈↓ α←␈↓primitive␈αoperations␈αin␈αterms␈αof␈αLISP's␈αS-exprs␈αand␈αprimitive␈αfunctions.␈αStill
␈↓ α←␈↓later␈α∩in␈α∩Section 7.2␈α∩we␈α∩will␈α∩discuss␈α∩low-level␈α∩implementation␈α∩of␈α∩this␈α⊃data
␈↓ α←␈↓structure in terms of conventional machines.
␈↓"β␈↓ α←␈↓␈↓ β'But␈α∞now␈α
we␈α∞will␈α
study␈α∞sequences␈α
as␈α∞abstract␈α
data␈α∞structures:␈α∞what␈α
are
␈↓ α←␈↓their␈α∞essential␈α∞structural␈α∞characteristics?␈α
What␈α∞properties␈α∞should␈α∞be␈α
present
␈↓ α←␈↓in␈α∞a␈α∞programming␈α∞language␈α∞to␈α∞allow␈α∞a␈α∞natural␈α∞and␈α∞flexible␈α∞representation?
␈↓ α←␈↓This␈α
discussion␈α
will␈α
shed␈α
light␈α
on␈α
the␈α
important␈α
problems␈α
of␈α
representation
␈↓ α←␈↓and abstraction.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α
sequence␈α
is␈α
an␈α
ordered␈αset␈α
of␈α
elements.␈↓π 25␈↓␈α
For␈α
example,␈α␈↓↓(x␈↓β1␈↓↓, x␈↓β2␈↓↓, x␈↓β3␈↓↓)␈↓,␈α
is
␈↓ α←␈↓the␈α∞standard␈α∞notation␈α∞for␈α∞a␈α∞sequence␈α∞of␈α∞the␈α∞three␈α∞elements␈α∞␈↓↓x␈↓β1␈↓↓,␈α∞x␈↓β2␈↓,␈α∞and␈α
␈↓↓x␈↓β3␈↓.
␈↓ α←␈↓The␈α∞length␈α∞of␈α∞a␈α∞sequence␈α∞is␈α∂defined␈α∞to␈α∞be␈α∞the␈α∞number␈α∞of␈α∞elements␈α∂in␈α∞that
␈↓ α←␈↓sequence.␈α⊂ We␈α⊂will␈α⊂allow␈α⊃sequences␈α⊂to␈α⊂have␈α⊂sub-sequences␈α⊂to␈α⊃an␈α⊂arbitrary
␈↓ α←␈↓finite␈α
depth.␈α
That␈αis,␈α
the␈α
elements␈αof␈α
a␈α
sequence␈αwill␈α
either␈α
be␈αindividuals␈α
or
␈↓ α←␈↓may␈α
themselves␈α
be␈α∞sequences.␈α
For␈α
example,␈α
a␈α∞sequence␈α
of␈α
length␈α
␈↓αn␈↓,␈α∞each␈α
of
␈↓ α←␈↓whose␈α∩elements␈α∪are␈α∩sequences␈α∩of␈α∪length␈α∩␈↓αm␈↓,␈α∩is␈α∪a␈α∩matrix.␈α∩ Here␈α∪are␈α∩BNF
␈↓ α←␈↓equations for sequences and their elements:
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 25␈↓For␈α⊂an␈α⊂alternative␈α⊃description␈α⊂of␈α⊂sequences␈α⊃and␈α⊂a␈α⊂discussion␈α⊃of␈α⊂a
␈↓ α←␈↓different view of data structures see page 41.
␈↓ α←␈↓␈↓1.6␈↓ εRSequences: Abstract Data Structures 27␈↓
␈↓"β␈↓ α←␈↓<seq>␈↓ ∧7::= ␈↓↓(␈↓ <seq elem>, ...,<seq elem> ␈↓↓)␈↓ ␈↓π 26␈↓
␈↓" ␈↓ α←␈↓<seq elem>␈↓ ∧7::= <indiv> | <seq>
␈↓" ␈↓ α←␈↓<indiv>␈↓ ∧7:: = <literal indiv> | <numeral> | -<numeral>
␈↓" ␈↓ α←␈↓<literal indiv>␈↓ ∧7:: = <indiv letter>
␈↓"β␈↓ α←␈↓␈↓ ∧7:: = <literal indiv><indiv letter>
␈↓"β␈↓ α←␈↓␈↓ ∧7:: = <literal indiv><digit>
␈↓" ␈↓ α←␈↓<numeral>␈↓ ∧7:: = <digit> | <numeral><digit>
␈↓" ␈↓ α←␈↓<indiv letter>␈↓ ∧7:: =␈↓↓ A | B | C ... | Z␈↓
␈↓" ␈↓ α←␈↓<digit>␈↓ ∧7:: = ␈↓↓0 | 1 | 2 ... | 9␈↓
␈↓"β␈↓ α←␈↓Notice␈α
that␈α
the␈α
structure␈α∞of␈α
<indiv>␈α
is␈α
the␈α∞same␈α
as␈α
that␈α
for␈α∞LISP's␈α
<atom>;
␈↓ α←␈↓the␈αonly␈αdifference␈αis␈αin␈αthe␈αfonts␈αused␈αfor␈αletters␈αand␈αdigits.␈αWe␈αhave␈αmade
␈↓ α←␈↓the␈α∞distinction␈α∂between␈α∞LISP␈α∞atoms␈α∂and␈α∞sequence␈α∂individuals␈α∞intentionally.
␈↓ α←␈↓Thus␈α⊂␈↓↓(A, (B, C), D, (E, B))␈↓␈α⊂is␈α⊂a␈α⊂sequence␈α∂of␈α⊂length␈α⊂four,␈α⊂whose␈α⊂second␈α∂and
␈↓ α←␈↓fourth␈αelements␈αare␈α
also␈αsequences␈αwhose␈α
length␈αis␈αtwo.␈α
We␈αwill␈αuse␈α
"␈↓↓( )␈↓"␈αas
␈↓ α←␈↓notation for the empty sequence.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α
want␈α
to␈α
write␈α∞LISP-like␈α
functions␈α
operating␈α
over␈α
sequences,␈α∞so␈α
we
␈↓ α←␈↓will␈α
at␈α
least␈αneed␈α
to␈α
give␈α
constructors,␈αselectors,␈α
recognizers,␈α
and␈αpredicates␈α
for
␈↓ α←␈↓sequences.␈α As␈αin␈αthe␈αcase␈αof␈αS-exprs,␈αwe␈αwill␈αinclude␈αthe␈αundefined␈αelement,
␈↓ α←␈↓and the full domain of sequences will be named
␈↓"∀␈↓ α←␈↓␈↓ ¬]␈↓Seq␈↓ = ␈↓<seq>␈↓∪{␈↓λB␈↓}
␈↓"∀␈↓ α←␈↓␈↓ β'As␈αon␈αpage 18,␈αwe␈αextend␈α
the␈αprimitive␈αLISP␈αoperations␈αto␈αinclude␈α
this
␈↓ α←␈↓new domain, by defining:
␈↓"∀␈↓ α←␈↓␈↓ ¬p␈↓S␈↓β2␈↓ = ␈↓S␈↓β1␈↓∪␈↓<seq>␈↓
␈↓"∀␈↓ α←␈↓and extend each operation appropriately over ␈↓S␈↓β2␈↓. For example:
␈↓""␈↓ α←␈↓α␈↓ ¬watom[␈↓↓A␈↓α] = ␈↓λB␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬wcar[␈↓↓A␈↓α] = ␈↓λB␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬wcar[␈↓↓(A, B)␈↓α] = ␈↓λB␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬wcons[␈↓↓A␈↓; ␈↓↓B␈↓α] = ␈↓λB␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬wissexpr[␈↓↓(A)␈↓α] = ␈↓
f␈↓α
␈↓"∀␈↓ α←␈↓␈↓ β'We␈αneed␈αto␈αdefine␈αsome␈αdata␈αstructure␈αoperations␈αspecific␈αto␈αsequences.
␈↓ α←␈↓What␈α
are␈αthe␈α
essential␈αcharacteristics␈α
of␈αa␈α
sequence?␈αFirst,␈α
a␈α
sequence␈αeither
␈↓ α←␈↓is␈α
empty␈α
or␈αhas␈α
elements.␈α
Thus␈αwe␈α
will␈α
want␈α
a␈αpredicate␈α
to␈α
test␈αfor␈α
emptyness.
␈↓ α←␈↓Next,␈α⊃if␈α⊂the␈α⊃sequence␈α⊂is␈α⊃non-empty,␈α⊂we␈α⊃should␈α⊂be␈α⊃able␈α⊂to␈α⊃select␈α⊂elements.
␈↓ α←␈↓Finally,␈α⊂given␈α⊂some␈α⊂elements,␈α⊂we␈α⊂should␈α⊂be␈α⊂able␈α⊂to␈α⊂build␈α⊂a␈α⊂new␈α∂sequence
␈↓ α←␈↓from them.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 26␈↓For the meaning of these ellipses see page 17.
␈↓ α←␈↓␈↓28 Symbolic expressions␈↓
(1.6␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Predicates␈α∞on␈α∞sequences␈α∞are␈α∞like␈α∞predicates␈α∞on␈α∞S-expressions,␈α
mapping
␈↓ α←␈↓sequences␈α∪to␈α∩truth␈α∪values␈α∩in␈α∪␈↓Tr␈↓.␈↓π 27␈↓␈α∩The␈α∪basic␈α∩predicate,␈α∪which␈α∪tests␈α∩for
␈↓ α←␈↓emptyness, is called ␈↓αnull␈↓.
␈↓"∀␈↓ α←␈↓␈↓ βK␈↓ ∧g␈↓
t␈↓ if ␈↓αx␈↓ is the empty sequence, ␈↓↓( )␈↓.
␈↓"β␈↓ α←␈↓␈↓ βK␈↓αnull[x]␈↓ is␈↓ ∧g␈↓
f␈↓ if ␈↓αx␈↓ is a non-empty sequence.
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ ∧g␈↓λB␈↓ otherwise.
␈↓""␈↓ α←␈↓␈↓ ¬w␈↓αnull[␈↓↓( )␈↓α] = ␈↓
t␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬wnull[␈↓↓(A, B)␈↓α] = ␈↓
f␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬wnull[␈↓
f␈↓α] = ␈↓λB␈↓
␈↓"∀␈↓ α←␈↓Thus␈α␈↓αnull␈↓␈α
gives␈αusable␈α
values␈αonly␈α
for␈αsequences.␈α
Since␈αwe␈α
intend␈αto␈α
operate
␈↓ α←␈↓on␈αdomains␈αwhich␈αcontain␈αdata␈αstructures␈αother␈αthan␈αsequences,␈αwe␈αwill␈αneed
␈↓ α←␈↓a␈α
recognizer␈α
to␈α
be␈α
sure␈α
that␈α
␈↓αnull␈↓␈α
is␈α
not␈α
applied␈α
to␈α
arguments␈α
which␈α
are␈α␈↓¬not␈↓
␈↓ α←␈↓sequences. We will name this recognizer ␈↓αisseq␈↓.
␈↓""␈↓ α←␈↓␈↓ ¬S␈↓αisseq[␈↓↓(A, B, C)␈↓α] = ␈↓
t␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬Sisseq[␈↓↓A␈↓α] = ␈↓
f␈↓
␈↓"⊃␈↓ α←␈↓␈↓ ¬S␈↓αisseq[A] = ␈↓
f␈↓
␈↓"⊃␈↓ α←␈↓␈↓ ¬S␈↓αisseq[␈↓
t␈↓α] = ␈↓
f␈↓
␈↓"⊃␈↓ α←␈↓␈↓ ¬S␈↓αisseq[␈↓↓( )␈↓α] = ␈↓
t␈↓
␈↓"⊃␈↓ α←␈↓␈↓ ¬S␈↓αisseq[␈↓λB␈↓α] = ␈↓λB␈↓
␈↓"∀␈↓ α←␈↓The␈α∂predicate␈α∂␈↓αisseq␈↓␈α∂is␈α∂total␈α∂over␈α∂all␈α∂domains,␈α∂whereas␈α∂␈↓αnull␈↓␈α∂is␈α⊂only␈α∂partial:
␈↓ α←␈↓total over ␈↓<seq>␈↓, but undefined for S-exprs.
␈↓"β␈↓ α←␈↓␈↓ β'While␈α∂on␈α∞the␈α∂subject␈α∞of␈α∂predicates,␈α∂there␈α∞are␈α∂a␈α∞couple␈α∂more␈α∂we␈α∞shall
␈↓ α←␈↓need.␈α∂ The␈α∂first␈α∂one␈α∂is␈α∂a␈α∂recognizer,␈α∂␈↓αisindiv␈↓,␈α∂which␈α∂will␈α∂give␈α∂value␈α∂␈↓
t␈↓␈α∂if␈α∞its
␈↓ α←␈↓argument␈αis␈αan␈αindividual,␈αgive␈α␈↓
f␈↓␈αif␈αits␈αargument␈αis␈αa␈αsequence,␈αand␈αwill␈αgive
␈↓ α←␈↓␈↓λB␈↓ otherwise.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∂second␈α⊂predicate␈α∂is␈α∂the␈α⊂extension␈α∂of␈α∂the␈α⊂equality␈α∂relation␈α⊂to␈α∂the
␈↓ α←␈↓class␈αof␈αsequence␈αindividuals.␈αWe␈αshall␈αuse␈αthe␈αsame␈αname,␈α␈↓αeq␈↓,␈αas␈αwe␈αdid␈αfor
␈↓ α←␈↓the␈αS-expression␈αpredicate.␈α In␈αfact,␈αwhenever␈αwe␈αdefine␈αa␈αnew␈α
abstract␈αdata
␈↓ α←␈↓type␈α∂we␈α∞will␈α∂assume␈α∂that␈α∞an␈α∂appropriate␈α∞version␈α∂of␈α∂␈↓αeq␈↓␈α∞is␈α∂available␈α∂for␈α∞the
␈↓ α←␈↓elements␈α∞of␈α∞the␈α∞base␈α∞domain.␈α∞One␈α∂of␈α∞our␈α∞first␈α∞tasks␈α∞will␈α∞be␈α∞to␈α∂extend␈α∞that
␈↓ α←␈↓equality␈α
relation␈α
to␈α∞the␈α
whole␈α
domain.␈α∞We␈α
will␈α
do␈α∞so␈α
for␈α
sequences␈α∞later␈α
in
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 27␈↓The␈α
reason␈αfor␈α
restructuring␈α
LISP␈αpredicates␈α
might␈α
now␈αbe␈α
apparent
␈↓ α←␈↓to␈αprevious␈α
users␈αof␈α
LISP:␈αif␈α
we␈αmapped␈α
the␈αtruth␈α
values␈αto␈α
the␈αatoms␈α␈↓αT␈↓␈α
and
␈↓ α←␈↓␈↓αNIL␈↓␈α→as␈α→is␈α→typically␈α→done,␈α→then␈α→we'd␈α→have␈α→to␈α→map␈α→truth␈α→values␈α→of
␈↓ α←␈↓sequence-predicates␈α⊂to␈α⊂representations␈α⊂as␈α⊂sequence␈α⊂elements,␈α⊂and␈α⊂we␈α∂would
␈↓ α←␈↓have␈α∃to␈α∀perpetuate␈α∃that␈α∃decision␈α∀for␈α∃every␈α∀new␈α∃abstract␈α∃data␈α∀structure
␈↓ α←␈↓domain that we wanted to introduce.
␈↓ α←␈↓␈↓1.6␈↓ εRSequences: Abstract Data Structures 29␈↓
␈↓"β␈↓ α←␈↓this␈αsection.␈αEquality␈αis␈αa␈αbasic␈αrelation␈αin␈αmathematics␈αso␈αit␈αis␈αnot␈αsurprising
␈↓ α←␈↓to␈αsee␈αit␈αplay␈αan␈αimportant␈αrole␈αhere.␈α
␈↓αeq␈↓␈αis␈αone␈αof␈αthe␈αfew␈αrelations␈αwhich␈α
we
␈↓ α←␈↓shall␈α⊂define␈α∂across␈α⊂all␈α⊂domains.␈α∂Functions␈α⊂or␈α⊂predicates␈α∂like␈α⊂␈↓αeq␈↓,␈α⊂which␈α∂are
␈↓ α←␈↓applicable on several domains, are called ␈↓↓polymorphic functions␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Next,␈αthe␈αselectors␈αfor␈αa␈α(non-empty)␈αsequence␈αinclude:␈α␈↓αfirst␈↓,␈α␈↓αsecond␈↓,␈αetc,
␈↓ α←␈↓where:
␈↓""␈↓ α←␈↓␈↓ ¬G␈↓αfirst[␈↓↓(A, B, C)␈↓α] = ␈↓↓A␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬Gsecond[␈↓↓(A, B, C)␈↓α] = ␈↓↓B␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬Gthird[␈↓↓(A, B)␈↓α] = ␈↓λB␈↓
␈↓"∀␈↓ α←␈↓It is also convenient to define an "all-but-first" selector, called ␈↓αrest␈↓.
␈↓""␈↓ α←␈↓␈↓ ¬;␈↓αrest[␈↓↓(A, B, C)␈↓α] = ␈↓↓(B, C)␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬;rest[␈↓↓(B, C)␈↓α] = ␈↓↓(C)␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬;rest[␈↓↓(C)␈↓α] = ␈↓↓( )␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬;rest[␈↓↓C␈↓α] = ␈↓λB␈↓
␈↓"⊃␈↓ α←␈↓␈↓ ¬;␈↓αrest[␈↓↓( )␈↓α] = ␈↓λB␈↓
␈↓"∀␈↓ α←␈↓In␈α
conjunction␈α
with␈α
␈↓αrest␈↓,␈αwe␈α
shall␈α
utilize␈α
a␈αconstructor,␈α
␈↓αconcat␈↓,␈α
which␈α
is␈αto␈α
add
␈↓ α←␈↓a single element to the front of a sequence.
␈↓""␈↓ α←␈↓␈↓ ¬↔␈↓αconcat[␈↓↓A␈↓α;␈↓↓(B,C)␈↓α] = ␈↓↓(A, B, C)␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬↔concat[␈↓↓A␈↓α;␈↓↓( )␈↓α] = ␈↓↓(A)␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬↔concat[␈↓↓(A)␈↓α;␈↓↓(B,C)␈↓α] = ␈↓↓((A), B, C)␈↓α
␈↓"⊃␈↓ α←␈↓α␈↓ ¬↔concat[␈↓↓(B,C)␈↓α;␈↓↓A␈↓α] = ␈↓λB␈↓
␈↓"⊃␈↓ α←␈↓␈↓ ¬↔␈↓αconcat[␈↓↓A␈↓α; ␈↓↓B␈↓α] = ␈↓λB␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α⊃final␈α⊃constructor␈α∩is␈α⊃called␈α⊃␈↓αseq␈↓;␈α⊃it␈α∩takes␈α⊃an␈α⊃arbitrary␈α∩number␈α⊃of
␈↓ α←␈↓sequence␈α∞elements␈α∞as␈α∂arguments␈α∞and␈α∞returns␈α∂a␈α∞sequence␈α∞consisting␈α∂of␈α∞those
␈↓ α←␈↓elements␈α⊂(in␈α⊂the␈α⊂obvious␈α⊂order).␈α⊂ Let␈α⊂␈↓λα␈↓β1␈↓, ..., ␈↓λα␈↓βn␈↓␈α⊂be␈α⊂elements␈α⊂of␈α⊂<seq␈α∂elem>,
␈↓ α←␈↓then:
␈↓"β␈↓ α←␈↓α␈↓ ¬⊃seq[␈↓λα␈↓β1␈↓α; ␈↓λα␈↓β2␈↓α; ...; ␈↓λα␈↓βn␈↓α] = ␈↓↓(␈↓λα␈↓β1␈↓↓, ..., ␈↓λα␈↓βn␈↓↓)␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'One␈αquestion␈αmay␈αhave␈α
come␈αto␈αmind:␈αhow␈α
do␈αwe␈αknow␈αwhen␈αwe␈α
have
␈↓ α←␈↓a␈α∃sufficient␈α∃set␈α∃of␈α∃functions␈α⊗for␈α∃the␈α∃manipulation␈α∃of␈α∃an␈α⊗abstract␈α∃data
␈↓ α←␈↓structure?␈α
How␈αdo␈α
we␈α
know␈αwe␈α
haven't␈αleft␈α
some␈α
crucial␈αfunctions␈α
out?␈αIf␈α
we
␈↓ α←␈↓have␈αenough,␈αhow␈αdo␈αwe␈αknow␈αthat␈αwe␈αhaven't␈αincluded␈αtoo␈αmany?␈αActually,
␈↓ α←␈↓this␈α∩second␈α∩case␈α⊃isn't␈α∩disastrous,␈α∩but␈α⊃when␈α∩implementing␈α∩the␈α∩functions␈α⊃it
␈↓ α←␈↓would␈α∂be␈α∞nice␈α∂to␈α∞minimize␈α∂the␈α∞number␈α∂of␈α∞primitives␈α∂we␈α∞have␈α∂to␈α∞program.
␈↓ α←␈↓These␈α
problems␈α
are␈αworthy␈α
of␈α
study␈α
and␈αare␈α
the␈α
concern␈α
of␈αanyone␈α
interested
␈↓ α←␈↓␈↓30 Symbolic expressions␈↓
(1.6␈↓
␈↓"β␈↓ α←␈↓in␈α∩the␈α⊃design␈α∩of␈α⊃programming␈α∩languages.␈α⊃We␈α∩will␈α⊃say␈α∩a␈α⊃bit␈α∩more␈α⊃about
␈↓ α←␈↓solutions to these questions beginning on page 36.
␈↓"β␈↓ α←␈↓␈↓ β'Notice␈α⊂that␈α⊂we␈α⊂have␈α⊂been␈α⊂describing␈α⊂the␈α⊂sequence␈α⊃functions␈α⊂without
␈↓ α←␈↓regard␈αto␈αany␈αunderlying␈αrepresentation.␈α We␈αhave␈αsaid␈αnothing␈α
about␈αthese
␈↓ α←␈↓sequence␈α∂operations␈α∂except␈α∂that␈α∂they␈α∂construct,␈α∂test,␈α∂or␈α∂select.␈α∂ We␈α∞consider
␈↓ α←␈↓sequences␈αas␈αabstract␈αdata␈α
structures,␈αsuitable␈αfor␈αmanipulation␈α
by␈αLISP-like
␈↓ α←␈↓algorithms;␈α⊂we␈α⊂define␈α∂algorithms␈α⊂over␈α⊂the␈α∂domain␈α⊂of␈α⊂sequences,␈α⊂using␈α∂the
␈↓ α←␈↓primitive␈αoperations,␈α
conditional␈αexpressions,␈α
and␈αrecursion.␈α
How␈αsequences
␈↓ α←␈↓are␈α∃represented␈α∃as␈α⊗S-exprs␈α∃or␈α∃represented␈α⊗on␈α∃a␈α∃machine,␈α⊗is␈α∃irrelevant.
␈↓ α←␈↓Sequences␈α_have␈α_certain␈α_inherent␈α_structural␈α_properties␈α_and␈α_it␈α→is␈α_those
␈↓ α←␈↓properties␈α⊗which␈α↔we␈α⊗must␈α↔understand␈α⊗␈↓¬before␈↓␈α↔we␈α⊗begin␈α↔thinking␈α⊗about
␈↓ α←␈↓representation.␈α In␈α
the␈αnext␈αsection␈α
we␈αwill␈αshow␈α
how␈αto␈α
represent␈αsequences
␈↓ α←␈↓as␈αcertain␈αS-expressions␈αand␈αsequence␈αoperations␈αas␈αLISP␈αoperations␈αon␈αthat
␈↓ α←␈↓representation.
␈↓"β␈↓ α←␈↓␈↓ β'Let's␈α∀develop␈α∪some␈α∀expertise␈α∪in␈α∀manipulating␈α∪sequences.␈α∀ The␈α∪first
␈↓ α←␈↓example␈α∪will␈α∪be␈α∪an␈α∪extension␈α∪of␈α∪the␈α∪equality␈α∪relation␈α∪to␈α∪sequences.␈α∩We
␈↓ α←␈↓perpetuate␈α∩the␈α∪name␈α∩␈↓αequal␈↓␈α∪from␈α∩S-exprs,␈α∪and␈α∩the␈α∪basic␈α∩structure␈α∪of␈α∩the
␈↓ α←␈↓definition␈α∪will␈α∩parallel␈α∪that␈α∪of␈α∩its␈α∪namesake;␈α∪but␈α∩the␈α∪components␈α∪of␈α∩the
␈↓ α←␈↓definition␈αwill␈α
involve␈αsequence␈α
operations␈αrather␈α
than␈αS-expr␈α
operations.␈αIt
␈↓ α←␈↓will␈αbe␈αof␈αvalue␈αto␈αcompare␈αthe␈αtwo␈αpredicates.␈α The␈αS-expr␈αversion␈αis␈αto␈αbe
␈↓ α←␈↓found on page 24.
␈↓"∀␈↓ α←␈↓α equal[x;y] <=␈↓ ∧7[isindiv[x] → [isindiv[y] → eq[x;y]; ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 isindiv[y] → ␈↓
f␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ∧7 null[x] → [null[y] → ␈↓
t␈↓α; ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 null[y] → ␈↓
f␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ∧7 equal[first[x];first[y]] → equal[rest[x];rest[y]];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 ␈↓
t␈↓α → ␈↓
f␈↓α]
␈↓"∀␈↓ α←␈↓This␈α_␈↓αequal␈↓␈α_works␈α_on␈α_sequences␈α_and␈α_sequence␈α_elements␈α_as␈α_its␈α_S-expr
␈↓ α←␈↓counterpart worked on dotted pairs and atoms.
␈↓"β␈↓ α←␈↓␈↓ β'Next,␈αwe␈αwill␈αwrite␈αa␈αpredicate␈α␈↓αmember␈↓␈αof␈αtwo␈αarguments␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓.␈α ␈↓αx␈↓␈αis
␈↓ α←␈↓to␈α
be␈α
an␈α
individual;␈α
␈↓αy␈↓␈α
is␈α
to␈α
be␈αa␈α
sequence;␈α
␈↓αmember␈↓␈α
is␈α
to␈α
return␈α
␈↓
t␈↓␈α
just␈α
in␈αthe
␈↓ α←␈↓case␈αthat␈α␈↓αx␈↓␈αis␈αan␈αelement␈αof␈αthe␈αsequence␈α␈↓αy␈↓.␈α What␈αdoes␈αthis␈αspecification␈αtell
␈↓ α←␈↓us?␈α The␈αpredicate␈α
is␈αpartial.␈αThe␈α
recursion␈αshould␈αbe␈α
on␈αthe␈αstructure␈α
of␈α␈↓αy␈↓;
␈↓ α←␈↓and␈α
termination␈α(with␈α
value␈α␈↓
f␈↓)␈α
should␈α
occur␈αif␈α
␈↓αy␈↓␈αis␈α
the␈α
empty␈αsequence.␈α
If␈α␈↓αy␈↓␈α
is
␈↓ α←␈↓not␈α
empty␈α
then␈α
it␈α
has␈α
a␈α
first␈α
element;␈αcall␈α
it␈α
␈↓αz␈↓.␈α
Compare␈α
␈↓αz␈↓␈α
with␈α
␈↓αx␈↓.␈α
If␈αthese
␈↓ α←␈↓elements␈αare␈αidentical␈α
then␈α␈↓αmember␈↓␈αshould␈αreturn␈α
␈↓
t␈↓;␈αotherwise␈αsee␈αif␈α
␈↓αx␈↓␈αoccurs
␈↓ α←␈↓in the remainder of the sequence ␈↓αy␈↓.
␈↓ α←␈↓␈↓1.6␈↓ εRSequences: Abstract Data Structures 31␈↓
␈↓"β␈↓ α←␈↓Notes:
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ We␈α∃cannot␈α⊗use␈α∃␈↓αeq␈↓␈α∃directly␈α⊗to␈α∃check␈α∃equality␈α⊗since,␈α∃though␈α∃␈↓αx␈↓␈α⊗is␈α∃an
␈↓ α←␈↓␈↓ β∂individual,␈α∞there␈α∞is␈α∞no␈α∂reason␈α∞that␈α∞the␈α∞elements␈α∂of␈α∞␈↓αy␈↓␈α∞need␈α∞be.␈α∂ We␈α∞will
␈↓ α←␈↓␈↓ β∂introduce␈αa␈αsubsidiary␈αpredicate␈α␈↓αsame␈↓␈αto␈αassure␈αthat␈α␈↓αeq␈↓␈αis␈αapplied␈αonly␈αto
␈↓ α←␈↓␈↓ β∂arguments of the correct type.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ Recall␈αthat␈α
we␈αcan␈αget␈α
the␈αfirst␈αelement␈α
of␈αa␈αsequence␈α
with␈α␈↓αfirst␈↓,␈α
and␈αthe
␈↓ α←␈↓␈↓ β∂rest of a sequence with ␈↓αrest␈↓.
␈↓"β␈↓ α←␈↓So here's ␈↓αmember␈↓:
␈↓"∀␈↓ α←␈↓α␈↓ ∧∪member[x;y] <=␈↓ ¬S[null[y] → ␈↓
f␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬S same[first[y];x] → ␈↓
t␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬S ␈↓
t␈↓α → member[x;rest[y]]]
␈↓"∀␈↓ α←␈↓α␈↓where:␈↓ ∧R␈↓αsame[u;v] <= [isindiv[u] → eq[u;v]; ␈↓
t␈↓α → ␈↓
f␈↓α]
␈↓"∀␈↓ α←␈↓␈↓ β'Next␈αis␈αan␈αarithmetic␈αexample␈αto␈αcalculate␈αthe␈αnumber␈αof␈αelements␈αin␈αa
␈↓ α←␈↓sequence.
␈↓"∀␈↓ α←␈↓α␈↓ ∧αlength[n] <= [null[n] → 0; ␈↓
t␈↓α → plus[1;length[rest[n]]]]
␈↓"β␈↓ α←␈↓␈↓ ∧M␈↓↓1.7 Lists: Representations of Sequences␈↓
␈↓"β␈↓ α←␈↓We␈α∂can␈α∂now␈α∂write␈α∂LISP-like␈α∂functions␈α∂describing␈α∂operations␈α∂on␈α∂sequences;
␈↓ α←␈↓the␈α∞algorithms␈α∂are␈α∞clean␈α∂and␈α∞understandable.␈α∂ However,␈α∞if␈α∂we␈α∞wish␈α∂to␈α∞run
␈↓ α←␈↓these␈α
programs␈α
in␈α
a␈α
LISP␈α
environment,␈αthen␈α
we␈α
have␈α
to␈α
represent␈α
the␈αdata
␈↓ α←␈↓structures␈α
and␈αthe␈α
algorithms␈α
in␈αterms␈α
understandable␈α
to␈αLISP.␈↓π 28␈↓␈α
This␈αis␈α
the
␈↓ α←␈↓problem␈αof␈αrepresentation.␈αGranted,␈αwe␈αcould␈αhave␈αovercome␈αthe␈αproblem␈αby
␈↓ α←␈↓representing␈αsequences␈α
directly␈αas␈α
LISP␈αS-expressions␈α
and␈αcould␈αhave␈α
written
␈↓ α←␈↓functions␈α∀in␈α∀LISP␈α∀which␈α∀used␈α∀␈↓αcar-cdr␈↓-chains␈α∀to␈α∀directly␈α∃manipulate␈α∀the
␈↓ α←␈↓representations.␈α∀ However,␈α∀the␈α∃resulting␈α∀programs␈α∀would␈α∀be␈α∃much␈α∀more
␈↓ α←␈↓difficult␈αto␈αread␈α
and␈αdebug␈αand␈α
understand.␈α More␈αimportant,␈α
the␈αprograms
␈↓ α←␈↓would␈α∩be␈α∩explicitly␈α∩tied␈α∩to␈α∩a␈α∩specific␈α∩representation␈α∩of␈α∩the␈α∪abstract␈α∩data
␈↓ α←␈↓structure.␈αAt␈α
some␈αlater␈α
date␈αit␈α
might␈αbe␈α
desired␈αto␈α
change␈αthe␈α
representation;
␈↓ α←␈↓then␈α∂many␈α⊂programs␈α∂would␈α∂have␈α⊂to␈α∂be␈α∂rewritten.␈α⊂ We␈α∂will␈α⊂illustrate␈α∂these
␈↓ α←␈↓difficulties␈α_soon.␈α_ In␈α_Section 2.3␈α↔we␈α_develop␈α_a␈α_complex␈α_algorithm␈α↔for
␈↓ α←␈↓differentiation␈αon␈α
a␈αclass␈αof␈α
polynomials,␈αmoving␈αfrom␈α
an␈αunclear␈αand␈α
highly
␈↓ α←␈↓representation-dependent␈α?␈ααformulation,␈α?␈ααto␈α?␈ααa␈α?␈αβclear,␈α?␈ααconcise,
␈↓ α←␈↓representation-independent algorithm.
␈↓"β␈↓ α←␈↓␈↓ β'Obviously␈α∩we␈α∩will␈α∩always␈α∪have␈α∩to␈α∩supply␈α∩a␈α∪representational␈α∩bridge
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 28␈↓If␈α⊃we␈α∩wish␈α⊃LISP␈α∩to␈α⊃run␈α⊃on␈α∩a␈α⊃conventional␈α∩machine␈α⊃we␈α∩have␈α⊃to
␈↓ α←␈↓represent␈αLISP's␈αdata␈αstructures␈αand␈αalgorithms␈αin␈αa␈αmanner␈αunderstandable
␈↓ α←␈↓to that hardware. This task is the subject of later chapters in the book.
␈↓ α←␈↓␈↓32 Symbolic expressions␈↓
(1.7␈↓
␈↓"β␈↓ α←␈↓between␈α∀the␈α∀abstract␈α∀data␈α∃structures␈α∀and␈α∀algorithms,␈α∀and␈α∃their␈α∀concrete
␈↓ α←␈↓counterparts.␈α∂ One␈α∂aspect␈α∂of␈α∂this␈α∂study␈α∂of␈α∂data␈α∂structures␈α∂is␈α∂to␈α∂understand
␈↓ α←␈↓what␈α∀is␈α∪required␈α∀to␈α∪build␈α∀this␈α∀bridge␈α∪and␈α∀how␈α∪best␈α∀to␈α∀represent␈α∪these
␈↓ α←␈↓requirements in a programming language.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊂first␈α⊂decision␈α⊂to␈α⊂be␈α⊂made␈α⊂is␈α⊂how␈α⊂to␈α⊂represent␈α⊂the␈α⊃abstract␈α⊂data
␈↓ α←␈↓structure;␈α
how␈α
should␈α
we␈α∞represent␈α
sequences␈α
as␈α
S-expressions?␈α∞How␈α
should
␈↓ α←␈↓we␈α⊂choose␈α⊂representations␈α⊃in␈α⊂general?␈α⊂Usually␈α⊂there␈α⊃is␈α⊂not␈α⊂just␈α⊃one␈α⊂"best"
␈↓ α←␈↓representation.␈α≠Some␈α≠obvious␈α≠considerations␈α≠involve␈α≠the␈α≠difficulty␈α≠of
␈↓ α←␈↓implementing␈α∪the␈α∀primitive␈α∪operations␈α∪(constructors,␈α∀selectors,␈α∪recognizers,
␈↓ α←␈↓and␈α
predicates)␈α
on␈α
the␈α
abstract␈α
data␈α
structure.␈α
Also␈α
we␈α
must␈α
keep␈α
in␈αmind␈α
the
␈↓ α←␈↓kinds␈α⊂of␈α⊂algorithms␈α⊂which␈α⊂we␈α⊃wish␈α⊂to␈α⊂write;␈α⊂computation␈α⊂takes␈α⊃time,␈α⊂and
␈↓ α←␈↓since this is computer science we should give consideration to efficiency.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α
reasonable␈α
choice␈αfor␈α
a␈α
representation␈αof␈α
sequences␈α
as␈αS-expressions
␈↓ α←␈↓is the following:
␈↓"∀␈↓ α←␈↓␈↓ ¬>␈↓λr␈↓∞(␈↓<indiv>␈↓∞)␈↓ = <atom>
␈↓" ␈↓ α←␈↓and for ␈↓λα␈↓β1␈↓, ..., ␈↓λα␈↓βn␈↓ in ␈↓<seq elem>␈↓:
␈↓"␈↓ α←␈↓∂␈↓ αw ␈↓λr␈↓∞(␈↓↓ (␈↓λα␈↓β1␈↓, ..., ␈↓λα␈↓βn␈↓↓) ␈↓∞)␈↓ = ␈↓∂␈↓ ¬S /\
␈↓"␈↓ α←␈↓∂␈↓ αw␈↓ ¬S / \
␈↓"␈↓ α←␈↓∂␈↓ αw␈↓ ¬S ␈↓λr␈↓∞(␈↓↓ ␈↓λα␈↓β1␈↓↓ ␈↓∞)␈↓∂ \
␈↓"␈↓ α←␈↓∂␈↓ αw␈↓ ¬S \
␈↓"␈↓ α←␈↓∂␈↓ αw␈↓ ¬S \
␈↓"␈↓ α←␈↓∂␈↓ αw␈↓ ¬S /\
␈↓"␈↓ α←␈↓∂␈↓ αw␈↓ ¬S / \
␈↓"␈↓ α←␈↓∂␈↓ αw␈↓ ¬S ␈↓λr␈↓∞(␈↓↓ ␈↓λα␈↓β2␈↓↓ ␈↓∞)␈↓∂
␈↓"␈↓ α←␈↓∂␈↓ αw␈↓ ¬S ...
␈↓"␈↓ α←␈↓∂␈↓ αw␈↓ ¬S \
␈↓"␈↓ α←␈↓∂␈↓ αw␈↓ ¬S /\
␈↓"␈↓ α←␈↓∂␈↓ αw␈↓ ¬S / \
␈↓"␈↓ α←␈↓∂␈↓ αw␈↓ ¬S / \
␈↓"␈↓ α←␈↓∂␈↓ αw␈↓ ¬S / ␈↓αNIL␈↓∂
␈↓"␈↓ α←␈↓∂␈↓ αw␈↓ ¬S ␈↓λr␈↓∞(␈↓↓ ␈↓λα␈↓βn␈↓↓ ␈↓∞)
␈↓"∀␈↓ α←␈↓The␈α∂right-hand␈α∞branch␈α∂in␈α∞this␈α∂LISP-tree␈α∞representation␈α∂of␈α∞a␈α∂sequence␈α∞will
␈↓ α←␈↓always␈α
point␈αto␈α
the␈α
rest␈αof␈α
the␈α
sequence␈αor␈α
will␈α
be␈αthe␈α
atom␈α
␈↓αNIL␈↓.␈α Notice␈α
that
␈↓ α←␈↓the description of the ␈↓λr␈↓-mapping is recursive. Thus for example:
␈↓ α←␈↓␈↓1.7␈↓ εVLists: Representations of Sequences 33␈↓
␈↓"⊃␈↓ α←␈↓∂␈↓ β3 ␈↓λr␈↓∞(␈↓↓ ((A,B,C),(D)) ␈↓∞)␈↓ = ␈↓∂␈↓ ε' /\
␈↓"␈↓ α←␈↓∂␈↓ β3␈↓ ε' / \
␈↓"␈↓ α←␈↓∂␈↓ β3␈↓ ε' / \
␈↓"␈↓ α←␈↓∂␈↓ β3␈↓ ε' ␈↓λr␈↓∞(␈↓↓ (A,B,C) ␈↓∞)␈↓∂ \
␈↓"␈↓ α←␈↓∂␈↓ β3␈↓ ε' \
␈↓"␈↓ α←␈↓∂␈↓ β3␈↓ ε' \
␈↓"␈↓ α←␈↓∂␈↓ β3␈↓ ε' /\
␈↓"␈↓ α←␈↓∂␈↓ β3␈↓ ε' / \
␈↓"␈↓ α←␈↓∂␈↓ β3␈↓ ε' / ␈↓αNIL␈↓∂
␈↓"␈↓ α←␈↓∂␈↓ β3␈↓ ε' ␈↓λr␈↓∞(␈↓↓ (D) ␈↓∞)␈↓∂
␈↓"∀␈↓ α←␈↓which␈αwill␈αfinally␈α
expand␈αto␈α␈↓α((A␈α.␈α
(B␈α.␈α(C␈α
.␈αNIL)))␈α.␈α((D␈α
.␈αNIL)␈α.␈αNIL))␈↓␈α
since
␈↓ α←␈↓␈↓λr␈↓∞(␈↓↓ (A,B,C) ␈↓∞)␈↓ is ␈↓α(A . (B . (C . NIL)))␈↓ and ␈↓λr␈↓∞(␈↓↓ (D) ␈↓∞)␈↓ is ␈↓α(D . NIL)␈↓
␈↓"β␈↓ α←␈↓␈↓ β'For␈α_convenience␈α→sake␈α_we␈α→will␈α_carry␈α→over␈α_the␈α→sequence␈α_notation
␈↓ α←␈↓-- ␈↓↓(A, B, C)␈↓ --␈αto␈αthat␈αfor␈αthe␈αrepresentation␈αin␈αLISP␈α-- ␈↓α(A, B, C)␈↓ --␈↓π 29␈↓␈αthinking
␈↓ α←␈↓of ␈↓α(A, B, C)␈↓ as an abbreviation for ␈↓α(A . (B . (C . NIL)))␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Next,␈αwhat␈α
about␈αa␈α
representation␈αfor␈αthe␈α
empty␈αsequence?␈α
Looking␈αat
␈↓ α←␈↓the␈α
representation␈αof␈α
a␈α
non-empty␈αsequence␈α
it␈α
appears␈αnatural␈α
to␈α
take␈α␈↓αNIL␈↓␈α
as
␈↓ α←␈↓␈↓λr␈↓∞(␈↓↓( )␈↓∞)␈↓␈α⊂since␈α∂after␈α⊂you␈α⊂have␈α∂removed␈α⊂all␈α⊂the␈α∂elements␈α⊂from␈α⊂the␈α∂sequence
␈↓ α←␈↓␈↓αNIL␈↓ is all that is left in the representation. To be consistent then:
␈↓"β␈↓ α←␈↓␈↓ ¬k␈↓λr␈↓∞(␈↓↓ ( ) ␈↓∞)␈↓ = ␈↓αNIL␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'This␈α∃gives␈α∀us␈α∃a␈α∀complete␈α∃specification␈α∀of␈α∃the␈α∀␈↓λr␈↓-mapping␈α∃for␈α∀the
␈↓ α←␈↓domain;␈αwe␈αhave␈αrepresented␈α
the␈αabstract␈αdomain␈αof␈α
sequences␈αin␈αa␈αsubset␈α
of
␈↓ α←␈↓the␈α↔domain␈α↔of␈α↔Symbolic␈α↔Expressions.␈α↔The␈α↔S-expr␈α↔representation␈α_of␈α↔a
␈↓ α←␈↓sequence is called a ␈↓↓list␈↓; and we will refer to the abbreviation,
␈↓"∀␈↓ α←␈↓␈↓ β2␈↓α(␈↓λα␈↓β1␈↓α, ..., ␈↓λα␈↓βn␈↓α)␈↓ for ␈↓α(␈↓λα␈↓β1␈↓α . (␈↓λα␈↓β2␈↓α . ... (␈↓λα␈↓βn␈↓α . NIL) ...))␈↓ as ␈↓↓list-notation␈↓.
␈↓"∀␈↓ α←␈↓␈↓ β'Sequences␈α↔are␈α↔the␈α↔abstract␈α_data␈α↔structure;␈α↔lists␈α↔are␈α↔one␈α_of␈α↔their
␈↓ α←␈↓representations.␈α_ Since␈α↔the␈α_atom␈α↔␈↓αNIL␈↓␈α_takes␈α↔on␈α_special␈α_significance␈α↔in
␈↓ α←␈↓list-notation it is endowed with the special name ␈↓↓list terminator␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'And␈αa␈αnotational␈αpoint:␈α
in␈αgraphical␈αinterpretation␈αof␈αlist-notation␈α
it␈αis
␈↓ α←␈↓often convenient to write:
␈↓"␈↓ α←␈↓
␈↓ ∧C⊂αααπααααα⊃␈↓ π∪ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
␈↓ ∧C~ ~ NIL ~ ␈↓as␈↓
␈↓ π∪ ~ ~≤'~
␈↓"␈↓ α←␈↓
␈↓ ∧C%ααα∀ααααα$␈↓ π∪ %ααα∀αα$
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 29␈↓Be␈α
aware␈α
that␈α
␈↓αA␈↓␈α
is␈α
an␈α
atom␈α
and␈α
␈↓↓A␈↓␈α
is␈α
a␈α
sequence␈α
element;␈α
they␈α
are␈α
␈↓¬not␈↓
␈↓ α←␈↓the same data structure.
␈↓ α←␈↓␈↓34 Symbolic expressions␈↓
(1.7␈↓
␈↓"β␈↓ α←␈↓For example ␈↓α(A, (B, C), D)␈↓ is:
␈↓"␈↓ α←␈↓
⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
~ A ~ #αβα→~ # ~ #αβα→~ D ~≤'~
␈↓"␈↓ α←␈↓
%ααα∀ααα$ %αβα∀ααα$ %ααα∀αα$
␈↓"␈↓ α←␈↓
~
␈↓"␈↓ α←␈↓
~ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
%αα→~ B ~ #αβα→~ C ~≤'~
␈↓"␈↓ α←␈↓
%ααα∀ααα$ %ααα∀αα$
␈↓"β␈↓ α←␈↓or, in "dotted-pair" notation: ␈↓α (A . ((B . (C . NIL)) . (D . NIL)))␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Finally, in list-notation the commas can be replaced by spaces␈↓π 30␈↓
␈↓"∀␈↓ α←␈↓␈↓ ¬εe.g. ␈↓α(A, (B, C), D) = (A (B C) D)
␈↓" ␈↓ α←␈↓α␈↓but beware: the "dots" in dot-notation are ␈↓¬never␈↓ optional!
␈↓"∀␈↓ α←␈↓␈↓ ¬βthat is ␈↓α(A . (B . C)) ␈↓λ≠␈↓α (A (B C))
␈↓"∀␈↓ α←␈↓␈↓ β'At␈αthis␈αpoint␈αwe␈αhave␈αan␈αintuitive␈αunderstanding␈αof␈αwhat␈αwe␈αmean␈αby
␈↓ α←␈↓"sequence";␈α∞we␈α∞have␈α∞described␈α∞selectors,␈α∞constructors,␈α∞and␈α∂recognizers,␈α∞albeit
␈↓ α←␈↓at␈α
an␈α
abstract␈α
level,␈α
for␈α
manipulating␈α
sequences,␈α
and␈α
we␈α
have␈α
represented␈α
our
␈↓ α←␈↓notion␈α∞of␈α∞sequences␈α∞as␈α∞a␈α∞subset␈α∞of␈α∞the␈α∞S-expressions␈α∞called␈α∞lists.␈α∞ The␈α∞final
␈↓ α←␈↓step␈αis␈α
to␈αrepresent␈α
our␈αsequence-manipulators␈α
as␈αcertain␈α
LISP␈αfunctions.␈α
Let
␈↓ α←␈↓␈↓αfirst␈↓βr␈↓␈α
be␈α
a␈αLISP␈α
function␈α
which␈α
will␈αrepresent␈α
the␈α
sequence␈αoperation␈α
␈↓αfirst␈↓.␈↓π 31␈↓
␈↓ α←␈↓Then for example we might expect:
␈↓"∀␈↓ α←␈↓α␈↓ ∧%␈↓λr␈↓∞(␈↓α first[␈↓↓(A, B, C)␈↓α] ␈↓∞)␈↓α = first␈↓βr␈↓α[(A, B, C)] = A␈↓
␈↓"∀␈↓ α←␈↓The␈αproblem␈αis␈αthat␈αthis␈αline␈αis␈αnot␈αquite␈αright.␈α LISP␈αfunctions␈αexpect␈αtheir
␈↓ α←␈↓inputs␈α
to␈αbe␈α
S-expressions␈αbut␈α
␈↓α(A, B, C)␈↓␈α
is␈α␈↓¬not␈↓␈α
an␈αS-expression.␈α
To␈αbe␈α
correct
␈↓ α←␈↓we ␈↓¬should␈↓ have written:
␈↓"∀␈↓ α←␈↓␈↓ ¬
␈↓αfirst␈↓βr␈↓α[(A . (B . (C . NIL)))] = A␈↓β
␈↓"∀␈↓ α←␈↓It␈α⊃might␈α⊃be␈α∩argued␈α⊃that␈α⊃␈↓α(A,␈α∩B,␈α⊃C)␈↓␈α⊃is␈α∩just␈α⊃a␈α⊃convenient␈α∩abbreviation␈α⊃for
␈↓ α←␈↓␈↓α(A . (B . (C . NIL)))␈↓,␈α⊗but␈α⊗even␈α∃so,␈α⊗if␈α⊗we␈α⊗wish␈α∃the␈α⊗machine␈α⊗to␈α⊗use␈α∃the
␈↓ α←␈↓abbreviation␈α∪we␈α∩must␈α∪be␈α∩able␈α∪to␈α∩express␈α∪that␈α∩translation␈α∪scheme␈α∪to␈α∩the
␈↓ α←␈↓machine.␈α∀ We␈α∀must␈α∀therefore␈α∃examine␈α∀the␈α∀implications␈α∀of␈α∃the␈α∀notation.
␈↓ α←␈↓Clearly␈α⊂it␈α⊃is␈α⊂easier␈α⊃to␈α⊂read␈α⊂and␈α⊃write␈α⊂in␈α⊃list␈α⊂notation␈α⊂and,␈α⊃as␈α⊂long␈α⊃as␈α⊂we
␈↓ α←␈↓perform␈α∀only␈α∀list-operations␈α∃on␈α∀lists,␈α∀there␈α∀is␈α∃no␈α∀reason␈α∀to␈α∀look␈α∃at␈α∀the
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 30␈↓This␈α∂convention␈α∂is␈α∞one␈α∂of␈α∂the␈α∞few␈α∂instances␈α∂of␈α∞a␈α∂"good"␈α∂bug.␈α∞The
␈↓ α←␈↓early␈α
LISP␈α∞papers␈α
required␈α∞full␈α
use␈α
of␈α∞commas,␈α
but␈α∞due␈α
to␈α∞a␈α
programming
␈↓ α←␈↓error␈α∩in␈α∪the␈α∩LISP␈α∩output␈α∪routine,␈α∩lists␈α∩were␈α∪printed␈α∩without␈α∪commas.␈α∩It
␈↓ α←␈↓looked so much better that the bug became institutionalized.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 31␈↓Indeed,␈α
once␈α
the␈α
␈↓λr␈↓-mapping␈α
is␈α
defined␈α
on␈α
the␈α
␈↓¬domain␈↓␈α
it␈α
is␈α
induced
␈↓ α←␈↓on the ␈↓¬operations␈↓.
␈↓ α←␈↓␈↓1.7␈↓ εVLists: Representations of Sequences 35␈↓
␈↓"β␈↓ α←␈↓underlying␈α⊃dotted-pair␈α⊃representation.␈↓π 32␈↓␈α⊃However,␈α⊃we␈α⊃must␈α⊃keep␈α⊃in␈α⊃mind
␈↓ α←␈↓that␈α⊃list␈α⊃operations␈α⊃are␈α⊃carried␈α⊂out␈α⊃on␈α⊃the␈α⊃machine␈α⊃using␈α⊃the␈α⊂dotted-pair
␈↓ α←␈↓representation.␈α⊂␈↓¬We␈↓␈α⊂might␈α⊂carry␈α⊂out␈α⊂the␈α⊂"list-to-dotted-pair"␈α⊂transformations
␈↓ α←␈↓implicitly,␈α
but␈α
a␈α
machine␈α
which␈α
evaluates␈α
LISP␈α
expressions␈α
will␈α
have␈αto␈α
have
␈↓ α←␈↓an␈α≤explict␈α≤transformation␈α≤mechanism.␈α≤ So␈α≤a␈α≤necessary␈α≤part␈α≥of␈α≤our
␈↓ α←␈↓representation␈α⊂of␈α⊂sequences␈α⊂is␈α∂the␈α⊂specification␈α⊂of␈α⊂transformations␈α∂between
␈↓ α←␈↓the␈α∪abstract␈α∀data␈α∪structure␈α∀notation␈α∪and␈α∪the␈α∀notation␈α∪of␈α∀the␈α∪underlying
␈↓ α←␈↓representation.␈α
We␈α
can␈α
give␈α
representations␈α
for␈α
the␈α
sequence␈αoperations.␈α
We
␈↓ α←␈↓should␈α⊃continue␈α⊂to␈α⊃write␈α⊂the␈α⊃subscript␈α⊂␈↓βr␈↓␈α⊃on␈α⊂the␈α⊃LISP␈α⊂representation␈α⊃of␈α⊂a
␈↓ α←␈↓sequence␈αoperation,␈αlike␈α␈↓αseq␈↓␈α
being␈αrepresented␈αby␈α␈↓αseq␈↓βr␈↓.␈αIn␈α
most␈αcircumstances
␈↓ α←␈↓the␈α
distinction␈α
between␈α
abstraction␈α
and␈α
representation␈α
will␈α
be␈α
clear,␈α
so␈αwe␈α
will
␈↓ α←␈↓usually␈αomit␈αthe␈α
subscript.␈α The␈αconstruction␈αof␈α
a␈αsequence␈αfrom␈αan␈α
arbitrary
␈↓ α←␈↓number␈αof␈αelements␈αwill␈αbe␈αrepresented␈αby␈αa␈αLISP␈αfunction␈α␈↓αseq␈↓βr␈↓.␈αWe␈αwill␈αuse
␈↓ α←␈↓␈↓αlist␈↓ interchangeably with ␈↓αseq␈↓βr␈↓.
␈↓"∀␈↓ α←␈↓␈↓ ¬o␈↓λr␈↓∞(␈↓α seq ␈↓∞)␈↓α = list
␈↓"β␈↓ α←␈↓␈↓αlist[␈↓λα␈↓β1␈↓α;␈α␈↓λα␈↓β2␈↓α;␈α...␈α;␈↓λα␈↓βn␈↓α]␈↓␈αgenerates␈αa␈αlist␈αconsisting␈αof␈αthe␈α␈↓λα␈↓βi␈↓␈αarguments.␈αThat␈αis,␈αfor
␈↓ α←␈↓␈↓ β'n␈↓λ≥␈↓1, ␈↓αlist␈↓ is the appropriately nested composition of ␈↓αcons␈↓es:
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ β=␈↓αcons[␈↓λα␈↓β1␈↓α;cons[␈↓λα␈↓β2␈↓α; ... cons[␈↓λα␈↓βn␈↓α;NIL]] ...] ␈↓, and for n = 0, ␈↓αlist[ ] = ( )␈↓
␈↓""␈↓ α←␈↓Examples:␈↓ ∧C␈↓αlist[A;B] = (A B)
␈↓"⊃␈↓ α←␈↓α␈↓ ∧Clist[A;B;C] = (A B C)
␈↓"⊃␈↓ α←␈↓α␈↓ ∧Clist[A;list[B;C]] = list[A;(B C)] = (A (B C))
␈↓"⊃␈↓ α←␈↓α␈↓ ∧Clist[NIL] = (NIL)␈↓
␈↓"∀␈↓ α←␈↓Notice␈α∞that␈α
␈↓αlist␈↓␈α∞is␈α
␈↓¬not␈↓␈α∞strictly␈α
a␈α∞LISP␈α
function␈α∞as␈α
we␈α∞have␈α∞prescribed␈α
them;
␈↓ α←␈↓␈↓αlist␈↓␈α
does␈αevaluate␈α
its␈αarguments,␈α
but␈αit␈α
can␈αtake␈α
an␈αarbitrary␈α
number␈αof␈α
them.
␈↓ α←␈↓On␈α⊃page 17␈α⊃we␈α⊃required␈α⊂that␈α⊃LISP␈α⊃functions␈α⊃be␈α⊂of␈α⊃fixed␈α⊃arity.␈α⊃ For␈α⊂the
␈↓ α←␈↓moment,␈α∂␈↓αlist␈↓␈α∂is␈α∂simply␈α∂a␈α∂notational␈α∂abbreviation␈α∂for␈α∂nested␈α∂applications␈α∂of
␈↓ α←␈↓␈↓αcons␈↓.␈α
The␈α∞representation␈α
of␈α∞the␈α
selector␈α
functions␈α∞should␈α
be␈α∞apparent␈α
from
␈↓ α←␈↓the␈α⊂graphical␈α⊂representation.␈α⊂We␈α∂leave␈α⊂it␈α⊂as␈α⊂an␈α∂exercise␈α⊂for␈α⊂the␈α⊂reader␈α∂to
␈↓ α←␈↓specify␈α∂representations␈α∂for␈α∂these␈α∂functions;␈α∂however,␈α∂here␈α∂are␈α∂a␈α∂few␈α∂of␈α∂the
␈↓ α←␈↓other representations:
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 32␈↓Indeed,␈αa␈αstrong␈αcase␈αcan␈αbe␈αmade␈αfor␈α␈↓¬never␈↓␈αallowing␈αany␈αoperations
␈↓ α←␈↓on␈α
lists␈α
␈↓¬except␈↓␈α
list␈α
operations!␈αSee␈α
the␈α
discussion␈α
of␈α
type-faults␈α
on␈αpage 23␈α
and
␈↓ α←␈↓page 241.
␈↓ α←␈↓␈↓36 Symbolic expressions␈↓
(1.7␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓λr␈↓∞(␈↓α isindiv ␈↓∞)␈↓α = atom
␈↓" ␈↓ α←␈↓α␈↓ β'␈↓λr␈↓∞(␈↓α isseq ␈↓∞)␈↓α = isstrictlist␈↓ where:
␈↓"β␈↓ α←␈↓α␈↓ βcisstrictlist[x] <=␈↓ ¬G[atom[x] → [eq[x; NIL] → ␈↓
t␈↓α; ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ¬G islistelement[car[x]] → isstrictlist[cdr[x]];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ¬G ␈↓
t␈↓α → ␈↓
f␈↓α]
␈↓"∀␈↓ α←␈↓α␈↓where:␈↓α␈↓ ∧∂islistelement[x] <= [atom[x] → ␈↓
t␈↓α; ␈↓
t␈↓α → isstrictlist[x]]
␈↓"∀␈↓ α←␈↓The␈α∪predicate␈α∀␈↓αatom␈↓␈α∪does␈α∀not␈α∪quite␈α∪characterize␈α∀␈↓αisindiv␈↓.␈α∪ We␈α∀have␈α∪been
␈↓ α←␈↓assuming that:
␈↓"∀␈↓ α←␈↓␈↓ βk␈↓λr␈↓∞(␈↓αf[t␈↓β1␈↓α; ...; t␈↓βn␈↓α]␈↓∞)␈↓ = ␈↓λr␈↓∞(␈↓αf␈↓∞)␈↓α[ ␈↓λr␈↓∞(␈↓αt␈↓β1␈↓α␈↓∞)␈↓α; ␈↓λr␈↓∞(␈↓αt␈↓β2␈↓α␈↓∞)␈↓α; ...; ␈↓λr␈↓∞(␈↓αt␈↓βn␈↓α␈↓∞)␈↓α ]
␈↓"∀␈↓ α←␈↓α␈↓but ␈↓ ∧N␈↓λr␈↓∞(␈↓αisindiv[( )]␈↓∞)␈↓ ␈↓λ≠␈↓ ␈↓λr␈↓∞(␈↓αisindiv␈↓∞)␈↓α[␈↓λr␈↓∞(␈↓α( )␈↓∞)␈↓α]␈↓
␈↓"∀␈↓ α←␈↓Some␈α
descriptions␈α
of␈αLISP␈α
use␈α
this␈αstrict␈α
definition␈α
of␈αlists,␈α
so␈α
that␈αelements
␈↓ α←␈↓of␈α⊃a␈α⊃list␈α⊂are␈α⊃either␈α⊃atomic␈α⊃or␈α⊂are␈α⊃lists␈α⊃themselves.␈α⊂ In␈α⊃practice␈α⊃it␈α⊃is␈α⊂often
␈↓ α←␈↓convenient␈α
to␈α
allow␈α
elements␈α
of␈α
a␈α
list␈α
to␈α
be␈α
arbitrary␈α
S-expressions.␈α
This␈α
more
␈↓ α←␈↓liberal interpretation of lists is expressed by the following recognizer:
␈↓"∀␈↓ α←␈↓␈↓ βE␈↓αislist[x] <= [atom[x] →[eq[x;NIL] → ␈↓
t␈↓α; ␈↓
t␈↓α → ␈↓
f␈↓α]; ␈↓
t␈↓α → islist[cdr[x]] ]
␈↓"∀␈↓ α←␈↓Therefore␈α≥␈↓α(A, (A . B), C)␈↓␈α≡is␈α≥a␈α≡list␈α≥of␈α≡three␈α≥elements.␈α≡ But␈α≥beware:
␈↓ α←␈↓␈↓↓(A, (A . B), C)␈↓ is ␈↓¬not␈↓ a sequence, and neither is ␈↓α(A, (A . B), C)␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Since␈αlists␈αmay␈α
have␈αdotted␈αpairs␈α
as␈αelements,␈αit␈α
is␈αnatural␈αto␈αextend␈α
␈↓αlist␈↓
␈↓ α←␈↓to handle such cases:
␈↓"∀␈↓ α←␈↓α␈↓ ∧Vlist[cons[A;B];car[(A . B)]] = ((A . B) A)
␈↓"∀␈↓ α←␈↓␈↓ β'To␈α∂summarize␈α∂the␈α∂accomplishments␈α∂of␈α∞this␈α∂section,␈α∂we␈α∂have␈α∂in␈α∞effect
␈↓ α←␈↓added␈αa␈α␈↓¬new␈↓␈αdata␈αstructure␈αto␈αthe␈αrepertoire␈αof␈αLISP.␈α The␈α
addition␈αprocess
␈↓ α←␈↓includes:
␈↓"β␈↓ α←␈↓␈↓ β'␈↓↓1.␈α⊃ The␈α⊂abstract␈α⊃operations.␈↓␈α⊂We␈α⊃give␈α⊃constructors,␈α⊂selectors,
␈↓ α←␈↓␈↓ βWand␈αpredicates␈αfor␈αthe␈αrecognition␈αof␈αinstances␈αof␈αthe␈αdata
␈↓ α←␈↓␈↓ βWstructure.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓↓2.␈α⊂ The␈α∂underlying␈α⊂representation.␈↓␈α∂We␈α⊂must␈α∂show␈α⊂how␈α∂the
␈↓ α←␈↓␈↓ βWnew␈αdata␈αstructure␈αcan␈αbe␈αrepresented␈αin␈αterms␈αof␈αexisting
␈↓ α←␈↓␈↓ βWdata structures.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓↓3.␈α
Abstract␈α
operations␈α
as␈α
concrete␈α
operations.␈↓␈α
We␈α
must␈α
write
␈↓ α←␈↓␈↓ βWLISP␈α→functions␈α→which␈α_faithfully␈α→mirror␈α→the␈α_intended
␈↓ α←␈↓␈↓ βWmeaning␈α
of␈αthe␈α
abstract␈α
operations␈αwhen␈α
interpreted␈αin␈α
the
␈↓ α←␈↓␈↓ βWunderlying representation.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓↓4.␈α→ The␈α→input/output␈α→transformations.␈↓␈α→We␈α→should␈α_give
␈↓ α←␈↓␈↓ βWconventions␈α∪for␈α∀transforming␈α∪to␈α∀and␈α∪from␈α∀the␈α∪internal
␈↓ α←␈↓␈↓ βWrepresentation.
␈↓"∀␈↓ α←␈↓␈↓ β'There␈α↔is␈α↔another␈α↔view␈α_of␈α↔the␈α↔representability␈α↔of␈α_data␈α↔structures
␈↓ α←␈↓([Mor 74]).␈α∩We␈α∩use␈α∩␈↓↓transfer␈α∩functions␈↓␈α∩which␈α∩are␈α∩mappings␈α∩between␈α⊃the
␈↓ α←␈↓␈↓1.7␈↓ εVLists: Representations of Sequences 37␈↓
␈↓"β␈↓ α←␈↓abstract␈α
structure␈α
and␈α
its␈α∞representation.␈α
We␈α
need␈α
two␈α
transfer␈α∞functions;␈α
a
␈↓ α←␈↓write-function,␈α␈↓↓W␈↓,␈αto␈αmap␈αthe␈αrepresentations␈αinto␈αthe␈αabstract␈αobjects;␈αand␈αa
␈↓ α←␈↓read-function, ␈↓↓R␈↓, to map the abstract objects to their representations.
␈↓"β␈↓ α←␈↓␈↓ β'Consider␈α∂the␈α∂problem␈α∂of␈α∂representing␈α∂sequences.␈α∂We␈α∂want␈α∂␈↓↓R␈↓␈α⊂to␈α∂map
␈↓ α←␈↓from␈αelements␈α
of␈α␈↓<seq␈α
elem>␈↓␈αto␈α
␈↓<sexpr>␈↓␈α(see␈α
page 27␈αand␈α
page 5);␈αand␈α
we
␈↓ α←␈↓want␈α
␈↓↓W␈↓␈αto␈α
map␈αfrom␈α
␈↓<sexpr>␈↓␈αto␈α
␈↓<seq␈α
elem>␈↓.␈α Before␈α
we␈αgive␈α
such␈α␈↓↓R␈↓␈α
and
␈↓ α←␈↓␈↓↓W␈↓, let's see what they will do for us. We could define ␈↓αfirst␈↓βr␈↓ such that:
␈↓"∀␈↓ α←␈↓␈↓ ¬?␈↓αfirst␈↓βr␈↓α[x] = ␈↓↓W␈↓α[car[␈↓↓R␈↓α[x]]]
␈↓"∀␈↓ α←␈↓What␈α∂the␈α∂equation␈α∂says␈α∂is␈α∂that␈α∂given␈α∂a␈α∂sequence␈α∂␈↓αx␈↓,␈α∂we␈α∂can␈α∂map␈α∂it␈α∂to␈α∂the
␈↓ α←␈↓S-expression␈αrepresentation␈αusing␈α␈↓↓R␈↓;␈αthe␈αresult␈αof␈αthis␈αmap␈αis␈αan␈αS-expr␈αand
␈↓ α←␈↓therefore␈αsuitable␈αfare␈αfor␈α␈↓αcar␈↓;␈αthe␈αresult␈αof␈αthe␈α␈↓αcar␈↓␈αoperation␈αis␈αthen␈αmapped
␈↓ α←␈↓back␈α⊃into␈α∩the␈α⊃set␈α∩of␈α⊃sequence␈α⊃elements␈α∩by␈α⊃␈↓↓W␈↓.␈α∩ The␈α⊃other␈α∩operations␈α⊃for
␈↓ α←␈↓manipulating␈α∞sequences␈α∞can␈α∞be␈α∞described␈α∞similarly.␈α∞ With␈α∂this␈α∞introduction,
␈↓ α←␈↓here are appropriate transfer functions:
␈↓"∀␈↓ α←␈↓α␈↓↓W␈↓α[e] <=␈↓ βK[isnil[e] → mknull[];
␈↓"β␈↓ α←␈↓α␈↓ βK atom[e] → mkindiv[e];
␈↓"β␈↓ α←␈↓α␈↓ βK ␈↓
t␈↓α → concat[␈↓↓W␈↓α[car[e]];␈↓↓W␈↓α[cdr[e]]] ]
␈↓"β␈↓ α←␈↓α␈↓↓R␈↓α[l] <=␈↓ β?[null[l] → NIL;
␈↓"β␈↓ α←␈↓α␈↓ β? isindiv[l] → atomize[l];
␈↓"β␈↓ α←␈↓α␈↓ β? ␈↓
t␈↓α → cons[␈↓↓R␈↓α[first[l]];␈↓↓R␈↓α[rest[l]]] ]
␈↓"∀␈↓ α←␈↓We␈α⊂have␈α⊃seen␈α⊂all␈α⊃of␈α⊂the␈α⊃functions␈α⊂and␈α⊃predicates␈α⊂involved␈α⊃in␈α⊂␈↓↓R␈↓␈α⊃and␈α⊂␈↓↓W␈↓
␈↓ α←␈↓except␈α
␈↓αatomize␈↓,␈α
␈↓αmknull␈↓␈αand␈α
␈↓αmkindiv␈↓.␈α
In␈α
terms␈αof␈α
our␈α
current␈αrepresentation␈α
of
␈↓ α←␈↓sequences,␈αthese␈α
three␈αfunctions␈α
are␈αessentially␈α
the␈αidentity␈αfunction,␈α
␈↓αi[x] <= x␈↓.
␈↓ α←␈↓However␈α∞that␈α∞is␈α
true␈α∞␈↓¬only␈↓␈α∞because␈α∞of␈α
the␈α∞particular␈α∞representations␈α∞that␈α
we
␈↓ α←␈↓picked;␈α
the␈α
functions␈α
need␈α
not␈αbe␈α
so␈α
simple.␈α
A␈α
more␈α
careful␈αinspection␈α
would
␈↓ α←␈↓show␈α∂that␈α∂␈↓αmkindiv␈↓␈α∂expects␈α∂as␈α∂input␈α∂an␈α∂atomic␈α∂S-expression␈α∂and␈α⊂outputs␈α∂a
␈↓ α←␈↓sequence␈α⊃individual;␈α⊃␈↓αatomize␈↓␈α⊃acts␈α∩conversely.␈α⊃If␈α⊃the␈α⊃representations␈α∩of␈α⊃the
␈↓ α←␈↓atomic␈α∩S-expressions␈α∪were␈α∩different␈α∩from␈α∪the␈α∩representations␈α∪of␈α∩sequence
␈↓ α←␈↓individuals, then we would have some work to do.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αreview␈α
what␈αhas␈αtranspired␈α
since␈αit␈αis␈α
a␈αmodel␈αof␈α
what␈αis␈α
to␈αcome.
␈↓ α←␈↓We␈α∀developed␈α∀a␈α∀new␈α∀abstract␈α∀data␈α∀structure␈α∀called␈α∀sequences;␈α∪discussed
␈↓ α←␈↓notational␈α↔conventions␈α_for␈α↔writing␈α_sequences;␈α↔described␈α_operations␈α↔and
␈↓ α←␈↓pertinent␈αcontrol␈αstructures␈αfor␈αwriting␈αalgorithms;␈αand␈αfinally␈αshowed␈αthat␈αit
␈↓ α←␈↓was␈α∞possible␈α∞to␈α∂represent␈α∞sequences␈α∞in␈α∂the␈α∞previously␈α∞developed␈α∂domain␈α∞of
␈↓ α←␈↓S-exprs.␈α⊂If␈α⊃we␈α⊂had␈α⊃a␈α⊂machine␈α⊃which␈α⊂could␈α⊃execute␈α⊂S-expr␈α⊃algorithms␈α⊂we
␈↓ α←␈↓could␈α∂encapsulate␈α∂that␈α∂machine␈α∂within␈α∂the␈α∂␈↓λr␈↓-mapping␈α∂such␈α∂that␈α∂we␈α∂could
␈↓ α←␈↓write␈α∞in␈α
sequence-notation␈α∞and␈α
have␈α∞it␈α
translated␈α∞internally␈α
to␈α∞S-expr␈α
form;
␈↓ α←␈↓we␈α∂could␈α∂write␈α∂sequence-algorithms␈α⊂and␈α∂have␈α∂them␈α∂execute␈α⊂correctly␈α∂using
␈↓ α←␈↓the␈α⊗␈↓λr␈↓-maps␈α⊗of␈α⊗the␈α↔sequence␈α⊗primitives;␈α⊗and␈α⊗finally␈α⊗it␈α↔would␈α⊗produce
␈↓ α←␈↓sequence-output␈α⊂rather␈α⊂than␈α⊂the␈α⊂internal␈α⊂S-expr␈α⊂form.␈α⊂For␈α⊂all␈α⊃intents␈α⊂and
␈↓ α←␈↓purposes␈αour␈αaugmented␈α
LISP␈αmachine␈αunderstands␈αsequences.␈α
Indeed,␈αthis
␈↓ α←␈↓␈↓38 Symbolic expressions␈↓
(1.7␈↓
␈↓"β␈↓ α←␈↓is␈αthe␈αway␈αmost␈αLISP␈αimplementations␈αare␈αorganized;␈αinput␈αmay␈αeither␈αbe␈αin
␈↓ α←␈↓S-expr␈α∀form␈α∀or␈α∀list-notation;␈α∀internally␈α∀all␈α∀data␈α∀structures␈α∀are␈α∃stored␈α∀as
␈↓ α←␈↓S-exprs;␈α
all␈α
algorithms␈α
operate␈α
on␈αthe␈α
S-expr␈α
form;␈α
and␈α
finally,␈α
any␈αS-exprs
␈↓ α←␈↓which can be interpreted as lists are output in list-notation.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∪will␈α∪approach␈α∪the␈α∀other␈α∪abstract␈α∪data␈α∪structure␈α∪problems␈α∀in␈α∪a
␈↓ α←␈↓similar␈α∩manner,␈α⊃first␈α∩developing␈α⊃the␈α∩data␈α⊃structures␈α∩independent␈α∩of␈α⊃their
␈↓ α←␈↓representation,␈αand␈αlater␈αshowing␈αhow␈αto␈αrepresent␈αthis␈αnew␈αdomain␈αin␈αterms
␈↓ α←␈↓of␈α∀some␈α∀previously␈α∀understood␈α∀domain.␈α∪We␈α∀will␈α∀see␈α∀in␈α∀Section 9.4␈α∪that
␈↓ α←␈↓much␈α∩of␈α⊃the␈α∩mapping␈α⊃from␈α∩input␈α∩through␈α⊃output␈α∩can␈α⊃be␈α∩specified␈α∩in␈α⊃a
␈↓ α←␈↓natural␈α∂style␈α∞and␈α∂LISP␈α∂can␈α∞automatically␈α∂generate␈α∞the␈α∂necessary␈α∂input␈α∞and
␈↓ α←␈↓output programs.
␈↓"β␈↓ α←␈↓␈↓ ∧⎇␈↓↓Problems involving list-notation␈↓
␈↓"→␈↓ α←␈↓1. Discuss ␈↓ ¬V␈↓αcons[␈↓λα␈↓β1␈↓α;cons[␈↓λα␈↓β2␈↓α;␈↓λα␈↓β3␈↓α]]␈↓
␈↓"β␈↓ α←␈↓␈↓ ββas opposed to ␈↓ ¬ε␈↓αcons[␈↓λα␈↓β1␈↓α;cons[␈↓λα␈↓β2␈↓α; cons[␈↓λα␈↓β3␈↓α; NIL]]]␈↓
␈↓"β␈↓ α←␈↓␈↓ ββas a representation for ␈↓α(␈↓λα␈↓β1␈↓α, ␈↓λα␈↓β2␈↓α, ␈↓λα␈↓β3␈↓α)␈↓
␈↓"∀␈↓ α←␈↓2. Translate the following lists into S-expr dotted-pair notation.
␈↓"β␈↓ α←␈↓␈↓ ββ␈↓↓a.␈↓α (A B C)␈↓ ∧C␈↓↓b.␈↓α (A)␈↓ εβ␈↓↓c.␈↓α ((A))␈↓ πC␈↓↓d.␈↓α (A (B (C)))␈↓ β␈↓↓e.␈↓α (NIL)
␈↓" ␈↓ α←␈↓Now go the other way and translate the following S-exprs into list notation.
␈↓" ␈↓ α←␈↓␈↓ ββ␈↓↓f.␈↓α ((A . (B . NIL)) . ((C . NIL) . NIL))␈↓ π[␈↓↓g.␈↓α (NIL . NIL)
␈↓" ␈↓ α←␈↓α␈↓ ββ␈↓↓h.␈↓α (CONS . ((QUOTE . (A . NIL)) . NIL))
␈↓"∀␈↓ α←␈↓3. Evaluate the following:
␈↓" ␈↓ α←␈↓␈↓ ββ␈↓↓a.␈↓α first[(A B)]␈↓ ε3␈↓↓b.␈↓α rest[(A B)]
␈↓" ␈↓ α←␈↓α␈↓ ββ␈↓↓c.␈↓α concat[A;(B C)]␈↓ ε3␈↓↓d.␈↓α concat[A;NIL]
␈↓" ␈↓ α←␈↓α␈↓ ββ␈↓↓e.␈↓α concat[eq[A;A];(A B C)]␈↓ ε3␈↓↓f.␈↓α first[rest[(A B)]]
␈↓"β␈↓ α←␈↓␈↓ ¬z␈↓↓1.8 A Respite␈↓
␈↓"β␈↓ α←␈↓¬␈↓ β'"...I␈α∪think␈α∩that␈α∪one␈α∪of␈α∩the␈α∪chief␈α∪difficulties␈α∩is␈α∪that␈α∪the␈α∩general
␈↓ α←␈↓¬␈↓ β'standard␈α
of␈α
programming␈α
is␈α
extremely␈αlow.␈α
...I␈α
think␈α
that␈α
I␈αwould
␈↓ α←␈↓¬␈↓ β'like␈α∞to␈α∂suggest␈α∞again␈α∞that␈α∂the␈α∞general␈α∞standards␈α∂of␈α∞programming
␈↓ α←␈↓¬␈↓ β'and␈α
the␈α
way␈α
in␈α
which␈αpeople␈α
are␈α
taught␈α
to␈α
program␈α
is␈αabominable.
␈↓ α←␈↓¬␈↓ β'They␈α∂are␈α∂over␈α∂and␈α∂over␈α∂again␈α∂taught␈α∂to␈α∂make␈α∂puns;␈α∂to␈α⊂do␈α∂shifts
␈↓ α←␈↓¬␈↓ β'instead␈α⊃of␈α⊂multiplying␈α⊃when␈α⊃they␈α⊂mean␈α⊃multiplying;␈α⊃to␈α⊂multiply
␈↓ α←␈↓¬␈↓ β'when␈α∂they␈α∂mean␈α⊂shifts;␈α∂to␈α∂confuse␈α⊂bit␈α∂patterns␈α∂and␈α⊂numbers␈α∂and
␈↓ α←␈↓¬␈↓ β'generally␈αto␈αsay␈αone␈αthing␈αwhen␈αthey␈αactually␈αmean␈αsomething␈αquite
␈↓ α←␈↓¬␈↓ β'different.␈αNow␈αthis␈α
is␈αthe␈αstandard␈αway␈α
of␈αwriting␈αa␈α
program␈αand
␈↓ α←␈↓¬␈↓ β'they␈αtake␈αgreat␈αpleasure␈αin␈αdoing␈αso-"Isn't␈αit␈αwonderful?␈αIt␈αsaves␈αa
␈↓ α←␈↓¬␈↓ β'quarter␈α
of␈α
a␈α
microsecond␈α∞somewhere␈α
every␈α
month".␈α
Now␈α
I␈α∞think␈α
we
␈↓ α←␈↓␈↓1.8␈↓ →A Respite 39␈↓
␈↓"β␈↓ α←␈↓¬␈↓ β'will␈α
not␈α
get␈α
a␈α
proper␈αstandard␈α
of␈α
programming␈α
...␈α
until␈α
we␈αcan␈α
have
␈↓ α←␈↓¬␈↓ β'some␈α∂proper␈α∂professional␈α∂standards␈α∂about␈α∂how␈α∂to␈α∂write␈α∞programs;
␈↓ α←␈↓¬␈↓ β'and␈α∞this␈α∞has␈α∞to␈α
be␈α∞done␈α∞by␈α∞teaching␈α
people␈α∞right␈α∞at␈α∞the␈α
beginning
␈↓ α←␈↓¬␈↓ β'how to write programs properly ..."
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ ∧=C. Strachey, ␈↓αConference on Software Engineering, 1968␈↓
␈↓"λ␈↓ α←␈↓This␈αsection␈α
summarizes␈αand␈αreflects␈α
on␈αthe␈αmaterial␈α
of␈αthis␈αchapter.␈α
First␈αa
␈↓ α←␈↓reiteration␈αof␈αa␈αprevious␈αadmonition:␈αthough␈αmost␈αof␈αthis␈αmaterial␈αmay␈αseem
␈↓ α←␈↓quite␈α
straightforward,␈α
the␈α
next␈α
chapter␈αwill␈α
begin␈α
to␈α
show␈α
you␈α
that␈αthings␈α
are
␈↓ α←␈↓not␈αall␈αthat␈αtrivial.␈α LISP␈αis␈αquite␈αpowerful.␈α The␈αpreceding␈αmaterial␈α␈↓¬is␈↓␈αbasic
␈↓ α←␈↓and the sooner it becomes second nature to you the better.
␈↓"β␈↓ α←␈↓␈↓ β'A␈αsecond␈αadmonition:␈αbesides␈αlearning␈αabout␈αthe␈αbasic␈αconstructs␈αof␈α
the
␈↓ α←␈↓language,␈αthe␈α
previous␈αmaterial␈αshould␈α
begin␈αto␈αconvince␈α
you␈αof␈αthe␈α
necessity
␈↓ α←␈↓for␈α⊂precise␈α∂specification␈α⊂of␈α∂programming␈α⊂languages.␈α∂In␈α⊂particular␈α⊂we␈α∂have
␈↓ α←␈↓seen␈α
that␈α
the␈α
process␈α
of␈α
evaluation␈α
of␈α
expressions␈α
must␈α
be␈α
spelled␈α
out␈αquite
␈↓ α←␈↓carefully.␈α∞Different␈α∂evaluation␈α∞schemes␈α∞lead␈α∂to␈α∞quite␈α∞different␈α∂effects.␈α∞Since
␈↓ α←␈↓evaluation␈α∂␈↓¬is␈↓␈α∂the␈α∞business␈α∂of␈α∂programming␈α∂languages␈α∞we␈α∂should␈α∂do␈α∂all␈α∞we
␈↓ α←␈↓can to make a precise specification.
␈↓"β␈↓ α←␈↓␈↓ β'And␈α∞a␈α
final␈α∞warning:␈α∞a␈α
major␈α∞point␈α∞of␈α
this␈α∞whole␈α∞book␈α
is␈α∞to␈α∞instill␈α
a
␈↓ α←␈↓respect␈α
for␈α∞abstraction␈α
as␈α∞a␈α
tool␈α
for␈α∞controlling␈α
complexity␈α∞in␈α
programming,
␈↓ α←␈↓and␈α⊃as␈α⊂a␈α⊃means␈α⊃of␈α⊂writing␈α⊃implementation␈α⊂independent␈α⊃programs.␈α⊃As␈α⊂we
␈↓ α←␈↓begin␈αwriting␈αmore␈αcomplex␈αalgorithms,␈α
the␈αpower␈αof␈αabstraction␈αwill␈α
become
␈↓ α←␈↓more␈αapparent,␈αbut␈α
the␈αlessons␈αwe␈α
learned␈αin␈αrepresenting␈α
sequences␈αcontain
␈↓ α←␈↓the essential ideas of abstraction and representation.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∞have␈α
now␈α∞seen␈α
two␈α∞examples␈α
of␈α∞abstract␈α
data␈α∞structures.␈α∞First,␈α
we
␈↓ α←␈↓studied␈α∩S-expressions␈α∩without␈α⊃any␈α∩consideration␈α∩for␈α∩their␈α⊃implementation;
␈↓ α←␈↓they␈αwere␈α
abstract␈αobjects␈α
of␈αsufficient␈αinterest␈α
in␈αtheir␈α
own␈αright.␈α
We␈αthen
␈↓ α←␈↓introduced␈α
the␈α
operations␈α
on␈α
the␈α
data␈αstructures:␈α
␈↓αcar,␈α
cdr,␈α
cons,␈α
eq␈↓␈α
and␈α␈↓αatom␈↓.
␈↓ α←␈↓Finally␈α⊃the␈α⊃␈↓↓control␈α∩structures␈↓,␈α⊃conditional␈α⊃expression␈α⊃and␈α∩recursion,␈α⊃were
␈↓ α←␈↓given.␈α∞Control␈α∞structures␈α∂are␈α∞used␈α∞to␈α∂direct␈α∞the␈α∞flow␈α∂of␈α∞the␈α∞algorithm␈α∂as␈α∞it
␈↓ α←␈↓executes.␈α∩ These␈α∩three␈α∩components,␈α∪data,␈α∩operations,␈α∩and␈α∩control,␈α∪are␈α∩the
␈↓ α←␈↓main␈α∂ingredients␈α⊂of␈α∂any␈α∂programming␈α⊂language.␈α∂ Most␈α∂languages␈α⊂have␈α∂an
␈↓ α←␈↓apparently␈α
richer␈αclass␈α
of␈αcontrol␈α
devices;␈α"while"-statements␈α
and␈α"DO"-loops
␈↓ α←␈↓are␈α
examples.␈αLater␈α
we␈α
will␈αshow␈α
how␈α
to␈αintroduce␈α
such␈α
constructs␈αinto␈α
LISP.
␈↓ α←␈↓Most␈α∞control␈α∂structures␈α∞are␈α∂explicit␈α∞language␈α∂constructs␈α∞like␈α∂the␈α∞conditional
␈↓ α←␈↓expression,␈αwhereas␈αrecursion␈αis␈αtypically␈αimplicit.␈↓π 33␈↓␈αThe␈αinteraction␈αbetween
␈↓ α←␈↓recursion␈α⊗and␈α⊗the␈α⊗procedure-calling␈α⊗mechanism␈α⊗gives␈α⊗LISP␈α⊗a␈α∃powerful
␈↓ α←␈↓control structure.
␈↓"β␈↓ α←␈↓␈↓ β'As␈α_we␈α↔introduce␈α_each␈α↔new␈α_abstract␈α↔data␈α_structure␈α↔we␈α_add␈α↔new
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 33␈↓However␈αsome␈αlanguages␈αdo␈αrequire␈αsome␈αkind␈αof␈αdeclaration␈αto␈αthe
␈↓ α←␈↓effect that a procedure is recursive.
␈↓ α←␈↓␈↓40 Symbolic expressions␈↓
(1.8␈↓
␈↓"β␈↓ α←␈↓operations␈α∪tailored␈α∪to␈α∪its␈α∪needs.␈α∪When␈α∪we␈α∪introduced␈α∪sequences␈α∪we␈α∩also
␈↓ α←␈↓introduced␈α␈↓αfirst,␈αrest,␈α
null, ...␈↓,etc.␈α We␈αdid␈αnot␈α
add␈αany␈αnew␈α
control␈αstructure,
␈↓ α←␈↓though␈α⊃a␈α⊂simpler␈α⊃control␈α⊂structure␈α⊃which␈α⊂operated␈α⊃on␈α⊃sequences,␈α⊂selecting
␈↓ α←␈↓elements␈α∪and␈α∪performing␈α∪operations␈α∀on␈α∪those␈α∪elements,␈α∪might␈α∀be␈α∪useful.
␈↓ α←␈↓There␈αis␈αa␈αnatural␈α
relationship␈αbetween␈αdata␈αstructure␈αand␈α
control␈αstructure;
␈↓ α←␈↓sometimes␈α∂we␈α⊂can␈α∂exploit␈α∂it␈α⊂to␈α∂good␈α∂measure.␈α⊂ When␈α∂we␈α⊂consider␈α∂abstract
␈↓ α←␈↓data␈α∂structures␈α∂in␈α⊂future␈α∂chapters␈α∂we␈α∂will␈α⊂again␈α∂see␈α∂the␈α⊂three␈α∂components:
␈↓ α←␈↓data, operations, and control.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
new␈α
feature␈αwhich␈α
we␈α
considered␈αin␈α
discussing␈α
sequences␈α
was␈αthe
␈↓ α←␈↓problem␈α∞of␈α
representation.␈α∞We␈α
showed␈α∞how␈α
to␈α∞represent␈α
sequences␈α∞in␈α
terms
␈↓ α←␈↓of␈α
S-expressions.␈αWe␈α
will␈αcontinue␈α
this␈αpyramiding␈α
of␈αdata␈α
structures␈α
in␈αthe
␈↓ α←␈↓future;␈α
we␈αwill␈α
consider␈αour␈α
work␈αdone␈α
as␈αsoon␈α
as␈αwe␈α
have␈α
a␈αrepresentation
␈↓ α←␈↓of␈αour␈αnew␈αdata␈αstructure␈αin␈αterms␈αof␈αan␈αexisting␈αone.␈αFinally␈αwe␈αwill␈αexhibit
␈↓ α←␈↓a␈α∪representation␈α∪of␈α∪the␈α∪underlying␈α∪layer␈α∪of␈α∪S-expressions.␈α∪Later␈α∀we␈α∪will
␈↓ α←␈↓discuss␈α∀different␈α∀representations␈α∀of␈α∪data␈α∀structures,␈α∀independent␈α∀of␈α∪their
␈↓ α←␈↓possible␈α
S-expression␈α
representation;␈α
there␈αare␈α
data␈α
structures␈α
which␈α
are␈αnot
␈↓ α←␈↓best␈α∞represented␈α∞as␈α∞S-expressions.␈α∞ A␈α∞further␈α∞consideration␈α∞appears␈α∞because
␈↓ α←␈↓of␈α⊂the␈α∂representation␈α⊂issue;␈α⊂even␈α∂though␈α⊂we␈α∂have␈α⊂represented␈α⊂a␈α∂particular
␈↓ α←␈↓data␈α∩structure␈α⊃as␈α∩a␈α∩complex␈α⊃S-expression␈α∩we␈α∩should␈α⊃not␈α∩operate␈α∩on␈α⊃that
␈↓ α←␈↓representation␈αwith␈αS-expression␈α
functions.␈αWe␈αshould␈α
refrain␈αfrom␈αusing␈α
␈↓αcar␈↓
␈↓ α←␈↓and␈α∪␈↓αcdr␈↓␈α∀on␈α∪lists␈α∪even␈α∀though␈α∪the␈α∪representation␈α∀is␈α∪well-known.␈α∀ In␈α∪our
␈↓ α←␈↓representation␈αof␈αlists␈α
we␈αcould␈αfind␈αthe␈α
n␈↓πth␈↓␈αelement␈αin␈α
a␈αlist␈αby␈αusing␈α
␈↓αcad␈↓πn-1␈↓αr␈↓.
␈↓ α←␈↓And␈α↔we␈α↔know␈α↔that␈α↔␈↓αcdr␈↓␈α↔represents␈α⊗the␈α↔␈↓αrest␈↓␈α↔of␈α↔the␈α↔list.␈α↔ Though␈α⊗our
␈↓ α←␈↓representation␈αof␈αsequences␈αis␈αsuch␈αthat␈α␈↓αfirst␈↓,␈α␈↓αrest␈↓␈αand␈α␈↓αconcat␈↓␈αare␈αidentical␈αto
␈↓ α←␈↓␈↓αcar␈↓,␈α␈↓αcdr␈↓,␈αand␈α␈↓αcons␈↓␈αrespectively,␈αwe␈αshould␈αuse␈αthe␈αnames␈α␈↓αfirst␈↓,␈α␈↓αrest␈↓,␈αand␈α␈↓αconcat␈↓
␈↓ α←␈↓to␈α*make␈α*it␈α)clear␈α*that␈α*we␈α)are␈α*operating␈α*on␈α*lists.␈α) These
␈↓ α←␈↓representation-dependent␈αcoding␈αtricks␈↓π 34␈↓␈αare␈αdangerous.␈αThey␈αare␈αreally␈α
type
␈↓ α←␈↓faults as discussed on page 23 and page 241.
␈↓"β␈↓ α←␈↓␈↓ β'For␈α_a␈α→more␈α_practical␈α→benefit,␈α_consider␈α→the␈α_problem␈α→of␈α_program
␈↓ α←␈↓modification.␈α⊗ We␈α⊗might␈α⊗wish␈α⊗to␈α⊗change␈α⊗the␈α⊗representation␈α⊗of␈α⊗a␈α∃data
␈↓ α←␈↓structure.␈αIf␈αthe␈αprogramming␈αhas␈αbeen␈αdone␈αin␈αterms␈αof␈α
abstract␈αoperations
␈↓ α←␈↓on␈α∃abstract␈α⊗data␈α∃structures␈α⊗then␈α∃only␈α∃those␈α⊗functions␈α∃which␈α⊗relate␈α∃the
␈↓ α←␈↓abstraction␈α∪to␈α∀the␈α∪representation␈α∀need␈α∪be␈α∀changed.␈α∪If␈α∀we␈α∪had␈α∀used␈α∪the
␈↓ α←␈↓representation␈αthroughout␈αthe␈αprogram,␈α
then␈αevery␈αuse␈αof␈α
the␈αrepresentation
␈↓ α←␈↓must␈α∪be␈α∪changed.␈α∪ While␈α∪we␈α∪are␈α∪discussing␈α∪some␈α∪of␈α∪the␈α∀more␈α∪practical
␈↓ α←␈↓implications␈α
of␈α
our␈αwork␈α
we␈α
should␈α
discuss␈αhow␈α
␈↓λB␈↓␈α
should␈α
be␈αunderstood.␈α
As
␈↓ α←␈↓things␈α∩currently␈α∩stand,␈α∪the␈α∩appearance␈α∩of␈α∩␈↓λB␈↓␈α∪in␈α∩any␈α∩application␈α∪of␈α∩strict
␈↓ α←␈↓functions␈α⊃will␈α⊃immediately␈α⊃cause␈α⊃the␈α⊃termination␈α⊃of␈α⊃the␈α⊃computation.␈α⊂ No
␈↓ α←␈↓information␈α⊃other␈α⊃than␈α⊃the␈α⊃fact␈α⊃that␈α⊂␈↓λB␈↓␈α⊃did␈α⊃appear␈α⊃results␈α⊃from␈α⊃such␈α⊂an
␈↓ α←␈↓occurrence.␈αIf␈αwe␈αthought␈α
of␈αthe␈αevaluation␈αof␈α
␈↓λB␈↓␈αas␈αresulting␈αin␈α
a␈αdivergent
␈↓ α←␈↓computation,␈αthen␈α
no␈αinformation␈αat␈α
all␈αwould␈αbe␈α
forthcoming.␈α In␈α
reality,␈αa
␈↓ α←␈↓LISP␈αimplementation␈α
can␈αhandle␈αmany␈α
computations␈αwhich␈αinvolve␈α
␈↓λB␈↓.␈αThe
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 34␈↓called "puns" by C. Strachey
␈↓ α←␈↓␈↓1.8␈↓ →A Respite 41␈↓
␈↓"β␈↓ α←␈↓computation␈α⊃might␈α⊃be␈α⊃terminated␈α⊃and␈α⊂an␈α⊃error␈α⊃printed;␈α⊃in␈α⊃an␈α⊂interactive
␈↓ α←␈↓implementation,␈α
the␈αuser␈α
might␈αbe␈α
given␈αan␈α
opportunity␈αto␈α
correct␈α
the␈αerror
␈↓ α←␈↓and␈α⊃have␈α∩the␈α⊃computation␈α∩continue;␈α⊃and␈α∩alas,␈α⊃some␈α∩implementations␈α⊃just
␈↓ α←␈↓continue␈αcomputation␈α
with␈αsome␈αarbitrary␈α
piece␈αof␈αinformation␈α
produced␈αby
␈↓ α←␈↓an␈αexcursion␈αinto␈αthe␈αsubsconscious␈αof␈αLISP.␈αDivergent␈αcomputations␈αcannot
␈↓ α←␈↓be␈α∩detected␈α∩in␈α∩such␈α∩a␈α∩clear␈α∩manner␈α∩and␈α∩implementations␈α∩differ␈α∪in␈α∩their
␈↓ α←␈↓handling␈α∞of␈α∞this␈α∞interpretation␈α∞of␈α∞␈↓λB␈↓.␈α∞ We␈α∞will␈α∞have␈α∞more␈α∞to␈α∞say␈α∂about␈α∞the
␈↓ α←␈↓implementation of ␈↓λB␈↓ in Section 6.23.
␈↓"β␈↓ α←␈↓␈↓ β'Later,␈α∞we␈α∞will␈α∞motivate␈α∞the␈α∞more␈α∞traditional␈α∞studies␈α∞of␈α∞data␈α
structures
␈↓ α←␈↓by␈αconsidering␈αthe␈αimplementations␈αof␈αLISP-related␈αlanguages.␈α But␈αthe␈αpath
␈↓ α←␈↓to␈α
those␈α
studies␈αis␈α
at␈α
least␈αas␈α
important.␈α
On␈αthe␈α
way␈α
we␈αwill␈α
show␈α
that␈αwe␈α
can
␈↓ α←␈↓exploit␈α
abstraction␈α
as␈α
a␈α
means␈α
for␈α
giving␈α
a␈α
clear␈α
specification␈α
of␈α
evaluation
␈↓ α←␈↓of␈α⊃LISP␈α⊂expressions,␈α⊃and␈α⊂the␈α⊃representational␈α⊂techniques␈α⊃we␈α⊂will␈α⊃use␈α⊂will
␈↓ α←␈↓involve␈α∪applications␈α∩of␈α∪abstract␈α∩data␈α∪structures.␈α∩A␈α∪more␈α∪tangible␈α∩benefit
␈↓ α←␈↓should␈α_be␈α→an␈α_increased␈α→awareness␈α_of␈α_the␈α→structure␈α_and␈α→behavior␈α_of
␈↓ α←␈↓programming␈α∨languages,␈α∨and␈α∨the␈α≡beginnings␈α∨of␈α∨a␈α∨better␈α∨style␈α≡of
␈↓ α←␈↓programming.
␈↓"β␈↓ α←␈↓␈↓ β'Another␈α⊂part␈α⊂of␈α⊃our␈α⊂investigation␈α⊂should␈α⊃be␈α⊂to␈α⊂answer␈α⊃the␈α⊂question
␈↓ α←␈↓"What␈α
is␈α
a␈αdata␈α
structure?".␈α
As␈α
we␈αmentioned␈α
at␈α
the␈α
beginning␈αof␈α
Section 1.6
␈↓ α←␈↓there␈αis␈α
a␈αdifferent␈αcharacterization␈α
of␈αsequences␈αwhich␈α
will␈αgive␈α
a␈αdifferent
␈↓ α←␈↓interpretation␈α
of␈α
data␈α
structures.␈α
The␈α
standard␈α
mathematical␈α
definition␈α
of␈αa
␈↓ α←␈↓sequence is as a function from the integers to a particular domain.
␈↓"β␈↓ α←␈↓␈↓ β'Thus a finite sequence ␈↓s␈↓ might be given as:
␈↓"∀␈↓ α←␈↓␈↓ ∧t␈↓s␈↓ = ␈↓{<1, s␈↓β1␈↓>, <2, s␈↓β2␈↓>, ...<n, s␈↓βn␈↓>}
␈↓"∀␈↓ α←␈↓To␈α∂select␈α∂components␈α∂of␈α⊂␈↓s␈↓,␈α∂we␈α∂use␈α∂ordinary␈α∂function␈α⊂application:␈α∂␈↓s(i)␈↓ = ␈↓s␈↓βi␈↓.
␈↓ α←␈↓Indeed,␈α
if␈α
you␈α
have␈α
programmed␈α
in␈α
a␈α
language␈α
which␈α
has␈α∞array␈α
constructs,
␈↓ α←␈↓you␈α
will␈α
recognize␈α
"application"␈α
as␈α
the␈α
style␈α
of␈α
notation␈α
used:␈α
A[3] selected␈α
the
␈↓ α←␈↓third component for the array A.
␈↓"β␈↓ α←␈↓␈↓ β'However␈α⊂this␈α⊂is␈α⊂quite␈α⊂different␈α⊂from␈α∂what␈α⊂we␈α⊂did␈α⊂in␈α⊂the␈α⊂section␈α∂on
␈↓ α←␈↓sequences.␈α∪ For␈α∪example,␈α∪if␈α∪␈↓↓(A, B, C)␈↓␈α∪is␈α∪a␈α∪sequence,␈α∪␈↓s␈↓,␈α∪then␈α∪in␈α∀the␈α∪new
␈↓ α←␈↓interpretation we should write:
␈↓"∀␈↓ α←␈↓␈↓ ¬⊂␈↓s␈↓ = ␈↓{<1, ␈↓↓A␈↓>, <2, ␈↓↓B␈↓>,<3, ␈↓↓C␈↓>}
␈↓"∀␈↓ α←␈↓Thus␈α∀␈↓s(2)␈↓␈α∀is␈α∪␈↓↓B␈↓,␈α∀etc.␈α∀What␈α∪has␈α∀happened␈α∀is␈α∪that␈α∀what␈α∀was␈α∪previously
␈↓ α←␈↓considered␈α⊂to␈α∂be␈α⊂a␈α∂data␈α⊂structure␈α⊂has␈α∂become␈α⊂a␈α∂function,␈α⊂and␈α⊂the␈α∂selector
␈↓ α←␈↓functions␈α∀on␈α∪the␈α∀data␈α∪structure␈α∀have␈α∪now␈α∀become␈α∪static␈α∀indices␈α∀on␈α∪the
␈↓ α←␈↓function. Or to make things more transparent:
␈↓"∀␈↓ α←␈↓␈↓ ∧K␈↓s␈↓ = ␈↓{<␈↓αfirst␈↓, ␈↓↓A␈↓>, <␈↓αsecond␈↓, ␈↓↓B␈↓>,<␈↓αthird␈↓, ␈↓↓C␈↓>}
␈↓"∀␈↓ α←␈↓Then␈αwe␈αwould␈αwrite␈α␈↓s(␈↓αfirst␈↓)␈↓␈αrather␈αthan␈α␈↓αfirst␈↓(s)␈↓.␈↓π 35␈↓␈αThis␈αidea␈αcan␈αeasily␈αbe
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 35␈↓The␈α∞language␈α∞PPL␈α∞(Polymorphic␈α∞Programming␈α∞Language)␈α∂lets␈α∞you
␈↓ α←␈↓do this: ␈↓αcar[s]␈↓ and ␈↓αs[car]␈↓ both work.
␈↓ α←␈↓␈↓42 Symbolic expressions␈↓
(1.8␈↓
␈↓"β␈↓ α←␈↓applied␈α≠to␈α~S-exprs␈α≠and␈α≠their␈α~functions.␈α≠In␈α~graphical␈α≠terms␈α≠we␈α~are
␈↓ α←␈↓representing␈α
the␈α
structures␈α
such␈α
that␈α
the␈α
arcs␈α
of␈α
the␈α
graph␈α
are␈α∞labeled␈α
with
␈↓ α←␈↓the␈α∂selector␈α∂indices.␈α∞With␈α∂L-trees␈α∂the␈α∞labeling␈α∂was␈α∂implicit:␈α∂left-branch␈α∞was
␈↓ α←␈↓␈↓αcar␈↓;␈α⊂right-branch␈α⊂was␈α⊂␈↓αcdr␈↓.␈α⊂ With␈α⊂explicit␈α⊂labels␈α⊂on␈α⊂the␈α⊂branches,␈α⊂the␈α∂trees
␈↓ α←␈↓need␈α⊃not␈α⊃be␈α⊃ordered.␈α⊃ Several␈α⊃languages␈α⊃implement␈α⊃such␈α⊃unordered␈α⊂trees;
␈↓ α←␈↓they␈α
are␈αcalled␈α
␈↓αstructure␈↓s␈αin␈α
Algol 68␈αand␈α
EL1,␈αand␈α
called␈α␈↓αrecord␈↓s␈α
in␈αPascal.
␈↓ α←␈↓Several␈α∩formalisms␈α⊃exploit␈α∩this␈α⊃view␈α∩of␈α⊃data␈α∩structures;␈α⊃in␈α∩particular␈α⊃the
␈↓ α←␈↓Vienna␈α∪Definition␈α∩Language ([Weg 72]),␈α∪which␈α∩is␈α∪a␈α∩direct␈α∪descendant␈α∩of
␈↓ α←␈↓LISP, represents its data in such a manner.
␈↓"β␈↓ α←␈↓␈↓ β'What␈α
then␈α∞is␈α
a␈α∞data␈α
structure?␈α∞It␈α
depends␈α
on␈α∞how␈α
you␈α∞look␈α
at␈α∞it.␈α
For
␈↓ α←␈↓our␈α∂immediate␈α∞purposes␈α∂we␈α∞will␈α∂try␈α∞to␈α∂remain␈α∞intuitive␈α∂and␈α∂informal.␈α∞ We
␈↓ α←␈↓will␈α
try␈α
to␈α
characterize␈α
an␈α
abstract␈α
data␈α
structure␈α
as␈α
a␈α
domain␈α
and␈α
a␈α
collection
␈↓ α←␈↓of␈α∂associated␈α∂operations␈α∞and␈α∂control␈α∂structures.␈α∞The␈α∂operations␈α∂and␈α∞control
␈↓ α←␈↓mechanisms␈αshould␈αallow␈αus␈αto␈α
describe␈αalgorithms␈αin␈αa␈αnatural␈α
manner␈αbut
␈↓ α←␈↓should, if at all possible, remain representation independent.
␈↓"β␈↓ α←␈↓␈↓ β'A␈αfew␈αtricks␈αwere␈αembedded␈αin␈αthe␈αproblem␈αsets.␈α Recall␈αproblem␈α␈↓↓h␈↓␈αon
␈↓ α←␈↓page 25.␈α∞The␈α∂composition␈α∞␈↓αatom[cons[␈α∞...]]␈↓␈α∂will␈α∞always␈α∞evaluate␈α∂to␈α∞␈↓
f␈↓␈α∂␈↓π 36␈↓␈α∞since
␈↓ α←␈↓the␈α
result␈α
of␈α∞␈↓αcons␈↓␈α
is␈α
always␈α∞non-atomic.␈α
In␈α
␈↓↓j␈↓,␈α
we␈α∞used␈α
atoms␈α
with␈α∞the␈α
same
␈↓ α←␈↓letter␈α⊃strings␈α⊃as␈α⊃predicate␈α⊃names,␈α⊂␈↓αATOM␈↓␈α⊃and␈α⊃␈↓αEQ␈↓.␈α⊃ ␈↓αATOM␈↓␈α⊃and␈α⊃␈↓αEQ␈↓␈α⊂are
␈↓ α←␈↓perfectly␈α∞good␈α
atoms,␈α∞and␈α
are␈α∞␈↓¬not␈↓␈α∞to␈α
be␈α∞confused␈α
with␈α∞the␈α∞LISP␈α
predicates.
␈↓ α←␈↓Problem ␈↓↓p␈↓␈α≠shows␈α≠that␈α≠conditional␈α≠expressions␈α≠may␈α≠appear␈α≠within␈α~a
␈↓ α←␈↓functional composition.
␈↓"β␈↓ α←␈↓␈↓ β'Notice␈α∀that␈α∀␈↓αtwist␈↓␈α∪in␈α∀problem␈α∀2␈α∀is␈α∪total␈α∀whereas␈α∀␈↓αfindem␈↓␈α∀is␈α∪partial.
␈↓ α←␈↓␈↓αfindem␈↓␈α⊂is␈α⊂partial␈α⊂since␈α⊂␈↓αy␈↓␈α⊃must␈α⊂be␈α⊂atomic.␈α⊂Both␈α⊂functions␈α⊂build␈α⊃new␈α⊂trees:
␈↓ α←␈↓␈↓αtwistem␈↓␈α⊃reverses␈α⊃left-␈α⊃and␈α⊃right-branches␈α⊃recursively;␈α⊃␈↓αfindem␈↓␈α⊃builds␈α⊃a␈α⊂tree
␈↓ α←␈↓with␈αthe␈αsame␈αbranching␈αstructure␈αas␈α␈↓αx␈↓,␈αbut␈αthe␈αterminal␈αnodes␈αcontain␈α␈↓αT␈↓␈αat
␈↓ α←␈↓the points where the atom ␈↓αy␈↓ appears in the original tree, and ␈↓αNIL␈↓ otherwise.
␈↓"β␈↓ α←␈↓␈↓ β'Be␈αclear␈αon␈αthe␈αdifference␈αbetween␈αthe␈αrepresentation␈αof␈αthe␈αempty␈αlist:
␈↓ α←␈↓␈↓αNIL␈↓,␈α↔and␈α↔the␈α↔list␈α↔consisting␈α↔of␈α↔␈↓αNIL␈↓:␈α↔␈↓α(NIL)␈↓;␈α↔note␈α↔that␈α↔␈↓α(NIL)␈↓␈α↔is␈α↔an
␈↓ α←␈↓abbreviation␈α
for␈α
␈↓α(NIL␈α∞.␈α
NIL)␈↓,␈α
which␈α
certainly␈α∞is␈α
not␈α
␈↓αNIL␈↓.␈α∞ List-notation␈α
is
␈↓ α←␈↓an␈α∂abbreviation␈α∂and␈α∂can␈α∂always␈α∂be␈α∞translated␈α∂back␈α∂into␈α∂a␈α∂S-expr,␈α∂but␈α∞not
␈↓ α←␈↓every S-expr is the representation of a list.
␈↓"β␈↓ α←␈↓␈↓ β'The distinction between ␈↓αconcat␈↓ and ␈↓αlist␈↓ is sometimes confusing:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ∧H␈↓αconcat[␈↓λα␈↓β1␈↓α; (␈↓λα␈↓β2␈↓α, ...␈↓λα␈↓βn␈↓α)]␈↓ is ␈↓α(␈↓λα␈↓β1␈↓α, ␈↓λα␈↓β2␈↓α, ... ␈↓λα␈↓βn␈↓α)
␈↓"∀␈↓ α←␈↓α␈↓ β'␈↓ ∧Ulist[␈↓λα␈↓β1␈↓α;(␈↓λα␈↓β2␈↓α, ... ␈↓λα␈↓βn␈↓α)]␈↓ is ␈↓α(␈↓λα␈↓β1␈↓α (␈↓λα␈↓β2␈↓α ... ␈↓λα␈↓βn␈↓α))␈↓
␈↓"∀␈↓ α←␈↓So␈α␈↓αconcat␈↓␈αwill␈αadd␈αa␈αnew␈αelement␈αto␈αthe␈αfront␈αof␈αan␈αexisting␈αlist,␈αwhereas␈α␈↓αlist␈↓
␈↓ α←␈↓will␈α
create␈α
a␈α
new␈αlist␈α
whose␈α
elements␈α
will␈α
be␈αthe␈α
values␈α
of␈α
the␈α
arguments␈αto
␈↓ α←␈↓␈↓αlist␈↓.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 36␈↓If␈α
it␈α∞has␈α
a␈α∞value␈α
at␈α∞all!␈α
If␈α
the␈α∞computation␈α
of␈α∞the␈α
arguments␈α∞to␈α
the
␈↓ α←␈↓␈↓αcons␈↓ does not terminate or gives ␈↓λB␈↓ then we won't get ␈↓
f␈↓.
␈↓ α←␈↓␈↓1.9␈↓ λ∃Becoming an Expert 43␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬1␈↓↓1.9 Becoming an Expert␈↓
␈↓"β␈↓ α←␈↓We␈αhave␈αalready␈α
traced␈αthe␈αdevelopment␈αof␈α
a␈αfew␈αLISP␈αalgorithms,␈α
and␈αwe
␈↓ α←␈↓have␈α
given␈α∞a␈α
few␈α∞programming␈α
hints.␈α
It␈α∞is␈α
time␈α∞to␈α
reinforce␈α∞these␈α
tentative
␈↓ α←␈↓starts␈α
with␈α∞a␈α
more␈α
intensive␈α∞study␈α
of␈α
the␈α∞techniques␈α
for␈α
writing␈α∞good␈α
LISP
␈↓ α←␈↓programs.␈α⊂ This␈α⊂section␈α⊂will␈α⊂spend␈α∂a␈α⊂good␈α⊂deal␈α⊂of␈α⊂time␈α⊂showing␈α∂different
␈↓ α←␈↓styles␈α⊃of␈α⊂definition,␈α⊃giving␈α⊂hints␈α⊃about␈α⊂how␈α⊃to␈α⊂write␈α⊃LISP␈α⊃functions,␈α⊂and
␈↓ α←␈↓increasing␈αyour␈αfamiliarity␈αwith␈αLISP.␈α For␈αthose␈αof␈αyou␈αwho␈αare␈αimpatiently
␈↓ α←␈↓waiting␈α
to␈α
see␈αsome␈α
␈↓¬real␈↓␈α
applications␈α
of␈αthis␈α
programming␈α
language,␈α
we␈αcan
␈↓ α←␈↓only␈α∩say␈α∩"be␈α∩patient".␈α∩ The␈α⊃next␈α∩chapter␈α∩␈↓¬will␈↓␈α∩develop␈α∩several␈α⊃non-trivial
␈↓ α←␈↓algorithms,␈αbut␈αwhat␈αwe␈αmust␈αdo␈αfirst␈αis␈αimprove␈αyour␈αskills,␈αeven␈αat␈αthe␈αrisk
␈↓ α←␈↓of worsening your disposition.
␈↓"β␈↓ α←␈↓␈↓ β'First␈α
some␈α
terminology␈αis␈α
appropriate:␈α
the␈αstyle␈α
of␈α
definition␈α
which␈αwe
␈↓ α←␈↓have␈αbeen␈αusing␈αis␈αcalled␈α␈↓↓definition␈αby␈αrecursion␈↓.␈αThe␈αbasic␈αcomponents␈αof
␈↓ α←␈↓such a definition are:
␈↓"β␈↓ α←␈↓␈↓ βW␈↓↓1.␈↓␈αA␈αbasis␈αcase:␈αwhat␈αto␈αcompute␈α
as␈αvalue␈αfor␈αthe␈αfunction␈αin␈αone␈α
or
␈↓ α←␈↓␈↓ βWmore␈αparticularly␈αsimple␈αcases.␈αA␈αbasis␈αcase␈αis␈αfrequently␈αreferred␈αto
␈↓ α←␈↓␈↓ βWas a termination case.
␈↓"β␈↓ α←␈↓␈↓↓REC␈↓
␈↓"β␈↓ α←␈↓␈↓ βW␈↓↓2.␈↓␈α∞A␈α∞general␈α∞case:␈α∞what␈α∞to␈α
compute␈α∞as␈α∞value␈α∞for␈α∞a␈α∞function,␈α
given
␈↓ α←␈↓␈↓ βWthe values of one or more previous computations with that function.
␈↓"β␈↓ α←␈↓You␈α∂should␈α∂compare␈α∂the␈α∂structure␈α∂of␈α∂a␈α∂␈↓↓REC␈↓-definition␈α∂of␈α∂a␈α∂function␈α∞with
␈↓ α←␈↓that␈α⊂of␈α⊂an␈α⊃␈↓↓IND␈↓-definition␈α⊂of␈α⊂a␈α⊂set␈α⊃(see␈α⊂␈↓↓IND␈↓␈α⊂on␈α⊂page 3).␈α⊃ Applications␈α⊂of
␈↓ α←␈↓␈↓↓REC␈↓-definitions␈α⊂are␈α⊂particularly␈α⊂useful␈α⊂in␈α⊂computing␈α⊂values␈α⊂of␈α⊂a␈α⊂function
␈↓ α←␈↓defined␈α∀over␈α∀a␈α∀set␈α∀which␈α∀has␈α∀been␈α∀defined␈α∀by␈α∀an␈α∀␈↓↓IND␈↓-definition.␈α∪For
␈↓ α←␈↓example,␈α⊂assume␈α⊂that␈α⊂we␈α∂have␈α⊂defined␈α⊂a␈α⊂set␈α∂␈↓A␈↓␈α⊂using␈α⊂␈↓↓IND␈↓␈α⊂then␈α⊂a␈α∂typical
␈↓ α←␈↓algorithm␈α
for␈αcomputing␈α
a␈αfunction␈α
␈↓f␈↓␈αover␈α
␈↓A␈↓␈αwould␈α
involve␈αtwo␈α
parts:␈αfirst,
␈↓ α←␈↓an␈α∂indication␈α∞of␈α∂how␈α∂to␈α∞compute␈α∂␈↓f␈↓␈α∂on␈α∞the␈α∂base␈α∂domain␈α∞of␈α∂␈↓A␈↓,␈α∂and␈α∞second,
␈↓ α←␈↓given␈αvalues␈αfor␈αsome␈αelements␈αof␈α␈↓A␈↓␈αsay␈α␈↓a␈↓β1␈↓, ...,␈↓a␈↓βn␈↓,␈αuse␈α␈↓↓IND␈↓␈αto␈αgenerate␈αa␈αnew
␈↓ α←␈↓element␈α␈↓a␈↓;␈α
then␈αspecify␈α
the␈αvalue␈α
of␈α␈↓f(a)␈↓␈αas␈α
a␈αfunction␈α
of␈αthe␈α
known␈αvalues
␈↓ α←␈↓of ␈↓f(a␈↓β1␈↓)␈↓, ..., ␈↓f(a␈↓βn␈↓)␈↓. That is exactly the structure of ␈↓↓REC␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Here␈αis␈αanother␈αattribute␈αof␈α␈↓↓IND␈↓-definitions:␈αSuppose␈αwe␈αhave␈αdefined
␈↓ α←␈↓a␈αset␈α␈↓A␈↓␈αusing␈α␈↓↓IND␈↓,␈αand␈αwe␈αwish␈αto␈αprove␈αthat␈αa␈αcertain␈αproperty␈α␈↓↓P␈↓␈αholds␈α
for
␈↓ α←␈↓every element of ␈↓A␈↓. We need only show that:
␈↓"β␈↓ α←␈↓␈↓ βW␈↓↓1.␈↓ ␈↓↓P␈↓ holds for every element of the base domain of ␈↓A␈↓.
␈↓"β␈↓ α←␈↓␈↓↓PRF␈↓
␈↓"β␈↓ α←␈↓␈↓ βW␈↓↓2.␈↓␈α⊃Using␈α⊂the␈α⊃technique␈α⊂we␈α⊃elaborated␈α⊂in␈α⊃defining␈α⊂the␈α⊃function␈α⊂␈↓f␈↓
␈↓ α←␈↓␈↓ βWabove,␈α∞if␈α∂we␈α∞can␈α∂show␈α∞that␈α∞␈↓↓P␈↓␈α∂holds␈α∞for␈α∂the␈α∞new␈α∂element␈α∞perhaps
␈↓ α←␈↓␈↓ βWrelying␈α⊂on␈α⊃proofs␈α⊂of␈α⊂␈↓↓P␈↓␈α⊃for␈α⊂sub-elements,␈α⊂then␈α⊃we␈α⊂should␈α⊃have␈α⊂a
␈↓ α←␈↓␈↓ βWconvincing argument that ␈↓↓P␈↓ holds over ␈↓¬all␈↓ of ␈↓A␈↓.
␈↓ α←␈↓␈↓44 Symbolic expressions␈↓
(1.9␈↓
␈↓"β␈↓ α←␈↓This␈αproof␈αtechnique␈αis␈αa␈αgeneralization␈αof␈αa␈αcommon␈αtechnique␈αfor␈αproving
␈↓ α←␈↓properties of the integers. In that context it is called mathematical induction.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∞are␈α∞seeing␈α∞an␈α∞interesting␈α∞parallel␈α∞between␈α∞inductive␈α∞definitions␈α∞of
␈↓ α←␈↓sets,␈α∪recursive␈α∪definitions␈α∩of␈α∪functions,␈α∪and␈α∩proofs␈α∪by␈α∪induction.␈α∪ As␈α∩we
␈↓ α←␈↓proceed,␈αwe␈αwill␈αexploit␈αvarious␈αaspects␈αof␈αthese␈αinterrelationships.␈α However
␈↓ α←␈↓our␈α∞task␈α∞at␈α
hand␈α∞is␈α∞more␈α
mundane:␈α∞to␈α∞develop␈α
facility␈α∞at␈α∞applying␈α∞␈↓↓REC␈↓␈α
to
␈↓ α←␈↓define␈α∂functions␈α∂over␈α∂the␈α∂␈↓↓IND␈↓-domains␈α∂of␈α∂symbolic␈α∂expressions,␈α∂␈↓S␈↓,␈α∂and␈α∞of
␈↓ α←␈↓sequences, ␈↓Seq␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'First␈α
let's␈α
verify␈α
that␈α
the␈αfunctions␈α
we␈α
have␈α
constructed␈α
so␈α
far␈αdo␈α
indeed
␈↓ α←␈↓satisfy␈α∀␈↓↓REC␈↓.␈α∀ Recall␈α∀our␈α∀example␈α∃of␈α∀␈↓αequal␈↓␈α∀on␈α∀page 24.␈α∀The␈α∃basis␈α∀case
␈↓ α←␈↓involves␈α∀a␈α∀calculation␈α∪on␈α∀members␈α∀of␈α∪␈↓<atom>␈↓;␈α∀there␈α∀we␈α∪rely␈α∀on␈α∀␈↓αeq␈↓␈α∪to
␈↓ α←␈↓distinguish␈α↔between␈α↔distinct␈α↔atoms.␈α↔ The␈α↔question␈α↔of␈α↔equality␈α↔for␈α↔two
␈↓ α←␈↓non-atomic␈α∂S-exprs␈α∂was␈α∂recast␈α∞as␈α∂a␈α∂question␈α∂of␈α∞equality␈α∂for␈α∂their␈α∂␈↓αcar␈↓s␈α∞and
␈↓ α←␈↓␈↓αcdr␈↓s.␈αBut␈α
that␈αtoo,␈α
is␈αproper␈αsince␈α
the␈αconstructed␈α
object␈αis␈α
manufactured␈αby
␈↓ α←␈↓␈↓αcons␈↓, and ␈↓αcar␈↓ and ␈↓αcdr␈↓ of that object select the components.
␈↓"β␈↓ α←␈↓␈↓ β'Similar␈α⊂justification␈α⊂for␈α⊂␈↓αlength␈↓␈α⊂on␈α⊂page 31␈α⊂can␈α⊂be␈α⊂given.␈α⊂ There␈α∂the
␈↓ α←␈↓domain␈α∩is␈α∩␈↓Seq␈↓.␈α∩The␈α∩base␈α∩domain␈α∩is␈α∩the␈α∩empty␈α∩sequence,␈α∩and␈α∪␈↓αlength␈↓␈α∩is
␈↓ α←␈↓defined␈αto␈αgive␈α␈↓α0␈↓␈αin␈αthat␈αcase.␈αThe␈αgeneral␈αcase␈αin␈αthe␈αrecursion␈αcomes␈αfrom
␈↓ α←␈↓the␈α∂␈↓↓IND␈↓-definition␈α⊂of␈α∂a␈α⊂sequence.␈↓π 37␈↓␈α∂Given␈α∂a␈α⊂sequence␈α∂␈↓↓s␈↓,␈α⊂we␈α∂made␈α⊂a␈α∂new
␈↓ α←␈↓sequence␈α∃by␈α⊗adding␈α∃a␈α⊗sequence␈α∃element␈α∃to␈α⊗the␈α∃front␈α⊗of␈α∃␈↓↓s␈↓.␈α⊗Again␈α∃the
␈↓ α←␈↓computation␈α∂of␈α∂␈↓αlength␈↓␈α∂parallels␈α∂this␈α∂construction,␈α∂saying␈α∂that␈α∂the␈α⊂length␈α∂of
␈↓ α←␈↓this new sequence is one more than the length of the sequence ␈↓↓s␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'For a more traditional example consider the factorial function, n!.
␈↓"λ␈↓ α←␈↓␈↓↓1.␈↓ The function is defined for non-negative integers.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ The value of the function for 0 is 1.
␈↓" ␈↓ α←␈↓␈↓↓3␈↓. Otherwise the value of n! is n times the value of (n-1)!.
␈↓"β␈↓ α←␈↓␈↓ β'It␈α
should␈α
now␈α
be␈α
clear␈α
how␈α
to␈α
write␈α
a␈α
LISP␈α
program␈α
for␈α
the␈αfactorial
␈↓ α←␈↓function:
␈↓"∀␈↓ α←␈↓α␈↓ ∧βfact[n] <= [eq[n;0] → 1; ␈↓
t␈↓α → times[n;fact[sub1[n]]]] ␈↓π 38␈↓α
␈↓"∀␈↓ α←␈↓The␈αimplication␈αis␈αthat␈αit␈αis␈αeasier␈αto␈αcompute␈α(n-1)!␈αthan␈αto␈αcompute␈αn!.␈αBut
␈↓ α←␈↓that␈αtoo␈αis␈αin␈αaccord␈αwith␈αour␈αconstruction␈αof␈αthe␈αintegers␈αusing␈αthe␈αsuccessor
␈↓ α←␈↓function.
␈↓"β␈↓ α←␈↓␈↓ β'These␈α
examples␈α
are␈α
typical␈α∞of␈α
LISP's␈α
recursive␈α
definitions.␈α∞ The␈α
body
␈↓ α←␈↓of␈α∞the␈α
definition␈α∞is␈α
a␈α∞conditional␈α
expression;␈α∞the␈α
first␈α∞few␈α∞branches␈α
involve
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 37␈↓Note␈α⊂(page 27)␈α∂that␈α⊂we␈α∂didn't␈α⊂give␈α∂an␈α⊂explicit␈α⊂␈↓↓IND␈↓-definition,␈α∂but
␈↓ α←␈↓rather␈α∀a␈α∀set␈α∀of␈α∀BNF␈α∃equations.␈α∀The␈α∀reader␈α∀should␈α∀supply␈α∃the␈α∀explicit
␈↓ α←␈↓definition.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 38␈↓␈↓αtimes␈↓␈α⊂is␈α⊃a␈α⊂LISP␈α⊃function␈α⊂which␈α⊃performs␈α⊂multiplication,␈α⊃and␈α⊂␈↓αsub1␈↓
␈↓ α←␈↓subtracts ␈↓α1␈↓ from its argument.
␈↓ α←␈↓␈↓1.9␈↓ λ∃Becoming an Expert 45␈↓
␈↓"β␈↓ α←␈↓special␈α∩cases,␈α⊃called␈α∩␈↓↓termination␈α⊃conditions␈↓.␈α∩ Then␈α⊃the␈α∩remainder␈α∩of␈α⊃the
␈↓ α←␈↓conditional␈α∩covers␈α∩the␈α∩general␈α∩case--␈α∪what␈α∩to␈α∩do␈α∩if␈α∩the␈α∩argument␈α∪to␈α∩the
␈↓ α←␈↓function is not one of the special cases.
␈↓"β␈↓ α←␈↓␈↓ β'Notice␈α⊃that␈α⊃␈↓αfact␈↓␈α⊂is␈α⊃a␈α⊃partial␈α⊂function,␈α⊃defined␈α⊃only␈α⊃for␈α⊂non-negative
␈↓ α←␈↓integers.␈α
When␈α
writing␈α
or␈α
reading␈α
LISP␈α
definitions␈α
pay␈α
particular␈α
attention
␈↓ α←␈↓to␈αthe␈αdomain␈α
of␈αdefinition␈αand␈α
the␈αrange␈αof␈α
values␈αproduced.␈αThe␈α
following
␈↓ α←␈↓general hints should also be useful:
␈↓"λ␈↓ α←␈↓␈↓↓1␈↓. Is␈αthe␈α
algorithm␈αto␈α
be␈αa␈α
LISP␈αfunction␈α
or␈αpredicate?␈α
This␈αinformation␈α
can
␈↓ α←␈↓␈↓ β∂be␈α⊃used␈α⊃to␈α⊃double-check␈α⊃uses␈α⊃of␈α⊃the␈α⊃definition.␈α⊃Don't␈α⊃use␈α∩a␈α⊃predicate
␈↓ α←␈↓␈↓ β∂where␈α≥a␈α≤S-expr-valued␈α≥function␈α≤is␈α≥expected;␈α≤and␈α≥don't␈α≥use␈α≤an
␈↓ α←␈↓␈↓ β∂S-expr-valued function where a list-value is expected.
␈↓" ␈↓ α←␈↓␈↓↓2␈↓. Are␈αthere␈αrestrictions␈αon␈αthe␈αargument␈αpositions?␈αFor␈αexample,␈αmust␈αsome
␈↓ α←␈↓␈↓ β∂of␈α
the␈α
arguments␈α
be␈α
truth␈α
values?␈α
Similar␈α
consistency␈α
checking␈α
as␈α
in␈α
␈↓↓1␈↓␈α
can
␈↓ α←␈↓␈↓ β∂be done with this information.
␈↓" ␈↓ α←␈↓␈↓↓3␈↓. Are␈α⊃the␈α⊃termination␈α⊃conditions␈α⊃compatible␈α⊃with␈α⊃the␈α⊃restrictions␈α⊃on␈α⊂the
␈↓ α←␈↓␈↓ β∂arguments?␈α If␈αit␈αis␈αa␈αrecursion␈αon␈αlists,␈αcheck␈αfor␈αthe␈αempty␈αlist;␈αif␈αit␈αis␈αa
␈↓ α←␈↓␈↓ β∂recursion on arbitrary S-exprs, then check for the appearance of an atom.
␈↓" ␈↓ α←␈↓␈↓↓4␈↓. Whenever␈α∪a␈α∪function␈α∪call␈α∪is␈α∀made␈α∪within␈α∪the␈α∪definition,␈α∪are␈α∀all␈α∪the
␈↓ α←␈↓␈↓ β∂restrictions on that function satisfied?
␈↓" ␈↓ α←␈↓␈↓↓5␈↓. Don't␈α
try␈αto␈α
do␈α
too␈αmuch.␈α
Try␈αto␈α
be␈α
lazy.␈αThere␈α
is␈αusually␈α
a␈α
very␈αsimple
␈↓ α←␈↓␈↓ β∂termination␈α∂case.␈α∂If␈α∂the␈α∂termination␈α∞case␈α∂looks␈α∂messy,␈α∂there␈α∂is␈α∞probably
␈↓ α←␈↓␈↓ β∂something␈α∂wrong␈α∂with␈α∂your␈α∂conception␈α∂of␈α∂the␈α∂program.␈α∂ If␈α⊂the␈α∂general
␈↓ α←␈↓␈↓ β∂case␈α∞looks␈α∞messy,␈α∞then␈α∞write␈α
some␈α∞subfunctions␈α∞to␈α∞perform␈α∞the␈α∞brunt␈α
of
␈↓ α←␈↓␈↓ β∂the calculation.
␈↓"β␈↓ α←␈↓Apply␈α
the␈αsuggestions␈α
when␈αwriting␈α
any␈α
subfunction.␈αWhen␈α
you␈αare␈α
finished,
␈↓ α←␈↓no␈α
function␈αwill␈α
do␈αvery␈α
much,␈αbut␈α
the␈αnet␈α
effect␈αof␈α
all␈αthe␈α
functions␈αacting
␈↓ α←␈↓in␈α⊂concert␈α∂is␈α⊂a␈α∂solution␈α⊂to␈α⊂your␈α∂problem.␈α⊂That␈α∂is␈α⊂part␈α∂of␈α⊂the␈α⊂mystique␈α∂of
␈↓ α←␈↓recursive programming.
␈↓"β␈↓ α←␈↓␈↓ β'As␈α⊃you␈α⊃may␈α⊃have␈α⊃discovered,␈α⊃the␈α⊃real␈α⊃difficulty␈α⊃in␈α∩programming␈α⊃is
␈↓ α←␈↓writing␈α⊂your␈α⊂own␈α⊂programs.␈α⊂But␈α∂who␈α⊂says␈α⊂programming␈α⊂is␈α⊂easy?␈α⊂LISP␈α∂at
␈↓ α←␈↓least␈αmakes␈αsome␈αof␈αyour␈αdecisions␈αeasy.␈αIts␈αconstructs␈αare␈αparticularly␈αfrugal.
␈↓ α←␈↓So␈α∞far␈α∂there␈α∞is␈α∂only␈α∞␈↓¬one␈↓␈α∂way␈α∞to␈α∞write␈α∂a␈α∞non-trivial␈α∂algorithm␈α∞in␈α∂LISP:␈α∞use
␈↓ α←␈↓recursion.␈α∩The␈α∩structure␈α∪of␈α∩the␈α∩program␈α∩flows␈α∪like␈α∩that␈α∩of␈α∪an␈α∩inductive
␈↓ α←␈↓argument.␈α⊂Find␈α⊂the␈α⊂right␈α⊂induction␈α⊂hypothesis␈α⊂and␈α⊂the␈α⊂inductive␈α⊂proof␈α⊂is
␈↓ α←␈↓easy;␈α
find␈α
the␈α
right␈α
structure␈α
on␈α
which␈α
to␈α
recur␈α
and␈α
recursive␈α
programming␈α
is
␈↓ α←␈↓easy.␈αIt's␈αeasier␈αto␈αbegin␈αwith␈αunary␈αfunctions;␈αthen␈αthere's␈αno␈αquestion␈αabout
␈↓ α←␈↓which␈αargument␈αis␈αto␈αbe␈αdecomposed.␈αThe␈αonly␈αdecision␈αis␈αhow␈αto␈αterminate
␈↓ α←␈↓the␈α∂recursion.␈α∂ If␈α∂the␈α∂argument␈α∂is␈α∂an␈α∂S-expr␈α∂we␈α∂typically␈α∂terminate␈α∂on␈α∂the
␈↓ α←␈↓occurrence␈αof␈αan␈αatom.␈αIf␈αthe␈αargument␈αis␈αa␈αlist,␈αthen␈αterminate␈αon␈α␈↓α( )␈↓.␈αIf␈αthe
␈↓ α←␈↓argument is a number then terminate on zero.
␈↓ α←␈↓␈↓46 Symbolic expressions␈↓
(1.9␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Consider␈α≠a␈α≤slightly␈α≠more␈α≤complicated␈α≠arithmetical␈α≤example,␈α≠the
␈↓ α←␈↓Fibonacci␈α
sequence:␈α
0, 1, 1, 2,␈α∞3, 5, 8, ... .␈α
This␈α
sequence␈α
can␈α∞be␈α
characterized
␈↓ α←␈↓by the following recurrence relation:
␈↓"∀␈↓ α←␈↓␈↓ ε'f(0) = 0
␈↓"β␈↓ α←␈↓␈↓ ε'f(1) = 1
␈↓"β␈↓ α←␈↓␈↓ ¬af(n) = f(n-1)+f(n-2)
␈↓"∀␈↓ α←␈↓The translation to a LISP function is easy:
␈↓"∀␈↓ α←␈↓α␈↓ ∧∪fib[n] <=␈↓ ¬↔[eq[n;0] → 0;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔ eq[n;1] → 1;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔ ␈↓
t␈↓α → plus[fib[sub1[n]];fib[sub1[sub1[n]]]]]
␈↓"∀␈↓ α←␈↓where ␈↓αplus␈↓ is a representation of the mathematical function ␈↓α+␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α∞few␈α∞additional␈α∞points␈α∞can␈α∞be␈α∞made␈α∞here.␈α∞Notice␈α∞that␈α∞an␈α
evaluation
␈↓ α←␈↓scheme␈α
may␈α
imply␈α
many␈αduplicate␈α
computations.␈α
For␈α
example,␈αcomputation
␈↓ α←␈↓of␈α∪␈↓αfib[5]␈↓␈α∪requires␈α∪the␈α∪computation␈α∪of␈α∪␈↓αfib[4]␈↓␈α∪and␈α∪␈↓αfib[3]␈↓.␈α∪ But␈α∪within␈α∩the
␈↓ α←␈↓calculation␈α⊂of␈α∂␈↓αfib[4]␈↓␈α⊂we␈α⊂again␈α∂calculate␈α⊂␈↓αfib[3]␈↓,␈α∂etc.␈α⊂ It␈α⊂would␈α∂be␈α⊂nice␈α⊂if␈α∂we
␈↓ α←␈↓could␈αrestructure␈αthe␈αdefinition␈αof␈α␈↓αfib␈↓␈αto␈αstop␈αthis␈αextra␈αcomputation.␈↓π 39␈↓␈αSince
␈↓ α←␈↓we␈α␈↓¬do␈↓␈αwish␈αto␈αrun␈αprograms␈αon␈αa␈αmachine␈αwe␈αshould␈αgive␈αsome␈αattention␈αto
␈↓ α←␈↓efficiency.␈↓π 40␈↓
␈↓"β␈↓ α←␈↓␈↓ β'We␈αwill␈αdefine␈α
another␈αfunction,␈αcalled␈α
␈↓αfib␈↓λ'␈↓,␈αon␈αthree␈α
variables␈α␈↓αx␈↓,␈α␈↓αy␈↓,␈α
and
␈↓ α←␈↓␈↓αn␈↓.␈α∂The␈α⊂variables,␈α∂␈↓αx␈↓␈α⊂and␈α∂␈↓αy␈↓,␈α⊂will␈α∂be␈α⊂used␈α∂to␈α⊂carry␈α∂the␈α⊂partial␈α∂computations.
␈↓ α←␈↓Consider:
␈↓"∀␈↓ α←␈↓α␈↓ ¬Sfib␈↓β1␈↓α[n] <= fib␈↓λ'␈↓α[n;0;1]
␈↓"β␈↓ α←␈↓α␈↓where:␈↓α␈↓ ∧∨fib␈↓λ'␈↓α[n;x;y] <=␈↓ ¬←[eq[n;0] → x;
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬← ␈↓
t␈↓α → fib␈↓λ'␈↓α[sub1[n];plus[x;y];x]]
␈↓"∀␈↓ α←␈↓This␈α∩example␈α⊃is␈α∩complicated␈α⊃enough␈α∩to␈α⊃warrant␈α∩closer␈α∩examination.␈α⊃The
␈↓ α←␈↓initial␈α∞call,␈α∞␈↓αfib␈↓β1␈↓α[n]␈↓,␈α∞has␈α∞the␈α∞effect␈α∞of␈α∞calling␈α∞␈↓αfib␈↓λ'␈↓␈α∞with␈α∞␈↓αx␈↓␈α∞initialized␈α∞to␈α∂␈↓α0␈↓␈α∞and
␈↓ α←␈↓with␈α␈↓αy␈↓␈αinitialized␈α
to␈α␈↓α1␈↓.␈αThe␈α
calls␈αon␈α␈↓αfib␈↓λ'␈↓␈α
within␈αthe␈αbody␈α
of␈αthe␈αdefinition,␈α
say
␈↓ α←␈↓the␈αi␈↓πth␈↓␈α
such␈αrecursive␈α
call,␈αhas␈α
the␈αeffect␈α
of␈αsaving␈α
the␈αi␈↓πth␈↓␈α
Fibonacci␈αnumber
␈↓ α←␈↓in ␈↓αx␈↓ and the i-1␈↓πst␈↓ in ␈↓αy␈↓.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 39␈↓An␈α⊂alternative␈α⊂solution␈α⊂is␈α∂to␈α⊂supply␈α⊂a␈α⊂different␈α⊂evaluation␈α∂scheme
␈↓ α←␈↓which might be able to remember previously calculated results. [Got 74].
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 40␈↓For␈α∂those␈α∂readers␈α∂with␈α∂some␈α∂programming␈α∂experience,␈α∂the␈α∂solution
␈↓ α←␈↓may␈α⊃appear␈α∩easy:␈α⊃assign␈α⊃the␈α∩partial␈α⊃computations␈α⊃to␈α∩temporary␈α⊃variables.
␈↓ α←␈↓The␈α∃problem␈α∃here␈α∃is␈α∃that␈α∃our␈α∃current␈α∃subset␈α∃of␈α∃LISP␈α∃doesn't␈α∃contain
␈↓ α←␈↓assignment.
␈↓ α←␈↓␈↓1.9␈↓ λ∃Becoming an Expert 47␈↓
␈↓"β␈↓ α←␈↓␈↓ β'For example:
␈↓"∀␈↓ α←␈↓α␈↓ ∧␈fib␈↓β1␈↓α[4]␈↓ ¬w= fib␈↓λ'␈↓α[4;0;1]
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬w= fib␈↓λ'␈↓α[3;1;0]
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬w= fib␈↓λ'␈↓α[2;1;1]
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬w= fib␈↓λ'␈↓α[1;2;1]
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬w= fib␈↓λ'␈↓α[0;3;2]
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬w= 3
␈↓"∀␈↓ α←␈↓Functions␈α→like␈α→␈↓αfib␈↓λ'␈↓,␈α→used␈α→to␈α→help␈α→␈↓αfib␈↓β1␈↓,␈α→are␈α→called␈α~"help functions"␈α→or
␈↓ α←␈↓␈↓↓auxiliary functions␈↓;␈α#variables␈α#like␈α#␈↓αx␈↓␈α#and␈α#␈↓αy␈↓␈α#in␈α#␈↓αfib␈↓λ'␈↓␈α$are␈α#called
␈↓ α←␈↓␈↓↓accumulators␈↓ ([Moor 74]),␈α∀since␈α∀they␈α∀are␈α∀used␈α∀to␈α∀accumulate␈α∀the␈α∀partial
␈↓ α←␈↓computations.␈α The␈αtechnique␈α
of␈αusing␈αauxiliary␈αfunctions␈α
and␈αaccumulators
␈↓ α←␈↓can␈α
also␈α
be␈αapplied␈α
to␈α
the␈αfactorial␈α
example.␈α
When␈α
viewed␈αcomputationally,
␈↓ α←␈↓the␈αresulting␈α
definition␈αwill␈α
be␈αmore␈α
efficient,␈αthough␈α
the␈αgain␈α
in␈αefficiency␈α
is
␈↓ α←␈↓not as apparent as that in the Fibonacci example.␈↓π 41␈↓
␈↓"β␈↓ α←␈↓Thus:
␈↓"∀␈↓ α←␈↓α␈↓ ¬Qfact␈↓β1␈↓α[n] <= fact␈↓λ'␈↓α[n;1]
␈↓"β␈↓ α←␈↓α␈↓where:␈↓α␈↓ βufact␈↓λ'␈↓α[n;x] <= [eq[n;0] → x; ␈↓
t␈↓α → fact␈↓λ'␈↓α[sub1[n];times[n;x]]]
␈↓"∀␈↓ α←␈↓It␈α
appears␈αthat␈α
the␈αpairs␈α
␈↓αfact,␈αfact␈↓β1␈↓␈α
and␈α␈↓αfib,␈α
fib␈↓β1␈↓␈αare␈α
equivalent.␈α
Perhaps␈αwe
␈↓ α←␈↓should␈αprove␈αthat␈αthis␈αis␈αso.␈α We␈αpresented␈αthe␈αcrucial␈αideas␈αfor␈αthe␈αproof␈αin
␈↓ α←␈↓the␈α∃discussion␈α∃on␈α∃page 43␈α∃concerning␈α∃␈↓↓IND␈↓,␈α∃␈↓↓REC␈↓␈α∃and␈α∃␈↓↓PRF␈↓.␈α∃ We␈α∃shall
␈↓ α←␈↓examine the question of proofs of equivalence in Section 2.10.
␈↓"β␈↓ α←␈↓␈↓ β'Auxiliary␈αfunctions␈αare␈αalso␈αapplicable␈αto␈αLISP␈αfunctions␈α
defined␈αover
␈↓ α←␈↓S-exprs:
␈↓"∀␈↓ α←␈↓α␈↓ βKlength[n] <= [null[n] → 0; ␈↓
t␈↓α → add1[length[rest[n]]]] ␈↓π 42␈↓α
␈↓"∀␈↓ α←␈↓α␈↓ βKlength␈↓β1␈↓α[n] <= length␈↓λ'␈↓α[n;0]
␈↓" ␈↓ α←␈↓α␈↓where: ␈↓αlength␈↓λ'␈↓α[n;x] <= [null[n] → x; ␈↓
t␈↓α → length␈↓λ'␈↓α[rest[n];add1[x]]]
␈↓" ␈↓ α←␈↓α␈↓Again, it appears that ␈↓αlength␈↓ is equivalent to ␈↓αlength␈↓β1␈↓.
␈↓"∀␈↓ α←␈↓␈↓ β'So␈α↔far␈α↔our␈α↔examples␈α↔have␈α↔either␈α↔been␈α↔numerical␈α↔or␈α↔have␈α↔been
␈↓ α←␈↓predicates.␈α∞ Predicates␈α∞only␈α∞require␈α∞traversing␈α∞existing␈α∞S-exprs;␈α∞certainly␈α
we
␈↓ α←␈↓will␈α∃want␈α∃to␈α∃write␈α∃algorithms␈α∃which␈α∃build␈α∃new␈α∃S-exprs.␈α⊗ Consider␈α∃the
␈↓ α←␈↓problem␈α∞of␈α∞writing␈α
a␈α∞LISP␈α∞algorithm␈α∞to␈α
reverse␈α∞a␈α∞list␈α
␈↓αx␈↓.␈α∞There␈α∞is␈α∞a␈α
simple
␈↓ α←␈↓informal␈α
computation:␈α
take␈αelements␈α
from␈α
the␈αfront␈α
of␈α
␈↓αx␈↓␈αand␈α
put␈α
them␈αonto
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 41␈↓The␈α⊃␈↓αfib␈↓β1␈↓␈α⊃example␈α⊃improves␈α⊃efficiency␈α⊃mostly␈α⊃by␈α∩calculating␈α⊃fewer
␈↓ α←␈↓intermediate␈α⊃results.␈α⊃The␈α⊃gain␈α⊃in␈α⊃the␈α⊃␈↓αfact␈↓β1␈↓␈α⊃example␈α⊃is␈α⊃involved␈α⊃with␈α⊃the
␈↓ α←␈↓machinery␈α≥necessary␈α≥to␈α≡actually␈α≥execute␈α≥the␈α≥program:␈α≡the␈α≥run-time
␈↓ α←␈↓environment,␈α_if␈α↔you␈α_wish.␈α↔We␈α_will␈α↔discuss␈α_this␈α↔when␈α_we␈α_talk␈α↔about
␈↓ α←␈↓implementation␈α∪of␈α∪LISP␈α∪in␈α∪Chapter 6.␈α∪The␈α∪whole␈α∪question␈α∪of:␈α∪"what␈α∩is
␈↓ α←␈↓efficient?" is open to discussion.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 42␈↓add1[x] <= x+1
␈↓ α←␈↓␈↓48 Symbolic expressions␈↓
(1.9␈↓
␈↓"β␈↓ α←␈↓the␈α∂front␈α∂of␈α∂a␈α∂new␈α∂list␈α∂␈↓αy␈↓.␈α∞Initially,␈α∂␈↓αy␈↓␈α∂should␈α∂be␈α∂␈↓α( )␈↓␈α∂and␈α∂the␈α∂process␈α∞should
␈↓ α←␈↓terminate when ␈↓αx␈↓ is empty.
␈↓"β␈↓ α←␈↓␈↓ β'For example, reversal of the list ␈↓α(A B C D)␈↓ would produce the sequence:
␈↓"β␈↓ α←␈↓α␈↓ ¬Sx␈↓ π∨y
␈↓"β␈↓ α←␈↓α␈↓ ¬S(A B C D)␈↓ π∨( )
␈↓"β␈↓ α←␈↓α␈↓ ¬S(B C D)␈↓ π∨(A)
␈↓"β␈↓ α←␈↓α␈↓ ¬S(C D)␈↓ π∨(B A)
␈↓"β␈↓ α←␈↓α␈↓ ¬S(D)␈↓ π∨(C B A)
␈↓"β␈↓ α←␈↓α␈↓ ¬S( )␈↓ π∨(D C B A)
␈↓"∀␈↓ α←␈↓What␈α∂follows␈α∂is␈α⊂␈↓αreverse␈↓,␈α∂where␈α∂we␈α⊂use␈α∂a␈α∂sub-function␈α⊂␈↓αrev␈↓λ'␈↓␈α∂to␈α∂do␈α⊂the␈α∂hard
␈↓ α←␈↓work and perform the initialization with the second argument to ␈↓αrev␈↓λ'␈↓.
␈↓"β␈↓ α←␈↓α␈↓ ¬Areverse[x] <= rev␈↓λ'␈↓α[x;( )]
␈↓" ␈↓ α←␈↓α␈↓ β↑rev␈↓λ'␈↓α[x;y] <= [null[x] → y; ␈↓
t␈↓α → rev␈↓λ'␈↓α[rest[x];concat[first[x];y]]]
␈↓"∀␈↓ α←␈↓This␈α
␈↓αreverse␈↓␈α
function␈αbuilds␈α
up␈α
the␈αnew␈α
list␈α
by␈α␈↓αconcat␈↓-ing␈α
the␈α
elements␈αonto
␈↓ α←␈↓the␈αsecond␈α
argument␈αof␈α
␈↓αrev␈↓λ'␈↓α␈↓.␈αSince␈α
␈↓αy␈↓␈αwas␈α
initialized␈αto␈α
␈↓α( )␈↓␈αwe␈α
are␈αassured␈α
that
␈↓ α←␈↓the␈αresulting␈αconstruct␈αwill␈α
be␈αa␈αlist.␈α We␈αwill␈α
see␈αa␈α"direct"␈αdefinition␈α
of␈αthe
␈↓ α←␈↓reversing function in a moment.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
development␈α
of␈α
an␈α
algorithm␈α
which␈α
constructs␈α
new␈α
objects␈α
may␈α
not
␈↓ α←␈↓always␈α∂be␈α∂so␈α∂straightforward.␈α∂Suppose␈α∂we␈α∂require␈α∂a␈α∂LISP␈α⊂function␈α∂named
␈↓ α←␈↓␈↓αappend␈↓␈α
of␈α
two␈α∞list␈α
arguments,␈α
␈↓αx␈↓␈α
and␈α∞␈↓αy␈↓,␈α
which␈α
is␈α
to␈α∞return␈α
a␈α
new␈α∞list␈α
which
␈↓ α←␈↓has ␈↓αx␈↓ appended onto the front of ␈↓αy␈↓. For example:
␈↓"⊂␈↓ α←␈↓α␈↓ ∧Cappend[(A B D);(C E)] = (A B D C E)
␈↓"⊃␈↓ α←␈↓α␈↓ ∧Cappend[A;(B C)] ␈↓ = ␈↓λB␈↓ since ␈↓αA␈↓ is not a list.
␈↓"⊃␈↓ α←␈↓␈↓ ∧C␈↓αappend[(A B C);( )] = append[( );(A B C)] = (A B C)
␈↓"∀␈↓ α←␈↓␈↓αappend␈↓␈αis␈αa␈αpartial␈αfunction;␈αit␈αshould␈αbe␈αdefined␈αby␈αrecursion,␈αbut␈αrecursion
␈↓ α←␈↓on␈α∩which␈α∩argument?␈α∩ If␈α∩either␈α∩argument␈α∩is␈α∩␈↓α( )␈↓␈α∩then␈α∩the␈α∩value␈α∩given␈α∩by
␈↓ α←␈↓␈↓αappend␈↓␈αis␈αthe␈αother␈αargument.␈αThe␈αnext␈αsimplest␈αcase␈αis␈αa␈αone-element␈αlist;␈αif
␈↓ α←␈↓exactly␈α⊃one␈α⊃of␈α⊃␈↓αx␈↓␈α⊃or␈α⊃␈↓αy␈↓␈α⊃is␈α⊃a␈α⊃singleton␈α⊃how␈α⊃does␈α⊃that␈α⊃help␈α⊃us␈α⊃discover␈α⊂the
␈↓ α←␈↓recurrence␈α∞relation␈α∞for␈α∞appending?␈α∞It␈α∞doesn't␈α∞help␈α∞much␈α∞if␈α∞␈↓αy␈↓␈α∞is␈α∞a␈α∞singleton;
␈↓ α←␈↓but if ␈↓αx␈↓ is a singleton, then ␈↓αappend␈↓ could give:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ¬5␈↓αconcat[first[x];y]␈↓ as result
␈↓"∀␈↓ α←␈↓So recursion on ␈↓αx␈↓ is likely. The definition now follows.
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ β,␈↓αappend[x;y] <= [null[x] → y; ␈↓
t␈↓α → concat[first[x];append[rest[x];y]]].␈↓
␈↓"∀␈↓ α←␈↓Notice␈α⊂that␈α∂the␈α⊂construction␈α⊂of␈α∂the␈α⊂result␈α∂is␈α⊂a␈α⊂bit␈α∂more␈α⊂obscure␈α⊂than␈α∂that
␈↓ α←␈↓involved␈α∞in␈α∂␈↓αreverse␈↓.␈α∞The␈α∂construction␈α∞has␈α∞to␈α∂"wait"␈α∞until␈α∂we␈α∞have␈α∂seen␈α∞the
␈↓ α←␈↓end of the list ␈↓αx␈↓. For example:
␈↓ α←␈↓␈↓1.9␈↓ λ∃Becoming an Expert 49␈↓
␈↓"∀␈↓ α←␈↓α␈↓ α←append[(A B C);(D E F)]␈↓ ¬S= concat[A;append[(B C);(D E F)]]
␈↓" ␈↓ α←␈↓α␈↓ α←␈↓ ¬S= concat[A;concat[B;append[(C);(D E F)]]]
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ¬S= concat[␈↓ ε?A;
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ¬S␈↓ ε?concat[␈↓ π∪B;
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ¬S␈↓ ε?␈↓ π∪concat[␈↓ πgC;
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ¬S␈↓ ε?␈↓ π∪␈↓ πgappend[( );(D E F)]]]]
␈↓" ␈↓ α←␈↓α␈↓ α←␈↓ ¬S= concat[A;concat[B;concat[C;(D E F)]]]
␈↓" ␈↓ α←␈↓α␈↓ α←␈↓ ¬S= concat[A;concat[B;(C D E F)]]
␈↓" ␈↓ α←␈↓α␈↓ α←␈↓ ¬S= concat[A;(B C D E F)]
␈↓" ␈↓ α←␈↓α␈↓ α←␈↓ ¬S= (A B C D E F)
␈↓"∀␈↓ α←␈↓We␈α⊂are␈α∂assured␈α⊂of␈α∂constructing␈α⊂a␈α∂list␈α⊂here␈α∂because␈α⊂␈↓αy␈↓␈α∂is␈α⊂a␈α∂list␈α⊂and␈α⊂we␈α∂are
␈↓ α←␈↓␈↓αconcat␈↓-ing␈α⊂onto␈α⊂the␈α⊂front␈α⊂of␈α⊂it.␈α⊂LISP␈α⊂functions␈α⊂which␈α⊂are␈α⊂to␈α⊃construct␈α⊂list
␈↓ α←␈↓output␈αby␈α␈↓αconcat␈↓-ing␈α␈↓¬must␈↓␈αconcatenate␈αonto␈α
the␈αfront␈αof␈αan␈αexisting␈α␈↓¬list␈↓.␈α
That
␈↓ α←␈↓list␈α⊗may␈α∃be␈α⊗either␈α∃non-empty␈α⊗or␈α∃the␈α⊗empty␈α∃list,␈α⊗␈↓α( )␈↓.␈α∃This␈α⊗is␈α⊗why␈α∃the
␈↓ α←␈↓termination␈α∂condition␈α∂on␈α∂a␈α∂list-constructing␈α∂function,␈α∂such␈α∂as␈α∂the␈α∂following
␈↓ α←␈↓function, ␈↓αdotem␈↓, returns ␈↓α( )␈↓.
␈↓"∀␈↓ α←␈↓αdotem[x;y] <= [␈↓ ∧+null[x] → ( );
␈↓"β␈↓ α←␈↓α␈↓ ∧+␈↓
t␈↓α → concat[cons[first[x];first[y]];dotem[rest[x];rest[y]]]]
␈↓"∀␈↓ α←␈↓The␈αarguments␈α
to␈α␈↓αdotem␈↓␈α
are␈αboth␈αlists␈α
assumed␈αto␈α
contain␈αthe␈α
same␈αnumber
␈↓ α←␈↓of␈αelements.␈α
The␈αvalue␈α
returned␈αis␈α
to␈αbe␈α
a␈αlist␈α
of␈αdotted␈α
pairs;␈αthe␈αelements␈α
of
␈↓ α←␈↓the pairs are the corresponding elements of the input lists.
␈↓"β␈↓ α←␈↓␈↓ β'Note␈α
the␈α
use␈α
of␈α
both␈α
␈↓αconcat␈↓␈α
and␈α␈↓αcons␈↓:␈α
␈↓αconcat␈↓␈α
is␈α
used␈α
to␈α
build␈α
the␈αfinal
␈↓ α←␈↓list␈α∂output;␈α⊂␈↓αcons␈↓␈α∂is␈α∂used␈α⊂to␈α∂build␈α∂the␈α⊂dotted␈α∂pairs.␈α∂Now␈α⊂if␈α∂we␈α⊂had␈α∂written
␈↓ α←␈↓␈↓αdotem␈↓␈αsuch␈αthat␈αit␈αknew␈αabout␈αour␈αrepresentation␈αof␈αlists,␈αthen␈α␈↓¬both␈↓␈αfunctions
␈↓ α←␈↓would have been ␈↓αcons␈↓. The definition would not have been as clear.
␈↓"β␈↓ α←␈↓Look at a computation as simple as ␈↓αdotem[(A);(B)]␈↓. This will involve
␈↓"∀␈↓ α←␈↓␈↓ ¬∪␈↓αconcat[cons[A;B];dotem[( );( )]]
␈↓"∀␈↓ α←␈↓α␈↓Now the evaluation of ␈↓αdotem[( );( )]␈↓ returns our needed ␈↓α( )␈↓, giving
␈↓"∀␈↓ α←␈↓α␈↓ ∧λconcat[cons[A;B];( )] = concat[(A . B);( )] = ((A . B))
␈↓"∀␈↓ α←␈↓If␈α
the␈α
termination␈αcondition␈α
of␈α
␈↓αdotem␈↓␈α
returned␈αanything␈α
other␈α
than␈α
␈↓α( )␈↓␈αthen
␈↓ α←␈↓the␈α∀list-construction␈α∀would␈α∃"get␈α∀off␈α∀on␈α∀the␈α∃wrong␈α∀foot"␈α∀and␈α∃would␈α∀not
␈↓ α←␈↓generate a list.
␈↓"β␈↓ α←␈↓␈↓ β'As promised on page 48, here is a "direct" definition of ␈↓αreverse␈↓.
␈↓"∀␈↓ α←␈↓αreverse[x] <=␈↓ ∧∨[null[x] → ( );
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ ␈↓
t␈↓α → append[reverse[rest[x]];concat[first[x];( )]]]
␈↓"∀␈↓ α←␈↓This␈α
reversing␈α
function␈α∞is␈α
not␈α
as␈α
efficient␈α∞as␈α
the␈α
previous␈α
one.␈α∞ Within␈α
the
␈↓ α←␈↓construction␈α
of␈αthe␈α
reversed␈αlist␈α
the␈α␈↓αappend␈↓␈α
function␈αis␈α
called␈αrepeatedly.␈α
You
␈↓ α←␈↓should evaluate something like ␈↓αreverse[(A B C D)]␈↓ to see the difficulty.
␈↓"β␈↓ α←␈↓␈↓ β'It␈α∂␈↓¬is␈↓␈α∂possible␈α∂to␈α∂write␈α∂a␈α∂directly␈α∂recursive␈α∂reversing␈α∂function␈α⊂with␈α∂no
␈↓ α←␈↓␈↓50 Symbolic expressions␈↓
(1.9␈↓
␈↓"β␈↓ α←␈↓auxiliary␈α⊃functions,␈α⊃no␈α⊃functions␈α⊃other␈α⊃than␈α⊃the␈α⊃primitives,␈α⊃and␈α⊃with␈α⊂not
␈↓ α←␈↓much␈α
clarity.␈α
We␈α
shall␈αpersist␈α
because␈α
it␈α
is␈αa␈α
good␈α
example␈α
of␈αdiscovering␈α
the
␈↓ α←␈↓general␈αcase␈α
of␈αthe␈α
recursion␈αby␈αcareful␈α
consideration␈αof␈α
examples.␈αLet␈αus␈α
call
␈↓ α←␈↓the function ␈↓αrev␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∃consider␈α⊗the␈α∃general␈α∃case␈α⊗first,␈α∃and␈α∃postpone␈α⊗the␈α∃termination
␈↓ α←␈↓conditions␈α⊃until␈α⊃later.␈α⊃Consider,␈α⊃for␈α⊃example,␈α⊃␈↓αrev[(A B C D)]␈↓.␈α⊃This␈α⊃should
␈↓ α←␈↓evaluate␈α∞to␈α∞␈↓α(D C B A)␈↓.␈α∞How␈α∞can␈α∞we␈α∞construct␈α∞this␈α∞list␈α∞by␈α∞recursive␈α∞calls␈α
on
␈↓ α←␈↓␈↓αrev␈↓?␈α
Assume␈α␈↓αx␈↓␈α
has␈αvalue␈α
␈↓α(A B C D)␈↓.␈α
Now␈αnote␈α
that␈α␈↓α(D C B A)␈↓␈α
is␈α
the␈αvalue
␈↓ α←␈↓of␈α
␈↓αconcat[D;(C B A)]␈↓.␈α
Then␈α
␈↓αD␈↓␈α∞is␈α
␈↓αfirst[rev[rest[x]]]␈↓␈α
(it␈α
is␈α
also␈α∞␈↓αfirst[rev[x]]␈↓␈α
but
␈↓ α←␈↓that␈αwould␈αnot␈αhelp␈αus␈αsince␈αthe␈αrecursion␈αmust␈αreduce␈αthe␈αcomplexity␈αof␈αthe
␈↓ α←␈↓argument).
␈↓"β␈↓ α←␈↓␈↓ β'How can we get ␈↓α(C B A)␈↓? Well:
␈↓"∀␈↓ α←␈↓α␈↓ ∧O(C B A)␈↓ ¬;= rev[(A B C)]
␈↓" ␈↓ α←␈↓α␈↓ ∧O␈↓ ¬;= rev[concat[A;(B C)]]
␈↓" ␈↓ α←␈↓α␈↓ ∧O␈↓ ¬; ␈↓(we are going after ␈↓αrest[x]␈↓ again,
␈↓"β␈↓ α←␈↓␈↓ ∧O␈↓ ¬; but first we can get ␈↓αA␈↓ from ␈↓αx.
␈↓" ␈↓ α←␈↓α␈↓ ∧O␈↓ ¬;= rev[concat[first[x];(B C)]]
␈↓" ␈↓ α←␈↓α␈↓ ∧O␈↓ ¬;= rev[concat[first[x];rev[(C B)]]]
␈↓" ␈↓ α←␈↓α␈↓ ∧O␈↓ ¬;= rev[concat[first[x];rev[rest[(D C B)]]]]
␈↓" ␈↓ α←␈↓α␈↓ ∧O␈↓ ¬;= rev[concat[first[x];rev[rest[rev[rest[x]]]]]]
␈↓"β␈↓ α←␈↓α␈↓That is, ␈↓αrev[x] ␈↓looks like␈↓α␈↓ ¬;concat[␈↓ ε∂first[rev[rest[x]]];
␈↓"β␈↓ α←␈↓α␈↓ ¬;␈↓ ε∂rev[concat[␈↓ π∪first[x];
␈↓"β␈↓ α←␈↓α␈↓ ¬;␈↓ ε∂␈↓ π∪rev[rest[rev[rest[x]]]]]]]
␈↓"∀␈↓ α←␈↓Now,␈α∩the␈α∪termination␈α∩conditions␈α∪are␈α∩simple.␈α∩First␈α∪␈↓αrev[( )]␈↓␈α∩gives␈α∪␈↓α( )␈↓.␈α∩ But
␈↓ α←␈↓notice␈αthat␈αthe␈αgeneral␈αcase␈αwhich␈αwe␈αjust␈αconstructed␈αhas␈α␈↓¬two␈↓␈α␈↓αconcat␈↓s.␈α That
␈↓ α←␈↓means␈αthe␈αshortest␈αlist␈αwhich␈αit␈αcan␈αmake␈αis␈αof␈αlength␈αtwo.␈α So␈αlists␈αof␈αlength
␈↓ α←␈↓one␈αare␈αalso␈αhandled␈αseparately:␈αthe␈αreverse␈αof␈αsuch␈αa␈αlist␈αis␈αitself.␈α Thus␈αthe
␈↓ α←␈↓complete definition should be:
␈↓"∀␈↓ α←␈↓αrev[x] <= [␈↓ βonull[x] → ( );
␈↓"β␈↓ α←␈↓α␈↓ βonull[rest[x]] → x;
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓
t␈↓α → concat[␈↓ ∧␈first[rev[rest[x]]];
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ ∧␈rev[concat[␈↓ εβfirst[x];
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ ∧␈␈↓ εβrev[rest[rev[rest[x]]]]]]] ]
␈↓"∀␈↓ α←␈↓␈↓ β'We␈α⊂have␈α⊂only␈α⊂hinted␈α⊂at␈α⊂the␈α⊂issue␈α⊂of␈α⊂efficiency␈α⊂in␈α⊂computation.␈α∂The
␈↓ α←␈↓question␈α≡of␈α≡efficiency␈α≡involves␈α≡deeper␈α≡questions␈α≡of␈α∨the␈α≡evaluation
␈↓ α←␈↓mechanisms.␈αWe␈αwill␈αreturn␈αto␈αthese␈αissues␈αafter␈αwe␈αhave␈αdiscussed␈αthe␈αLISP
␈↓ α←␈↓evaluation scheme more completely.
␈↓ α←␈↓␈↓1.9␈↓ λ∃Becoming an Expert 51␈↓
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"∀␈↓ α←␈↓1 Use the following definition:
␈↓"∀␈↓ α←␈↓α␈↓ ββ␈↓ βKmatch[k;m] <=␈↓ ¬[null[k] → NO;
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ βK␈↓ ¬ null[m] → NO;
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ βK␈↓ ¬ eq[first[k];first[m]] → first[k];
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ βK␈↓ ¬ ␈↓
t␈↓α → match[rest[k];rest[m]]]
␈↓"∀␈↓ α←␈↓and evaluate:
␈↓"β␈↓ α←␈↓␈↓ ββ␈↓↓a.␈↓α match[(X);(X)]␈↓ ¬␈↓↓b.␈↓α match[(A B E);(J O E)]
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓↓c.␈↓α match[(F O O); (B A Z)]
␈↓"β␈↓ α←␈↓2. Now write your own functions:
␈↓"β␈↓ α←␈↓␈↓ β'␈↓↓a.␈↓α␈αamong[x;y]␈α<=␈α...␈α:␈αamong␈↓␈αis␈αto␈αbe␈αa␈αpredicate;␈α␈↓αx␈↓␈αis␈αan␈αatom;␈α␈↓αy␈↓␈αis␈αa␈αlist
␈↓ α←␈↓␈↓ ∧+of␈α
atoms.␈α
␈↓αamong␈↓␈α
is␈αto␈α
return␈α
␈↓
f␈↓␈α
if␈α
␈↓αx␈↓␈αis␈α
not␈α
found␈α
as␈αan␈α
element
␈↓ α←␈↓␈↓ ∧+of ␈↓αy␈↓; otherwise, ␈↓αamong␈↓ is to return ␈↓
t␈↓.
␈↓"β␈↓ α←␈↓␈↓ ββe.g. ␈↓αamong[A;(A B C)] = among[A;(C D E A)] = ␈↓
t␈↓α
␈↓"β␈↓ α←␈↓α␈↓ ββ among[A1;(A2 B2)] = ␈↓
f␈↓α.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓↓b.␈↓α␈α∞anywhere[x;y]␈α∞<=␈α
...␈α∞:␈α∞anywhere␈↓␈α∞is␈α
a␈α∞predicate;␈α∞␈↓αx␈↓␈α∞is␈α
an␈α∞atom;␈α∞␈↓αy␈↓␈α∞is␈α
an
␈↓ α←␈↓␈↓ ∧[arbitrary␈αS-expr␈αor␈αlist.␈α␈↓αanywhere␈↓␈αis␈αto␈αreturn␈α␈↓
t␈↓␈αjust␈αin␈αthe
␈↓ α←␈↓␈↓ ∧[case that ␈↓αx␈↓ appears somewhere in ␈↓αy␈↓.
␈↓"β␈↓ α←␈↓␈↓ ββe.g. ␈↓αanywhere[A;(A B C)] = anywhere[A;((A . B) . C)] = ␈↓
t␈↓α
␈↓"β␈↓ α←␈↓α␈↓ ββ anywhere[A;(B C D)] = ␈↓
f␈↓α.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓↓c.␈↓α␈α
collectpair[z;x;y]␈α<=␈α
...␈α:␈α
x␈↓␈αand␈α
␈↓αy␈↓␈αare␈α
atoms;␈α␈↓αz␈↓␈α
is␈αan␈α
S-expression␈αor␈α
list,
␈↓ α←␈↓␈↓ ∧␈some␈α∀of␈α∀whose␈α∀subexpressions,␈α∀may␈α∀begin␈α∀␈↓α(x ...)␈↓␈α∀or
␈↓ α←␈↓␈↓ ∧␈␈↓α(y ...)␈↓.␈α∀ ␈↓αcollectpair␈↓␈α∃is␈α∀to␈α∀return␈α∃a␈α∀dotted␈α∃pair␈α∀whose
␈↓ α←␈↓␈↓ ∧␈␈↓αcar␈↓-part␈α⊃is␈α⊂a␈α⊃list␈α⊂of␈α⊃all␈α⊂the␈α⊃occurrences␈α⊂of␈α⊃␈↓α(x ...)␈↓␈α⊂and
␈↓ α←␈↓␈↓ ∧␈whose ␈↓αcdr␈↓-part is a list of all occurrences of ␈↓α(y ...)␈↓.
␈↓"β␈↓ α←␈↓␈↓ ββe.g. ␈↓αcollectpair[((A 1) ((B . 2) (C A 4)));A;B] = (((A 1) (A 4)) . ((B . 2)))
␈↓"β␈↓ α←␈↓␈↓ β'␈↓↓d.␈↓α␈α
pred[x]␈α
<=␈α
...␈α
:␈α
x␈↓␈α
is␈α
a␈α
positive␈α
integer.␈α
␈↓αpred␈↓␈α
is␈α
a␈α
function,␈αreturning␈α
the
␈↓ α←␈↓␈↓ ∧∨predecessor␈αof␈αits␈αargument.␈αThe␈αonly␈αarithmetic␈αfunction␈αyou
␈↓ α←␈↓␈↓ ∧∨may use is ␈↓αadd1␈↓.
␈↓"β␈↓ α←␈↓␈↓ ββe.g. ␈↓αpred[3] = 2; pred[0] ␈↓is undefined␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ββ pred[add1[x]] = x ␈↓ for ␈↓αx ␈↓λ≥␈↓α 0.␈↓
␈↓"β␈↓ α←␈↓␈↓ β'␈↓↓e.␈↓α␈αsignum[x]␈α<=␈α...␈α
:␈αx␈↓␈αis␈αan␈α
integer.␈α␈↓αsignum␈↓␈αreturns␈α␈↓αNEGATIVE␈↓,␈α
␈↓αZERO␈↓,
␈↓ α←␈↓␈↓ ∧7or␈α⊃␈↓αPOSITIVE␈↓␈α⊃depending␈α⊂on␈α⊃the␈α⊃sign␈α⊂of␈α⊃␈↓αx␈↓.␈α⊃You␈α⊃may␈α⊂use
␈↓ α←␈↓␈↓ ∧7␈↓αadd1␈↓ and ␈↓αsub1␈↓ but no comparision function other than ␈↓αeq␈↓.
␈↓ α←␈↓␈↓52 Symbolic expressions␈↓
(1.9␈↓
␈↓"β␈↓ α←␈↓␈↓ β'␈↓↓f.␈↓α␈αmaxdepth[l]␈α<=␈α...␈α:␈αl␈↓␈αis␈α
a␈αlist.␈αThis␈αfunction␈αis␈αto␈αfind␈α
the␈αmaximum
␈↓ α←␈↓␈↓ ∧Cdepth␈α∂of␈α⊂nesting␈α∂of␈α∂any␈α⊂element␈α∂in␈α∂␈↓αl␈↓.␈α⊂Assume␈α∂that␈α∂␈↓αl␈↓␈α⊂is␈α∂a
␈↓ α←␈↓␈↓ ∧Cstrict␈α⊃list␈α⊃(see␈α⊃page 36);␈α⊃that␈α⊃is,␈α⊃any␈α⊃sub-element␈α⊃is␈α⊂either
␈↓ α←␈↓␈↓ ∧Catomic or is itself a strict list. For example
␈↓" ␈↓ α←␈↓␈↓ ∧.␈↓αmaxdepth[( )] = 0; maxdepth[(((B) C) A)] = 3␈↓
␈↓ α←␈↓␈↓2.␈↓ λqApplications 53␈↓
␈↓" ␈↓ α←␈↓␈↓ ␈↓↓CHAPTER 2␈↓
␈↓"β␈↓ α←␈↓␈↓ Applications of LISP␈↓
␈↓"β␈↓ α←␈↓¬␈↓ β'"...All␈α⊃the␈α⊃time␈α⊂I␈α⊃design␈α⊃programs␈α⊂for␈α⊃nonexisting␈α⊃machines␈α⊂and
␈↓ α←␈↓¬␈↓ β'add:␈α∪`if␈α∩we␈α∪now␈α∪had␈α∩a␈α∪machine␈α∩comprising␈α∪the␈α∪primitives␈α∩here
␈↓ α←␈↓¬␈↓ β'assumed, then the job is done.'
␈↓"β␈↓ α←␈↓¬␈↓ β'...␈αIn␈αactual␈αpractice,␈αof␈αcourse,␈αthis␈αideal␈αmachine␈αwill␈αturn␈αout␈αnot
␈↓ α←␈↓¬␈↓ β'to␈αexist,␈α
so␈αour␈αnext␈α
task␈α--structurally␈αsimilar␈α
to␈αthe␈α
original␈αone--
␈↓ α←␈↓¬␈↓ β'is␈α⊃to␈α⊂program␈α⊃the␈α⊂simulation␈α⊃of␈α⊂the␈α⊃"upper"␈α⊂machine....␈α⊃But␈α⊂this
␈↓ α←␈↓¬␈↓ β'bunch␈α
of␈α
programs␈α∞is␈α
written␈α
for␈α
a␈α∞machine␈α
that␈α
in␈α∞all␈α
probability
␈↓ α←␈↓¬␈↓ β'will␈α⊂not␈α⊂exist,␈α⊂so␈α⊃our␈α⊂next␈α⊂job␈α⊂will␈α⊂be␈α⊃to␈α⊂simulate␈α⊂it␈α⊂in␈α⊃terms␈α⊂of
␈↓ α←␈↓¬␈↓ β'programs␈αfor␈αa␈αnext␈αlower␈αlevel␈αmachine,␈αetc.,␈αuntil␈αfinally␈α
we␈αhave
␈↓ α←␈↓¬␈↓ β'a program that can be executed by our hardware...."
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ πKE. W. Dijkstra, [Dij 72]
␈↓"λ␈↓ α←␈↓␈↓ ¬g␈↓↓2.1 Introduction␈↓
␈↓"β␈↓ α←␈↓There␈αare␈αseveral␈αways␈αof␈αinterpreting␈αthis␈αremark␈αof␈αDijkstra.␈α Anyone␈α
who
␈↓ α←␈↓has␈α∞programmed␈α∞at␈α∞a␈α∞level␈α
higher␈α∞than␈α∞machine␈α∞language␈α∞has␈α
experienced
␈↓ α←␈↓the␈α
phenomenon.␈αThe␈α
act␈α
of␈αprogramming␈α
in␈α
a␈αhigh-level␈α
language␈α
is␈αthat␈α
of
␈↓ α←␈↓writing␈α
algorithms␈α
for␈α
a␈α
nonexistent␈α
high-level␈α
machine.␈α
Typically␈αhowever,
␈↓ α←␈↓␈↓54 Applications␈↓
(2.1␈↓
␈↓"β␈↓ α←␈↓the␈α⊗changes␈α↔of␈α⊗representation␈α↔from␈α⊗machine␈α⊗to␈α↔machine␈α⊗are␈α↔all␈α⊗done
␈↓ α←␈↓automatically:␈αfrom␈αhigh-level,␈αto␈αassembly␈αlanguage,␈αand␈αfinally␈αto␈αhardware
␈↓ α←␈↓instructions.
␈↓"β␈↓ α←␈↓␈↓ β'A␈αrelated␈αview␈α
of␈αDijkstra's␈αremark␈α
involves␈αour␈αdiscussions␈αof␈α
abstract
␈↓ α←␈↓data␈α↔structures␈α↔and␈α↔algorithms.␈α⊗ We␈α↔express␈α↔our␈α↔algorithms␈α↔and␈α⊗data
␈↓ α←␈↓structures␈α_in␈α↔terms␈α_of␈α_abstractions␈α↔independent␈α_of␈α↔how␈α_they␈α_may␈α↔be
␈↓ α←␈↓represented␈α∃in␈α∃a␈α∃machine;␈α∃indeed␈α∀we␈α∃can␈α∃use␈α∃the␈α∃ideas␈α∃of␈α∀abstraction
␈↓ α←␈↓␈↓¬regardless␈↓␈αof␈αwhether␈αthe␈αformalism␈αwill␈αfind␈αa␈αrepresentation␈αon␈αa␈αmachine.
␈↓ α←␈↓This␈α⊂use␈α⊂of␈α⊂abstraction␈α⊂is␈α⊃the␈α⊂true␈α⊂sense␈α⊂of␈α⊂the␈α⊂programming␈α⊃style␈α⊂called
␈↓ α←␈↓"structured␈α≡programming".␈α≡ We␈α≡will␈α≡see␈α≡in␈α≡this␈α≡chapter␈α≡how␈α≥this
␈↓ α←␈↓programming␈α∞style␈α∞is␈α∞a␈α∞natural␈α∞result␈α∞of␈α∞writing␈α∞representation-independent
␈↓ α←␈↓LISP programs.
␈↓"β␈↓ α←␈↓␈↓ β'As␈α∀we␈α∀have␈α∀previously␈α∀remarked,␈α∀we␈α∀will␈α∀see␈α∀a␈α∀close␈α∪relationship
␈↓ α←␈↓between␈α⊂the␈α∂structure␈α⊂of␈α∂an␈α⊂algorithm␈α⊂and␈α∂the␈α⊂structure␈α∂of␈α⊂the␈α⊂data.␈α∂ We
␈↓ α←␈↓have␈αseen␈α
this␈αalready␈α
on␈αa␈α
small␈αscale:␈α
list-algorithms␈αtend␈α
to␈αrecur␈α
"linearly"
␈↓ α←␈↓on␈α␈↓αrest␈↓␈αto␈α␈↓α( )␈↓;␈αS-expr␈αalgorithms␈αtend␈αto␈αrecur␈α"left-and-right"␈αon␈α␈↓αcar␈↓␈αand␈α␈↓αcdr␈↓,
␈↓ α←␈↓finally␈αdecomposing␈αthe␈αexpression␈αto␈αatoms.␈α Indeed,␈αthe␈αinstances␈αof␈αcontrol
␈↓ α←␈↓structures␈α
appearing␈α
in␈α
an␈α
algorithm␈α
typically␈α
parallel␈α
the␈α
style␈α
of␈αinductive
␈↓ α←␈↓definition of the data structure which the algorithm is examining.␈↓π 1␈↓
␈↓"β␈↓ α←␈↓␈↓ β'If a structure is defined as:
␈↓"∀␈↓ α←␈↓␈↓ ¬a␈↓
D␈↓ ::= ␈↓
D␈↓β1␈↓ | ␈↓
D␈↓β2␈↓ | ␈↓
D␈↓β3␈↓
␈↓"β␈↓ α←␈↓e.g.␈↓ ¬∂ <seq elem> ::= <indiv> | <seq>
␈↓"∀␈↓ α←␈↓then␈α
we␈α
can␈αexpect␈α
to␈α
find␈α
a␈αconditional␈α
expression␈α
whose␈αpredicate␈α
positions
␈↓ α←␈↓are filled by the recognizers for the ␈↓
D␈↓βi␈↓'s.
␈↓"β␈↓ α←␈↓␈↓ β'If the structure is defined as:
␈↓"∀␈↓ α←␈↓␈↓ ¬v␈↓
D␈↓ ::= ␈↓
D␈↓β1␈↓ ... ␈↓
D␈↓β1␈↓
␈↓"β␈↓ α←␈↓e.g.␈↓ ∧b <seq> ::= ␈↓↓(␈↓<seq elem>␈↓↓,␈↓ ..., <seq elem>␈↓↓)␈↓
␈↓"∀␈↓ α←␈↓that␈α∂is,␈α∂a␈α∂homogeneous␈α∂sequence␈α∂of␈α∂elements,␈α∂then␈α∂we␈α∂will␈α∂have␈α∂a␈α∞"linear"
␈↓ α←␈↓recursion like that experienced in list-algorithms.␈↓π 2␈↓
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 1␈↓The␈α
ideas␈α
sketched␈αhere␈α
have␈α
more␈αformal␈α
explanations␈α
in␈αalgebraic
␈↓ α←␈↓notions; see [Hen 75].
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 2␈↓␈αIndeed␈α
there␈αare␈α
other␈αforms␈α
of␈αcontrol␈α
like␈αiteration␈α
or␈α␈↓αlit␈↓␈α
(page 196)
␈↓ α←␈↓which are related to such data structures.
␈↓ α←␈↓␈↓2.1␈↓ λmIntroduction 55␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Finally␈α
if␈α
the␈α
structure␈α
is␈α
defined␈α
with␈α
a␈α
fixed␈α
number␈α
of␈α
components
␈↓ α←␈↓as:
␈↓"∀␈↓ α←␈↓␈↓ ¬R␈↓
D␈↓ ::= ␈↓
D␈↓β1␈↓ ␈↓
D␈↓β2␈↓ ␈↓
D␈↓β3␈↓... ␈↓
D␈↓βn␈↓
␈↓"β␈↓ α←␈↓e.g. ␈↓ ∧␈ <sexpr> ::= ␈↓α(␈↓<sexpr> . <sexpr>␈↓α)␈↓
␈↓"∀␈↓ α←␈↓then␈α~we␈α~can␈α~expect␈α~occurrences␈α→of␈α~selector␈α~functions␈α~to␈α~extract␈α→the
␈↓ α←␈↓components from the structure.␈↓π 3␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Thus␈α↔a␈α↔data-structure␈α↔algorithm␈α↔tends␈α↔to␈α↔"pass␈α↔off"␈α↔its␈α↔work␈α⊗to
␈↓ α←␈↓subfunctions␈α⊃which␈α⊂will␈α⊃operate␈α⊂on␈α⊃the␈α⊂components␈α⊃of␈α⊂the␈α⊃data␈α⊂structure.
␈↓ α←␈↓Thus␈αif␈αa␈α
structure␈αof␈αtype␈α
␈↓
D␈↓␈αis␈αmade␈α
up␈αof␈αcomponents␈α
of␈αtypes␈α␈↓
D␈↓β1␈↓,␈α␈↓
D␈↓β2␈↓,␈α
␈↓
D␈↓β3␈↓,
␈↓ α←␈↓and␈α∪␈↓
D␈↓β4␈↓,␈α∪then␈α∪the␈α∪structure␈α∪of␈α∪an␈α∪algorithm␈α∪␈↓αf␈↓␈α∪operating␈α∪on␈α∪␈↓
D␈↓␈α∪typically
␈↓ α←␈↓involves␈α
calls␈αon␈α
subfunctions␈α␈↓αf␈↓β1␈↓␈α
through␈α␈↓αf␈↓β4␈↓␈α
to␈αhandle␈α
the␈αsubcomputations.
␈↓ α←␈↓Each␈α␈↓αf␈↓βi␈↓␈αwill␈αin␈αturn␈αbreak␈αup␈αits␈α␈↓
D␈↓βi␈↓.␈α Thus␈αthe␈αtype-structure␈αof␈αthe␈αcall␈αon␈α␈↓αf␈↓
␈↓ α←␈↓would be:
␈↓"∀␈↓ α←␈↓␈↓ ∧d␈↓αf[␈↓
D␈↓α] = g[f␈↓β1␈↓α[␈↓
D␈↓β1␈↓α];f␈↓β2␈↓α[␈↓
D␈↓β2␈↓α];f␈↓β3␈↓α[␈↓
D␈↓β3␈↓α];f␈↓β4␈↓α[␈↓
D␈↓β4␈↓α]]
␈↓"∀␈↓ α←␈↓This␈α_is␈α_the␈α_essence␈α→of␈α_level-wise␈α_programming:␈α_we␈α_write␈α→␈↓αf,␈α_f␈↓β1␈↓α, ... , f␈↓β4␈↓
␈↓ α←␈↓independently␈α∪of␈α∪the␈α∀representation␈α∪of␈α∪their␈α∀data␈α∪structures.␈α∪ ␈↓αf␈↓␈α∀will␈α∪run
␈↓ α←␈↓provided␈α∂that␈α⊂the␈α∂␈↓αf␈↓βi␈↓'s␈α⊂are␈α∂available.␈α⊂As␈α∂we␈α⊂write␈α∂the␈α⊂␈↓αf␈↓βi␈↓'s␈α∂we␈α⊂will␈α∂probably
␈↓ α←␈↓invoke␈α~computations␈α~on␈α~components␈α→of␈α~the␈α~corresponding␈α~␈↓
D␈↓βi␈↓.␈α→Those
␈↓ α←␈↓computations␈α
are␈α
in␈α
turn␈α
executed␈α
by␈α
subfunctions␈α
which␈α
we␈α
have␈α
to␈α
write.
␈↓ α←␈↓This␈αprocess␈αof␈αelaboration␈αterminates␈αwhen␈αall␈αsubfunctions␈αare␈αwritten␈αand
␈↓ α←␈↓all␈α
data␈αstructures␈α
have␈αreceived␈α
concrete␈α
representations.␈αIn␈α
LISP␈αthis␈α
means
␈↓ α←␈↓the␈αlowest␈αlevel␈αfunctions␈αare␈αexpressed␈αin␈αterms␈αof␈αLISP␈αprimitives␈αand␈αthe
␈↓ α←␈↓data␈αstructures␈αare␈αrepresented␈αin␈αterms␈αof␈αS-exprs.␈αThus␈αat␈αthe␈αhighest␈αlevel
␈↓ α←␈↓we␈α∞tend␈α∞to␈α∞think␈α∞of␈α∞a␈α∞data␈α∞structure␈α∞as␈α∞a␈α∞class␈α∞of␈α∞behaviors;␈α∞we␈α∞don't␈α
care
␈↓ α←␈↓about␈α∪the␈α∪internal␈α∪mechanisms␈α∀which␈α∪implement␈α∪that␈α∪behavior.␈α∀ At␈α∪the
␈↓ α←␈↓lowest␈α↔level,␈α_machine-language␈α↔routines␈α↔simulate␈α_␈↓¬one␈↓␈α↔of␈α_many␈α↔possible
␈↓ α←␈↓representations.
␈↓"β␈↓ α←␈↓␈↓ β'This␈α⊃process␈α∩of␈α⊃elaboration␈α∩of␈α⊃abstract␈α∩algorithm␈α⊃and␈α∩abstract␈α⊃data
␈↓ α←␈↓structure␈αmay␈αmodify␈αthe␈αtop-level␈αdefinition␈αof␈α␈↓αf␈↓.␈αIn␈α
reality,␈αimplementation
␈↓ α←␈↓considerations␈αmay␈α
effect␈αsome␈α
earlier␈αdecisions␈α
and␈αrequire␈α
replanning␈αof␈α
an
␈↓ α←␈↓earlier␈αstrategy.␈α At␈αthat␈αtime␈αthe␈αcomplete␈αplan␈αshould␈αbe␈αre-examined;␈αlocal
␈↓ α←␈↓modifications␈α
may␈αhave␈α
global␈αrepercussions.␈α
A␈αprogramming␈α
style␈αis␈α
not␈αa
␈↓ α←␈↓panacea;␈α∩it␈α∩is␈α∩no␈α⊃substitute␈α∩for␈α∩clear␈α∩thinking.␈α⊃ It␈α∩only␈α∩helps␈α∩control␈α⊃the
␈↓ α←␈↓complexity of the programming process.
␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 3␈↓You␈α
may␈α
have␈α∞noticed␈α
that␈α
we␈α∞are␈α
therefore␈α
dealing␈α∞with␈α
essentially
␈↓ α←␈↓"context-free"␈α∩abstract␈α⊃data␈α∩structures;␈α⊃i.e.,␈α∩those␈α⊃generated␈α∩by␈α⊃context-free
␈↓ α←␈↓grammars. See [Hop 69].
␈↓ α←␈↓␈↓56 Applications␈↓
(2.2␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧c␈↓↓2.2 Examples of LISP Applications␈↓
␈↓"β␈↓ α←␈↓The␈α∩next␈α∩few␈α∩sections␈α∩will␈α∩examine␈α∩some␈α∩non-trivial␈α∩problems␈α⊃involving
␈↓ α←␈↓computations␈α∂on␈α∂data␈α∂structures.␈α∂ We␈α∂will␈α∂describe␈α∂the␈α∂problem␈α∞intuitively,
␈↓ α←␈↓pick␈αan␈αinitial␈αrepresentation␈αfor␈αthe␈αproblem,␈αwrite␈αthe␈αLISP␈αalgorithm,␈αand
␈↓ α←␈↓in␈α_some␈α_cases␈α_"tune"␈α→the␈α_algorithm␈α_by␈α_picking␈α_"more␈α→efficient"␈α_data
␈↓ α←␈↓representations.
␈↓"β␈↓ α←␈↓␈↓ β'The examples share other important characteristics:
␈↓"λ␈↓ α←␈↓␈↓↓1.␈↓ We␈αexamine␈αthe␈αproblem␈αdomain␈αand␈αattempt␈αto␈αrepresent␈αits␈αelements␈αas
␈↓ α←␈↓␈↓ β∂data structures.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ We␈α
reflect␈α
on␈α
our␈α(intuitive)␈α
algorithm␈α
and␈α
try␈αto␈α
express␈α
it␈α
as␈αa␈α
LISP-like
␈↓ α←␈↓␈↓ β∂data-structure manipulating function.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ While␈α∀performing␈α∀␈↓↓1␈↓␈α∃and␈α∀␈↓↓2␈↓,␈α∀we␈α∀might␈α∃have␈α∀to␈α∀modify␈α∀some␈α∃of␈α∀our
␈↓ α←␈↓␈↓ β∂decisions.␈αSomething␈αassumed␈αto␈αbe␈αstructure␈αmight␈αbetter␈αbe␈αrepresented
␈↓ α←␈↓␈↓ β∂as␈α⊂algorithm,␈α⊂or␈α⊂some␈α⊂algorithm␈α⊃might␈α⊂be␈α⊂better␈α⊂repesented␈α⊂as␈α⊃a␈α⊂data
␈↓ α←␈↓␈↓ β∂structure.
␈↓" ␈↓ α←␈↓␈↓↓4.␈↓ When␈α∪the␈α∀decisions␈α∪are␈α∪made,␈α∀we␈α∪evaluate␈α∪the␈α∀LISP␈α∪function␈α∀on␈α∪a
␈↓ α←␈↓␈↓ β∂representation of a problem.
␈↓" ␈↓ α←␈↓␈↓↓5.␈↓ We reinterpret the data-structure output as an answer to our problem.
␈↓"β␈↓ α←␈↓Pictorially in terms of LISP:
␈↓"⊃␈↓ α←␈↓informal => LISP function␈↓ ¬w|
␈↓"␈↓ α←␈↓algorithm␈↓ ¬w|
␈↓"␈↓ α←␈↓␈↓ ¬w| evaluation
␈↓"␈↓ α←␈↓␈↓ ¬w|========> interpret
␈↓"␈↓ α←␈↓␈↓ ¬w| S-expr output as answer
␈↓"␈↓ α←␈↓␈↓ ¬w|
␈↓"␈↓ α←␈↓domain => S-expressions␈↓ ¬w|
␈↓"∀␈↓ α←␈↓␈↓ β'Whenever␈αwe␈αwrite␈αcomputer␈α
programs,␈αwhatever␈αlanguage␈αwe␈α
use,␈αwe
␈↓ α←␈↓always␈α⊂go␈α⊃through␈α⊂a␈α⊃similar␈α⊂representation␈α⊂problem.␈α⊃The␈α⊂process␈α⊃is␈α⊂more
␈↓ α←␈↓apparent␈α⊂in␈α⊂a␈α⊃higher-level␈α⊂language␈α⊂like␈α⊃FORTRAN␈α⊂or␈α⊂ALGOL,␈α⊃and␈α⊂is
␈↓ α←␈↓most␈α⊂noticeable␈α⊂in␈α⊂a␈α⊂language␈α⊂like␈α⊂LISP␈α⊂which␈α⊂primarily␈α⊂deals␈α⊂with␈α⊂data
␈↓ α←␈↓structures.
␈↓"β␈↓ α←␈↓␈↓ β'When␈α∂we␈α∞deal␈α∂with␈α∞numerical␈α∂algorithms,␈α∞the␈α∂representation␈α∞problem
␈↓ α←␈↓has␈αusually␈αbeen␈α
settled␈αin␈αthe␈αtransformation␈α
from␈αreal-world␈αsituation␈α
to␈αa
␈↓ α←␈↓numerical␈α⊂problem.␈α⊂One␈α∂has␈α⊂to␈α⊂think␈α∂more␈α⊂explicitly␈α⊂about␈α∂representation
␈↓ α←␈↓when␈α∞we␈α∞deal␈α∞with␈α∞structures␈α∞like␈α
arrays␈α∞or␈α∞matrices.␈α∞We␈α∞are␈α∞encoding␈α
our
␈↓ α←␈↓information␈α∪in␈α∪the␈α∀array.␈α∪But␈α∪the␈α∀preceding␈α∪diagram␈α∪occurs␈α∀within␈α∪the
␈↓ α←␈↓machine, even for strictly non-structured numerical calculation.
␈↓ α←␈↓␈↓2.2␈↓ π~Examples of LISP Applications 57␈↓
␈↓"⊃␈↓ α←␈↓numerical => machine
␈↓"␈↓ α←␈↓algorithm instructions␈↓ ¬;|
␈↓"␈↓ α←␈↓␈↓ ¬;| execution
␈↓"␈↓ α←␈↓␈↓ ¬;|========> interpret
␈↓"␈↓ α←␈↓␈↓ ¬;| binary number as answer
␈↓"␈↓ α←␈↓␈↓ ¬;|
␈↓"␈↓ α←␈↓numbers => binary␈↓ ¬;|
␈↓"␈↓ α←␈↓ representation
␈↓"∀␈↓ α←␈↓The␈αencodings␈αare␈αdone␈αby␈αthe␈αinput␈αroutines.␈αThe␈αresult␈αof␈αthe␈αexecution␈αis
␈↓ α←␈↓presented to the external world by the output routines.
␈↓"β␈↓ α←␈↓␈↓ β'However,␈α!when␈α!we␈α!come␈α!to␈α!data-structure␈α!computations,␈α the
␈↓ α←␈↓representation␈α⊃problem␈α⊃really␈α⊂becomes␈α⊃apparent.␈α⊃ We␈α⊂have␈α⊃to␈α⊃think␈α⊂more
␈↓ α←␈↓about␈α∞what␈α∞we␈α∞are␈α∞doing␈α∞since␈α∞we␈α∞lack␈α∞certain␈α∞preconceptions␈α∞or␈α
intuitions
␈↓ α←␈↓about␈αsuch␈αcomputations.␈αMore␈αimportantly,␈αwe␈αare␈αtrying␈αto␈αrepresent␈αactual
␈↓ α←␈↓problems␈α∞␈↓¬directly␈↓␈α∂as␈α∞machine␈α∞problems.␈α∂We␈α∞do␈α∞not␈α∂attempt␈α∞to␈α∂first␈α∞analyze
␈↓ α←␈↓them␈α∂into␈α∂a␈α∂complex␈α∂mathematical␈α∂theory,␈α∂but␈α∂try␈α∂to␈α∂express␈α⊂our␈α∂intuitive
␈↓ α←␈↓theory␈α
directly␈α
as␈αmanipulations␈α
of␈α
data-structures.␈α This␈α
is␈α
a␈α
different␈αkind
␈↓ α←␈↓of␈α⊃thinking,␈α⊂due␈α⊃wholly␈α⊂to␈α⊃the␈α⊃advent␈α⊂of␈α⊃computers.␈α⊂ Indeed␈α⊃the␈α⊃field␈α⊂of
␈↓ α←␈↓computation␈αhas␈αexpanded␈α
so␈αmuch␈αas␈αto␈α
make␈αthe␈αterm␈α"computer"␈α
obsolete.
␈↓ α←␈↓"Structure␈α⊃processor"␈α∩is␈α⊃more␈α∩indicative␈α⊃of␈α∩the␈α⊃proper␈α∩level␈α⊃at␈α∩which␈α⊃we
␈↓ α←␈↓should view "computers".
␈↓"β␈↓ α←␈↓␈↓ β'We␈αhave␈αalready␈α
seen␈αa␈αsimple␈α
example␈αof␈αthe␈α
representation␈αproblem
␈↓ α←␈↓in the discussion of list-notation beginning in Section 1.6.
␈↓"⊃␈↓ α←␈↓sequence␈↓ ¬w|
␈↓"␈↓ α←␈↓ algorithm => LISP function␈↓ ¬w|
␈↓"␈↓ α←␈↓␈↓ ¬w| evaluation
␈↓"␈↓ α←␈↓␈↓ ¬w|=========> interpret
␈↓"␈↓ α←␈↓␈↓ ¬w| S-expr result as answer.
␈↓"␈↓ α←␈↓␈↓ ¬w|
␈↓"␈↓ α←␈↓sequence␈↓ ¬w|
␈↓"␈↓ α←␈↓ expression => S-expression␈↓ ¬w|
␈↓"β␈↓ α←␈↓␈↓ β'The␈α↔following␈α↔sections␈α_deal␈α↔with␈α↔representation␈α↔of␈α_complex␈α↔data
␈↓ α←␈↓structure problems in LISP.
␈↓"β␈↓ α←␈↓␈↓ ¬T␈↓↓2.3 Differentiation␈↓
␈↓"β␈↓ α←␈↓This␈α→example␈α→will␈α→describe␈α→a␈α→rudimentary␈α→differentiation␈α→routine␈α→for
␈↓ α←␈↓polynomials␈α⊂in␈α⊂several␈α∂variables.␈α⊂ We␈α⊂will␈α∂develop␈α⊂this␈α⊂algorithm␈α∂through
␈↓ α←␈↓several␈α&stages.␈α&We␈α&will␈α'begin␈α&by␈α&doing␈α&a␈α&very␈α'direct,␈α&but
␈↓ α←␈↓representation-dependent,␈α∩implementation.␈α∩ We␈α∩will␈α∩encode␈α∩polynomials␈α⊃as
␈↓ α←␈↓special␈α∞LISP␈α∂lists␈α∞and␈α∞will␈α∂express␈α∞the␈α∞differentiation␈α∂algorithm␈α∞as␈α∂a␈α∞LISP
␈↓ α←␈↓program␈αoperating␈αon␈αthat␈αrepresentation.␈α When␈αthis␈αprogram␈αis␈αcompletely
␈↓ α←␈↓␈↓58 Applications␈↓
(2.3␈↓
␈↓"β␈↓ α←␈↓specified␈α∞we␈α
will␈α∞then␈α
scrutinize␈α∞it,␈α
attempting␈α∞to␈α
see␈α∞just␈α
how␈α∞much␈α∞of␈α
the
␈↓ α←␈↓program␈α∞and␈α∞data␈α∂structure␈α∞is␈α∞␈↓¬representation␈↓␈α∞and␈α∂how␈α∞much␈α∞is␈α∂essential␈α∞to
␈↓ α←␈↓the algorithm.
␈↓"β␈↓ α←␈↓␈↓ β'You␈αshould␈α
recognize␈αtwo␈α
facts␈αabout␈α
the␈αdifferentiation␈α
algorithm␈αfor
␈↓ α←␈↓polynomials:␈α⊗first,␈α⊗the␈α⊗algorithm␈α⊗operates␈α⊗on␈α⊗forms␈α⊗(or␈α↔expressions)␈α⊗as
␈↓ α←␈↓arguments␈αand␈αreturns␈α
forms␈αas␈αvalues.␈α
Previously␈αdiscussed␈αalgorithms␈α
have
␈↓ α←␈↓operated␈α∞on␈α∞simple␈α∞values␈α
and␈α∞produced␈α∞simple␈α∞values.␈α∞The␈α
differentiation
␈↓ α←␈↓algorithm␈αtakes␈α
expressions␈αas␈α
arguments␈αand␈αproduces␈α
a␈αnew␈α
expression␈αas
␈↓ α←␈↓value.␈α∂ Second,␈α∂you␈α∂should␈α∂realize␈α∂that␈α∂the␈α∂algorithm␈α∂for␈α∂differentiation␈α∂is
␈↓ α←␈↓␈↓¬recursive␈↓.␈α
The␈αquestion␈α
of␈αdifferentiating␈α
a␈αsum␈α
is␈αreduced␈α
to␈αthe␈α
ability␈αto
␈↓ α←␈↓differentiate␈α~each␈α~summand.␈α~ Similar␈α~relationships␈α~hold␈α~for␈α→products,
␈↓ α←␈↓differences,␈α_and␈α_powers.␈α_ There␈α↔must␈α_be␈α_some␈α_termination␈α↔conditions.
␈↓ α←␈↓Differentiation␈α∂of␈α∂a␈α∂variable,␈α∂say␈α∂␈↓αx␈↓,␈α∂with␈α∞respect␈α∂to␈α∂␈↓αx␈↓␈α∂is␈α∂defined␈α∂to␈α∂be␈α∞the
␈↓ α←␈↓number␈α⊂one;␈α⊂differentiating␈α⊂a␈α⊂constant,␈α⊂or␈α∂a␈α⊂variable␈α⊂not␈α⊂equal␈α⊂to␈α⊂␈↓αx␈↓␈α∂with
␈↓ α←␈↓respect␈α_to␈α↔␈↓αx␈↓␈α_gives␈α_a␈α↔result␈α_of␈α↔zero.␈α_ This␈α_begins␈α↔to␈α_sound␈α_like␈α↔the
␈↓ α←␈↓␈↓↓IND␈↓-definitions␈α
of␈α
sets␈α(in␈α
this␈α
case␈α
the␈αset␈α
of␈α
polynomials)␈α
and␈αthe␈α
associated
␈↓ α←␈↓␈↓↓REC␈↓-definitions␈α
of␈α
algorithms␈α
(in␈αthis␈α
case␈α
differentiation␈α
of␈αpolynomials).␈α
If
␈↓ α←␈↓this␈α
␈↓¬is␈↓␈α
the␈α
mold␈α
into␈α
which␈α
our␈α
current␈α
problem␈α
fits,␈α
then␈α
we␈α
must␈α
give␈α
an
␈↓ α←␈↓inductive␈α
definition␈α
of␈α
our␈α
set␈α
of␈α
polynomials.␈α
Though␈α
polynomials␈α∞can␈α
be
␈↓ α←␈↓arbitrarily␈α∀complex,␈α∃involving␈α∀the␈α∃operations␈α∀of␈α∃addition,␈α∀multiplication,
␈↓ α←␈↓negation,␈αand␈αexponentiation,␈αtheir␈αgeneral␈αformat␈αis␈αvery␈αsimple␈αif␈α
they␈αare
␈↓ α←␈↓described␈α_in␈α↔our␈α_LISP-like␈α↔notation␈α_where␈α↔the␈α_operation␈α_precedes␈α↔its
␈↓ α←␈↓operands.␈α↔We␈α⊗assume␈α↔that␈α⊗binary␈α↔plus,␈α⊗times,␈α↔and␈α↔exponentiation␈α⊗are
␈↓ α←␈↓symbolized␈α⊂by␈α⊂+,␈α⊂*,␈α⊂and␈α⊂↑;␈α⊂we␈α⊂will␈α⊂write␈α⊂␈↓α+[x;2]␈↓␈α⊂instead␈α⊂of␈α⊂the␈α⊂usual␈α⊂infix
␈↓ α←␈↓notation␈α⊗␈↓αx+2␈↓.␈α∃ The␈α⊗general␈α⊗term␈α∃for␈α⊗this␈α∃LISP-like␈α⊗notation␈α⊗is␈α∃␈↓↓prefix
␈↓ α←␈↓↓notation␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Here are some examples of infix and prefix representations:
␈↓"∀␈↓ α←␈↓␈↓ ¬;␈↓↓infix␈↓ εoprefix
␈↓" ␈↓ α←␈↓α␈↓ ¬;x*z+2y␈↓ εo+[*[x;z]; *[2;y]]
␈↓"β␈↓ α←␈↓α␈↓ ¬;x*y*z␈↓ εo*[x;*[y;z]]
␈↓"∀␈↓ α←␈↓␈↓ β'We␈αnow␈αgive␈αan␈αinductive␈αdefinition␈αof␈αthe␈αset␈αof␈αpolynomials␈αwe␈αwish
␈↓ α←␈↓to consider. The definition will involve an inductive definition of terms.
␈↓"λ␈↓ α←␈↓␈↓↓1.␈↓ Any term is a polynomial.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ If p␈↓β1␈↓ and p␈↓β2␈↓ are polynomials then the "sum" of p␈↓β1␈↓ and p␈↓β2␈↓ is a polynomial.
␈↓"β␈↓ α←␈↓where:
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ Constants and variables are terms.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ If t␈↓β1␈↓ and t␈↓β2␈↓ are terms then the "product" of t␈↓β1␈↓ and t␈↓β2␈↓ is a term.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ If␈αt␈↓β1␈↓␈αis␈αa␈αvariable␈αand␈αt␈↓β2␈↓␈αis␈αa␈αconstant␈αthen␈α"t␈↓β1␈↓␈αraised␈αto␈αthe␈αt␈↓β2␈↓␈↓πth␈↓␈αpower"␈αis
␈↓ α←␈↓␈↓ β∂a term.
␈↓" ␈↓ α←␈↓␈↓↓4.␈↓ If t␈↓β1␈↓ is a term then "minus" t␈↓β1␈↓ is a term.
␈↓ α←␈↓␈↓2.3␈↓ λPDifferentiation 59␈↓
␈↓"β␈↓ α←␈↓We␈αnow␈αgive␈αa␈αBNF␈αdescription␈αof␈αthe␈αabove␈αset␈αusing␈αthe␈αsyntax␈αof␈αprefix
␈↓ α←␈↓notation:
␈↓"∀␈↓ α←␈↓<poly>␈↓ βo::= <term> | <plus>[<poly>;<poly>]
␈↓" ␈↓ α←␈↓<term>␈↓ βo::= <constant>
␈↓"β␈↓ α←␈↓␈↓ βo::= <variable>
␈↓"β␈↓ α←␈↓␈↓ βo::= <times>[<term>;<term>]
␈↓"β␈↓ α←␈↓␈↓ βo::= <expt>[<variable>;<constant>]
␈↓"β␈↓ α←␈↓␈↓ βo::= <minus><term>
␈↓" ␈↓ α←␈↓<constant>␈↓ βo::= <numeral>
␈↓" ␈↓ α←␈↓<plus>␈↓ βo::= +
␈↓" ␈↓ α←␈↓<times>␈↓ βo::= *
␈↓" ␈↓ α←␈↓<expt>␈↓ βo::= ↑
␈↓" ␈↓ α←␈↓<minus>␈↓ βo::= -
␈↓" ␈↓ α←␈↓<variable>␈↓ βo::= <identifier>
␈↓"∀␈↓ α←␈↓␈↓ β'It␈αis␈αeasy␈α
to␈αwrite␈αrecursive␈αalgorithms␈α
in␈αLISP;␈αthe␈αonly␈α
problem␈αhere
␈↓ α←␈↓is␈α⊗that␈α⊗the␈α⊗domain␈α⊗and␈α⊗range␈α⊗of␈α⊗LISP␈α⊗functions␈α⊗is␈α⊗S-exprs,␈α⊗not␈α⊗the
␈↓ α←␈↓polynomials.␈α⊂ We␈α⊂need␈α⊂to␈α⊂represent␈α⊂arbitrary␈α⊂polynomials␈α⊂as␈α⊂S-exprs.␈α⊂ We
␈↓ α←␈↓will do the representation in lists rather than S-exprs.
␈↓"β␈↓ α←␈↓␈↓ β'Let␈α
␈↓
R␈↓␈α∞be␈α
a␈α∞function␈α
mapping␈α
polynomals␈α∞to␈α
their␈α∞representation␈α
such
␈↓ α←␈↓that␈α
a␈αvariable␈α
is␈αmapped␈α
to␈α
its␈αuppercase␈α
counterpart␈αin␈α
the␈α
vocabulary␈αof
␈↓ α←␈↓LISP atoms. Thus:
␈↓"∀␈↓ α←␈↓␈↓ ¬α␈↓
R␈↓∞(␈↓<variable>␈↓∞)␈↓ = <literal atom>
␈↓"∀␈↓ α←␈↓␈↓ β'Let␈α∩constants␈α⊃(numerals),␈α∩be␈α⊃just␈α∩the␈α⊃LISP␈α∩numerals;␈α⊃these␈α∩are␈α⊃also
␈↓ α←␈↓respectable LISP atoms. Thus:
␈↓"∀␈↓ α←␈↓␈↓ ¬⊗␈↓
R␈↓␈↓∞(␈↓<numeral>␈↓∞)␈↓ = <numeral>
␈↓"∀␈↓ α←␈↓We␈α∃have␈α∃now␈α∃specified␈α∃a␈α∃representation␈α∃for␈α∃the␈α∃base␈α∃domains␈α∃of␈α∃the
␈↓ α←␈↓inductive␈αdefinition␈αof␈αour␈αpolynomials.␈αIt␈αis␈αtime␈αto␈αdevelop␈αthe␈αtermination
␈↓ α←␈↓cases for the recursive definition of differentiation.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αknow␈αfrom␈αdifferential␈αcalculus␈αthat␈α
if␈α␈↓αu␈↓␈αis␈αa␈αconstant␈αor␈αa␈α
variable
␈↓ α←␈↓then:
␈↓"∀␈↓ α←␈↓␈↓ βKd␈↓αu␈↓/d␈↓αx␈↓ =␈↓ ∧C1 if ␈↓αx = u
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧C␈↓0 otherwise
␈↓"∀␈↓ α←␈↓We␈α
will␈α
represent␈α
the␈α
d-operator␈αas␈α
a␈α
binary␈α
LISP␈α
function␈α
named␈α␈↓αdiff␈↓.␈α
The
␈↓ α←␈↓application,␈α∪d␈↓αu␈↓/d␈↓αx␈↓␈α∀will␈α∪be␈α∀represented␈α∪as␈α∀␈↓αdiff[u;x]␈↓.␈α∪ Since␈α∀constants␈α∪and
␈↓ α←␈↓variables␈αare␈αboth␈αrepresented␈αas␈αatoms,␈α
we␈αcan␈αcheck␈αfor␈αboth␈αof␈αthese␈α
cases
␈↓ α←␈↓by␈α⊃using␈α⊃the␈α⊃predicate␈α⊃␈↓αisindiv␈↓.␈α⊂ Thus␈α⊃a␈α⊃representation␈α⊃of␈α⊃the␈α⊂termination
␈↓ α←␈↓cases might be:
␈↓"∀␈↓ α←␈↓␈↓ βK␈↓αdiff[u;x] <= [isindiv[u] → [eq[x;u] → 1; ␈↓
t␈↓α → 0] ... ]␈↓
␈↓"∀␈↓ α←␈↓Notice␈αwe␈αwrite␈αthe␈αabbreviation,␈α␈↓αisindiv␈↓␈αinstead␈αof␈α␈↓αisindiv␈↓βr␈↓.␈α You␈α
should␈αbe
␈↓ α←␈↓␈↓60 Applications␈↓
(2.3␈↓
␈↓"β␈↓ α←␈↓a bit wary of our definition already: ␈↓αdiff[1;1]␈↓ will evaluate to ␈↓α1␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Now␈αthat␈αwe␈αhave␈αcovered␈α
the␈αtermination␈αcase,␈αwhat␈αcan␈αbe␈α
done␈αfor
␈↓ α←␈↓the␈αrepresentation␈αof␈αthe␈αremaining␈αclass␈αof␈αterms␈αand␈αpolynomials?␈α
That␈αis,
␈↓ α←␈↓how should we represent sums and products?
␈↓"β␈↓ α←␈↓␈↓ β'First, we will represent the operations *, +, -, and ↑ as atoms:
␈↓"∀␈↓ α←␈↓␈↓ ¬←␈↓
R␈↓␈↓∞(␈↓ + ␈↓∞)␈↓ = ␈↓αPLUS␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬←␈↓
R␈↓␈↓∞(␈↓ * ␈↓∞)␈↓ = ␈↓αTIMES␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬←␈↓
R␈↓␈↓∞(␈↓ - ␈↓∞)␈↓ = ␈↓αMINUS␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬←␈↓
R␈↓␈↓∞(␈↓ ↑ ␈↓∞)␈↓ = ␈↓αEXPT␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'We␈αwill␈αnow␈αextend␈αthe␈αmapping␈α␈↓
R␈↓␈αto␈αoccurrences␈αof␈αbinary␈αoperators
␈↓ α←␈↓by mapping to three-element lists:
␈↓"∀␈↓ α←␈↓␈↓ ∧.␈↓
R␈↓␈↓∞(␈↓α ␈↓λα␈↓α[␈↓λβ␈↓β1␈↓α;␈↓λβ␈↓β2␈↓α] ␈↓∞)␈↓ = ␈↓α(␈↓
R␈↓∞(␈↓λα␈↓∞)␈↓α, ␈↓
R␈↓∞(␈↓λβ␈↓β1␈↓∞)␈↓, ␈↓
R␈↓∞(␈↓λβ␈↓β2␈↓∞)␈↓α)
␈↓"∀␈↓ α←␈↓Unary applications will result in two-element lists:
␈↓"∀␈↓ α←␈↓␈↓ ¬␈↓
R␈↓␈↓∞(␈↓α ␈↓λα␈↓α[␈↓λβ␈↓α] ␈↓∞)␈↓ = ␈↓α(␈↓
R␈↓∞(␈↓λα␈↓∞)␈↓α, ␈↓
R␈↓∞(␈↓λβ␈↓∞)␈↓α)
␈↓"∀␈↓ α←␈↓For example:␈↓ ¬∃␈↓
R␈↓∞(␈↓α +[x; 2] ␈↓∞)␈↓ = ␈↓α(PLUS X 2)␈↓
␈↓"β␈↓ α←␈↓For a more complicated example, the polynomial
␈↓"∀␈↓ α←␈↓α␈↓ ε
x␈↓π2␈↓α + 2yz + u
␈↓"∀␈↓ α←␈↓will be translated to the following prefix notation:
␈↓"∀␈↓ α←␈↓α␈↓ ¬⊂+[↑[x;2]; +[*[2;*[y;z]]; u]] ␈↓π 4␈↓α
␈↓"∀␈↓ α←␈↓From this it's easy to get the list form:
␈↓"∀␈↓ α←␈↓α␈↓ βK(PLUS (EXPT X 2) (PLUS (TIMES 2 (TIMES Y Z)) U))
␈↓"∀␈↓ α←␈↓Now we can complete the differentiation algorithm for + and *. We know:
␈↓"∀␈↓ α←␈↓␈↓ βKd[␈↓αf + g␈↓]/d␈↓αx␈↓ = d␈↓αf/␈↓d␈↓αx + ␈↓d␈↓αg␈↓/d␈↓αx.
␈↓"∀␈↓ α←␈↓Expressing this phrase as part of ␈↓αdiff␈↓,
␈↓" ␈↓ α←␈↓we would see: ␈↓αu = ␈↓
R␈↓∞(␈↓α f + g ␈↓∞)␈↓ = ␈↓α(PLUS, ␈↓
R␈↓∞(␈↓α f ␈↓∞)␈↓α, ␈↓
R␈↓∞( ␈↓αg ␈↓∞)␈↓α)␈↓
␈↓" ␈↓ α←␈↓where:␈↓ βd␈↓αsecond[u] = ␈↓
R␈↓∞( ␈↓αf ␈↓∞)␈↓ and, ␈↓α third[u] = ␈↓
R␈↓∞(␈↓α g ␈↓∞)␈↓ ␈↓π 5␈↓
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 4␈↓This␈α∞is␈α∞messier␈α∞than␈α∞it␈α∞really␈α∞needs␈α∞to␈α∞be␈α∞because␈α∞we␈α∞assume␈α∞that␈α∞+
␈↓ α←␈↓and␈α
*␈αare␈α
binary.␈α
You␈αshould␈α
also␈αnotice␈α
that␈α
our␈α␈↓
R␈↓-mapping␈α
is␈αapplicable␈α
to
␈↓ α←␈↓a larger class of expressions than just <poly>. Look at ␈↓α(x + y)*(z + 2).
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 5␈↓As␈α
we␈αintimated␈α
earlier,␈α
we␈αhave␈α
entered␈α
an␈αunwise␈α
course␈α
here.␈αWe
␈↓ α←␈↓have␈α≥tied␈α≡the␈α≥algorithm␈α≡for␈α≥symbolic␈α≡differentiation␈α≥to␈α≡a␈α≥specific
␈↓ α←␈↓representation␈α⊃for␈α⊃polynomials.␈α⊃Believing␈α⊃that␈α⊃much␈α⊃can␈α⊃be␈α⊃learned␈α⊃from
␈↓ α←␈↓seeing␈α∩mistakes,␈α∩we␈α⊃will␈α∩use␈α∩that␈α∩representation,␈α⊃and␈α∩on␈α∩page 62␈α∩we␈α⊃will
␈↓ α←␈↓examine our decision.
␈↓ α←␈↓␈↓2.3␈↓ λPDifferentiation 61␈↓
␈↓"β␈↓ α←␈↓The result of differentiating ␈↓αu␈↓ is to be a new list of three elements:
␈↓"λ␈↓ α←␈↓␈↓↓1.␈↓ The symbol ␈↓αPLUS␈↓.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ The effect of ␈↓αdiff␈↓ operating ␈↓
R␈↓∞( ␈↓αf ␈↓∞)␈↓
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ The effect of ␈↓αdiff␈↓ operating ␈↓
R␈↓∞( ␈↓αg ␈↓∞)␈↓
␈↓"β␈↓ α←␈↓Thus another part of the algorithm:
␈↓"∀␈↓ α←␈↓α␈↓ β.eq[first[u];PLUS] → list [PLUS; diff[second[u];x];diff[third[u];x]]
␈↓"∀␈↓ α←␈↓α␈↓ β'␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'd[␈↓αf*g]␈↓/d␈↓αx␈↓ is defined to be ␈↓αf* ␈↓d␈↓αg␈↓/d␈↓αx + g *␈↓d␈↓αf/␈↓d␈↓αx␈↓.
␈↓"∀␈↓ α←␈↓So here's another part of ␈↓αdiff␈↓: ␈↓α
␈↓"∀␈↓ α←␈↓α␈↓ βKeq[first[u];TIMES] →␈↓ ¬wlist[␈↓ ε'PLUS;
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ¬w␈↓ ε'list[TIMES; second[u];diff[third[u];x]];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ¬w␈↓ ε'list[TIMES;third[u];diff[second[u];x]]]
␈↓"∀␈↓ α←␈↓Finally, here's an example. We know:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ¬Rd[␈↓αx*y + x]␈↓/d␈↓αx = y + 1␈↓
␈↓"∀␈↓ α←␈↓Try: ␈↓α
␈↓"∀␈↓ α←␈↓αdiff [(PLUS (TIMES X Y) X); X]
␈↓"β␈↓ α←␈↓α␈↓ βK= list[PLUS; diff[(TIMES X Y); X];diff[X;X]]
␈↓"β␈↓ α←␈↓α␈↓ βK= list[␈↓ ∧∨PLUS;
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧∨list[␈↓ ∧OPLUS;
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧∨␈↓ ∧Olist[TIMES; X; diff[Y;X]];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧∨␈↓ ∧Olist[TIMES; Y; diff[X;X]]];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧∨diff[X;X]]
␈↓"β␈↓ α←␈↓α␈↓ βK= list[␈↓ ∧∨PLUS;
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧∨list[␈↓ ∧OPLUS;
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧∨␈↓ ∧Olist[TIMES; X ;0];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧∨␈↓ ∧Olist[TIMES; Y;1]];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧∨1 ]
␈↓"∀␈↓ α←␈↓α␈↓ βK=(PLUS (PLUS (TIMES X 0) (TIMES Y 1)) 1)
␈↓ α←␈↓␈↓62 Applications␈↓
(2.3␈↓
␈↓"∀␈↓ α←␈↓which can be interpreted as:
␈↓"∀␈↓ α←␈↓␈↓α␈↓ εx*0 + y*1 + 1 ␈↓
␈↓"∀␈↓ α←␈↓Now␈α
it␈α
is␈α
clear␈α
that␈αwe␈α
have␈α
the␈α
right␈α
answer;␈αit␈α
is␈α
equally␈α
clear␈α
that␈αthe␈α
final
␈↓ α←␈↓representation␈α
leaves␈α∞much␈α
to␈α
be␈α∞desired.␈α
There␈α
are␈α∞obvious␈α
simplifications
␈↓ α←␈↓which␈α⊂would␈α⊂have␈α⊂done␈α⊂before␈α⊂we␈α⊂would␈α⊂consider␈α⊂this␈α⊂output␈α⊂acceptable.
␈↓ α←␈↓This␈α∂example␈α∞is␈α∂a␈α∞particularly␈α∂simple␈α∞case␈α∂for␈α∞algebraic␈α∂simplification.␈α∞We
␈↓ α←␈↓can␈α∃easily␈α∃write␈α∃a␈α∃LISP␈α∃program␈α∃to␈α∃perform␈α∃simplifications␈α⊗like␈α∃those
␈↓ α←␈↓expected␈α
here:␈α
like␈α
replacing␈α
␈↓α0*x␈↓␈α
by␈α␈↓α0␈↓,␈α
and␈α
␈↓αx*1␈↓␈α
by␈α
␈↓αx␈↓.␈α
But␈α
the␈αgeneral␈α
problem
␈↓ α←␈↓of␈α
writing␈α
simplifiers,␈α
or␈α
indeed␈α
of␈α
recognizing␈α
what␈α
is␈α
a␈α
"simplification",␈α
is
␈↓ α←␈↓quite␈α∞difficult.␈α∞ A␈α
whole␈α∞branch␈α∞of␈α∞computer␈α
science␈α∞has␈α∞grown␈α∞up␈α
around
␈↓ α←␈↓symbolic␈αand␈αalgebraic␈αmanipulation␈αof␈αexpressions.␈αOne␈αof␈αthe␈αcrucial␈αparts
␈↓ α←␈↓of␈α∩such␈α⊃an␈α∩endeavor␈α∩is␈α⊃a␈α∩sophisticated␈α∩simplifier.␈α⊃ For␈α∩more␈α∩details␈α⊃and
␈↓ α←␈↓examples of the power of such systems see [Hea 68], [MAC 74], or [Mos 74].
␈↓"β␈↓ α←␈↓␈↓ ¬x␈↓↓Points to note␈↓
␈↓"∀␈↓ α←␈↓This␈α⊗problem␈α⊗of␈α⊗representation␈α⊗is␈α⊗typical␈α⊗of␈α⊗data␈α↔structure␈α⊗algorithms
␈↓ α←␈↓regardless␈α
of␈α
what␈α
language␈α
you␈α
use.␈α
That␈α
is,␈α
once␈α
you␈α
have␈α
decided␈α
what
␈↓ α←␈↓the␈α
informal␈αalgorithm␈α
is,␈αpick␈α
a␈α
representation␈αwhich␈α
makes␈αyour␈α
algorithms
␈↓ α←␈↓clean.␈α∞Examine␈α∞the␈α
interplay␈α∞between␈α∞the␈α
algorithm␈α∞and␈α∞the␈α
representation,
␈↓ α←␈↓and␈α∩continue␈α∩to␈α∪examine␈α∩your␈α∩decisions␈α∩as␈α∪you␈α∩refine␈α∩your␈α∪method.␈α∩ In
␈↓ α←␈↓Section 2.6␈α∞we␈α∞will␈α
see␈α∞a␈α∞series␈α∞of␈α
representations,␈α∞each␈α∞becoming␈α∞more␈α
and
␈↓ α←␈↓more␈α∂"efficient"␈α∂and␈α∂each␈α∂requiring␈α∞more␈α∂"knowledge"␈α∂being␈α∂built␈α∂into␈α∞the
␈↓ α←␈↓algorithm.␈α The␈αremainder␈αof␈αthis␈αsection␈αwill␈αreexamine␈αour␈αrepresentations
␈↓ α←␈↓in the differentiation algorithm.
␈↓"β␈↓ α←␈↓␈↓ β'First, here is the complete ␈↓αdiff␈↓ algorithm for + and *: ␈↓α
␈↓"∀␈↓ α←␈↓αdiff[u;x] <=␈↓ ∧π[isindiv[u] → [eq[x;u] → 1; ␈↓
t␈↓α → 0];
␈↓"β␈↓ α←␈↓α␈↓ ∧π eq[first [u]; PLUS] → list␈↓ εW[PLUS;
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ εW diff[second[u]; x];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ εW diff[third[u]; x]];
␈↓"β␈↓ α←␈↓α␈↓ ∧π eq[first[u]; TIMES] → list[␈↓ εoPLUS;
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ εW␈↓ εolist[␈↓ π∨TIMES;
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ εW␈↓ εo␈↓ π∨second[u];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ εW␈↓ εo␈↓ π∨diff[third[u]; x]];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ εW␈↓ εolist[␈↓ π∨TIMES;
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ εW␈↓ εo␈↓ π∨third[u];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ εW␈↓ εo␈↓ π∨diff[second[u]; x]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧π ␈↓
t␈↓α → ␈↓λB␈↓α] ␈↓π 6␈↓α
␈↓"∀␈↓ α←␈↓As␈αwe␈αmentioned␈αearlier,␈αthe␈αcurrent␈αmanifestation␈αof␈α␈↓αdiff␈↓␈αencodes␈αtoo␈αmuch
␈↓ α←␈↓of␈αour␈α
particular␈αrepresentation␈α
for␈αpolynomials.␈α
The␈αseparation␈αof␈α
algorithm
␈↓ α←␈↓from␈α↔representation␈α↔is␈α↔beneficial␈α⊗from␈α↔at␈α↔least␈α↔two␈α↔standpoints.␈α⊗ First,
␈↓ α←␈↓changing␈αrepresentation␈αshould␈αhave␈αa␈α
minimal␈αeffect␈αon␈αthe␈αstructure␈αof␈α
the
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 6␈↓The element ␈↓λB␈↓ is not strictly part of LISP.
␈↓ α←␈↓␈↓2.3␈↓ λPDifferentiation 63␈↓
␈↓"β␈↓ α←␈↓algorithm;␈αbut␈α␈↓αdiff␈↓␈αknows␈αthat␈αvariables␈αare␈αrepresented␈αas␈αatoms␈αand␈αknows
␈↓ α←␈↓that␈α∀a␈α∪sum␈α∀is␈α∪represented␈α∀as␈α∀a␈α∪list␈α∀whose␈α∪␈↓αfirst␈↓-part␈α∀is␈α∀␈↓αPLUS␈↓.␈α∪ Second,
␈↓ α←␈↓readability␈α
of␈α
the␈α
algorithm␈α
suffers␈α
greatly.␈α
How␈α
much␈α
of␈α
␈↓αdiff␈↓␈α
really␈α
needs
␈↓ α←␈↓to␈αknow␈αabout␈α
the␈αrepresentation␈αand␈α
how␈αcan␈αwe␈α
improve␈αthe␈αreadability␈α
of
␈↓ α←␈↓␈↓αdiff␈↓?
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
uses␈αof␈α
␈↓αfirst␈↓,␈α␈↓αsecond␈↓,␈α
and␈α␈↓αthird␈↓␈α
are␈αnot␈α
particularly␈αmnemonic.␈↓π 7␈↓␈α
We
␈↓ α←␈↓used␈α␈↓αsecond␈↓␈α
to␈αget␈αthe␈α
first␈αargument␈αto␈α
a␈αsum␈αor␈α
product␈αand␈αused␈α
␈↓αthird␈↓␈αto
␈↓ α←␈↓get␈αthe␈αsecond.␈α We␈αused␈α␈↓αfirst␈↓␈αto␈αextract␈αthe␈αoperator.␈α However␈α␈↓αfirst␈↓,␈α␈↓αsecond␈↓,
␈↓ α←␈↓and␈α→␈↓αthird␈↓␈α~select␈α→components␈α~of␈α→sequences;␈α~they␈α→know␈α~nothing␈α→about
␈↓ α←␈↓polynomials. We want to refer to polynomials as abstract data structures.
␈↓"β␈↓ α←␈↓Let's define the selectors:
␈↓"∀␈↓ α←␈↓α␈↓ ¬wop[x] <= first[x]
␈↓" ␈↓ α←␈↓α␈↓ ¬warg␈↓β1␈↓α[x] <= second[x]
␈↓" ␈↓ α←␈↓α␈↓ ¬warg␈↓β2␈↓α[x] <= third[x]
␈↓"∀␈↓ α←␈↓Then ␈↓αdiff␈↓ becomes:
␈↓"∀␈↓ α←␈↓αdiff[u;x] <=␈↓ ∧π[isindiv[u] → [eq[x;u] → 1; ␈↓
t␈↓α → 0];
␈↓"β␈↓ α←␈↓α␈↓ ∧π eq[op[u]; PLUS] → list␈↓ εK[PLUS;
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ εK diff[arg␈↓β1␈↓α[u]; x];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ εK diff[arg␈↓β2␈↓α[u]; x]];
␈↓"β␈↓ α←␈↓α␈↓ ∧π eq[op[u]; TIMES] → list␈↓ εK[PLUS;
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ εK list␈↓ ε{[TIMES;
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ εK␈↓ ε{ arg␈↓β1␈↓α[u];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ εK␈↓ ε{ diff[arg␈↓β2␈↓α[u]; x]];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ εK list␈↓ ε{[TIMES;
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ εK␈↓ ε{ arg␈↓β2␈↓α[u];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ εK␈↓ ε{ diff[arg␈↓β1␈↓α[u]; x]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧π ␈↓
t␈↓α → ␈↓λB␈↓α]
␈↓"∀␈↓ α←␈↓Still,␈α∂there␈α∂is␈α⊂much␈α∂of␈α∂the␈α⊂representation␈α∂present.␈α∂Recognition␈α⊂of␈α∂variables
␈↓ α←␈↓and␈αother␈αterms␈αcan␈αbe␈αabstracted.␈αWe␈αneed␈αonly␈αrecognize␈αwhen␈αa␈αterm␈αis␈αa
␈↓ α←␈↓sum,␈α∂a␈α∂product,␈α∂a␈α∂variable␈α∂or␈α⊂a␈α∂constant.␈α∂ To␈α∂test␈α∂for␈α∂the␈α∂occurrence␈α⊂of␈α∂a
␈↓ α←␈↓numeral␈α∩we␈α∩shall␈α∩assume␈α∩a␈α∩unary␈α∩LISP␈α∩predicate␈α∩called␈α∪␈↓αnumberp␈↓␈α∩which
␈↓ α←␈↓returns␈α␈↓
t␈↓␈αjust␈α
in␈αthe␈αcase␈αthat␈α
its␈αargument␈αis␈αa␈α
numeral.␈α Then,␈αin␈α
terms␈αof
␈↓ α←␈↓the␈α∞current␈α∞representation,␈α∞we␈α∂could␈α∞define␈α∞such␈α∞recognizers␈α∂and␈α∞predicates
␈↓ α←␈↓as:
␈↓"⊂␈↓ α←␈↓α␈↓ ¬issum[x] <= eq[op[x];PLUS]
␈↓"⊃␈↓ α←␈↓α␈↓ ¬isprod[x] <= eq[op[x];TIMES]
␈↓"⊃␈↓ α←␈↓α␈↓ ¬isconst[x] <= numberp[x]
␈↓"⊃␈↓ α←␈↓α␈↓ ¬isvar[x] <= [isindiv[x] → not[isconst[x]]; ␈↓
t␈↓α → ␈↓
f␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ ¬samevar[x;y] <= eq[x;y]␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 7␈↓However, they are more readable than ␈↓αcar-cdr␈↓-chains.
␈↓ α←␈↓␈↓64 Applications␈↓
(2.3␈↓
␈↓"β␈↓ α←␈↓Now we can rewrite ␈↓αdiff␈↓ as:
␈↓"∀␈↓ α←␈↓αdiff[u;x] <=␈↓ ∧π[isvar[u] → [samevar[x;u] → 1; ␈↓
t␈↓α → 0];
␈↓"β␈↓ α←␈↓α␈↓ ∧π isconst[u] → 0;
␈↓"β␈↓ α←␈↓α␈↓ ∧π issum[u] → list[␈↓ ¬←PLUS;
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ¬←diff[arg␈↓β1␈↓α[u]; x];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ¬←diff[arg␈↓β2␈↓α[u]; x]];
␈↓" ␈↓ α←␈↓α␈↓ ∧π isprod[u] → list[␈↓ ¬←PLUS;
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ¬←list[␈↓ ε∂TIMES;
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ¬←␈↓ ε∂arg␈↓β1␈↓α[u];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ¬←␈↓ ε∂diff[arg␈↓β2␈↓α[u]; x]];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ¬←list[␈↓ ε∂TIMES;
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ¬←␈↓ ε∂arg␈↓β2␈↓α[u];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ¬←␈↓ ε∂diff[arg␈↓β1␈↓α[u]; x]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧π ␈↓
t␈↓α → ␈↓λB␈↓α]
␈↓"∀␈↓ α←␈↓Readability␈α∂is␈α∂certainly␈α∂improving,␈α∂but␈α∂the␈α∂representation␈α∂is␈α∂still␈α∂known␈α∂to
␈↓ α←␈↓␈↓αdiff␈↓.␈α When␈αwe␈αbuild␈αthe␈αresult␈αof␈αthe␈αsum␈αor␈αproduct␈αof␈αderivatives␈αwe␈αuse
␈↓ α←␈↓knowledge of the representation. It would be better to define:
␈↓"∀␈↓ α←␈↓α␈↓ ¬λmakesum[x;y] <= list[PLUS;x;y]
␈↓""␈↓ α←␈↓α␈↓ ∧smakeprod[x;y] <= list[TIMES;x;y]
␈↓"∀␈↓ α←␈↓Then the new ␈↓αdiff␈↓ is:
␈↓"∀␈↓ α←␈↓αdiff[u;x] <=␈↓ ∧π[isvar[u] → [samevar[x;u] → 1; ␈↓
t␈↓α → 0];
␈↓"β␈↓ α←␈↓α␈↓ ∧π isconst[u] → 0;
␈↓"β␈↓ α←␈↓α␈↓ ∧π issum[u] → makesum[␈↓ ε'diff[arg␈↓β1␈↓α[u]; x];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ε'diff[arg␈↓β2␈↓α[u]; x]];
␈↓"β␈↓ α←␈↓α␈↓ ∧π isprod[u] → makesum[␈↓ ε'makeprod[␈↓ π+arg␈↓β1␈↓α[u];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ε'␈↓ π+diff[arg␈↓β2␈↓α[u]; x]];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ε'makeprod[␈↓ π+arg␈↓β2␈↓α[u];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ε'␈↓ π+diff [arg␈↓β1␈↓α[u]; x]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧π ␈↓
t␈↓α → ␈↓λB␈↓α]
␈↓"∀␈↓ α←␈↓In␈α∀the␈α∃process,␈α∀␈↓αdiff␈↓␈α∃has␈α∀become␈α∀much␈α∃more␈α∀understandable␈α∃and,␈α∀more
␈↓ α←␈↓importantly,␈α→the␈α_details␈α→of␈α_the␈α→representation␈α_have␈α→been␈α→relegated␈α_to
␈↓ α←␈↓subfunctions.␈α∩Changing␈α⊃representation␈α∩simply␈α⊃requires␈α∩supplying␈α⊃different
␈↓ α←␈↓subfunctions.␈αNo␈αchanges␈α
need␈αbe␈αmade␈α
to␈α␈↓αdiff␈↓.␈αThere␈α
has␈αonly␈αbeen␈αa␈α
slight
␈↓ α←␈↓decrease␈αin␈αefficiency.␈α The␈αtermination␈αcondition␈αin␈αthe␈αoriginal␈α␈↓αdiff␈↓␈αis␈αa␈α
bit
␈↓ α←␈↓more␈α~succinct,␈α→but␈α~speaking␈α~precisely␈α→it␈α~was␈α→incorrect.␈α~The␈α~gain␈α→in
␈↓ α←␈↓independence␈α∀far␈α∀outweighs␈α∃the␈α∀slight␈α∀efficiency␈α∃consideration.␈α∀ Looking
␈↓ α←␈↓back,␈α→first␈α~we␈α→abstracted␈α~the␈α→selector␈α~functions:␈α→those␈α~which␈α→selected
␈↓ α←␈↓components;␈α∩next␈α∪we␈α∩abstracted␈α∩the␈α∪recognizers:␈α∩the␈α∪predicates␈α∩indicating
␈↓ α←␈↓which␈α⊂kind␈α⊂of␈α∂term␈α⊂was␈α⊂present;␈α⊂finally␈α∂we␈α⊂modified␈α⊂the␈α⊂constructors:␈α∂the
␈↓ α←␈↓functions␈αwhich␈αmake␈αnew␈αterms.␈α These␈αthree␈αcomponents␈αof␈αprogramming:
␈↓ α←␈↓␈↓2.3␈↓ λPDifferentiation 65␈↓
␈↓"β␈↓ α←␈↓selectors,␈α∂recognizers,␈α∂and␈α∂constructors,␈α∂will␈α∂appear␈α∂again␈α∂on␈α∂page 164␈α∂in␈α∂a
␈↓ α←␈↓discussion of McCarthy's abstract syntax.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∞␈↓αdiff␈↓␈α∂algorithm␈α∞is␈α∞much␈α∂more␈α∞abstract␈α∞now,␈α∂in␈α∞the␈α∞sense␈α∂that␈α∞the
␈↓ α←␈↓representation␈α⊂of␈α⊂the␈α⊂domain␈α⊂and␈α⊂the␈α⊂representation␈α⊂of␈α⊂the␈α⊂functions␈α∂and
␈↓ α←␈↓predicates␈αwhich␈αmanipulate␈αthat␈αdomain␈αhave␈αbeen␈αextracted␈αout.␈↓π 8␈↓␈α
This␈αis
␈↓ α←␈↓our␈α∀␈↓λr␈↓-mapping␈α∪again;␈α∀we␈α∪mapped␈α∀the␈α∪domain␈α∀of␈α∪<poly>'s␈α∀to␈α∀lists␈α∪and
␈↓ α←␈↓mapped␈α↔the␈α_constructors,␈α↔selectors,␈α_and␈α↔recognizers␈α_to␈α↔list-manipulating
␈↓ α←␈↓functions.␈α∂ Thus␈α∞the␈α∂data␈α∂types␈α∞of␈α∂the␈α∞arguments␈α∂␈↓αu␈↓␈α∂and␈α∞␈↓αx␈↓␈α∂are␈α∂<poly>␈α∞and
␈↓ α←␈↓<var>␈α
respectively,␈α
␈↓¬not␈↓␈α
list␈α
and␈α
atom.␈αTo␈α
stress␈α
this␈α
point␈α
we␈α
should␈αmake␈α
one
␈↓ α←␈↓more␈α∀transformation␈α∃on␈α∀␈↓αdiff␈↓.␈α∃We␈α∀have␈α∃frequently␈α∀said␈α∃that␈α∀there␈α∃is␈α∀a
␈↓ α←␈↓substantial␈α∃parallel␈α∀between␈α∃a␈α∀data␈α∃structure␈α∀and␈α∃the␈α∃algorithms␈α∀which
␈↓ α←␈↓manipulate it. Paralleling the BNF definition of <poly> on page 59, we write:
␈↓"∀␈↓ α←␈↓αdiff[u;x] <=␈↓ ∧π[isterm[u] → diffterm[u;x];
␈↓"β␈↓ α←␈↓α␈↓ ∧π issum[u] → makesum[␈↓ ε'diff[arg␈↓β1␈↓α[u]; x];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ε'diff[arg␈↓β2␈↓α[u]; x]];
␈↓"β␈↓ α←␈↓α␈↓ ∧π ␈↓
t␈↓α → ␈↓λB␈↓α]
␈↓"∀␈↓ α←␈↓αdiffterm[u;x] <=␈↓ ∧7[isconst[u] → 0;
␈↓"β␈↓ α←␈↓α␈↓ ∧7 isvar[u] → [samevar[x;u] → 1; ␈↓
t␈↓α → 0];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 isprod[u] → makesum[␈↓ εWmakeprod[␈↓ π[arg␈↓β1␈↓α[u];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ εW␈↓ π[diff[arg␈↓β2␈↓α[u]; x]];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ εWmakeprod[␈↓ π[arg␈↓β2␈↓α[u];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ εW␈↓ π[diff[arg␈↓β1␈↓α[u]; x]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 ␈↓
t␈↓α → ␈↓λB␈↓α]
␈↓"∀␈↓ α←␈↓To␈αsatisfy␈αour␈αcomplaint␈αof␈αpage 59␈αthat␈α␈↓αdiff[1;␈α1]␈↓␈αgives␈αa␈αdefined␈αresult,␈αwe
␈↓ α←␈↓should also add:
␈↓"∀␈↓ α←␈↓α␈↓ βKdiff␈↓λ'␈↓α[u; x] <= [isvar[x] → [ispoly[u] → diff[u; x]]; ␈↓
t␈↓α → ␈↓λB␈↓α]
␈↓"∀␈↓ α←␈↓Finally,␈αnotice␈αthat␈αour␈αabstraction␈αprocess␈αhas␈αmasked␈αthe␈αorder-dependence
␈↓ α←␈↓of␈αconditional␈αexpressions.␈αExactly␈αone␈αof␈αthe␈αrecognizers␈αwill␈αbe␈αsatisfied␈αby
␈↓ α←␈↓the form ␈↓αu␈↓.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Extend␈α⊂the␈α⊂version␈α⊂of␈α⊂␈↓αdiff␈↓␈α⊃of␈α⊂your␈α⊂choice␈α⊂to␈α⊂handle␈α⊃differentiation␈α⊂of
␈↓ α←␈↓␈↓ β∂powers such as ␈↓α↑[x; 3]␈↓.
␈↓" ␈↓ α←␈↓2. Extend ␈↓αdiff␈↓ to handle unary minus.
␈↓" ␈↓ α←␈↓3. Extend␈α∞␈↓αdiff␈↓␈α∞to␈α∞handle␈α∂differentiation␈α∞of␈α∞the␈α∞trigonometric␈α∂functions,␈α∞␈↓αsin␈↓
␈↓ α←␈↓␈↓ β∂and␈α
␈↓αcos␈↓␈α
and␈α
their␈α
composition␈α
with␈α
polynomials.␈α
For␈α
example␈α∞it␈α
should
␈↓ α←␈↓␈↓ β∂handle ␈↓αsin␈↓π2␈↓αx + cos(x␈↓π3␈↓α + 5x -2)␈↓.
␈↓" ␈↓ α←␈↓4. Write an algorithm to handle integration of polynomials.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 8␈↓To␈αbe␈αparticularly␈α
precise,␈αour␈αreferences␈αto␈α
␈↓α0␈↓␈αand␈α␈↓α1␈↓␈αshould␈α
really␈αbe
␈↓ α←␈↓␈↓αmkconst[0]␈↓ and ␈↓αmkconst[1]␈↓, signifying the functions which make constants.
␈↓ α←␈↓␈↓66 Applications␈↓
(2.4␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬T␈↓↓2.4 Tree Searching␈↓
␈↓"β␈↓ α←␈↓A␈α⊃natural␈α⊃application␈α⊃of␈α⊃LISP's␈α⊃recursive␈α⊃power␈α⊃occurs␈α⊃in␈α⊃tree␈α⊂searching
␈↓ α←␈↓algorithms.␈α
These␈α
algorithms␈α
are␈α∞the␈α
heart␈α
of␈α
programs␈α
which␈α∞play␈α
games.
␈↓ α←␈↓A␈α⊃ubiquitous␈α⊃feature␈α⊃of␈α⊃sophisticated␈α∩game␈α⊃playing␈α⊃is␈α⊃"a␈α⊃strategy".␈α∩ In␈α⊃a
␈↓ α←␈↓simple␈α∀game,␈α∀for␈α∀example␈α∀tic-tac-toe,␈α∀an␈α∀optimal␈α∀strategy␈α∀may␈α∃be␈α∀easily
␈↓ α←␈↓computable.␈α∩ In␈α∩games␈α⊃like␈α∩checkers␈α∩and␈α⊃chess,␈α∩the␈α∩algorithmic␈α⊃approach
␈↓ α←␈↓would␈αrequire␈αenormous␈αcomputational␈αpower;␈αheuristic␈αmethods␈αare␈αapplied
␈↓ α←␈↓to reduce the computational requirements.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αheart␈αof␈αthis␈αstrategy␈αformation␈α
is␈αoften␈αa␈αtree␈αstructure.␈α That␈α
tree
␈↓ α←␈↓will␈α
have␈αnodes␈α
representing␈α
"possible␈αmoves".␈α
In␈αa␈α
single-person␈α
game,␈αthe
␈↓ α←␈↓evaluation␈αof␈αthe␈αtree␈α
will␈αresult␈αin␈αa␈α"best␈α
move";␈αany␈αmove␈αthat␈αwins.␈α
In␈αa
␈↓ α←␈↓two-person␈α∪game␈α∪we␈α∪must␈α∪be␈α∪more␈α∪careful;␈α∪the␈α∪branching␈α∪structure␈α∩will
␈↓ α←␈↓represent␈α∪␈↓¬both␈↓␈α∪your␈α∩moves␈α∪and␈α∪those␈α∪of␈α∩the␈α∪opponent,␈α∪and␈α∪the␈α∩position
␈↓ α←␈↓evaluation␈α∪must␈α∪take␈α∪that␈α∪into␈α∪account:␈α∪"Now␈α∪if␈α∪I␈α∪move␈α∪here,␈α∪then␈α∩my
␈↓ α←␈↓opponent will move there, ... ."
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∩tree-structured␈α∩data␈α∪and␈α∩recursive␈α∩programming␈α∩style␈α∪of␈α∩LISP,
␈↓ α←␈↓allow␈α_simple␈α↔formulations␈α_of␈α_complex␈α↔tree␈α_strategies.␈α_ The␈α↔description
␈↓ α←␈↓involves␈α
discussion␈α
of␈α
the␈α
abstract␈α
data␈α
structures␈α
and␈α∞their␈α
representations.
␈↓ α←␈↓The␈αobjects␈αare␈αfinitely␈αbranching␈αtrees;␈αthat␈αis,␈αwe␈αassume␈αthat␈αany␈αnode␈αin
␈↓ α←␈↓a␈αtree␈αcan␈αhave␈αany␈αfinite␈αnumber␈αof␈αbranches.␈αWe␈αwill␈αalso␈αassume␈αthat␈αthe
␈↓ α←␈↓trees␈α
will␈α∞terminate␈α
on␈α
all␈α∞of␈α
their␈α
branches.␈α∞ We␈α
need␈α
a␈α∞recognizer,␈α
named
␈↓ α←␈↓␈↓αis_term␈↓,␈α∂which␈α∞will␈α∂return␈α∞␈↓
t␈↓␈α∂if␈α∂the␈α∞tree␈α∂is␈α∞the␈α∂trivial␈α∞terminal␈α∂tree␈α∂with␈α∞no
␈↓ α←␈↓branches.␈αA␈αterminal␈αtree␈αmay␈αeither␈αbe␈αa␈α␈↓αWIN␈↓␈αor␈αa␈α␈↓αLOSS␈↓.␈αIf␈αit's␈αa␈αwin,␈αwe
␈↓ α←␈↓know␈α∞how␈α∂to␈α∞achieve␈α∞our␈α∂goal;␈α∞if␈α∞it's␈α∂a␈α∞␈↓αLOSS␈↓,␈α∞then␈α∂we␈α∞look␈α∂further.␈α∞That
␈↓ α←␈↓"further"␈αsays␈αexamine␈αthe␈αalternatives␈αthe␈αimmediate␈αparent␈αof␈αthat␈αnode;␈αif
␈↓ α←␈↓there aren't any alternatives then back up to the grandparent.
␈↓"β␈↓ α←␈↓␈↓ β'If␈α
a␈α
tree␈αhas␈α
branches␈α
they␈αare␈α
located␈α
by␈αthe␈α
selector␈α
␈↓αbranches␈↓.␈α We␈α
will
␈↓ α←␈↓assume␈α⊗those␈α⊗branches␈α∃are␈α⊗presented␈α⊗as␈α∃an␈α⊗ordered␈α⊗sequence,␈α∃perhaps
␈↓ α←␈↓ordered␈αby␈α
their␈αplausible␈α
value.␈αTherefore␈αwe␈α
will␈αuse␈α
the␈αselectors␈α␈↓αfirst␈↓␈α
and
␈↓ α←␈↓␈↓αrest␈↓ to select candidate branches.
␈↓"∀␈↓ α←␈↓αeval_tree[tr] <= [␈↓ ∧Cis_term[tr] → [is_win[tr] → tr; ␈↓
t␈↓α → LOSS␈↓];
␈↓"β␈↓ α←␈↓␈↓ ∧C␈↓
t␈↓α → eval_branches[branches[tr]]]
␈↓"∀␈↓ α←␈↓αeval_branches[l] <= [␈↓ ∧gnull[l] → LOSS;
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧geq[LOSS;eval_tree[first[l]]] → eval_branches[rest[l]];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧g␈↓
t␈↓α → first[l]]
␈↓"∀␈↓ α←␈↓The␈α
simplicity␈α
of␈α∞the␈α
description␈α
is␈α
pleasing.␈α∞It␈α
encourages␈α
us␈α
to␈α∞proceed␈α
to
␈↓ α←␈↓more complex tree strategies.
␈↓"β␈↓ α←␈↓␈↓ β'Attempts␈α∪at␈α∪exhaustive␈α∪search␈α∪of␈α∪game␈α∪trees␈α∀becomes␈α∪prohibitively
␈↓ α←␈↓expensive␈α_when␈α→applied␈α_to␈α_games␈α→like␈α_checkers␈α_and␈α→chess.␈α_However,
␈↓ α←␈↓computers␈α⊂have␈α⊂had␈α⊃reasonable␈α⊂success␈α⊂at␈α⊃checkers,␈α⊂and␈α⊂are␈α⊃beginning␈α⊂to
␈↓ α←␈↓play␈α⊃passable␈α⊂chess.␈α⊃A␈α⊃recent␈α⊂article,␈α⊃[Sug 77],␈α⊂addresses␈α⊃the␈α⊃feasibility␈α⊂of
␈↓ α←␈↓␈↓2.4␈↓ λRTree Searching 67␈↓
␈↓"β␈↓ α←␈↓home␈α∀chess␈α∃machines.␈α∀Those␈α∀successes␈α∃are␈α∀based␈α∀on␈α∃more␈α∀sophisticated
␈↓ α←␈↓analysis␈α∃of␈α∃game␈α∃trees.␈α∃The␈α∃ideas␈α∃involved␈α∃in␈α∃that␈α∃analysis␈α⊗are␈α∃easily
␈↓ α←␈↓expressed in LISP.
␈↓"β␈↓ α←␈↓␈↓ β'In the following discussions we will make several assumptions.
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ Our␈αopponent␈αis␈αas␈αsmart␈αas␈αwe␈αare.␈αThis␈αassumption␈αallows␈αus␈αto␈αuse␈α
␈↓¬our␈↓
␈↓ α←␈↓␈↓ β∂evaluation function in evaluating the positions of our opponent.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ We␈α
assume␈α
that␈α
our␈α
opponent␈α∞is␈α
also␈α
trying␈α
to␈α
win.␈α
Therefore␈α∞his␈α
move
␈↓ α←␈↓␈↓ β∂will␈α∂reflect␈α∂his␈α∂best␈α∞attempt␈α∂to␈α∂defeat␈α∂us.␈α∞ Since␈α∂we␈α∂are␈α∂using␈α∂the␈α∞same
␈↓ α←␈↓␈↓ β∂position-evaluator,␈α∞his␈α∞"maximal␈α∞harm"␈α∞is␈α∞our␈α∞"minimal␈α∞good".␈α∂ We␈α∞are
␈↓ α←␈↓␈↓ β∂thus␈αfollowing␈αa␈α"␈↓↓max-min␈↓"␈αstrategy␈αwherein␈αwe␈αattempt␈αto␈αfind␈αthe␈αbest
␈↓ α←␈↓␈↓ β∂move which our opponent cannot turn into a disaster for us.
␈↓"∀␈↓ α←␈↓␈↓ β'From␈α∪these␈α∀ideas␈α∪we␈α∀formulate␈α∪our␈α∀position␈α∪evaluation␈α∀strategy␈α∪as
␈↓ α←␈↓follows:
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ Grow␈αa␈αtree␈αof␈α
moves.␈αFirst␈αour␈αpossible␈α
moves␈αfrom␈αa␈αposition,␈α
then␈αhis
␈↓ α←␈↓␈↓ β∂counter␈α⊂moves;␈α∂then␈α⊂our␈α∂responses,␈α⊂etc.␈α∂Continue␈α⊂this␈α∂until␈α⊂the␈α∂branch
␈↓ α←␈↓␈↓ β∂terminates or until a termination condition is forced.␈↓π 9␈↓
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ Once the tree is built, we evaluate the terminal nodes.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ The␈α
values␈αare␈α
propagated␈αback␈α
up␈αthe␈α
tree␈αusing␈α
the␈αmin-max␈α
idea.␈α If
␈↓ α←␈↓␈↓ β∂the␈α⊂preceding␈α⊂node␈α⊂is␈α⊂ours,␈α⊂we␈α⊂assign␈α⊂that␈α⊂node␈α⊂the␈α⊂maximum␈α⊂of␈α∂the
␈↓ α←␈↓␈↓ β∂branch␈α
values;␈αif␈α
the␈α
preceding␈αnode␈α
is␈αhis␈α
we␈α
assign␈αthe␈α
minimum␈αof␈α
the
␈↓ α←␈↓␈↓ β∂values.␈α⊂We␈α⊂proceed␈α⊂in␈α⊂this␈α⊂fashion,␈α⊂finally␈α⊂returning␈α⊂the␈α⊂value␈α⊂of␈α⊂the
␈↓ α←␈↓␈↓ β∂"best path".
␈↓"∀␈↓ α←␈↓We␈α∂will␈α∂simplify␈α∂matters␈α∂somewhat,␈α⊂returning␈α∂only␈α∂the␈α∂"value"␈α∂of␈α⊂the␈α∂best
␈↓ α←␈↓path.␈↓π 10␈↓ First, we develop some subfunctions:
␈↓"∀␈↓ α←␈↓αmaxlist[l;f] <= [null[l] → -∞; ␈↓
t␈↓α → max[␈↓ εKf[first[l]];
␈↓"β␈↓ α←␈↓α␈↓ εKmaxlist[rest[l];f]]
␈↓"∀␈↓ α←␈↓αminlist[l;f] <= [null[l] → ∞; ␈↓
t␈↓α → min[␈↓ εKf[first[l]];
␈↓"β␈↓ α←␈↓α␈↓ εKminlist[rest[l];f]]
␈↓"∀␈↓ α←␈↓The␈α⊃"␈↓α∞␈↓"␈α∩denotes␈α⊃a␈α⊃number,␈α∩bigger␈α⊃than␈α⊃any␈α∩other␈α⊃value␈α∩our␈α⊃evaluation
␈↓ α←␈↓function␈α␈↓αf␈↓␈αcan␈αconcoct.␈α
The␈α␈↓αf␈↓␈αis␈αa␈αdifferent␈α
kind␈αof␈αvariable␈αfrom␈α
those␈αwe
␈↓ α←␈↓have␈α∂seen␈α∂before.␈α∂ It␈α∂is␈α∂used␈α∂as␈α∂a␈α∂LISP␈α∂function␈α∂within␈α∂the␈α∂bodies␈α∂of␈α∞the
␈↓ α←␈↓definition,␈α∃yet␈α∃passed␈α∃as␈α∃a␈α∀variable.␈α∃It␈α∃is␈α∃therefore␈α∃called␈α∃a␈α∀functional
␈↓ α←␈↓variable.␈αWe␈αwill␈αdiscuss␈αsuch␈αvariables␈αin␈αthe␈αnext␈αchapter,␈αbut␈αfor␈αnow␈αthe
␈↓ α←␈↓intent should be clear from some examples:
␈↓"∀␈↓ α←␈↓α␈↓ βRmaxlist[(1 3 5 2);add1] = 6 ␈↓and␈↓α minlist[(1 3 5 2);add1] = 2
␈↓"∀␈↓ α←␈↓With those preliminaries, we are ready to present the mini-max strategy:
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 9␈↓We␈αassume␈α
we␈αhave␈α
methods␈αfor␈α
determining␈αwhen␈α
a␈αmove␈αis␈α
already
␈↓ α←␈↓present in the tree.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 10␈↓We␈αshould␈αreally␈αreturn␈αthe␈αbest␈αvalue␈α␈↓¬and␈↓␈αa␈αdescription␈αof␈αthe␈αbest
␈↓ α←␈↓path.
␈↓ α←␈↓␈↓68 Applications␈↓
(2.4␈↓
␈↓"β␈↓ α←␈↓αmaxpos[p] <= [␈↓ ∧∨is_term[p] → value[p];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓
t␈↓α → maxlist[branches[p]; minpos]]
␈↓"∀␈↓ α←␈↓αminpos[p] <= [␈↓ ∧∨is_term[p] → value[p];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓
t␈↓α → minlist[branches[p]; maxpos]]
␈↓"∀␈↓ α←␈↓␈↓αmaxpos␈↓␈αgives␈α
the␈αvalue␈αof␈α
a␈αposition␈αfor␈α
the␈αmaximizing␈αplayer␈α
and␈α␈↓αminpos␈↓
␈↓ α←␈↓gives␈α∪the␈α∪value␈α∩of␈α∪a␈α∪position␈α∩for␈α∪the␈α∪minimizing␈α∩player.␈α∪ ␈↓αvalue␈↓␈α∪is␈α∩the
␈↓ α←␈↓terminal position evaluation function.
␈↓"β␈↓ α←␈↓␈↓ β'What's␈αeven␈αmore␈αinteresting␈αis␈αthat␈αthere␈αis␈αa␈αsimple␈αtechnique␈αwhich
␈↓ α←␈↓will␈αallow␈αus␈αto␈αdiscover␈αthe␈αoptimal␈αpath,␈αusually␈αwithout␈αhaving␈αto␈αvisit␈αall
␈↓ α←␈↓the␈α∞nodes.␈α∞The␈α∞technique,␈α∞discovered␈α∂by␈α∞John␈α∞McCarthy␈α∞in␈α∞1958,␈α∂is␈α∞called
␈↓ α←␈↓␈↓εa␈↓↓-␈↓εb␈↓↓ pruning␈↓;␈α
it␈α
is␈αbased␈α
on␈α
the␈αobservation␈α
that␈α
if␈αour␈α
opponent␈α
is␈αassured
␈↓ α←␈↓that␈α
he␈α
can␈α
force␈α
us␈α
into␈αan␈α
unfavorable␈α
position␈α
then␈α
he␈α
won't␈α
make␈αa␈α
move
␈↓ α←␈↓which␈αwould␈α
give␈αus␈α
a␈α␈↓¬better␈↓␈α
position.␈α That's␈α
obvious;␈αwhat␈α
is␈α␈↓¬not␈↓␈αobvious␈α
is
␈↓ α←␈↓that␈α∀he␈α∀can␈α∀often␈α∀make␈α∀such␈α∃decisions␈α∀on␈α∀the␈α∀basis␈α∀of␈α∀only␈α∃a␈α∀partial
␈↓ α←␈↓evaluation of the tree. Consider:
␈↓"⊃␈↓ α←␈↓∂␈↓ β' O
␈↓"␈↓ α←␈↓∂␈↓ β' ~
␈↓"␈↓ α←␈↓∂␈↓ β' ⊂αααααα∀ααααα⊃ opponent␈↓'␈↓∂s moves
␈↓"␈↓ α←␈↓∂␈↓ β' ~ ~
␈↓"␈↓ α←␈↓∂␈↓ β' N M
␈↓"␈↓ α←␈↓∂␈↓ β' ⊂αααβααα⊃ ⊂αααβαα ...⊃ our moves
␈↓"␈↓ α←␈↓∂␈↓ β' ~ ~ ~ ~
␈↓"␈↓ α←␈↓∂␈↓ β' 7 3 4 ? ...
␈↓"∀␈↓ α←␈↓Since␈αwe␈αare␈αto␈αevaluate␈αthe␈αposition␈αat␈α␈↓∂N␈↓,␈αwe␈αmaximize␈αthe␈αposition,␈αgetting
␈↓ α←␈↓␈↓∂7␈↓;␈α∞that␈α∂becomes␈α∞the␈α∂value␈α∞of␈α∂node␈α∞␈↓∂N␈↓.␈α∂It␈α∞is␈α∂up␈α∞to␈α∂our␈α∞opponent␈α∂to␈α∞evaluate
␈↓ α←␈↓position␈α
␈↓∂O␈↓,␈α∞and␈α
he␈α∞now␈α
knows␈α
we're␈α∞going␈α
to␈α∞get␈α
a␈α
␈↓∂7␈↓␈α∞if␈α
he␈α∞moves␈α
to␈α∞␈↓∂N␈↓.␈α
He
␈↓ α←␈↓looks␈αquestioningly␈αat␈α"?";␈αif␈αthat␈α
value␈αis␈αgreater␈αthan␈α␈↓∂7␈↓␈αthen␈αhe␈α
immediately
␈↓ α←␈↓rejects␈αmove␈α␈↓∂M␈↓␈αwithout␈αexamining␈αthe␈αother␈αpossibilities;␈αthings␈αcan␈α
only␈αget
␈↓ α←␈↓worse␈αfor␈αhim.␈αIf␈α"?"␈α
is␈αless␈αthan␈α␈↓∂7␈↓,␈αthen␈α
he␈αlooks␈αat␈αadditional␈αalternatives␈α
at
␈↓ α←␈↓␈↓∂M␈↓.␈α
Once␈α
our␈α
opponent␈α
is␈α
finished␈α
evaluating␈α
the␈α
position,␈α
then␈α
it's␈α
our␈αturn␈α
to
␈↓ α←␈↓play␈α∂the␈α∂game␈α∂at␈α∂the␈α∂position␈α⊂above␈α∂␈↓∂O␈↓,␈α∂only␈α∂now␈α∂we␈α∂will␈α∂try␈α⊂to␈α∂maximize
␈↓ α←␈↓what␈α
that␈α
stingy␈α
individual␈αhas␈α
left␈α
us.␈α
We␈α
let␈α␈↓εa␈↓␈α
be␈α
the␈α
value␈α
which␈αmust␈α
be
␈↓ α←␈↓exceeded␈αfor␈αa␈αposition␈αto␈αbe␈αdesirable␈αby␈αthe␈αposition␈αabout␈αto␈αplay;␈αand␈αlet
␈↓ α←␈↓␈↓εb␈↓␈α⊃be␈α⊃the␈α⊃value␈α⊃which␈α⊃must␈α∩␈↓¬not␈↓␈α⊃be␈α⊃exceeded␈α⊃if␈α⊃the␈α⊃move␈α⊃leading␈α∩to␈α⊃the
␈↓ α←␈↓position␈α⊂would␈α⊂be␈α⊂made␈α⊂by␈α⊂the␈α∂opponent;␈α⊂in␈α⊂the␈α⊂above␈α⊂example␈α⊂␈↓∂7␈↓␈α⊂is␈α∂the
␈↓ α←␈↓␈↓εb␈↓-value␈αwhen␈αevaluating␈α␈↓∂M␈↓.␈αWith␈αthat,␈αwe␈αmodify␈αthe␈αmin-max␈αalgorithms␈αto
␈↓ α←␈↓include ␈↓εa␈↓-␈↓εb␈↓ pruning.
␈↓ α←␈↓␈↓2.4␈↓ λRTree Searching 69␈↓
␈↓"∀␈↓ α←␈↓αmaxlist␈↓βαβ␈↓α[l;f;␈↓εa␈↓α;␈↓εb␈↓α] <= [␈↓ ¬null[l] → ␈↓εa␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ¬f[first[l]] ␈↓λ≥␈↓ ␈↓εb␈↓α → ␈↓εb␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ¬␈↓
t␈↓α → maxlist␈↓βαβ␈↓α[␈↓ εKrest[l];
␈↓"β␈↓ α←␈↓α␈↓ ¬␈↓ εKf;
␈↓"β␈↓ α←␈↓α␈↓ ¬␈↓ εKmax[␈↓εa␈↓α;f[first[l]]];
␈↓"β␈↓ α←␈↓α␈↓ ¬␈↓ εK␈↓εb␈↓α]]
␈↓" ␈↓ α←␈↓αminlist␈↓βαβ␈↓α[l;f;␈↓εa␈↓α;␈↓εb␈↓α] <= [␈↓ ¬null[l] → ␈↓εb␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ¬f[first[l]] ␈↓λ≤␈↓ ␈↓εa␈↓α → ␈↓εa␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ¬␈↓
t␈↓α → minlist␈↓βαβ␈↓α[␈↓ εKrest[l];
␈↓"β␈↓ α←␈↓α␈↓ ¬␈↓ εKf;
␈↓"β␈↓ α←␈↓α␈↓ ¬␈↓ εK␈↓εa␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ¬␈↓ εKmin[␈↓εb␈↓α;f[first[l]]]]]
␈↓"∀␈↓ α←␈↓αmaxpos␈↓βαβ␈↓α[p;␈↓εa␈↓α;␈↓εb␈↓α] <= [␈↓ ∧␈is_term[p] → max[␈↓εa␈↓α;min[␈↓εb␈↓α;value[p]];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓
t␈↓α → maxlist␈↓βαβ␈↓α[branches[p]; minpos␈↓β1␈↓α;␈↓εa␈↓α;␈↓εb␈↓α]]
␈↓" ␈↓ α←␈↓αminpos␈↓β1␈↓α[x] <= minpos␈↓βαβ␈↓α[x;␈↓εa␈↓α;␈↓εb␈↓α]
␈↓"∀␈↓ α←␈↓αminpos␈↓βαβ␈↓α[p;␈↓εa␈↓α;␈↓εb␈↓α] <= [␈↓ ∧␈is_term[p] → max[␈↓εa␈↓α;min[␈↓εb␈↓α;value[p]];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓
t␈↓α → minlist␈↓βαβ␈↓α[branches[p]; maxpos␈↓β1␈↓α;␈↓εa␈↓α;␈↓εb␈↓α]]
␈↓" ␈↓ α←␈↓αmaxpos␈↓β1␈↓α[x] <= maxpos␈↓βαβ␈↓α[x;␈↓εa␈↓α;␈↓εb␈↓α]
␈↓"∀␈↓ α←␈↓The␈α∂process␈α∞can␈α∂be␈α∞initialized␈α∂with␈α∞␈↓εa␈↓␈α∂and␈α∞␈↓εb␈↓␈α∂set␈α∞to␈α∂␈↓α-∞␈↓␈α∞and␈α∂␈↓α∞␈↓␈α∞respectively.
␈↓ α←␈↓Tighter␈α∞bounds␈α
on␈α∞"acceptablility"␈α∞can␈α
be␈α∞enforced␈α
by␈α∞picking␈α∞different␈α
␈↓εa␈↓'s
␈↓ α←␈↓and␈α
␈↓εb␈↓'s.␈α
The␈α
effect␈α
will␈α
be␈α
to␈α
shorten␈α
the␈α
search␈α
time␈α
while,␈αperhaps,␈α
ignoring
␈↓ α←␈↓some winning moves; ␈↓αcaveat emptor␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'This␈α⊗␈↓¬not␈↓␈α∃a␈α⊗trivial␈α⊗algorithm.␈α∃However␈α⊗its␈α∃description␈α⊗as␈α⊗a␈α∃LISP
␈↓ α←␈↓program is about as simple and as compact as you will find; anywhere.
␈↓"β␈↓ α←␈↓␈↓ ¬r␈↓↓2.5 Data Bases␈↓
␈↓"β␈↓ α←␈↓One␈αof␈αthe␈αmore␈αintriguing␈αapplications␈αof␈αLISP␈αis␈αin␈αthe␈αarea␈αof␈αdata␈αbase
␈↓ α←␈↓management.␈α∞In␈α∞this␈α∂section␈α∞we␈α∞introduce␈α∞the␈α∂ideas␈α∞and␈α∞suggest␈α∂how␈α∞LISP
␈↓ α←␈↓can be applied to the problems.
␈↓"β␈↓ α←␈↓␈↓ β'A␈αdata␈αbase␈αis␈αa␈αcollection␈αof␈αobjects␈αtogether␈αwith␈αa␈αset␈αof␈αfunctions␈αto
␈↓ α←␈↓pose␈αquestions␈α
about␈αthe␈α
objects␈αin␈α
the␈αbase,␈α
to␈αselect␈α
objects␈αfrom␈α
the␈αbase,
␈↓ α←␈↓and␈αto␈αconstruct␈αnew␈α
entries␈αin␈αthe␈αbase.␈α
Expressed␈αdifferently,␈αa␈αdata␈αbase␈α
is
␈↓ α←␈↓an␈α
abstract␈α
data␈α
structure.␈α
We␈α
need␈α
to␈α
locate␈α
information␈α
in␈α
the␈α∞base.␈α
We
␈↓ α←␈↓should␈αbe␈αable␈αto␈αask␈α
the␈αsystem␈αfor␈αa␈αspecific␈α
object␈αor␈αwe␈αshould␈αbe␈αable␈α
to
␈↓ α←␈↓partially␈α
specify␈αour␈α
request␈α("find␈α
all␈αbooks␈α
about␈αLISP"␈α
or␈α"find␈α
all␈αbooks
␈↓ α←␈↓about␈αLISP␈αpublished␈αbefore␈α1975").␈α We␈αshould␈αbe␈αable␈αto␈αadd␈αentries␈αand
␈↓ α←␈↓delete entries, but we will postpone these kinds of requests until later.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αrepresentational␈αdetails␈αof␈αobjects␈αwill␈αbe␈αsuppressed␈αas␈αusual,␈αand
␈↓ α←␈↓we␈α⊃will␈α∩concentrate␈α⊃on␈α⊃the␈α∩abstract␈α⊃properties.␈α⊃ In␈α∩our␈α⊃first␈α∩example,␈α⊃the
␈↓ α←␈↓␈↓70 Applications␈↓
(2.5␈↓
␈↓"β␈↓ α←␈↓objects␈αin␈αthe␈αdata␈αbase␈αwill␈αrepresent␈αconstants:␈αan␈αobject␈αwill␈αhave␈αa␈αname
␈↓ α←␈↓and a collection of properties and values.
␈↓"⊃␈↓ α←␈↓
⊂αααααααπααααα⊃
␈↓"␈↓ α←␈↓
~ prop1 ~ val1~
␈↓"␈↓ α←␈↓
εαααααααβαααααλ
␈↓"␈↓ α←␈↓
~ prop2 ~ val2~
␈↓"␈↓ α←␈↓
# # #
␈↓"␈↓ α←␈↓
εαααααααβαααααλ
␈↓"␈↓ α←␈↓
~ propn ~ valn~
␈↓"␈↓ α←␈↓
%ααααααα∀ααααα$
␈↓"∀␈↓ α←␈↓␈↓ An object representation ␈↓
␈↓"λ␈↓ α←␈↓␈↓ β'For␈α∂example,␈α∂a␈α⊂data␈α∂base␈α∂dealing␈α⊂with␈α∂business␈α∂supplies␈α⊂might␈α∂have
␈↓ α←␈↓objects named boxes. Each box has properties like size and contents.
␈↓"β␈↓ α←␈↓␈↓ β'Not␈α∪all␈α∀objects␈α∪need␈α∀to␈α∪have␈α∀the␈α∪same␈α∀number␈α∪of␈α∀properties.␈α∪For
␈↓ α←␈↓example␈α⊂in␈α⊂a␈α⊂data␈α⊃base␈α⊂whose␈α⊂objects␈α⊂are␈α⊂bibliographic␈α⊃references,␈α⊂books
␈↓ α←␈↓need␈αnot␈α
have␈αpage␈αreferences,␈α
whereas␈αjournal␈α
articles␈αrequire␈αthem;␈α
journal
␈↓ α←␈↓references␈αdon't␈αinclude␈αa␈αpublisher␈αwhereas␈αbooks␈αdo.␈α The␈αprograms␈α
which
␈↓ α←␈↓manipulate␈α∪the␈α∩data␈α∪base␈α∪must␈α∩be␈α∪structured␈α∩to␈α∪take␈α∪changeablility␈α∩into
␈↓ α←␈↓account.
␈↓"β␈↓ α←␈↓␈↓ β'Here␈αare␈αsome␈αexamples:␈αthe␈αfirst␈αone␈αwas␈αextracted␈αfrom␈αthe␈αside␈αof␈αa
␈↓ α←␈↓Xerox␈α∞paper␈α∞box;␈α∞the␈α∞second␈α∂might␈α∞be␈α∞a␈α∞representation␈α∞of␈α∂a␈α∞bibliographic
␈↓ α←␈↓entry for this book.
␈↓"⊃␈↓ α←␈↓
␈↓ β' ⊂αααααααπαααααααααααα⊃
␈↓"␈↓ α←␈↓
␈↓ β' ~ NAME ~ 4029258 ~
␈↓"␈↓ α←␈↓
␈↓ β' εαααααααβααααααααααααλ
␈↓"␈↓ α←␈↓
␈↓ β' ~ SIZE ~ 8-1/2 x 11 ~
␈↓"␈↓ α←␈↓
␈↓ β' εαααααααβααααααααααααλ
␈↓"␈↓ α←␈↓
␈↓ β' ~ COLOR ~ WHITE ~
␈↓"␈↓ α←␈↓
␈↓ β' εαααααααβααααααααααααλ
␈↓"␈↓ α←␈↓
␈↓ β' ~ AMNT ~ 10 REAMS ~
␈↓"␈↓ α←␈↓
␈↓ β' %ααααααα∀αααααααααααα$
␈↓"␈↓ α←␈↓
␈↓ β' ⊂ααααααααπααααααααααααααααααααα⊃
␈↓"␈↓ α←␈↓
␈↓ β' ~ AUTHOR ~ ALLEN, JOHN, R. ~
␈↓"␈↓ α←␈↓
␈↓ β' εααααααααβαααααααααααααααααααααλ
␈↓"␈↓ α←␈↓
␈↓ β' ~ TITLE ~ THE ANATOMY OF LISP ~
␈↓"␈↓ α←␈↓
␈↓ β' εααααααααβαααααααααααααααααααααλ
␈↓"␈↓ α←␈↓
␈↓ β' ~ TYPE ~ BOOK ~
␈↓"␈↓ α←␈↓
␈↓ β' εααααααααβαααααααααααααααααααααλ
␈↓"␈↓ α←␈↓
␈↓ β' ~ PUBL ~ MCGRAW-HILL ~
␈↓"␈↓ α←␈↓
␈↓ β' εααααααααβαααααααααααααααααααααλ
␈↓"␈↓ α←␈↓
␈↓ β' ~ DATE ~ 1977 ~
␈↓"␈↓ α←␈↓
␈↓ β' %αααααααα∀ααααααααααααααααααααα$
␈↓"∀␈↓ α←␈↓␈↓ β'Given␈α∞a␈α
data␈α∞base␈α
of␈α∞objects,␈α
we␈α∞need␈α
to␈α∞be␈α
able␈α∞to␈α∞manipulate␈α
these
␈↓ α←␈↓objects␈α
in␈α∞meaningful␈α
ways.␈α∞We␈α
will␈α∞not␈α
address␈α∞the␈α
problems␈α∞of␈α
designing
␈↓ α←␈↓␈↓2.5␈↓
Data Bases 71␈↓
␈↓"β␈↓ α←␈↓input␈α⊃and␈α⊃output,␈α⊃but␈α⊃will␈α⊃concern␈α⊃ourselves␈α⊃solely␈α⊃with␈α⊃the␈α⊃problems␈α⊃of
␈↓ α←␈↓semantics␈α∂of␈α∂data␈α∂base␈α∂primitives:␈α∂how␈α∂can␈α∂we␈α∂use␈α∂the␈α∂information␈α⊂in␈α∂the
␈↓ α←␈↓base?
␈↓"β␈↓ α←␈↓␈↓ β'In␈αrequesting␈αinformation␈αfrom␈αa␈αdata␈αbase,␈αwe␈αtypically␈αspecify␈αpart␈αof
␈↓ α←␈↓the␈α
request␈αand␈α
expect␈α
the␈αsystem␈α
to␈α
come␈αup␈α
with␈α
a␈αset␈α
of␈αpossibilities␈α
which
␈↓ α←␈↓fit␈α∂our␈α∂description.␈α∂For␈α∂example,␈α∂the␈α∂request:␈α∂"find␈α∂all␈α∂books␈α∂about␈α∞LISP",
␈↓ α←␈↓specifies␈α
that␈αwe␈α
are␈αinterested␈α
only␈αin␈α
books,␈αnot␈α
in␈αjournal␈α
articles␈αor␈α
course
␈↓ α←␈↓notes;␈α∞the␈α∞topic␈α∞is␈α∞specified␈α∞to␈α∞be␈α∞LISP,␈α∞but␈α∞the␈α∞system␈α∞is␈α∞free␈α∞to␈α∂select␈α∞the
␈↓ α←␈↓other␈α∃components:␈α∃the␈α∀author,␈α∃the␈α∃title,␈α∃the␈α∀publisher␈α∃and␈α∃the␈α∃date␈α∀of
␈↓ α←␈↓publication.␈α⊗The␈α⊗objects␈α⊗which␈α↔are␈α⊗specified␈α⊗are␈α⊗called␈α↔␈↓↓constants␈↓,␈α⊗the
␈↓ α←␈↓unspecified␈α∪components␈α∪are␈α∩␈↓↓variables␈↓.␈α∪ A␈α∪request␈α∩is␈α∪a␈α∪structure␈α∪called␈α∩a
␈↓ α←␈↓␈↓↓pattern␈↓␈α
and␈α
consists␈αof␈α
an␈α
ordered␈α
collection␈αof␈α
constants␈α
and␈αvariables.␈α
The
␈↓ α←␈↓elements␈α∂in␈α∂the␈α∂data␈α∂base␈α∂are␈α∞also␈α∂patterns;␈α∂for␈α∂this␈α∂example,␈α∂they␈α∞contain
␈↓ α←␈↓only␈αconstants;␈αsuch␈αconstant␈αpatterns␈αare␈αalso␈αcalled␈αrecords.␈α The␈αprocess␈αof
␈↓ α←␈↓discovering␈αwhether␈α
or␈αnot␈αa␈α
record␈αin␈α
the␈αdata␈αbase␈α
matches␈αthe␈α
request␈αis
␈↓ α←␈↓called ␈↓↓pattern matching␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α⊃describe␈α∩a␈α⊃simple␈α∩pattern␈α⊃matcher␈α⊃named␈α∩␈↓αmatch␈↓.␈α⊃It␈α∩expects␈α⊃two
␈↓ α←␈↓arguments.␈α
The␈αfirst␈α
argument␈αis␈α
a␈αconstant␈α
pattern␈αcalled␈α
␈↓αpat␈↓.␈α
The␈αsecond
␈↓ α←␈↓argument,␈α⊂␈↓αexp␈↓␈α⊂represents␈α⊂a␈α⊂request;␈α⊂it␈α∂may␈α⊂be␈α⊂constant,␈α⊂or␈α⊂it␈α⊂may␈α∂contain
␈↓ α←␈↓variables.␈α⊂ If␈α⊂it␈α⊃does␈α⊂contain␈α⊂variables,␈α⊃then␈α⊂the␈α⊂pattern␈α⊃matching␈α⊂process
␈↓ α←␈↓must␈αestablish␈αa␈αmatch␈αbetween␈αthose␈αvariables␈αand␈αcomponents␈αof␈αour␈αdata
␈↓ α←␈↓base␈α→object.␈α~The␈α→value␈α~returned␈α→by␈α→␈↓αmatch␈↓␈α~will␈α→either␈α~represent␈α→the
␈↓ α←␈↓associations␈α
built␈α
up␈α
to␈α
match␈α
the␈αconstant␈α
pattern␈α
to␈α
the␈α
expression,␈α
or␈αthe
␈↓ α←␈↓value returned will indicate failure if no match is possible.
␈↓"β␈↓ α←␈↓␈↓ β'Patterns␈α∞will␈α
be␈α∞represented␈α
as␈α∞lists␈α
with␈α∞atoms␈α∞representing␈α
constants,
␈↓ α←␈↓and␈α∪variables␈α∪represented␈α∪as␈α∀lower-case␈α∪greek␈α∪letters.␈α∪ We␈α∀will␈α∪represent
␈↓ α←␈↓failure␈α∞by␈α
returning␈α∞the␈α∞atom␈α
␈↓αNO␈↓.␈α∞In␈α∞the␈α
case␈α∞that␈α∞a␈α
match␈α∞is␈α∞possible,␈α
we
␈↓ α←␈↓will␈α∂return␈α∂a␈α∂list␈α∂of␈α∂pairs,␈α∂where␈α∞each␈α∂pair␈α∂is␈α∂a␈α∂variable␈α∂and␈α∂its␈α∞matching
␈↓ α←␈↓constant.
␈↓""␈↓ α←␈↓For example:␈↓α␈↓ ∧[match[(A (B C));(A (B ␈↓εa␈↓α))] = ((␈↓εa␈↓α C))
␈↓"⊃␈↓ α←␈↓α␈↓ ∧[match[(A B C);(A ␈↓εa␈↓α ␈↓εb␈↓α)] = ((␈↓εa␈↓α B) (␈↓εb␈↓α C))
␈↓"⊃␈↓ α←␈↓α␈↓ ∧[match[(A B C);(A C ␈↓εb␈↓α)] = NO
␈↓"∀␈↓ α←␈↓Pattern matching can become quite complex. For example:
␈↓""␈↓ α←␈↓␈↓ βc␈↓αmatch[(A (B C) (D C));(A (B ␈↓εa␈↓α) (␈↓εb␈↓α C))] = ((␈↓εa␈↓α C) (␈↓εb␈↓α D))
␈↓"⊃␈↓ α←␈↓α␈↓ βcmatch[(A (B C) (D C));(A (B ␈↓εa␈↓α) (␈↓εa␈↓α C))] = NO
␈↓"∀␈↓ α←␈↓The␈α
second␈α∞example␈α
fails␈α∞since␈α
once␈α∞we␈α
have␈α∞associated␈α
␈↓αC␈↓␈α∞with␈α
␈↓εa␈↓␈α∞we␈α
must
␈↓ α←␈↓use␈αthat␈α
association␈αthroughout␈α
the␈αrest␈αof␈α
the␈αpattern␈α
match;␈αand␈α␈↓α(D C)␈↓␈α
does
␈↓ α←␈↓not match ␈↓α(␈↓εa␈↓α C)␈↓ when ␈↓εa␈↓ denotes ␈↓αC␈↓.␈↓π 11␈↓
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 11␈↓This assumes that the match proceeds in a left-to-right order.
␈↓ α←␈↓␈↓72 Applications␈↓
(2.5␈↓
␈↓"β␈↓ α←␈↓␈↓ β'We␈α⊂will␈α⊂write␈α⊂␈↓αmatch␈↓␈α⊂in␈α∂terms␈α⊂of␈α⊂a␈α⊂subfunction␈α⊂named␈α⊂␈↓αmatch␈↓λ'␈↓.␈α∂ This
␈↓ α←␈↓subfunction␈α∪carries␈α∪a␈α∩third␈α∪argument,␈α∪␈↓αmlist␈↓,␈α∩which␈α∪represents␈α∪the␈α∪list␈α∩of
␈↓ α←␈↓partial␈α∃matches.␈α∃Whenever␈α⊗we␈α∃locate␈α∃a␈α⊗variable␈α∃in␈α∃the␈α⊗expression,␈α∃we
␈↓ α←␈↓examine␈α∂the␈α∞current␈α∂␈↓αmlist␈↓.␈α∞If␈α∂the␈α∞variable␈α∂appears,␈α∞then␈α∂we␈α∞must␈α∂check␈α∞its
␈↓ α←␈↓entry␈αagainst␈α
the␈αcorresponding␈αpart␈α
of␈αthe␈αpattern.␈α
If␈αthe␈αvariable␈α
does␈αnot
␈↓ α←␈↓occur␈α
in␈α
␈↓αmlist␈↓,␈α
then␈α
we␈α
associate␈αthe␈α
variable␈α
with␈α
the␈α
appropriate␈α
part␈αof␈α
the
␈↓ α←␈↓constant pattern.
␈↓"β␈↓ α←␈↓αmatch[pat;exp] <= match␈↓λ'␈↓α[pat;exp;( )]
␈↓"∀␈↓ α←␈↓αmatch␈↓λ'␈↓α[pat;exp;mlist] <=␈↓ ¬/[equal[mlist;NO] → NO;
␈↓"β␈↓ α←␈↓α␈↓ ¬/ isconst[exp] → [␈↓ ππsameconst[pat;exp] → mlist;
␈↓"β␈↓ α←␈↓α␈↓ ¬/␈↓ εK␈↓ ππ␈↓
t␈↓α → NO];
␈↓"β␈↓ α←␈↓α␈↓ ¬/ isvar[exp] → check[␈↓ π+pat;
␈↓"β␈↓ α←␈↓α␈↓ ¬/␈↓ εK␈↓ ππ␈↓ π+exp;
␈↓"β␈↓ α←␈↓α␈↓ ¬/␈↓ εK␈↓ ππ␈↓ π+lookup[pat;mlist];
␈↓"β␈↓ α←␈↓α␈↓ ¬/␈↓ εK␈↓ ππ␈↓ π+mlist];
␈↓"β␈↓ α←␈↓α␈↓ ¬/ ␈↓
t␈↓α → match␈↓λ'␈↓α[␈↓ εKsuffix[pat];
␈↓"β␈↓ α←␈↓α␈↓ ¬/␈↓ εKsuffix[exp];
␈↓"β␈↓ α←␈↓α␈↓ ¬/␈↓ εKmatch␈↓λ'␈↓α[prefix[pat];prefix[exp];mlist]]
␈↓"∀␈↓ α←␈↓αcheck[var;exp;val;mlist] <= [␈↓ ¬Gnot[val] → concat[mkent[var;exp];mlist];
␈↓"β␈↓ α←␈↓α␈↓ ¬; sameconst[exp;val] → mlist;
␈↓"β␈↓ α←␈↓α␈↓ ¬; ␈↓
t␈↓α → NO]
␈↓"∀␈↓ α←␈↓αlookup[var;l] <=␈↓ ∧7[null[l] → ␈↓
f␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ∧7 samevar[var;name[first[l]]] → val[first[l]];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 ␈↓
t␈↓α → lookup[var;rest[l]]]
␈↓"β␈↓ α←␈↓To␈α∂complete␈α∂our␈α∂description␈α∂of␈α∞␈↓αmatch␈↓␈α∂we␈α∂should␈α∂supply␈α∂the␈α∂data␈α∞structure
␈↓ α←␈↓manipulating␈α
functions:␈α
␈↓αisconst␈↓,␈α
␈↓αisvar␈↓,␈α␈↓αprefix␈↓,␈α
␈↓αsuffix␈↓,␈α
␈↓αsamevar␈↓,␈α
and␈α␈↓αsameconst␈↓;
␈↓ α←␈↓and␈α∪␈↓αmkent␈↓,␈α∪␈↓αname␈↓,␈α∀and␈α∪␈↓αval␈↓.␈α∪The␈α∪first␈α∀five␈α∪are␈α∪related,␈α∪dealing␈α∀with␈α∪the
␈↓ α←␈↓representation␈α∞of␈α∞patterns;␈α∞the␈α∂final␈α∞three␈α∞involve␈α∞the␈α∞representation␈α∂of␈α∞the
␈↓ α←␈↓match␈αlist.␈α
Note␈αthat␈αwe␈α
␈↓¬have␈↓␈αassumed␈α
that␈α␈↓αmlist␈↓␈αis␈α
a␈αlist.␈α
We␈αwill␈αrestrict␈α
the
␈↓ α←␈↓match␈αalgorithm␈αto␈αsimple␈αmatches␈αon␈αtree␈αstructure.␈α We␈αrepresent␈α␈↓αprefix␈↓␈αas
␈↓ α←␈↓␈↓αfirst␈↓␈α∪and␈α∩␈↓αsuffix␈↓␈α∪and␈α∩␈↓αrest␈↓␈α∪though␈α∩much␈α∪more␈α∩general␈α∪interpretations␈α∩are
␈↓ α←␈↓possible.␈α
We␈α
leave␈α
it␈α
to␈α
the␈αreader␈α
to␈α
supply␈α
representations␈α
of␈α
the␈αmissing
␈↓ α←␈↓functions.
␈↓"β␈↓ α←␈↓␈↓ β'Given␈α∂a␈α∂basic␈α⊂pattern␈α∂matcher,␈α∂we␈α∂can␈α⊂begin␈α∂to␈α∂elaborate␈α∂on␈α⊂a␈α∂data
␈↓ α←␈↓base␈α
management␈α
system.␈α
We␈α
need␈α
some␈α
means␈α
of␈α
controlling␈α
the␈αmatcher.␈α
If
␈↓ α←␈↓several␈αentries␈αin␈αthe␈αsystem␈αmatch␈αthe␈αinquiry,␈αthen␈αwe␈αmust␈αdecide␈α
how␈αto
␈↓ α←␈↓manage␈α∃the␈α∃matches.␈α∀In␈α∃simple␈α∃cases␈α∀we␈α∃could␈α∃make␈α∀a␈α∃list␈α∃of␈α∃all␈α∀the
␈↓ α←␈↓possibilities.␈α If␈αthe␈αnumber␈α
of␈αmatches␈αis␈αvery␈α
large␈αwe␈αmight␈αwant␈αto␈α
return
␈↓ α←␈↓a␈αfew␈αat␈αa␈αtime,␈αremembering␈αwhere␈αwe␈αwere␈αin␈αthe␈αsearch␈αof␈αthe␈αbase.␈α The
␈↓ α←␈↓natural␈αextension␈αof␈αthis␈αidea␈αis␈αto␈αallow␈αa␈αpotentially␈αinfinite␈αset␈αof␈αelements
␈↓ α←␈↓present␈α
in␈α
the␈α
data␈α
base.␈α
In␈α
programming␈α
languages␈α
we␈α
are␈α
able␈α
to␈αtalk␈α
about
␈↓ α←␈↓such potentialities by using a procedure.
␈↓ α←␈↓␈↓2.5␈↓
Data Bases 73␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Instead␈α∂of␈α∂having␈α∂objects␈α∂explicitly␈α∞stored␈α∂in␈α∂the␈α∂base,␈α∂we␈α∂may␈α∞allow
␈↓ α←␈↓procedures␈αto␈αoccur␈αas␈αdata␈αbase␈αelements.␈α Such␈αa␈αprocedure␈αwould␈αgenerate
␈↓ α←␈↓elements.␈α
For␈α
example,␈α
instead␈α
of␈α
storing␈α
the␈α
integers␈α
as␈α
explicit␈α∞objects,␈α
we
␈↓ α←␈↓could␈α⊗store␈α⊗a␈α⊗procedure␈α↔to␈α⊗generate␈α⊗the␈α⊗integers.␈α⊗This␈α↔introduces␈α⊗two
␈↓ α←␈↓problems:␈αhow␈αdo␈αwe␈αstore␈αprocedures␈αas␈αdata␈αobjects;␈αand,␈αassuming␈αthat␈α
we
␈↓ α←␈↓have␈αcalled␈αsuch␈αa␈αprocedure␈αand␈αit␈αhas␈αdelivered␈αan␈αexplicit␈αobject,␈αhow␈αdo
␈↓ α←␈↓we␈α∞represent␈α
the␈α∞notion␈α∞that␈α
the␈α∞␈↓¬next␈↓␈α
time␈α∞we␈α∞call␈α
that␈α∞procedure,␈α∞we␈α
want
␈↓ α←␈↓the␈α␈↓¬next␈↓␈αobject?␈α That␈αis,␈αa␈αprocedure␈αnamed␈α␈↓αget_next_integer␈↓␈αshould␈αreturn
␈↓ α←␈↓␈↓α1␈↓␈αthe␈αfirst␈αtime␈αit␈αis␈αcalled,␈αbut␈αknow␈αto␈αreturn␈α␈↓α2␈↓␈αthe␈αnext␈αtime␈αit␈αis␈αcalled␈αin
␈↓ α←␈↓the␈α
same␈α
context.␈α
It␈α
must␈α
also␈α
know␈α∞to␈α
return␈α
␈↓α1␈↓␈α
when␈α
it␈α
is␈α
called␈α
in␈α∞a␈α
new
␈↓ α←␈↓context.
␈↓"β␈↓ α←␈↓␈↓ β'Other␈αpossible␈α
extensions␈αinvolve␈α
the␈αoperations␈α
on␈αthe␈α
base.␈α Assume
␈↓ α←␈↓that␈αthe␈αbase␈αcontains␈α"all␈αroses␈αare␈αred"␈αand␈αknows␈αthat␈αobject␈αO␈↓β1␈↓␈αis␈αa␈αrose;
␈↓ α←␈↓if␈α
we␈α
ask␈αthe␈α
data␈α
base␈αfor␈α
all␈α
red␈αobjects,␈α
we␈α
should␈αexpect␈α
to␈α
see␈αO␈↓β1␈↓␈α
appear
␈↓ α←␈↓as␈α∞a␈α∞candidate.␈α∞That␈α∞expectation␈α
requires␈α∞a␈α∞deductive␈α∞ability␈α∞built␈α∞into␈α
the
␈↓ α←␈↓base␈α
manipulator.␈α
That␈αis,␈α
we␈α
need␈α
not␈αhave␈α
explicitly␈α
stored␈αthe␈α
information
␈↓ α←␈↓in␈αthe␈αbase,␈αbut␈αwe␈αexpect␈αto␈α
be␈αable␈αto␈αdeduce␈αfacts␈αfrom␈αinformation␈αin␈α
the
␈↓ α←␈↓base using some relationships and reasoning ability.
␈↓"β␈↓ α←␈↓␈↓ β'There␈αare␈αat␈αleast␈αtwo␈αways␈α
the␈α"roses␈αare␈αred"␈αproblem␈αcan␈α
be␈αsolved.
␈↓ α←␈↓Notice␈α⊂that␈α⊂"all␈α⊃roses␈α⊂are␈α⊂red"␈α⊃is␈α⊂much␈α⊂like␈α⊃a␈α⊂procedure;␈α⊂given␈α⊃an␈α⊂object
␈↓ α←␈↓which␈α∞is␈α∞a␈α
rose,␈α∞it␈α∞generates␈α∞an␈α
object␈α∞which␈α∞is␈α∞red.␈α
So,␈α∞on␈α∞entering␈α∞a␈α
rose
␈↓ α←␈↓object␈αin␈αthe␈αdata␈αbase,␈αthe␈αsystem␈αcould␈αalso␈αexplicitly␈αadd␈αthe␈αfact␈αthat␈αthe
␈↓ α←␈↓rose␈α∪was␈α∪red.␈α∀This␈α∪is␈α∪an␈α∀example␈α∪of␈α∪an␈α∪␈↓↓input␈α∀demon␈↓.␈α∪A␈α∪demon␈α∀is␈α∪a
␈↓ α←␈↓procedure␈αwhich␈αis␈αnot␈αexplicitly␈αcalled␈αbut␈αis␈αactivated␈αby␈αthe␈αoccurrence␈αof
␈↓ α←␈↓another␈α∂event.␈α∂ Whenever␈α∂an␈α∂object␈α∂is␈α∞added␈α∂to␈α∂the␈α∂base␈α∂the␈α∂collection␈α∞of
␈↓ α←␈↓input␈αdemons␈αis␈αchecked.␈αIf␈αan␈αapplicable␈αdemon␈αis␈αfound,␈αit␈αis␈αactivated;␈αits
␈↓ α←␈↓activation might activate other demons.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∞activation␈α∞of␈α∞a␈α∞demon␈α∞is␈α
a␈α∞different␈α∞kind␈α∞of␈α∞procedure␈α∞call␈α
than
␈↓ α←␈↓previously␈α
seen.␈αThe␈α
activation␈αis␈α
done␈α
on␈αpattern␈α
matching␈αrather␈α
than␈αby␈α
a
␈↓ α←␈↓user-initiated␈α∪call.␈α∪ Thus␈α∩the␈α∪calling␈α∪style␈α∩is␈α∪generally␈α∪known␈α∪as␈α∩␈↓↓pattern
␈↓ α←␈↓↓directed␈αinvocation␈↓ ([Hew 72], [Bau 72]).␈α The␈αdemon␈αprocedure␈αis␈αstored␈αin
␈↓ α←␈↓the␈α∪data␈α∪base␈α∪along␈α∪with␈α∪a␈α∪pattern␈α∪which␈α∪determines␈α∪conditions␈α∪for␈α∩its
␈↓ α←␈↓activation.␈α∞In␈α∞the␈α∂case␈α∞of␈α∞an␈α∂input␈α∞demon,␈α∞an␈α∞input␈α∂to␈α∞the␈α∞base␈α∂initiates␈α∞a
␈↓ α←␈↓match␈α
of␈α
the␈α
input␈α
demon␈α
patterns␈αagainst␈α
the␈α
input.␈α
If␈α
a␈α
match␈α
is␈αfound,␈α
the
␈↓ α←␈↓corresponding␈α_procedures␈α_are␈α_executed.␈α_ The␈α_match␈α_process␈α→can␈α_bind
␈↓ α←␈↓variables␈αto␈α
parts␈αof␈αpatterns␈α
and␈αtherefore␈αthe␈α
procedure␈αtypically␈αhas␈α
access
␈↓ α←␈↓to the match information.
␈↓"β␈↓ α←␈↓␈↓ β'Let's␈α∀establish␈α∀some␈α∀notation␈α∀and␈α∀give␈α∀an␈α∀example.␈α∀ To␈α∀introduce
␈↓ α←␈↓records␈α∩to␈α∩our␈α∩system␈α∩we␈α∪use␈α∩a␈α∩unary␈α∩procedure␈α∩named␈α∪␈↓αadd_item␈↓.␈α∩ The
␈↓ α←␈↓argument to ␈↓αadd_item␈↓ is the record we wish to add.
␈↓"∀␈↓ α←␈↓␈↓ ¬I␈↓αadd_item[(ROSE O1)]␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'We␈αwill␈αuse␈αa␈αternary␈αprocedure␈αnamed␈α␈↓αadd_demon␈↓␈αto␈αinsert␈αdemons␈αin
␈↓ α←␈↓the␈αbase.␈αThe␈αfirst␈αargument␈αis␈αthe␈αtype␈αof␈αdemon;␈αso␈αfar␈αwe␈αhave␈αdiscussed
␈↓ α←␈↓␈↓74 Applications␈↓
(2.5␈↓
␈↓"β␈↓ α←␈↓demons␈α∂invoked␈α∂by␈α∂adding␈α∂elements;␈α∂we␈α∂will␈α∂also␈α∂have␈α∂demons␈α∂which␈α∂are
␈↓ α←␈↓applied␈α∂when␈α∂items␈α∂are␈α∂removed,␈α∂or␈α∂when␈α∂items␈α∂are␈α∂accessed.␈α∂These␈α∂three
␈↓ α←␈↓types␈α
will␈αbe␈α
named␈α␈↓αADD␈↓,␈α
␈↓αREMOVE␈↓,␈αand␈α
␈↓αFETCH␈↓.␈α The␈α
second␈αargument␈α
is
␈↓ α←␈↓the␈α⊂pattern␈α⊂which␈α⊂will␈α⊂invoke␈α⊂this␈α∂demon;␈α⊂and␈α⊂the␈α⊂third␈α⊂argument␈α⊂is␈α∂the
␈↓ α←␈↓action to be taken if the pattern matches. For example:
␈↓"∀␈↓ α←␈↓␈↓ ∧∀␈↓αadd_demon[ADD;(ROSE ␈↓εa␈↓α);add_item[(RED ␈↓εa␈↓α))]]␈↓
␈↓"∀␈↓ α←␈↓Demons are also used to monitor the removal of information from the base.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
third␈αuse␈α
of␈αdemons␈α
is␈αinvolved␈α
with␈αanother␈α
possible␈α
solution␈αto
␈↓ α←␈↓the␈α"all␈αroses␈αare␈αred"␈αproblem.␈α Instead␈αof␈αexplicitly␈αadding␈αthe␈αfact␈αthat␈α␈↓αO1␈↓
␈↓ α←␈↓is␈αa␈α
red␈αobject␈α
we␈αmight␈α
wait␈αuntil␈α
a␈αrequest␈α
for␈αred␈α
objects␈αoccurs.␈α
At␈αthat
␈↓ α←␈↓time␈αwe␈α
could␈αuse␈α
the␈α"all␈α
roses␈αare␈α
red"␈αdemon␈α
␈↓¬backwards␈↓.␈α That␈α
is,␈αwe␈α
could
␈↓ α←␈↓look␈αfor␈αany␈αroses␈α
in␈αthe␈αdata␈αbase;␈α
the␈αassertion␈αthat␈αa␈α
"rose"␈αobject␈αis␈αalso␈α
a
␈↓ α←␈↓"red"␈α⊂object␈α⊂allows␈α⊂us␈α⊂to␈α⊂accept␈α⊂"rose"␈α⊂objects␈α⊂as␈α⊂solutions␈α⊂to␈α⊂our␈α∂inquiry.
␈↓ α←␈↓This␈α
feature␈αintroduces␈α
a␈α
certain␈αdeductive␈α
capability␈α
to␈αour␈α
system.␈α
It␈αalso
␈↓ α←␈↓introduces some organizational problems.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α
have␈α
to␈α
recognize␈α
when␈αa␈α
procedure␈α
is␈α
capable␈α
of␈αproducing␈α
objects
␈↓ α←␈↓of␈α⊂the␈α⊃desired␈α⊂type.␈α⊃We␈α⊂therefore␈α⊂index␈α⊃these␈α⊂data␈α⊃base␈α⊂procedures␈α⊃by␈α⊂a
␈↓ α←␈↓pattern␈α∞which␈α∞tells␈α
what␈α∞the␈α∞procedure␈α
accomplishes.␈α∞That␈α∞pattern␈α∞is␈α
called
␈↓ α←␈↓the␈α∃procedure's␈α∃goal␈α∀and␈α∃the␈α∃invocation␈α∃of␈α∀such␈α∃a␈α∃procedure␈α∃is␈α∀again
␈↓ α←␈↓pattern-directed, but has an added connotation of being ␈↓↓goal-oriented␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Again,␈αwe␈αintroduce␈αsome␈αnotation␈αand␈αan␈αexample.␈αLet␈αthe␈αrequest␈α
for
␈↓ α←␈↓a data base item be given by:
␈↓"∀␈↓ α←␈↓α␈↓ ¬↔fetch[␈↓εa␈↓α]␈↓, where ␈↓εa␈↓ is a pattern.
␈↓"∀␈↓ α←␈↓Since␈α∂a␈α∂␈↓αfetch␈↓␈α∂request␈α∂might␈α∂discover␈α∂several␈α∂possibilities,␈α∂some␈α∂being␈α∞items
␈↓ α←␈↓and␈α∞some␈α∞being␈α∞goal-directed␈α∞procedures,␈α∞we␈α∞need␈α∞a␈α∞way␈α∞of␈α∂examining␈α∞the
␈↓ α←␈↓selected information.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αintroduce␈αa␈αfunction␈αnamed␈α␈↓αtry_next␈↓,␈αwhose␈αsingle␈αargument␈αis␈αthe
␈↓ α←␈↓result␈αof␈αa␈α␈↓αfetch␈↓.␈αEach␈αcall␈αon␈α␈↓αtry_next␈↓␈αeither␈αproduces␈αa␈αnew␈αitem␈αor␈αsignals
␈↓ α←␈↓that no more items exist on the fetch list.
␈↓"β␈↓ α←␈↓␈↓ β'An␈α∞extension␈α
to␈α∞this␈α
basic␈α∞data␈α
base␈α∞manipulating␈α
system␈α∞has␈α
become
␈↓ α←␈↓convenient␈αin␈αartificial␈αintelligence␈αresearch.␈αLet␈αus␈αassume␈αwe␈αwish␈αto␈α
derive
␈↓ α←␈↓a␈α
plan␈αor␈α
scheme␈αfor␈α
achieving␈αa␈α
desired␈αgoal.␈α
In␈αthe␈α
derivation␈α
process␈αwe
␈↓ α←␈↓will␈αmake␈αhypotheses␈αand␈αthen␈αpursue␈αtheir␈αimplications.␈αA␈αsimilar␈αbehavior
␈↓ α←␈↓can␈α
be␈α
simulated␈α
if␈α
we␈α
allow␈αthe␈α
creation␈α
of␈α
multiple␈α
data␈α
bases.␈α
Each␈αbase
␈↓ α←␈↓corresponds␈α⊃to␈α⊃a␈α⊃hypothetical␈α⊃situation␈α⊃or␈α⊃world,␈α⊃and␈α⊃the␈α⊃␈↓αfetch␈↓-ing␈α⊃of␈α⊂an
␈↓ α←␈↓object␈α⊂in␈α∂a␈α⊂world␈α∂corresponds␈α⊂to␈α∂asking␈α⊂whether␈α∂or␈α⊂not␈α∂a␈α⊂desired␈α⊂state␈α∂is
␈↓ α←␈↓attainable in that world.
␈↓"β␈↓ α←␈↓␈↓ β'Instead␈α⊂of␈α∂requiring␈α⊂that␈α∂all␈α⊂transformations␈α∂occur␈α⊂in␈α∂one␈α⊂data␈α∂base,
␈↓ α←␈↓several␈αsystems␈α
([Con 73],␈α[QA4 72])␈α
have␈αimplemented␈α
a␈αlayered␈α
data␈αbase.
␈↓ α←␈↓In␈α∂this␈α⊂situation␈α∂we␈α⊂are␈α∂able␈α⊂to␈α∂add,␈α⊂delete␈α∂and␈α⊂fetch␈α∂from␈α⊂specified␈α∂data
␈↓ α←␈↓bases.␈α⊂We␈α∂add␈α⊂two␈α⊂operations␈α∂␈↓αpush_base␈↓␈α⊂and␈α⊂␈↓αpop_base␈↓␈α∂which␈α⊂allow␈α⊂us␈α∂to
␈↓ α←␈↓manipulate whole data bases as objects.
␈↓ α←␈↓␈↓2.5␈↓
Data Bases 75␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈α≥control␈α≤structures␈α≥necessary␈α≥for␈α≤handling␈α≥such␈α≥data␈α≤base
␈↓ α←␈↓manipulations␈αmay␈αbe␈αvery␈αnon-structured;␈αsome␈αof␈αthe␈αimplementation␈αideas
␈↓ α←␈↓for␈α∪such␈α∪control␈α∪will␈α∪be␈α∪discussed␈α∪in␈α∪Section 4.5.␈α∪ We␈α∪will␈α∪discuss␈α∪some
␈↓ α←␈↓details␈α↔of␈α↔the␈α⊗data␈α↔structure␈α↔implementation␈α⊗in␈α↔Section 5.6.␈α↔ For␈α⊗more
␈↓ α←␈↓information see [McD 75] and [Con 73].
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Recall␈α∂our␈α⊂discussion␈α∂of␈α⊂␈↓αmatch␈↓␈α∂on␈α∂page 72.␈α⊂Supply␈α∂a␈α⊂representation␈α∂for
␈↓ α←␈↓␈↓ β∂match lists and supply the eight data structure functions.
␈↓" ␈↓ α←␈↓2. The␈α∩␈↓αmatch␈↓␈α∪routine␈α∩we␈α∪developed␈α∩on␈α∪page 72␈α∩required␈α∪that␈α∩␈↓αpat␈↓␈α∪be␈α∩a
␈↓ α←␈↓␈↓ β∂constant␈α∩pattern.␈α∩Write␈α∩a␈α∩more␈α∩general␈α∩pattern␈α∩matcher␈α∩named␈α∩␈↓αunify␈↓
␈↓ α←␈↓␈↓ β∂which␈α∞allows␈α∞either␈α
␈↓αpat␈↓␈α∞or␈α∞␈↓αexp␈↓␈α∞to␈α
contain␈α∞variables.␈α∞ This␈α∞more␈α
gereral
␈↓ α←␈↓␈↓ β∂match routine is called a unifier ([Rob 65]).
␈↓" ␈↓ α←␈↓␈↓ β'For example:
␈↓""␈↓ α←␈↓␈↓ βc␈↓αunify[(A (B ␈↓εa␈↓α) A); (A (␈↓εb␈↓α D) ␈↓εd␈↓α)] = ((␈↓εa␈↓α D) (␈↓εb␈↓α B) (␈↓εd␈↓α A))␈↓
␈↓"⊃␈↓ α←␈↓α␈↓ βcunify[(A (B ␈↓εa␈↓α) A); (A (␈↓εb␈↓α D) ␈↓εb␈↓α)] = NO
␈↓"⊃␈↓ α←␈↓α␈↓ βcunify[(␈↓εa␈↓α A ␈↓εa␈↓α); (␈↓εb␈↓α ␈↓εb␈↓α B)] = NO
␈↓"β␈↓ α←␈↓␈↓ ¬≠␈↓↓2.6 Algebra of Polynomials␈↓
␈↓"β␈↓ α←␈↓Assume␈αthat␈αwe␈αwant␈αto␈αperform␈αaddition␈αand␈αmultiplication␈αof␈αpolynomials
␈↓ α←␈↓and␈α⊃further␈α⊃assume␈α⊃that␈α⊃each␈α⊃polynomial␈α⊃is␈α⊃of␈α⊃the␈α⊃form␈α⊂␈↓αp␈↓β1␈↓α + p␈↓β2␈↓α + ... + p␈↓βn␈↓
␈↓ α←␈↓where␈α∪each␈α∩term,␈α∪␈↓αp␈↓βi␈↓,␈α∩is␈α∪a␈α∪product␈α∩of␈α∪variables␈α∩and␈α∪constants.␈α∪ The␈α∩two
␈↓ α←␈↓components␈α∞of␈α∞each␈α
term␈α∞are␈α∞a␈α
constant␈α∞part␈α∞called␈α
the␈α∞coefficient,␈α∞and␈α
the
␈↓ α←␈↓variable␈α⊂part.␈α⊂ We␈α⊂shall␈α⊂assume␈α⊃without␈α⊂loss␈α⊂of␈α⊂generality␈α⊂that␈α⊂the␈α⊃set␈α⊂of
␈↓ α←␈↓variables␈α∩which␈α∩appear␈α∩in␈α∩the␈α∩polynomials␈α∩are␈α∩lexicographically␈α⊃ordered,
␈↓ α←␈↓e.g. ␈↓αx < y < z␈↓;␈α∞and␈α∞assume␈α∞that␈α∞each␈α∞variable␈α∞part␈α∞obeys␈α∞that␈α∂ordering;␈α∞thus
␈↓ α←␈↓we␈αwould␈αinsist␈αthat␈α␈↓αxzy␈↓π2␈↓␈αbe␈αwritten␈α␈↓αxy␈↓π2␈↓αz␈↓.␈α We␈αdo␈αnot␈αassume␈αthat␈αthe␈αterms
␈↓ α←␈↓are␈α_ordered␈α_within␈α→the␈α_polynomial;␈α_thus␈α→␈↓αx + xy␈↓␈α_and␈α_␈↓αxy + x␈↓␈α→are␈α_both
␈↓ α←␈↓acceptable.␈α We␈αfurther␈αassume␈αthat␈αthe␈αvariables␈αof␈αeach␈α␈↓αp␈↓βi␈↓␈αare␈αdistinct␈αand
␈↓ α←␈↓that␈αno␈α
␈↓αp␈↓βi␈↓␈αhas␈α␈↓α0␈↓␈α
as␈αits␈α
coefficient.␈α The␈αstandard␈α
algorithm␈αfor␈α
the␈αaddition
␈↓ α←␈↓of␈α⊂␈↓λS␈↓πn␈↓βi=1␈↓αp␈↓βi␈↓␈α⊃with␈α⊂␈↓λS␈↓πm␈↓βj=1␈↓αq␈↓βj␈↓␈α⊂indicates␈α⊃that␈α⊂␈↓αq␈↓βj␈↓␈α⊂can␈α⊃be␈α⊂combined␈α⊂with␈α⊃a␈α⊂␈↓αp␈↓βi␈↓␈α⊃if␈α⊂the
␈↓ α←␈↓variable␈α
parts␈αof␈α
these␈α
terms␈αare␈α
identical.␈α In␈α
this␈α
case␈αthe␈α
resulting␈αterm␈α
has
␈↓ α←␈↓the␈α∀same␈α∀variable␈α∀part␈α∀but␈α∀has␈α∀a␈α∀coefficient␈α∀equal␈α∀to␈α∀the␈α∀sum␈α∃of␈α∀the
␈↓ α←␈↓coefficients␈α~of␈α~␈↓αp␈↓βi␈↓␈α~and␈α~␈↓αq␈↓βj␈↓.␈α~ We␈α~will␈α~examine␈α~four␈α≠representations␈α~of
␈↓ α←␈↓polynomials,␈α
before␈α
finally␈α
writing␈α∞any␈α
algorithms.␈α
To␈α
aid␈α
in␈α∞the␈α
discussion
␈↓ α←␈↓we will use the polynomial ␈↓αx␈↓π2␈↓α - 2y - z␈↓ as our canonical example.
␈↓ α←␈↓␈↓76 Applications␈↓
(2.6␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬S␈↓↓First representation␈↓
␈↓"∀␈↓ α←␈↓We␈αcould␈αuse␈αthe␈αrepresentation␈αof␈αthe␈αdifferentiation␈αexample.␈α This␈αwould
␈↓ α←␈↓result in our example assuming the form:
␈↓"∀␈↓ α←␈↓α␈↓ α}(PLUS (TIMES 1 (EXPT X 2)) (PLUS (TIMES -2 Y) (TIMES -1 Z)))
␈↓"∀␈↓ α←␈↓The␈αabove␈α
conventions␈αspecify␈αan␈α
unambiguous␈αrepresentation␈αfor␈α
our␈αclass
␈↓ α←␈↓of␈αpolynomials.␈αStrictly␈αspeaking,␈αwe␈αdid␈αnot␈αneed␈αto␈αimpose␈αthe␈αordering␈αon
␈↓ α←␈↓the␈αset␈αof␈αvariables.␈αHowever,␈αwe␈αneed␈αto␈αimpose␈αsome␈αadditional␈αconstraints
␈↓ α←␈↓before␈α↔we␈α↔have␈α↔data␈α↔structures␈α⊗which␈α↔are␈α↔well-suited␈α↔to␈α↔the␈α↔class␈α⊗of
␈↓ α←␈↓polynomial algorithms we wish to represent.
␈↓"β␈↓ α←␈↓␈↓ ¬D␈↓↓Second representation␈↓
␈↓"∀␈↓ α←␈↓We␈αare␈αreally␈αonly␈αinterested␈αin␈αtesting␈αthe␈αequality␈αof␈αthe␈αvariable␈αparts;␈αwe
␈↓ α←␈↓will␈α⊃not␈α⊃be␈α⊃manipulating␈α⊃variable␈α⊃parts␈α⊃in␈α⊃any␈α⊃other␈α⊃way.␈α⊃ So␈α⊃we␈α⊂might
␈↓ α←␈↓simply␈α⊂represent␈α⊂the␈α∂variable␈α⊂part␈α⊂as␈α∂a␈α⊂list␈α⊂of␈α∂pairs;␈α⊂each␈α⊂pair␈α⊂contains␈α∂a
␈↓ α←␈↓variable␈αname␈αand␈αthe␈α
corresponding␈αvalue␈αof␈αthe␈αexponent.␈α
Knowing␈αthat
␈↓ α←␈↓polynomials␈αare␈α
always␈αsums,␈α
and␈αknowing␈αthe␈α
class␈αof␈α
algorithms␈αwe␈αwish␈α
to
␈↓ α←␈↓implement, we write ␈↓λS ␈↓αp␈↓βi␈↓ as:
␈↓"∀␈↓ α←␈↓␈↓ ¬!␈↓α( (␈↓rep of ␈↓αp␈↓β1␈↓α), (␈↓rep of ␈↓αp␈↓β2␈↓α), ...)␈↓
␈↓"∀␈↓ α←␈↓This representation would make our example appears as:
␈↓"∀␈↓ α←␈↓α␈↓ β&((TIMES 1 ((X . 2))) (TIMES -2 ((Y . 1))) (TIMES -1 ((Z . 1))))
␈↓"∀␈↓ α←␈↓This␈αrepresentation␈αis␈αsufficient␈αand␈αit␈αdoes␈αhave␈αthe␈αflexibility␈αwe␈αneed,␈αbut
␈↓ α←␈↓it␈α
is␈αstill␈α
not␈αterribly␈α
satisfying.␈α We␈α
are␈α
ignoring␈αtoo␈α
much␈αof␈α
the␈αstructure␈α
in
␈↓ α←␈↓our class of polynomials.
␈↓"β␈↓ α←␈↓␈↓ ¬L␈↓↓Third representation␈↓
␈↓"∀␈↓ α←␈↓We␈α
know␈α
that␈α
the␈α
occurrence␈α∞of␈α
variables␈α
is␈α
ordered␈α
in␈α
each␈α∞variable␈α
part;
␈↓ α←␈↓we␈αcan␈αassume␈αthat␈αwe␈αknow␈αthe␈αclass␈αof␈αvariables␈αwhich␈αmay␈αappear␈αin␈α
any
␈↓ α←␈↓polynomial. So instead of writing ␈↓αx␈↓π2␈↓αy␈↓π3␈↓αz␈↓ as
␈↓"∀␈↓ α←␈↓␈↓ ¬A␈↓α((X . 2) (Y . 3) (Z . 1))␈↓,
␈↓" ␈↓ α←␈↓we could write: ␈↓α(2 3 1)␈↓ assuming ␈↓αx, y, z␈↓ are the only variables.
␈↓"∀␈↓ α←␈↓In␈α∞a␈α∞further␈α∞simplification,␈α∞notice␈α∂that␈α∞the␈α∞␈↓αTIMES␈↓␈α∞in␈α∞the␈α∂representation␈α∞is
␈↓ α←␈↓superfluous.␈α
We␈α
␈↓¬always␈↓␈α∞multiply␈α
the␈α
coefficient␈α
by␈α∞the␈α
variable␈α
part.␈α∞So␈α
we
␈↓ α←␈↓could␈α∀simply␈α∃␈↓αconcat␈↓␈α∀the␈α∀coefficient␈α∃onto␈α∀the␈α∀front␈α∃of␈α∀the␈α∃variable␈α∀part
␈↓ α←␈↓representation.
␈↓"∀␈↓ α←␈↓␈↓ β'Let's stop for some examples.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ ¬;␈↓↓term␈↓ εorepresentation
␈↓"β␈↓ α←␈↓↓␈↓ β'␈↓ ¬;␈↓α2xyz␈↓ εo(2 1 1 1)
␈↓" ␈↓ α←␈↓α␈↓ β'␈↓ ¬;2x␈↓π2␈↓αz␈↓ εo(2 2 0 1)
␈↓" ␈↓ α←␈↓α␈↓ β'␈↓ ¬;4z␈↓π3␈↓α␈↓ εo(4 0 0 3)
␈↓ α←␈↓␈↓2.6␈↓ πoAlgebra of Polynomials 77␈↓
␈↓"β␈↓ α←␈↓Thus our canonical polynomial would now be represented as:
␈↓"∀␈↓ α←␈↓α␈↓ ¬∂((1 2 0 0) (-2 0 1 0) (-1 0 0 1))
␈↓"∀␈↓ α←␈↓This␈α⊗representation␈α⊗is␈α⊗not␈α⊗too␈α⊗bad;␈α⊗the␈α⊗␈↓αfirst␈↓-part␈α⊗of␈α⊗any␈α⊗term␈α⊗is␈α∃the
␈↓ α←␈↓coefficient;␈α∪the␈α∪␈↓αrest␈↓-part␈α∪is␈α∪the␈α∪variable␈α∪part.␈α∪ For␈α∪example,␈α∪the␈α∀test␈α∪for
␈↓ α←␈↓equality of variable parts is now simply a call on ␈↓αequal␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Let's start thinking about the structure of the main algorithm.
␈↓"β␈↓ α←␈↓␈↓ ¬E␈↓↓Fourth representation␈↓
␈↓"∀␈↓ α←␈↓The␈αalgorithm␈α
for␈αthe␈α
sum␈αmust␈α
compare␈αterms.␈α
Finding␈αsimilar␈α
terms,␈αit␈α
will
␈↓ α←␈↓generate␈α∪an␈α∪appropriate␈α∪new␈α∪term,␈α∪otherwise␈α∪it␈α∪simply␈α∪copies␈α∪the␈α∩terms.
␈↓ α←␈↓When␈α∩we␈α∩pick␈α∪a␈α∩␈↓αp␈↓βi␈↓␈α∩from␈α∪the␈α∩first␈α∩polynomial␈α∩we␈α∪would␈α∩like␈α∩to␈α∪find␈α∩a
␈↓ α←␈↓corresponding␈α∪␈↓αq␈↓βj␈↓␈α∪with␈α∪the␈α∪minimum␈α∪amount␈α∪of␈α∪searching.␈α∪ This␈α∪can␈α∪be
␈↓ α←␈↓accomplished␈α∀if␈α∀we␈α∀can␈α∀order␈α∀the␈α∀terms␈α∀in␈α∀the␈α∀polynomials.␈α∀A␈α∪natural
␈↓ α←␈↓ordering␈α≠can␈α≠be␈α≤induced␈α≠on␈α≠the␈α≤terms␈α≠by␈α≠ordering␈α≤the␈α≠numerical
␈↓ α←␈↓representation␈α∀of␈α∃the␈α∀exponents.␈α∃ For␈α∀sake␈α∀of␈α∃argument,␈α∀assume␈α∃that␈α∀a
␈↓ α←␈↓maximum␈α∞of␈α∞two␈α
digits␈α∞will␈α∞be␈α
needed␈α∞to␈α∞express␈α
the␈α∞exponent␈α∞of␈α∞any␈α
one
␈↓ α←␈↓variable.␈αThus␈αthe␈αexponent␈αof␈α␈↓αx␈↓π2␈↓␈α
will␈αbe␈αrepresented␈αas␈α␈↓α02␈↓,␈αor␈αthe␈α
exponent
␈↓ α←␈↓of␈α_␈↓αz␈↓π10␈↓␈α_will␈α→be␈α_represented␈α_as␈α_␈↓α10␈↓.␈α→Combining␈α_this␈α_with␈α→our␈α_ordered
␈↓ α←␈↓representation of variable parts, we arrive at:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ¬;␈↓↓term␈↓ εorepresentation
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ¬;43x␈↓π2␈↓αy␈↓π3␈↓αz␈↓π4␈↓ εo␈↓α(43, 020304)
␈↓" ␈↓ α←␈↓α␈↓ β'␈↓ ¬;2x␈↓π2␈↓αz␈↓ εo(2, 020001)
␈↓" ␈↓ α←␈↓α␈↓ β'␈↓ ¬;4z␈↓π3␈↓α␈↓ εo(4, 000003)
␈↓"∀␈↓ α←␈↓Now␈αwe␈αcan␈αorder␈αon␈αthe␈αnumeric␈αrepresentation␈αof␈αthe␈αvariable␈αpart␈αof␈αthe
␈↓ α←␈↓term.␈α≤ One␈α≠more␈α≤change␈α≠of␈α≤representation,␈α≠which␈α≤will␈α≠result␈α≤in␈α≠a
␈↓ α←␈↓simplification in storage requirements:
␈↓"∀␈↓ α←␈↓␈↓ ¬∞represent ␈↓α ax␈↓πA␈↓αy␈↓πB␈↓αz␈↓πC␈↓ as ␈↓α(a . ABC)
␈↓"∀␈↓ α←␈↓This gives our final representation:
␈↓"∀␈↓ α←␈↓α␈↓ ¬∃((1 . 20000) (-2 . 100) (-1 . 1))
␈↓"∀␈↓ α←␈↓Note that ␈↓α20000 > 100 > 1␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Finally␈α→we␈α→will␈α→write␈α→the␈α→algorithm.␈α→ We␈α→will␈α→assume␈α→that␈α→the
␈↓ α←␈↓polynomials␈α∀are␈α∀initially␈α∀ordered␈α∀and␈α∪will␈α∀write␈α∀the␈α∀algorithm␈α∀so␈α∀as␈α∪to
␈↓ α←␈↓maintain␈α⊗that␈α⊗ordering.␈α↔ Each␈α⊗term␈α⊗is␈α⊗a␈α↔dotted␈α⊗pair␈α⊗of␈α↔elements:␈α⊗the
␈↓ α←␈↓coefficient and a representation of the variable part.
␈↓"β␈↓ α←␈↓␈↓ β'As␈α∃in␈α∀the␈α∃previous␈α∀differentiation␈α∃example,␈α∀we␈α∃should␈α∃attempt␈α∀to
␈↓ α←␈↓extract the algorithm from the representation.
␈↓"β␈↓ α←␈↓We shall define:
␈↓"∀␈↓ α←␈↓␈↓ ∧Z␈↓αcoef[x] <= car[x]␈↓ and ␈↓α expo[x] <= cdr[x]
␈↓"∀␈↓ α←␈↓To test the ordering we will use the LISP predicate:
␈↓"∀␈↓ α←␈↓␈↓ ∧H␈↓αgreaterp[x;y] ␈↓gives ␈↓
t␈↓ if ␈↓αx␈↓ is greater than ␈↓αy␈↓.
␈↓ α←␈↓␈↓78 Applications␈↓
(2.6␈↓
␈↓"β␈↓ α←␈↓In␈α∂the␈α∂construction␈α∞of␈α∂the␈α∂`sum'␈α∞polynomial␈α∂we␈α∂will␈α∞generate␈α∂new␈α∂terms␈α∞by
␈↓ α←␈↓combining␈αcoefficients.␈α So␈α
a␈αconstructor␈αnamed␈α
␈↓αmknode␈↓␈αis␈αneeded.␈α
In␈αterms
␈↓ α←␈↓of the latest representation ␈↓αmknode␈↓ is defined as:
␈↓"∀␈↓ α←␈↓␈↓ ¬:␈↓αmknode[x;y] <= cons[x;y]␈↓
␈↓"∀␈↓ α←␈↓So here's a graphical representation of our example polynomial:
␈↓"∀␈↓ α←␈↓␈↓ ε⊃␈↓αx␈↓π2␈↓α - 2y - z ␈↓
␈↓"⊃␈↓ α←␈↓∂ /\
␈↓"␈↓ α←␈↓∂ / \
␈↓"␈↓ α←␈↓∂ / \
␈↓"␈↓ α←␈↓∂ / \
␈↓"␈↓ α←␈↓∂ /\ \
␈↓"␈↓ α←␈↓∂ / \ /\
␈↓"␈↓ α←␈↓∂ 1 20000 / \
␈↓"␈↓ α←␈↓∂ / \
␈↓"␈↓ α←␈↓∂ /\ \
␈↓"␈↓ α←␈↓∂ / \ \
␈↓"␈↓ α←␈↓∂ -2 100 /\
␈↓"␈↓ α←␈↓∂ / \
␈↓"␈↓ α←␈↓∂ / ␈↓αNIL␈↓∂
␈↓"␈↓ α←␈↓∂ /\
␈↓"␈↓ α←␈↓∂ / \
␈↓"␈↓ α←␈↓∂ -1 1
␈↓"β␈↓ α←␈↓Here's the algorithm:
␈↓"∀␈↓ α←␈↓αpolyadd[p;q] <=
␈↓"β␈↓ α←␈↓α␈↓ βc[nullpoly[p] → q;
␈↓"β␈↓ α←␈↓α␈↓ βc nullpoly[q] → p;
␈↓"β␈↓ α←␈↓α␈↓ βc greaterp[expo[first[p]];expo[first[q]]] → concat[first[p];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧␈␈↓ ¬k␈↓ λ␈↓ λ;polyadd[rest[p];q]];
␈↓"β␈↓ α←␈↓α␈↓ βc lessp[expo[first[p]];expo[first[q]]] → concat[first[q];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧␈␈↓ ¬k␈↓ λpolyadd[p;rest[q]]];
␈↓"β␈↓ α←␈↓α␈↓ βc zerop[plus[coef[first[p]];coef[first[q]]]] → polyadd[rest[p];rest[q]];
␈↓"β␈↓ α←␈↓α␈↓ βc ␈↓
t␈↓α → concat[␈↓ ∧␈mknode[␈↓ ¬kplus[coef[first[p]];coef[first[q]]];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧␈␈↓ ¬kexpo[first[p]]];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧␈polyadd[rest[p];rest[q]]]]
␈↓"∀␈↓ α←␈↓α␈↓where:␈↓α␈↓ εW␈↓ ¬Yzerop[x] <= eq[x;0]
␈↓"β␈↓ α←␈↓Notice that our algorithm is quite abstract.
␈↓"β␈↓ α←␈↓␈↓ β'Now for an explanation and example. The form of ␈↓αpolyadd␈↓ is:
␈↓"β␈↓ α←␈↓␈↓α[␈↓↓p␈↓β1␈↓α → ␈↓↓e␈↓β1␈↓α; ␈↓↓p␈↓β2␈↓α → ␈↓↓e␈↓β2␈↓α; ␈↓↓p␈↓β3␈↓α → ␈↓↓e␈↓β3␈↓α; ␈↓↓p␈↓β4␈↓α → ␈↓↓e␈↓β4␈↓α; ␈↓↓p␈↓β5␈↓α → ␈↓↓e␈↓β5␈↓α; ␈↓↓p␈↓β6␈↓α → ␈↓↓e␈↓β6␈↓α]
␈↓ α←␈↓␈↓2.6␈↓ πoAlgebra of Polynomials 79␈↓
␈↓"β␈↓ α←␈↓␈↓↓p␈↓β1␈↓α → ␈↓↓e␈↓β1␈↓ and ␈↓↓p␈↓β2␈↓α → ␈↓↓e␈↓β2␈↓ check if either polynomial is empty.
␈↓" ␈↓ α←␈↓␈↓↓p␈↓β3␈↓α␈α∂→␈α∂␈↓↓e␈↓β3␈↓␈α∂and␈α∂␈↓↓p␈↓β4␈↓α␈α∂→␈α∂␈↓↓e␈↓β4␈↓␈α∂examine␈α∂the␈α∂ordering␈α∂of␈α∂terms␈α∂so␈α∂that␈α⊂the␈α∂resultant
␈↓ α←␈↓␈↓ βWpolynomial retains the ordering.
␈↓" ␈↓ α←␈↓␈↓↓p␈↓β5␈↓ or ␈↓↓p␈↓β6␈↓ will not be reached unless the variable parts are equal.
␈↓" ␈↓ α←␈↓␈↓↓p␈↓β5␈↓α␈α
→␈α
␈↓↓e␈↓β5␈↓.␈α
Since␈α
the␈α
variable␈α
parts␈α
are␈α
equal,␈α
we␈α
can␈α
combine␈α
terms.␈α
However,
␈↓ α←␈↓␈↓ βWwe␈α
must␈αcheck␈α
for␈αcancellations␈α
and␈αnot␈α
include␈αany␈α
terms␈αwith␈α
zero
␈↓ α←␈↓␈↓ βWcoefficient in our resultant polynomial.
␈↓" ␈↓ α←␈↓␈↓↓p␈↓β6␈↓α → ␈↓↓p␈↓β6␈↓. In the final case we must add a new node to our polynomial.
␈↓"β␈↓ α←␈↓Here's an informal execution of ␈↓αpolyadd:
␈↓"∀␈↓ α←␈↓αpolyadd[ x+y+z; x␈↓π2␈↓α-2y-z ]
␈↓"β␈↓ α←␈↓α␈↓ βK= concat[x␈↓π2␈↓α;polyadd[x+y+z; -2y-z]]
␈↓"β␈↓ α←␈↓α␈↓ βK= concat[x␈↓π2␈↓α;concat[x;polyadd[y+z; -2y-z]]]
␈↓"β␈↓ α←␈↓α␈↓ βK= concat[x␈↓π2␈↓α;concat[x;concat[node[1+-2;y];polyadd[z;-z]]]]
␈↓"β␈↓ α←␈↓α␈↓ βK= concat[x␈↓π2␈↓α;concat[x;concat[-y;polyadd[z; -z]]]]
␈↓"β␈↓ α←␈↓α␈↓ βK= concat[x␈↓π2␈↓α;concat[x;concat[-y;polyadd[( );( )]]]]
␈↓"β␈↓ α←␈↓α␈↓ βK= concat[x␈↓π2␈↓α;concat[x;concat[-y;( )]]]
␈↓"β␈↓ α←␈↓α␈↓ ε"= x␈↓π2␈↓α+x-y
␈↓"∀␈↓ α←␈↓Extensive␈α∂work␈α∂has␈α∞been␈α∂done␈α∂on␈α∞polynomial␈α∂manipulating␈α∂algorithms␈α∞for
␈↓ α←␈↓efficient storage and fast execution ([Got 76]).
␈↓"β␈↓ α←␈↓␈↓ ε≡␈↓↓Problem␈↓
␈↓"→␈↓ α←␈↓1. Write␈α⊗an␈α⊗algorithm,␈α⊗␈↓αpolymult␈↓,␈α⊗to␈α⊗perform␈α⊗the␈α⊗multiplication␈α⊗of␈α∃two
␈↓ α←␈↓␈↓ β∂polynomials.
␈↓"β␈↓ α←␈↓␈↓ ¬π␈↓↓2.7 Evaluation of Polynomials␈↓
␈↓"β␈↓ α←␈↓Though␈α
you␈α
are␈α
undoubtedly␈α
quite␈αtired␈α
of␈α
looking␈α
at␈α
polynomials,␈α
there␈αis
␈↓ α←␈↓at␈α
least␈α
one␈α
more␈α
operation␈α
which␈α
is␈α
usefully␈α
performed␈α
on␈αpolynomials.␈α
The
␈↓ α←␈↓operation␈α
is␈αevaluation.␈α
Given␈α
an␈αarbitrary␈α
polynomial,␈αand␈α
values␈α
for␈αany
␈↓ α←␈↓of␈α
the␈α
variables␈αwhich␈α
it␈α
contains,␈αwe␈α
would␈α
like␈αto␈α
compute␈α
its␈α
value.␈αFirst
␈↓ α←␈↓we␈α
will␈αassume␈α
that␈αthe␈α
substitutions␈α
of␈αvalues␈α
for␈αvariables␈α
has␈αalready␈α
been
␈↓ α←␈↓carried␈αout.␈αThus␈αwe␈αare␈αdealing␈αwith␈αpolynomials␈αof␈αthe␈αform:␈α␈↓λS␈↓πn␈↓βi=1␈↓αp␈↓βi␈↓␈αwhere
␈↓ α←␈↓␈↓αp␈↓βi␈↓ is a product of powers of constants. For example:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ε␈↓α2␈↓π3␈↓α + 3*4␈↓π2␈↓α + 5␈↓
␈↓"∀␈↓ α←␈↓This could be represented as:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ β←␈↓α(PLUS (EXPT 2 3) (PLUS (TIMES 3 (EXPT 4 2)) 5))␈↓
␈↓"∀␈↓ α←␈↓We␈α≤have␈α≤taken␈α≤this␈α≠general␈α≤representation␈α≤because␈α≤we␈α≤have␈α≠great
␈↓ α←␈↓expectations of generalizing the resulting algorithm.
␈↓ α←␈↓␈↓80 Applications␈↓
(2.7␈↓
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∂describe␈α∂a␈α∂LISP␈α∂function,␈α⊂␈↓αvalue␈↓,␈α∂which␈α∂will␈α∂take␈α∂such␈α⊂an␈α∂S-expr
␈↓ α←␈↓representation␈αand␈αcompute␈αits␈αvalue.␈αInput␈αto␈α␈↓αvalue␈↓␈αwill␈αbe␈αnumerals␈αor␈αlists
␈↓ α←␈↓beginning␈α∃with␈α∀either␈α∃␈↓αPLUS,␈α∀TIMES,␈↓␈α∃or␈α∀␈↓αEXPT␈↓␈α∃and␈α∀followed␈α∃by␈α∀two
␈↓ α←␈↓numerals or other expressions of the same form.
␈↓"∀␈↓ α←␈↓<constexp>␈↓ βo::= <constant>
␈↓"β␈↓ α←␈↓␈↓ βo::= <sum>
␈↓"β␈↓ α←␈↓␈↓ βo::= <prod>
␈↓"β␈↓ α←␈↓␈↓ βo::= <expt>
␈↓" ␈↓ α←␈↓<sum>␈↓ βo::= ␈↓α(PLUS␈↓ <constexp> <constexp> ␈↓α)␈↓
␈↓" ␈↓ α←␈↓<prod>␈↓ βo::= ␈↓α(TIMES␈↓ <constexp> <constexp> ␈↓α)␈↓
␈↓" ␈↓ α←␈↓<expt>␈↓ βo::= ␈↓α(EXPT␈↓ <constexp> <constexp> ␈↓α)␈↓
␈↓"→␈↓ α←␈↓␈↓ β'The␈αvalue␈αof␈αa␈αnumeral␈αis␈αthat␈αnumeral;␈αto␈αevaluate␈αthe␈αother␈αforms␈αof
␈↓ α←␈↓input␈α∀we␈α∀should␈α∀perform␈α∀the␈α∀operation␈α∀represented.␈α∀We␈α∀must␈α∪therefore
␈↓ α←␈↓assume␈α
that␈α
operations␈α
of␈α
addition,␈α
multiplication,␈α
and␈α
exponentiation␈αexist.
␈↓ α←␈↓Assume␈α∞they␈α∞are␈α∞named␈α∞+,␈α∞*,␈α∞and␈α∞↑,␈α∞respectively.␈α∞What␈α∞then␈α∞should␈α∞be␈α∞the
␈↓ α←␈↓value␈α∞of␈α∞a␈α∞representation␈α∂of␈α∞a␈α∞sum?␈α∞ It␈α∞should␈α∂be␈α∞the␈α∞result␈α∞of␈α∂adding␈α∞the
␈↓ α←␈↓value␈α
of␈αthe␈α
representations␈α
of␈αthe␈α
two␈α
summands␈αor␈α
operands.␈α
That␈αis,␈α
␈↓αvalue␈↓
␈↓ α←␈↓is recursive. It should now be clear how to write ␈↓αvalue␈↓:
␈↓"∀␈↓ α←␈↓αvalue[x] <=␈↓ β{[isconstant[x] → x;
␈↓"β␈↓ α←␈↓α␈↓ β{ issum[x] → +[value[arg␈↓β1␈↓α[x]];value[arg␈↓β2␈↓α[x]]];
␈↓"β␈↓ α←␈↓α␈↓ β{ isprod[x] → *[value[arg␈↓β1␈↓α[x]];value[arg␈↓β2␈↓α[x]]];
␈↓"β␈↓ α←␈↓α␈↓ β{ isexpt[x] → ↑[value[arg␈↓β1␈↓α[x]];value[arg␈↓β2␈↓α[x]]]]
␈↓"⊂␈↓ α←␈↓α␈↓where:␈↓α␈↓ ¬isconstant[x] <= numberp[x]
␈↓"⊃␈↓ α←␈↓α␈↓ ¬issum[x] <= eq[first[x];PLUS]
␈↓"⊃␈↓ α←␈↓α␈↓ ¬isprod[x] <= eq[first[x];TIMES]
␈↓"⊃␈↓ α←␈↓α␈↓ ¬isexpt[x] <= eq[first[x];EXPT]
␈↓"∀␈↓ α←␈↓␈↓ β'Compare the structure of the evaluator with that of the BNF equations.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Show how to extend ␈↓α value␈↓ to handle binary and unary minus.
␈↓" ␈↓ α←␈↓2. Write␈α⊗an␈α∃algorithm␈α⊗␈↓αinstantiate␈↓␈α∃which␈α⊗will␈α∃take␈α⊗two␈α⊗arguments,␈α∃one
␈↓ α←␈↓␈↓ β∂representing␈α∀a␈α∪set␈α∀of␈α∪variables␈α∀and␈α∪values,␈α∀the␈α∪other␈α∀representing␈α∪a
␈↓ α←␈↓␈↓ β∂polynomial.␈α
The␈αalgorithm␈α
is␈αto␈α
return␈α
a␈αrepresentation␈α
of␈αthe␈α
polynomial
␈↓ α←␈↓␈↓ β∂which would result from substituting the values for the variables.
␈↓" ␈↓ α←␈↓3. We␈αwould␈α
like␈αto␈αrepresent␈α
expressions␈αlike␈α
2+3+4␈αas␈α␈↓α(PLUS 2 3 4)␈↓␈α
rather
␈↓ α←␈↓␈↓ β∂than␈α~␈↓α(PLUS (PLUS 2 3) 4)␈↓␈α→or␈α~␈↓α(PLUS 2 (PLUS 3 4))␈↓;␈α~or␈α→represent
␈↓ α←␈↓␈↓ β∂2*3*4+5+6␈αas␈α␈↓α(PLUS (TIMES 2 3 4) 5 6)␈↓.␈α Write␈αa␈αnew␈αversion␈αof␈α␈↓αvalue␈↓
␈↓ α←␈↓␈↓ β∂which can evaluate such n-ary representations of + and *.
␈↓ α←␈↓␈↓2.7␈↓ πFEvaluation of Polynomials 81␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬∧␈↓↓More on polynomial evaluation␈↓
␈↓"∀␈↓ α←␈↓Though␈α
it␈αshould␈α
be␈αclear␈α
that␈α
the␈αcurrent␈α
␈↓αvalue␈↓␈αfunction␈α
does␈α
perform␈αthe
␈↓ α←␈↓appropriate␈α→calculation,␈α~it␈α→should␈α~be␈α→equally␈α→clear␈α~that␈α→the␈α~class␈α→of
␈↓ α←␈↓expressions␈α
which␈α
␈↓αvalue␈↓␈α
handles␈α
is␈α
not␈α
particularly␈α
powerful.␈α
We␈αmight␈α
wish
␈↓ α←␈↓to evaluate requests like:
␈↓"∀␈↓ α←␈↓␈↓↓A␈↓␈↓ ∧π"What is the value of ␈↓αx*y + 2*z␈↓ when ␈↓αx=4, y=2,␈↓ and ␈↓αz=1␈↓?"
␈↓"∀␈↓ α←␈↓Now␈α∪the␈α∪function␈α∪␈↓αinstantiate␈↓,␈α∪requested␈α∪in␈α∪problem␈α∪2␈α∪above,␈α∪offers␈α∩one
␈↓ α←␈↓solution:␈α∀make␈α∀a␈α∀new␈α∀copy␈α∀of␈α∀the␈α∀representation␈α∀of␈α∀␈↓αx*y + 2*z␈↓␈α∀with␈α∪the
␈↓ α←␈↓variables␈α
replaced␈α
by␈αtheir␈α
values.␈↓π 12␈↓␈α
This␈α
would␈αresult␈α
in␈α
a␈αrepresentation␈α
of
␈↓ α←␈↓␈↓α4*2 +2*1␈↓,␈αand␈αthis␈α
new␈αexpression␈αis␈α
suitable␈αfare␈αfor␈α␈↓αvalue␈↓.␈α
Computationally,
␈↓ α←␈↓this␈α∂is␈α∂a␈α∂terrible␈α∂solution.␈α⊂ ␈↓αinstantiate␈↓␈α∂will␈α∂go␈α∂through␈α∂the␈α∂structure␈α⊂of␈α∂the
␈↓ α←␈↓expression␈α
looking␈α
for␈α
instances␈α
of␈αvariables,␈α
and␈α
when␈α
located,␈α
will␈αreplace
␈↓ α←␈↓them␈α∞with␈α
the␈α∞appropriate␈α
values.␈α∞␈↓αvalue␈↓␈α
then␈α∞goes␈α
through␈α∞the␈α∞structure␈α
of
␈↓ α←␈↓the␈α⊃resulting␈α⊂expression␈α⊃performing␈α⊂the␈α⊃evaluation.␈α⊂We␈α⊃desire␈α⊃a␈α⊂function,
␈↓ α←␈↓␈↓αvalue␈↓λ'␈↓,␈αwhich␈αcombines␈αthe␈αtwo␈αprocesses:␈αthe␈αbasic␈αstructure␈αof␈α␈↓αvalue␈↓λ'␈↓␈αis␈α
that
␈↓ α←␈↓of␈α⊃mild-mannered␈α⊂␈↓αvalue␈↓,␈α⊃but␈α⊂when␈α⊃a␈α⊂variable,␈α⊃say␈α⊂␈↓αx␈↓,␈α⊃is␈α⊃recognized␈α⊂inside
␈↓ α←␈↓␈↓αvalue␈↓λ'␈↓␈α∂then␈α∂␈↓αvalue␈↓λ'␈↓␈α∂would␈α∂look␈α∂at␈α∂a␈α∂table␈α∂like␈α∂that␈α∂expected␈α⊂by␈α∂␈↓αinstantiate␈↓,
␈↓ α←␈↓find ␈↓αx␈↓ and return the value associated with the entry for ␈↓αx␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Let's␈αformalize␈αour␈αintuitions␈αabout␈α␈↓αvalue␈↓λ'␈↓.␈αIt␈αwill␈αbe␈αa␈αfunction␈α
of␈αtwo
␈↓ α←␈↓arguments.␈αThe␈αfirst␈αwill␈αbe␈αa␈α
representation␈αof␈αa␈αpolynomial;␈αthe␈αsecond␈α
will
␈↓ α←␈↓be␈α⊃a␈α⊃representation␈α⊃of␈α⊃the␈α⊃table␈α⊃of␈α⊃variables␈α⊃and␈α⊃values.␈α⊃You␈α⊃may␈α⊃have
␈↓ α←␈↓noticed␈αthat␈αthe␈αoriginal␈αversion␈α
of␈α␈↓αvalue␈↓␈α␈↓¬does␈↓␈αhandle␈αexpressions␈α
which␈αare
␈↓ α←␈↓not␈α
actually␈α
constant␈α
polynomials;␈α
␈↓α(2 + 3)*4␈↓␈αfor␈α
example.␈α
Since␈α
we␈α
will␈αwish
␈↓ α←␈↓to␈α
apply␈α∞our␈α
evaluation␈α∞functions␈α
to␈α∞more␈α
general␈α∞classes␈α
of␈α∞expressions␈α
we
␈↓ α←␈↓will␈α∂continue,␈α∂indeed␈α∂encourage,␈α⊂this␈α∂generality.␈α∂ Regardless␈α∂of␈α∂the␈α⊂class␈α∂of
␈↓ α←␈↓expressions␈αwe␈αwish␈αto␈αexamine,␈αit␈αis␈αthe␈αstructure␈αof␈αthe␈αtable␈αwhich␈αshould
␈↓ α←␈↓be␈α⊂the␈α⊂first␈α⊂order␈α⊂of␈α⊂business.␈α⊂ An␈α∂appropriate␈α⊂table,␈α⊂␈↓αtbl␈↓,␈α⊂will␈α⊂be␈α⊂a␈α⊂set␈α∂of
␈↓ α←␈↓ordered␈α≥pairs␈α≥␈↓α<name␈↓βi␈↓α, val␈↓βi␈↓α>␈↓;␈α≥thus␈α≥for␈α≥the␈α≥above␈α≥example␈α≥the␈α≤table
␈↓ α←␈↓␈↓α{<x, 4>, <y, 2>, <z, 1>}␈↓␈α∩would␈α∩suffice.␈α∩ Following␈α∩our␈α∩dictum␈α∩of␈α∩abstraction
␈↓ α←␈↓and␈α∂representation-independent␈α∂programming,␈α∂we␈α∂will␈α∂not␈α∂worry␈α∂about␈α∂the
␈↓ α←␈↓representational␈αproblems␈αof␈αsuch␈αtables.␈αWe␈αwill␈αsimply␈αassume␈αthat␈α"tables"
␈↓ α←␈↓are␈α
instances␈α
of␈α
an␈αabstract␈α
data␈α
structure␈α
called␈α␈↓<table>␈↓,␈α
and␈α
we␈α
will␈αonly
␈↓ α←␈↓concern␈α∞ourselves␈α∞for␈α∞the␈α∞moment␈α∞with␈α∞the␈α∞kinds␈α∞of␈α∞operations␈α∞we␈α∂need␈α∞to
␈↓ α←␈↓perform.␈α≤We␈α≥will␈α≤need␈α≥two␈α≤selector␈α≤functions:␈α≥␈↓αname␈↓,␈α≤to␈α≥select␈α≤the
␈↓ α←␈↓variable-component␈α
of␈α
a␈α∞table␈α
entry;␈α
and␈α∞␈↓αval␈↓,␈α
to␈α
select␈α∞the␈α
value-component.
␈↓ α←␈↓A␈α⊂complete␈α⊂discussion␈α∂of␈α⊂such␈α⊂a␈α⊂data␈α∂structure␈α⊂would␈α⊂entail␈α⊂discussion␈α∂of
␈↓ α←␈↓constructors␈α
and␈α
recognizers,␈αand␈α
perhaps␈α
other␈αfunctions,␈α
but␈α
for␈αthe␈α
current
␈↓ α←␈↓␈↓αvalue␈↓λ'␈↓, these two functions will suffice.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 12␈↓We␈α∞have␈α
seen␈α∞this␈α
substitution␈α∞and␈α
simplification␈α∞process␈α∞before␈α
in
␈↓ α←␈↓discussing␈α∞␈↓αequal␈↓␈α∞on␈α
page 24.␈α∞It␈α∞is␈α
a␈α∞useful␈α∞model␈α
for␈α∞computation,␈α∞but␈α
does
␈↓ α←␈↓not reflect current implementation practice. However, see [Ber 75].
␈↓ α←␈↓␈↓82 Applications␈↓
(2.7␈↓
␈↓"β␈↓ α←␈↓␈↓ β'␈↓αvalue␈↓λ'␈↓␈α⊗will␈α⊗need␈α⊗a␈α⊗table-function,␈α⊗␈↓αlocate␈↓,␈α⊗to␈α⊗locate␈α↔an␈α⊗appropriate
␈↓ α←␈↓variable-value␈α⊂entry.␈α⊂The␈α⊂binary␈α⊂function␈α∂␈↓αlocate␈↓␈α⊂will␈α⊂take␈α⊂an␈α⊂argument,␈α∂␈↓αx␈↓,
␈↓ α←␈↓representing␈αa␈αvariable;␈αand␈αan␈αargument,␈α␈↓αtbl␈↓,␈αrepresenting␈αa␈αtable.␈α␈↓αlocate␈↓␈αwill
␈↓ α←␈↓match␈α∞␈↓αx␈↓␈α∂against␈α∞the␈α∂␈↓αname␈↓-part␈α∞of␈α∂each␈α∞element␈α∞in␈α∂␈↓αtbl␈↓;␈α∞if␈α∂a␈α∞match␈α∂is␈α∞found
␈↓ α←␈↓then␈αthe␈αcorresponding␈α␈↓αval␈↓-part␈αis␈αreturned.␈αIf␈αno␈αmatch␈αis␈αfound␈αthen␈α␈↓αlocate␈↓
␈↓ α←␈↓is undefined.
␈↓"β␈↓ α←␈↓␈↓ β'So␈αfar,␈αlittle␈αstructure␈αhas␈αbeen␈αimposed␈αon␈αelements␈αof␈α␈↓<table>␈↓;␈αtables
␈↓ α←␈↓are␈α
either␈α∞empty␈α
or␈α∞not;␈α
but␈α
if␈α∞a␈α
table␈α∞is␈α
non-empty␈α
then␈α∞each␈α
element␈α∞is␈α
a
␈↓ α←␈↓pair␈α~with␈α~recognizable␈α~components␈α→of␈α~␈↓αname␈↓␈α~and␈α~␈↓αval␈↓.␈α~ However,␈α→the
␈↓ α←␈↓specification␈α⊂of␈α⊂algorithms␈α⊂to␈α⊂examine␈α⊂elements␈α⊂of␈α⊂␈↓<table>␈↓␈α⊂imposes␈α⊂more
␈↓ α←␈↓structure␈α∩on␈α⊃our␈α∩tables.␈α∩ If␈α⊃we␈α∩were␈α⊃dealing␈α∩with␈α∩mathematical␈α⊃functions
␈↓ α←␈↓rather␈αthan␈α
algorithms␈αthen␈αa␈α
side␈αcondition␈αto␈α
the␈αeffect␈αthat␈α
a␈αtable␈αhad␈α
no
␈↓ α←␈↓pairs␈α⊗with␈α⊗duplicate␈α⊗first␈α↔elements␈α⊗would␈α⊗be␈α⊗sufficient␈α↔(and␈α⊗required).
␈↓ α←␈↓However,␈α∩we␈α∩are␈α∩dealing␈α∩with␈α∩algorithms␈α∩and␈α∩therefore␈α∩must␈α∩describe␈α∩a
␈↓ α←␈↓method for locating elements.
␈↓"β␈↓ α←␈↓␈↓ β'Recursion␈α∃is␈α∃the␈α∃only␈α∃method␈α∃we␈α∃have␈α∃for␈α∃specifying␈α⊗␈↓αlocate␈↓,␈α∃and
␈↓ α←␈↓recursion␈α∂operates␈α∂by␈α∞decomposing␈α∂a␈α∂structure.␈α∞Sets␈α∂are␈α∂notorious␈α∂for␈α∞their
␈↓ α←␈↓lack␈αof␈αstructure;␈α
there␈αis␈αno␈α
order␈αto␈αthe␈αelements␈α
of␈αa␈αset.␈α
But␈αif␈αwe␈α
are␈αto
␈↓ α←␈↓write␈α
a␈αLISP␈α
algorithm␈αfor␈α
␈↓αlocate␈↓,␈αthat␈α
algorithm␈αwill␈α
have␈αto␈α
be␈αrecursive␈α
on
␈↓ α←␈↓the␈α"structure"␈α
of␈α␈↓αtbl␈↓,␈α
and␈αso␈α
we␈αimpose␈αan␈α
ordering␈αon␈α
the␈αelements␈α
of␈αthat
␈↓ α←␈↓table.␈α∀That␈α∀is,␈α∀we␈α∀will␈α∀represent␈α∪tables␈α∀as␈α∀␈↓¬sequences␈↓.␈α∀We␈α∀know␈α∀how␈α∪to
␈↓ α←␈↓represent sequences in LISP: we use lists.
␈↓"β␈↓ α←␈↓␈↓ β'With this introduction, here's ␈↓αlocate␈↓:␈↓π 13␈↓
␈↓"∀␈↓ α←␈↓αlocate[x;tbl] <=␈↓ ∧+[eq[name[first[tbl]];x] → val[first[tbl]];
␈↓"β␈↓ α←␈↓α␈↓ ∧+ ␈↓
t␈↓α → locate[x;rest[tbl]] ]
␈↓"∀␈↓ α←␈↓The␈α⊗effect␈α⊗of␈α⊗␈↓αlocate␈↓␈α⊗is␈α⊗to␈α⊗find␈α∃the␈α⊗␈↓¬first␈↓␈α⊗element␈α⊗of␈α⊗␈↓αtbl␈↓␈α⊗which␈α⊗has␈α∃a
␈↓ α←␈↓␈↓αname␈↓-component␈α≥which␈α≥matches␈α≥␈↓αx␈↓.␈α≥ Having␈α≥found␈α≥that␈α≡match,␈α≥the
␈↓ α←␈↓corresponding␈α␈↓αval␈↓-part␈αis␈αreturned.␈αIf␈αthere␈αwere␈αother␈αmatches␈αfurther␈αalong
␈↓ α←␈↓in␈α∞the␈α∞sequence␈α∞␈↓αlocate␈↓␈α∞would␈α∞not␈α∞see␈α∞them.␈α∞ Other␈α∞representations␈α∞of␈α∞tables
␈↓ α←␈↓are certainly possible. This representation will be useful in later applications.
␈↓"β␈↓ α←␈↓And here's the new more powerful ␈↓αvalue␈↓λ'␈↓:
␈↓"∀␈↓ α←␈↓αvalue␈↓λ'␈↓α[x;tbl] <=␈↓ ∧7[isconstant[x] → x;
␈↓"β␈↓ α←␈↓α␈↓ ∧7 isvar[x] → locate[x;tbl];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 issum[x] → +[␈↓ ¬wvalue␈↓λ'␈↓α[arg␈↓β1␈↓α[x];tbl];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ¬wvalue␈↓λ'␈↓α[arg␈↓β2␈↓α[x];tbl]];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 isprod[x] → *[␈↓ ¬wvalue␈↓λ'␈↓α[arg␈↓β1␈↓α[x];tbl];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ¬wvalue␈↓λ'␈↓α[arg␈↓β2␈↓α[x];tbl]];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 isexpt[x] → ↑[␈↓ ¬wvalue␈↓λ'␈↓α[arg␈↓β1␈↓α[x];tbl];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ¬wvalue␈↓λ'␈↓α[arg␈↓β2␈↓α[x];tbl]] ]
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 13␈↓The␈α
interpretation␈α
of␈α
␈↓αtbl␈↓␈α
as␈αa␈α
function␈α
implies␈α
that␈α
␈↓αlocate␈↓␈αrepresents
␈↓ α←␈↓function␈α∞application;␈α∞i.e.,␈α∞␈↓αlocate[x;tbl] ␈↓is␈↓α tbl(x)␈↓.This␈α∞is␈α∞a␈α∞very␈α∞acceptable␈α
view
␈↓ α←␈↓of table lookup.
␈↓ α←␈↓␈↓2.7␈↓ πFEvaluation of Polynomials 83␈↓
␈↓"β␈↓ α←␈↓Notice␈α⊂that␈α⊂␈↓αtbl␈↓␈α⊂is␈α⊂carried␈α⊃through␈α⊂as␈α⊂an␈α⊂explicit␈α⊂argument␈α⊂to␈α⊃␈↓αvalue␈↓λ'␈↓␈α⊂even
␈↓ α←␈↓though␈α⊂it␈α∂is␈α⊂only␈α∂accessed␈α⊂when␈α∂a␈α⊂variable␈α∂is␈α⊂recognized.␈α∂ Notice␈α⊂too␈α∂that
␈↓ α←␈↓much␈α∞of␈α∞the␈α∞structure␈α∞of␈α∞␈↓αvalue␈↓λ'␈↓␈α
is␈α∞quite␈α∞repetitious;␈α∞the␈α∞lines␈α∞which␈α
handle
␈↓ α←␈↓sums,␈α∪products,␈α∩and␈α∪exponentiation␈α∩are␈α∪identical␈α∩except␈α∪for␈α∪the␈α∩function
␈↓ α←␈↓which␈α⊂finally␈α∂gets␈α⊂applied␈α⊂to␈α∂the␈α⊂evaluated␈α⊂arguments.␈α∂ That␈α⊂is,␈α⊂the␈α∂basic
␈↓ α←␈↓structure␈α
of␈α␈↓αvalue␈↓λ'␈↓␈α
is␈α
potentially␈αof␈α
broader␈α
application␈αthan␈α
just␈α
the␈αsimple
␈↓ α←␈↓class␈α
of␈α
polynomials.␈α
In␈α
keeping␈α
with␈α
our␈α
search␈α
for␈α
generality,␈α
let's␈α
pursue
␈↓ α←␈↓␈↓αvalue␈↓λ'␈↓ a little further.
␈↓"β␈↓ α←␈↓␈↓ β'What ␈↓αvalue␈↓λ'␈↓ says is:
␈↓"λ␈↓ α←␈↓␈↓↓1.␈↓ The value of a constant is that constant.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ The␈αvalue␈αof␈αa␈αvariable␈αis␈αthe␈αcurrent␈αvalue␈αassociated␈αwith␈αthat␈αvariable
␈↓ α←␈↓␈↓ β∂in the table.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ The␈αvalue␈α
of␈αa␈αfunction␈α
call␈αis␈αthe␈α
result␈αof␈αapplying␈α
the␈αfunction␈α
to␈αthe
␈↓ α←␈↓␈↓ β∂evaluated␈α∩arguments.␈α∩It␈α∩just␈α∩turns␈α∩out␈α∩that␈α∩the␈α∩only␈α∪functions␈α∩␈↓αvalue␈↓λ'␈↓
␈↓ α←␈↓␈↓ β∂knows about are binary sums, products, and exponentiation.
␈↓"β␈↓ α←␈↓Let's clean up ␈↓αvalue␈↓λ'␈↓ a bit.
␈↓"∀␈↓ α←␈↓αvalue␈↓λ'␈↓α[x;tbl] <=␈↓ ∧+[isconstant[x] → x;
␈↓"β␈↓ α←␈↓α␈↓ ∧+ isvar[x] → locate[x;tbl];
␈↓"β␈↓ α←␈↓α␈↓ ∧+ isfun_args[x] → apply[␈↓ εcfun[x];
␈↓"β␈↓ α←␈↓α␈↓ ∧+␈↓ εceval_args[args[x];tbl]];
␈↓"β␈↓ α←␈↓α␈↓ ∧+ ␈↓
t␈↓α → ␈↓λB␈↓α]
␈↓"∀␈↓ α←␈↓The␈α⊂changes␈α∂are␈α⊂in␈α⊂the␈α∂third␈α⊂branch␈α∂of␈α⊂the␈α⊂conditional.␈α∂We␈α⊂have␈α⊂a␈α∂new
␈↓ α←␈↓recognizer,␈α∞␈↓αisfun_args␈↓␈α∂to␈α∞recognize␈α∂function␈α∞application.␈α∂We␈α∞have␈α∂two␈α∞new
␈↓ α←␈↓selector␈α∃functions;␈α∃␈↓αfun␈↓␈α∃selects␈α∀the␈α∃representation␈α∃of␈α∃the␈α∃function␈α∀-- sum,
␈↓ α←␈↓product,␈α
or␈α
power␈α
in␈α
the␈α
simple␈α
case;␈α
␈↓αargs␈↓␈α
selects␈α
the␈α
arguments␈α
or␈α
parameters
␈↓ α←␈↓to␈α⊂the␈α∂function␈α⊂-- in␈α⊂this␈α∂case␈α⊂all␈α∂functions␈α⊂are␈α⊂binary.␈α∂We␈α⊂have␈α⊂two␈α∂new
␈↓ α←␈↓functions␈α
to␈α
define:␈α
␈↓αeval_args␈↓,␈α∞which␈α
is␈α
supposed␈α
to␈α
evaluate␈α∞the␈α
arguments
␈↓ α←␈↓finding␈αvalues␈αfor␈αany␈αof␈αthe␈αvariables;␈αand␈α␈↓αapply␈↓,␈αwhich␈αis␈αused␈αto␈αperform
␈↓ α←␈↓the desired operation on the evaluated arguments.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αare␈αstill␈αtrying␈αto␈αremain␈αas␈αrepresentation-free␈αas␈αpossible:␈αthus␈αthe
␈↓ α←␈↓generalization␈α_of␈α↔the␈α_algorithm␈α_␈↓αvalue␈↓λ'␈↓,␈α↔and␈α_thus␈α↔the␈α_care␈α_in␈α↔picking
␈↓ α←␈↓representations␈α∪for␈α∪the␈α∪data␈α∪structures.␈α∪ We␈α∪need␈α∪to␈α∪make␈α∀another␈α∪data
␈↓ α←␈↓structure␈α∪decision␈α∪now;␈α∪when␈α∪writing␈α∩the␈α∪function␈α∪␈↓αeval_args␈↓,␈α∪we␈α∪will␈α∩be
␈↓ α←␈↓giving␈α∀a␈α∃recursive␈α∀algorithm.␈α∃ This␈α∀algorithm␈α∀will␈α∃be␈α∀recursive␈α∃on␈α∀the
␈↓ α←␈↓structure␈αof␈αthe␈αfirst␈αargument,␈αwhich␈αis␈αa␈αrepresentation␈αof␈αthe␈αarguments␈αto
␈↓ α←␈↓the␈α⊂function.␈α⊂In␈α⊂contrast␈α⊂to␈α∂our␈α⊂position␈α⊂when␈α⊂writing␈α⊂the␈α⊂function␈α∂␈↓αlocate␈↓,
␈↓ α←␈↓there␈α⊂␈↓¬is␈↓␈α⊃a␈α⊂natural␈α⊃structure␈α⊂on␈α⊃the␈α⊂arguments␈α⊃to␈α⊂a␈α⊃function:␈α⊂they␈α⊃form␈α⊂a
␈↓ α←␈↓sequence.␈αThat␈αis␈α␈↓αf[1;2;3]␈↓␈αis␈αtypically␈αnot␈αthe␈αsame␈αas␈α␈↓αf[3;2;1]␈↓␈αor␈α␈↓αf␈↓␈αapplied␈αto
␈↓ α←␈↓any␈α⊃other␈α⊃permutation␈α⊃of␈α⊃{1, 2, 3}.␈α⊃Thus␈α⊃writing␈α⊃␈↓αeval_args␈↓␈α⊃as␈α∩a␈α⊃function,
␈↓ α←␈↓recursive␈α⊃on␈α⊃the␈α⊂sequence-structure␈α⊃of␈α⊃its␈α⊂first␈α⊃argument,␈α⊃is␈α⊃quite␈α⊂natural.
␈↓ α←␈↓Here is ␈↓αeval_args␈↓:
␈↓ α←␈↓␈↓84 Applications␈↓
(2.7␈↓
␈↓"β␈↓ α←␈↓αeval_args[args;tbl] <=␈↓ ∧␈[null[args] → ();
␈↓"β␈↓ α←␈↓α␈↓ ∧␈ ␈↓
t␈↓α → concat[␈↓ ε≠value␈↓λ'␈↓α[first[args];tbl];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ε≠eval_args[rest[args];tbl]] ]
␈↓"∀␈↓ α←␈↓Notice␈α∀that␈α∀we␈α∪have␈α∀written␈α∀␈↓αeval_args␈↓␈α∪without␈α∀any␈α∀bias␈α∀toward␈α∪binary
␈↓ α←␈↓functions;␈α⊗it␈α⊗will␈α⊗evaluate␈α↔a␈α⊗sequence␈α⊗of␈α⊗arbitrary␈α⊗length,␈α↔returning␈α⊗a
␈↓ α←␈↓sequence representing the evaluated arguments.
␈↓"β␈↓ α←␈↓␈↓ β'There␈αshould␈αbe␈αno␈α
real␈αsurprises␈αin␈α␈↓αapply␈↓;␈α
it␈αgets␈αthe␈αrepresentation␈α
of
␈↓ α←␈↓the function name and the sequence of evaluated arguments and does its job:
␈↓"∀␈↓ α←␈↓αapply[fn; evargs] <=␈↓ ∧␈[issum[fn] → +[␈↓ εKarg␈↓β1␈↓α[evargs];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ εKarg␈↓β2␈↓α[evargs]];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈ isprod[fn] → *[␈↓ εKarg␈↓β1␈↓α[evargs];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ εKarg␈↓β2␈↓α[evargs]];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈ isexpt[fn] → ↑[␈↓ εKarg␈↓β1␈↓α[evargs];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ εKarg␈↓β2␈↓α[evargs]] ]
␈↓"∀␈↓ α←␈↓If␈α∞we␈α∞should␈α∂desire␈α∞to␈α∞recognize␈α∞more␈α∂functions␈α∞then␈α∞we␈α∞need␈α∂only␈α∞modify
␈↓ α←␈↓␈↓αapply␈↓.␈αThat␈αwould␈αbe␈αa␈αsatisfactory␈αshort-term␈αsolution,␈αbut␈αwe␈αwould␈αlike␈αa
␈↓ α←␈↓more␈α⊃general␈α⊃function-definition␈α⊃facility.␈α⊂Such␈α⊃a␈α⊃feature␈α⊃would␈α⊃allow␈α⊂new
␈↓ α←␈↓functions␈αto␈αbe␈αdefined␈αduring␈αa␈αcomputation;␈αthen␈αif␈αan␈αapplication␈αof␈αthat
␈↓ α←␈↓function␈α∩were␈α∪needed,␈α∩the␈α∩␈↓αvalue␈↓-function␈α∪would␈α∩find␈α∩that␈α∪definition␈α∩and
␈↓ α←␈↓apply␈α⊂␈↓¬it␈↓␈α⊂in␈α⊂a␈α⊂manner␈α⊂analogous␈α⊂to␈α⊂the␈α⊂way␈α⊂the␈α⊂pre-defined␈α⊂functions␈α∂are
␈↓ α←␈↓applied.␈α∞How␈α∞far␈α∞away␈α∞are␈α∞we␈α∞from␈α∞this␈α∞more␈α∞desirable␈α∞super-␈↓αvalue␈↓?␈α
Well
␈↓ α←␈↓␈↓αvalue␈↓λ'␈↓␈α
is␈α
already␈α
well-endowed␈α
with␈α
a␈α
mechanism␈α
for␈α
locating␈αvalues;␈α
perhaps
␈↓ α←␈↓we␈α∂can␈α∞exploit␈α∂this␈α∞judiciously␈α∂placed␈α∂code.␈α∞ In␈α∂what␈α∞context␈α∂would␈α∂we␈α∞be
␈↓ α←␈↓interested in locating function definitions? Here's an example:
␈↓"∀␈↓ α←␈↓␈↓↓B␈↓␈↓ ∧π"What is the value of ␈↓αf[4;2;1]␈↓ when ␈↓αf[x;y;z] <= x*y + 2*z␈↓?"
␈↓"∀␈↓ α←␈↓If␈αwe␈αhave␈αa␈αmeans␈αof␈αrecovering␈αthe␈αdefinition␈αof␈α␈↓αf␈↓,␈αthen␈αwe␈αcan␈αreduce␈α
the
␈↓ α←␈↓problem␈αto␈α␈↓↓A␈↓␈αof␈αpage 81.␈α We␈αwill␈αutilize␈αthe␈αtable-mechanism,␈αand␈αtherefore
␈↓ α←␈↓will␈α⊃use␈α∩␈↓αlocate␈↓␈α⊃to␈α⊃retrieve␈α∩the␈α⊃definition␈α∩of␈α⊃the␈α⊃function␈α∩␈↓αf␈↓.␈α⊃ In␈α∩our␈α⊃prior
␈↓ α←␈↓applications␈αof␈α␈↓αlocate␈↓␈αwe␈αwould␈αfind␈αa␈αconstant␈αas␈αthe␈αassociated␈αvalue.␈αNow,
␈↓ α←␈↓given␈α∂the␈α∂name␈α∂␈↓αf␈↓,␈α∂we␈α∂would␈α∂expect␈α∂to␈α∂find␈α∂the␈α∂definition␈α∂of␈α∂the␈α∞function.
␈↓ α←␈↓The␈αquestion␈αthen,␈αis␈αhow␈αdo␈αwe␈αrepresent␈αthe␈αdefinition␈αof␈α␈↓αf␈↓?␈α Certainly␈α
the
␈↓ α←␈↓body␈α
of␈α
the␈α
function,␈α
␈↓αx*y + 2*z␈↓,␈α
is␈α
one␈α
of␈α
the␈α
necessary␈α
ingredients,␈α
but␈α
is␈α
that
␈↓ α←␈↓all?␈α⊃Given␈α∩the␈α⊃expression␈α∩␈↓αx*y + 2*z␈↓␈α⊃can␈α∩we␈α⊃successfully␈α∩compute␈α⊃␈↓αf[4;2;1]␈↓?
␈↓ α←␈↓Not␈αyet;␈αwe␈αneed␈αto␈α
know␈αthe␈αcorrespondence␈αbetween␈αthe␈αvalues␈α
␈↓α1, 2, 4␈↓␈αand
␈↓ α←␈↓the␈α→variables,␈α~␈↓αx,␈α→y,␈α~z␈↓.␈α→That␈α~information␈α→is␈α~present␈α→in␈α~our␈α→notation
␈↓ α←␈↓␈↓αf[x;y;z] <= ...␈↓,␈α
and␈α
is␈α
a␈α
crucial␈α
part␈α
of␈α
the␈α
definition␈α
of␈α
␈↓αf␈↓.␈α
That␈α
is,␈α
the␈α␈↓¬order␈↓␈α
of
␈↓ α←␈↓the␈α∂variables␈α∂appearing␈α∞after␈α∂the␈α∂function␈α∞name␈α∂is␈α∂an␈α∞integral␈α∂part␈α∂of␈α∞the
␈↓ α←␈↓definition: ␈↓αf[y;z;x] <= x*y +2*z␈↓ defines a different function.
␈↓"β␈↓ α←␈↓␈↓ β'Since␈α⊂we␈α⊂are␈α⊃now␈α⊂talking␈α⊂about␈α⊃␈↓¬representations␈↓␈α⊂of␈α⊂functions,␈α⊃we␈α⊂are
␈↓ α←␈↓entering␈α∞the␈α∞realm␈α
of␈α∞abstract␈α∞data␈α∞structures␈α
again.␈α∞We␈α∞have␈α∞a␈α
reasonable
␈↓ α←␈↓understanding now of the essential components of such a representation.
␈↓ α←␈↓␈↓2.7␈↓ πFEvaluation of Polynomials 85␈↓
␈↓"β␈↓ α←␈↓␈↓ β'For our purposes, a function has three parts:
␈↓"λ␈↓ α←␈↓␈↓↓1.␈↓ A name; ␈↓αf␈↓ in the current example.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ A formal parameter list; ␈↓α[x;y;z]␈↓ here.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ A body; ␈↓αx*y + 2*z␈↓ in the example.
␈↓"β␈↓ α←␈↓We␈α
do␈α
not␈α
need␈α
a␈α
complete␈α
study␈α
of␈α
representations␈α
for␈α
functions␈α
yet.␈αFor␈α
our
␈↓ α←␈↓current␈α∂discussions␈α∞we␈α∂can␈α∞assume␈α∂a␈α∞representation␈α∂exists,␈α∞and␈α∂that␈α∂we␈α∞are
␈↓ α←␈↓supplied with three selectors to retrieve the components mentioned above.
␈↓"λ␈↓ α←␈↓␈↓↓1.␈↓ ␈↓αname␈↓␈α∀selects␈α∀the␈α∀name␈α∀component␈α∀from␈α∀the␈α∀representation.␈α∃We␈α∀have
␈↓ α←␈↓␈↓ β∂actually seen ␈↓αname␈↓ before in the definition ␈↓αlocate␈↓ on page 82.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ ␈↓αvarlist␈↓␈α∩selects␈α∩the␈α∩list␈α∪of␈α∩variables␈α∩from␈α∩the␈α∩representation.␈α∪ We␈α∩have
␈↓ α←␈↓␈↓ β∂already␈α
seen␈α
that␈αthe␈α
natural␈α
way␈αto␈α
think␈α
about␈αthis␈α
component␈α
is␈α
as␈αa
␈↓ α←␈↓␈↓ β∂sequence. Thus the name ␈↓αvarlist␈↓.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ ␈↓αbody␈↓ selects the expression which is the content of the definition.
␈↓"β␈↓ α←␈↓Given␈α⊂a␈α⊂function␈α⊂represented␈α⊂in␈α⊂the␈α⊂table␈α⊂according␈α⊂to␈α⊃these␈α⊂conventions,
␈↓ α←␈↓how␈α∂do␈α∂we␈α⊂use␈α∂the␈α∂information␈α∂to␈α⊂effect␈α∂the␈α∂evaluation␈α∂of␈α⊂something␈α∂like
␈↓ α←␈↓␈↓αf[4;2;1]␈↓?␈α⊗ First␈α⊗␈↓αvalue␈↓λ'␈↓␈α⊗will␈α⊗see␈α⊗the␈α⊗representation␈α⊗of␈α⊗␈↓αf[4;2;1]␈↓;␈α↔it␈α⊗should
␈↓ α←␈↓recognize␈α
this␈α
as␈αan␈α
instance␈α
of␈α
function-application␈αat␈α
the␈α
following␈α
line␈αof
␈↓ α←␈↓␈↓αvalue␈↓λ'␈↓:
␈↓"∀␈↓ α←␈↓α␈↓ ∧ isfun_args[x] → apply[fun[x];eval_args[args[x];tbl]]
␈↓"∀␈↓ α←␈↓This␈α
should␈α
cause␈α
an␈α
evaluation␈α
of␈α
the␈α
arguments␈α
and␈α
then␈α
pass␈α
on␈αthe␈α
work
␈↓ α←␈↓to ␈↓αapply␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Clever␈α∂␈↓αapply␈↓␈α∂should␈α∞soon␈α∂realize␈α∂that␈α∞␈↓αf␈↓␈α∂is␈α∂not␈α∞the␈α∂name␈α∂of␈α∂a␈α∞known
␈↓ α←␈↓function.␈α
It␈α∞should␈α
then␈α∞extract␈α
the␈α
definition␈α∞of␈α
␈↓αf␈↓␈α∞from␈α
the␈α∞table;␈α
associate
␈↓ α←␈↓(or␈α↔bind)␈α↔the␈α↔evaluated␈α_arguments␈α↔(␈↓α4, 2, 1␈↓)␈α↔with␈α↔the␈α↔variables␈α_of␈α↔the
␈↓ α←␈↓parameter␈α≤list␈α≤(␈↓αx, y, z␈↓),␈α≤making␈α≤a␈α≤new␈α≤table␈α≤with␈α≤name-value␈α≠pairs
␈↓ α←␈↓(␈↓α<x, 4>, <y, 2>, <z, 1>␈↓).␈α∩ Now␈α∩we␈α∩are␈α∩back␈α∩to␈α∩the␈α∩setting␈α∩of␈α∩problem␈α∩␈↓↓A␈↓␈α∩of
␈↓ α←␈↓page 81.␈α∀ We␈α∃should␈α∀ask␈α∃␈↓αvalue␈↓λ'␈↓␈α∀to␈α∀evaluate␈α∃the␈α∀␈↓αbody␈↓-component␈α∃of␈α∀the
␈↓ α←␈↓function␈α⊃using␈α⊃the␈α⊃new␈α⊃␈↓αtbl␈↓.␈α⊃ This␈α⊃works␈α⊃fine␈α⊃for␈α⊃␈↓αx␈↓,␈α⊃␈↓αy␈↓,␈α⊃and␈α⊃␈↓αz␈↓;␈α∩within␈α⊃the
␈↓ α←␈↓evaluation␈α∀of␈α∪the␈α∀body␈α∀of␈α∪␈↓αf␈↓␈α∀we␈α∪will␈α∀find␈α∀the␈α∪right␈α∀bindings␈α∀for␈α∪these
␈↓ α←␈↓variables.␈α∂But␈α∂we␈α∂might␈α∂also␈α∂need␈α∂some␈α∂information␈α∂from␈α∂the␈α∂original␈α∞␈↓αtbl␈↓.
␈↓ α←␈↓The␈α∀evaluation␈α∪of␈α∀the␈α∪body␈α∀of␈α∪␈↓αf␈↓␈α∀might␈α∪entail␈α∀the␈α∪application␈α∀of␈α∪some
␈↓ α←␈↓function definition present in ␈↓αtbl␈↓. For example, the representation of
␈↓"∀␈↓ α←␈↓␈↓ βz"what is ␈↓αg[2]␈↓ where ␈↓αg[x] <= x+s[x];␈↓ and ␈↓αs[x] <= x*x␈↓ ?"
␈↓"∀␈↓ α←␈↓Within␈α⊂the␈α⊃body␈α⊂of␈α⊂␈↓αg␈↓␈α⊃we␈α⊂need␈α⊃the␈α⊂definition␈α⊂of␈α⊃␈↓αs␈↓.␈α⊂Therefore,␈α⊃instead␈α⊂of
␈↓ α←␈↓building␈α∞a␈α∞new␈α
table␈α∞we␈α∞will␈α
add␈α∞the␈α∞new␈α
bindings␈α∞to␈α∞the␈α
front␈α∞of␈α∞the␈α
old
␈↓ α←␈↓table.␈α∂Since␈α∂␈↓αlocate␈↓␈α∂begins␈α∞its␈α∂search␈α∂from␈α∂the␈α∂front␈α∞of␈α∂the␈α∂table␈α∂we␈α∂will␈α∞be
␈↓ α←␈↓assured␈αof␈αfinding␈αthe␈αnew␈αbindings;␈α
since␈αthe␈αold␈αtable␈αis␈αstill␈α
accessible␈αwe
␈↓ α←␈↓are assured of finding any necessary previous bindings.
␈↓ α←␈↓␈↓86 Applications␈↓
(2.7␈↓
␈↓"β␈↓ α←␈↓␈↓ β'We␈α
should␈α
be␈α
able␈α
to␈α
create␈α
a␈α
new␈α
␈↓αvalue␈↓λ''␈↓␈α
now.␈α
Looking␈α
at␈α
the␈αfiner
␈↓ α←␈↓detail␈α
of␈α␈↓αvalue␈↓λ'␈↓␈α
and␈α␈↓αapply␈↓,␈α
we␈α
can␈αsee␈α
a␈αfew␈α
other␈αmodifications␈α
need␈α
to␈αbe
␈↓ α←␈↓made.␈α∩ ␈↓αapply␈↓λ'␈↓␈α∩will␈α∩locate␈α∩the␈α∩function␈α∩definition␈α∩and␈α∩thus␈α∩␈↓αtbl␈↓␈α∩should␈α⊃be
␈↓ α←␈↓included as a third argument to ␈↓αapply␈↓λ'␈↓. That is, inside ␈↓αapply␈↓λ'␈↓ we will have:
␈↓"∀␈↓ α←␈↓␈↓ ∧B␈↓αisfun[fn] → apply␈↓λ'␈↓α[locate[fn;tbl];evargs;tbl]␈↓;
␈↓"∀␈↓ α←␈↓After␈α∞␈↓αlocate␈↓␈α∂has␈α∞done␈α∞its␈α∂work,␈α∞this␈α∂line␈α∞(above)␈α∞will␈α∂invoke␈α∞␈↓αapply␈↓λ'␈↓␈α∂with␈α∞a
␈↓ α←␈↓function␈αdefinition␈αas␈αfirst␈αargument.␈αWe␈αshould␈αprepare␈α␈↓αapply␈↓λ'␈↓␈αfor␈αsuch␈αan
␈↓ α←␈↓eventuality with the following addition:
␈↓"∀␈↓ α←␈↓α␈↓ βfisdef[fn] → value␈↓λ''␈↓α[body[fn];newtbl[varlist[fn];evargs;tbl]];
␈↓"∀␈↓ α←␈↓What does this incredible line say? It says
␈↓"∀␈↓ α←␈↓␈↓ β3"Evaluate␈α≠the␈α≠body␈α≠of␈α≠the␈α≠function␈α≠using␈α≠a␈α≠new␈α~table
␈↓ α←␈↓␈↓ β3manufactured␈α
from␈αthe␈α
old␈αtable␈α
by␈αadding␈α
the␈αpairings␈α
of␈αthe
␈↓ α←␈↓␈↓ β3elements␈α_of␈α_the␈α_formal␈α_parameter␈α_list␈α_with␈α→the␈α_evaluated
␈↓ α←␈↓␈↓ β3arguments."
␈↓"∀␈↓ α←␈↓It␈α
also␈α
says␈α
we␈α
should␈α
write␈α
␈↓αnewtbl␈↓.␈α
This␈α
LISP␈α
function␈α
will␈α
make␈α
a␈α
new␈α
table
␈↓ α←␈↓by␈αadding␈αnew␈αname-value␈αpairs␈αto␈αan␈αexisting␈αtable.␈α So␈αwe'd␈αbetter␈αname␈αa
␈↓ α←␈↓constructor to generate a new name-value pair:
␈↓"∀␈↓ α←␈↓␈↓αmkent␈↓␈αis␈αthe␈αconstructor␈αto␈αmake␈αnew␈αentries.␈αIt␈αwill␈αtake␈αtwo␈αarguments:␈αthe
␈↓ α←␈↓␈↓ β'first will be the name, the second will be the value.
␈↓"∀␈↓ α←␈↓␈↓ β'Since␈α∞we␈α∞have␈α
assumed␈α∞that␈α∞the␈α
structure␈α∞of␈α∞tables,␈α∞variable-lists,␈α
and
␈↓ α←␈↓calling␈α
sequences␈α
to␈α
functions␈α
are␈α␈↓¬all␈↓␈α
sequences,␈α
we␈α
will␈α
write␈α␈↓αnewtbl␈↓␈α
assuming
␈↓ α←␈↓this representation.
␈↓"∀␈↓ α←␈↓αnewtbl[vars;vals;tbl] <=␈↓ ¬[null[vars] → tbl;
␈↓"β␈↓ α←␈↓α␈↓ ¬ ␈↓
t␈↓α → concat[␈↓ ε≠mkent[first[vars];first[vals]];
␈↓"β␈↓ α←␈↓α␈↓ ¬␈↓ ε≠newtbl[␈↓ ε{rest[vars];
␈↓"β␈↓ α←␈↓α␈↓ ¬␈↓ ε≠␈↓ ε{rest[vals];
␈↓"β␈↓ α←␈↓α␈↓ ¬␈↓ ε≠␈↓ ε{tbl]] ]
␈↓"∀␈↓ α←␈↓And finally here's the new ␈↓αvalue␈↓λ''␈↓α-apply␈↓λ'␈↓ pair:
␈↓"∀␈↓ α←␈↓αvalue␈↓λ''␈↓α[x;tbl] <=␈↓ ∧C[isconstant[x] → x;
␈↓"β␈↓ α←␈↓α␈↓ ∧C isvar[x] → locate[x;tbl];
␈↓"β␈↓ α←␈↓α␈↓ ∧C isfun_args[x] → apply␈↓λ'␈↓α[␈↓ ππfun[x];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ππeval_args[args[x];tbl];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ππtbl] ]
␈↓ α←␈↓␈↓2.7␈↓ πFEvaluation of Polynomials 87␈↓
␈↓"∀␈↓ α←␈↓αapply␈↓λ'␈↓α[fn;evargs;tbl] <=␈↓ ¬↔[issum[fn] → +[arg␈↓β1␈↓α[evargs];arg␈↓β2␈↓α[evargs]];
␈↓"β␈↓ α←␈↓α␈↓ ¬↔ isprod[fn] → *[arg␈↓β1␈↓α[evargs];arg␈↓β2␈↓α[evargs]];
␈↓"β␈↓ α←␈↓α␈↓ ¬↔ isexpt[fn] → ↑[arg␈↓β1␈↓α[evargs];arg␈↓β2␈↓α[evargs]];
␈↓"β␈↓ α←␈↓α␈↓ ¬↔ isfun[fn] → apply␈↓λ'␈↓α[locate[fn;tbl];evargs;tbl];
␈↓"β␈↓ α←␈↓α␈↓ ¬↔ isdef[fn] → value␈↓λ''␈↓α[␈↓ π∨body[fn];
␈↓"β␈↓ α←␈↓α␈↓ ¬↔␈↓ π∨newtbl[␈↓ π␈varlist[fn];
␈↓"β␈↓ α←␈↓α␈↓ ¬↔␈↓ π∨␈↓ π␈evargs;tbl]] ]
␈↓"β␈↓ α←␈↓αeval_args[args;tbl] <=␈↓ ∧␈[null[args] → ( );
␈↓"β␈↓ α←␈↓α␈↓ ∧␈ ␈↓
t␈↓α → concat[␈↓ ε∂value␈↓λ''␈↓α[first[args];tbl];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ε∂eval_args[rest[args];tbl]] ]
␈↓"∀␈↓ α←␈↓␈↓ β'Let's␈αgo␈αthrough␈αa␈αcomplete␈αevaluation␈αof␈α␈↓↓B␈↓␈αof␈αpage 84.␈α As␈αbefore,␈αwe
␈↓ α←␈↓will␈αuse␈α␈↓
R␈↓␈αas␈αa␈αmapping␈αfrom␈αexpressions␈αto␈αrepresentations.␈α Thus␈αwe␈αwant
␈↓ α←␈↓to pursue:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ βm␈↓αvalue␈↓λ''␈↓α[␈↓
R␈↓∞(␈↓α f[4;2;1] ␈↓∞)␈↓α; ␈↓
R␈↓∞(␈↓α{ <f, [[x;y;z] x*y + 2*z]> }␈↓∞)␈↓α]␈↓.
␈↓"∀␈↓ α←␈↓Let␈αus␈αdenote␈αthe␈αinitial␈αsymbol␈αtable,␈α␈↓
R␈↓∞(␈↓α{ <f, [[x;y;z] x*y + 2*z]> }␈↓∞)␈↓␈αas␈α␈↓αinit␈↓.
␈↓ α←␈↓This␈α
will␈αsimplify␈α
many␈αof␈α
the␈αexpressions.␈α
Notice␈αthat␈α
our␈αrepresentation␈α
of
␈↓ α←␈↓␈↓αf␈↓␈αin␈α␈↓αinit␈↓␈αhas␈αassociated␈αthe␈αvariable␈αlist␈α␈↓α[x;y;z]␈↓␈αwith␈αthe␈αbody␈αof␈αthe␈α
function.
␈↓ α←␈↓Thus␈α↔␈↓αlocate␈↓,␈α↔operating␈α↔on␈α↔this␈α↔table␈α↔with␈α↔the␈α↔name␈α↔␈↓αf␈↓,␈α↔will␈α↔return␈α↔a
␈↓ α←␈↓representation of ␈↓α[[x;y;z] x*y + 2*z]␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∨recognizer␈α∨␈↓αisfun_args␈↓␈α∨should␈α∨be␈α∨satisfied␈α∨and␈α∨thus␈α≡the
␈↓ α←␈↓computation should reduce to:
␈↓"∀␈↓ α←␈↓␈↓ βK␈↓αapply␈↓λ'␈↓α[␈↓ ∧7fun[␈↓
R␈↓∞(␈↓α f[4;2;1] ␈↓∞)␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7eval_args[args[␈↓
R␈↓∞(␈↓α f[4;2;1] ␈↓∞)␈↓α];init];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7init]␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'or:␈↓ βw␈↓αapply␈↓λ'␈↓α[ ␈↓
R␈↓∞(␈↓α f ␈↓∞)␈↓α ;eval_args[ ␈↓
R␈↓∞(␈↓α [4;2;1] ␈↓∞)␈↓α; init]; init ]
␈↓"∀␈↓ α←␈↓αeval_args␈↓␈α
will␈α
build␈α
a␈α
sequence␈α
of␈α
the␈α
evaluated␈α
arguments:␈α
␈↓α(4, 2, 1)␈↓,␈α
resulting
␈↓ α←␈↓in:
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ¬¬apply␈↓λ'␈↓α[ ␈↓
R␈↓∞(␈↓α f ␈↓∞)␈↓α ;(4, 2, 1) ; init ]
␈↓"∀␈↓ α←␈↓αapply␈↓λ'␈↓ should decide that ␈↓αf␈↓ satisfies ␈↓αisfun␈↓ giving:
␈↓"∀␈↓ α←␈↓α␈↓ β'␈↓ ∧)apply␈↓λ'␈↓α[ locate[ ␈↓
R␈↓∞(␈↓α f ␈↓∞)␈↓α ; init ]; (4, 2, 1) ; init ]␈↓
␈↓"∀␈↓ α←␈↓␈↓αlocate␈↓ will retrieve the definition, and
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ ∧∀␈↓αapply␈↓λ'␈↓α[ ␈↓
R␈↓∞(␈↓α [[x;y;z] x*y + 2*z] ␈↓∞)␈↓α ; (4, 2, 1) ; init ]␈↓
␈↓"β␈↓ α←␈↓should be the result.
␈↓ α←␈↓␈↓88 Applications␈↓
(2.7␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Next,␈α␈↓αapply␈↓λ'␈↓␈αshould␈αrealize␈αthat␈α␈↓
R␈↓∞(␈↓α␈α[[x;y;z]␈αx*y␈α+␈α2*z]␈α␈↓∞)␈↓␈αsatisfies␈α␈↓αisdef␈↓
␈↓ α←␈↓and thus:
␈↓"∀␈↓ α←␈↓␈↓ βK␈↓αvalue␈↓λ''␈↓α[␈↓ ∧+body[␈↓
R␈↓∞(␈↓α [[x;y;z] x*y + 2*z] ␈↓∞)␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧+newtbl[␈↓ ¬varlist[␈↓
R␈↓∞(␈↓α [[x;y;z] x*y + 2*z] ␈↓∞)␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧+␈↓ ¬(4,2,1);
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧+␈↓ ¬init]]␈↓
␈↓"∀␈↓ α←␈↓or:␈↓ βE␈↓αvalue␈↓λ''␈↓α[ ␈↓
R␈↓∞(␈↓α [x*y + 2*z] ␈↓∞)␈↓α ;newtbl[ ␈↓
R␈↓∞(␈↓α [x;y;z] ␈↓∞)␈↓α ;(4,2,1);init]]␈↓
␈↓" ␈↓ α←␈↓after ␈↓αbody␈↓ and ␈↓αvarlist␈↓ are finished.
␈↓"∀␈↓ α←␈↓␈↓
R␈↓∞(␈↓α [x;y;z] ␈↓∞)␈↓␈αis␈α␈↓α(␈↓
R␈↓∞(␈↓α x ␈↓∞)␈↓α, ␈↓
R␈↓∞(␈↓α y ␈↓∞)␈↓α, ␈↓
R␈↓∞(␈↓α z ␈↓∞)␈↓α)␈↓,␈αand␈αtherefore␈αthe␈αcomputation
␈↓ α←␈↓of ␈↓αnewtbl␈↓ will build a new table with entries for ␈↓αx, y, ␈↓ and ␈↓αz␈↓ on the front:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ∧β␈↓
R␈↓∞(␈↓α{ <x, 4>, <y, 2>, <z, 1>, <f, [[x;y;z] x*y + 2*z]> }␈↓∞)␈↓.
␈↓"∀␈↓ α←␈↓Thus we call ␈↓αvalue␈↓λ''␈↓ with:
␈↓"∀␈↓ α←␈↓␈↓ β∂␈↓αvalue␈↓λ''␈↓α[␈↓ βo␈↓
R␈↓∞(␈↓α [x*y + 2*z] ␈↓∞)␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓
R␈↓∞(␈↓α{ <x, 4>, <y, 2>, <z, 1>, <f, [[x;y;z] x*y + 2*z]> }␈↓∞)␈↓α]␈↓
␈↓"∀␈↓ α←␈↓Now we're back at problem ␈↓↓A␈↓ of page 81.
␈↓"β␈↓ α←␈↓␈↓ ¬X␈↓↓Time to take stock␈↓
␈↓"∀␈↓ α←␈↓We␈α∃have␈α∃written␈α∃a␈α∀reasonably␈α∃sophisticated␈α∃algorithm␈α∃here;␈α∃we␈α∀should
␈↓ α←␈↓examine␈α↔the␈α↔results␈α↔quite␈α↔carefully.␈α⊗ Notice␈α↔that␈α↔we␈α↔have␈α↔written␈α⊗the
␈↓ α←␈↓algorithm␈α↔with␈α⊗almost␈α↔no␈α⊗concern␈α↔for␈α⊗representation.␈α↔We␈α↔␈↓¬assume␈↓␈α⊗that
␈↓ α←␈↓representations␈α
are␈α
available␈α
for␈αsuch␈α
varied␈α
things␈α
as␈αarithmetic␈α
expressions,
␈↓ α←␈↓tables,␈αcalls␈αon␈αfunctions,␈αand␈αeven␈αfunction␈αdefinitions.␈αVery␈αseldom␈αdid␈αwe
␈↓ α←␈↓commit␈α
ourselves␈α
to␈α
anything␈α
close␈α
to␈α
a␈α
concrete␈α
representation,␈α
and␈αthen␈α
only
␈↓ α←␈↓with␈α
great␈αreluctance.␈α
It␈α
was␈αwith␈α
some␈α
sadness␈αthat␈α
we␈α
imposed␈αa␈α
sequencing
␈↓ α←␈↓on␈α∪elements␈α∪of␈α∪tables.␈α∪ Variable␈α∪lists␈α∪and␈α∪calling␈α∪sequences␈α∪were␈α∪not␈α∩as
␈↓ α←␈↓traumatic;␈α
we␈α
claimed␈α
their␈α
natural␈αstructure␈α
was␈α
a␈α
sequence.␈α
As␈α
always,␈αif␈α
we
␈↓ α←␈↓wish␈α≠to␈α≠run␈α≠these␈α≠programs␈α≠on␈α≠a␈α≠machine␈α≠we␈α≠must␈α≠supply␈α≠some
␈↓ α←␈↓representations,␈α
but␈αeven␈α
then␈αthe␈α
representations␈α
will␈αonly␈α
interface␈αwith␈α
our
␈↓ α←␈↓algorithms at the constructors, selectors and recognizers.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∩have␈α∩made␈α∩some␈α⊃more␈α∩serious␈α∩representational␈α∩decisions␈α∩in␈α⊃the
␈↓ α←␈↓structure␈α
of␈α
the␈αalgorithm.␈α
We␈α
have␈α
encoded␈αa␈α
version␈α
of␈α
the␈α␈↓↓CBV␈↓-scheme
␈↓ α←␈↓of␈αpage 16.␈α
We␈αhave␈αseen␈α
what␈αkinds␈α
of␈αdifficulties␈αthat␈α
can␈αcause.␈α
We␈αwill
␈↓ α←␈↓spend␈α∪a␈α∪large␈α∪amount␈α∪of␈α∪time␈α∪in␈α∪Chapter 3␈α∪discussing␈α∪the␈α∪problems␈α∩of
␈↓ α←␈↓evaluation.␈↓π 14␈↓
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 14␈↓A␈α→second␈α→decision␈α→was␈α~implied␈α→in␈α→our␈α→handling␈α~of␈α→function
␈↓ α←␈↓definitions;␈α↔namely␈α_we␈α↔bound␈α_the␈α↔function␈α↔name␈α_to␈α↔a␈α_data␈α↔structure
␈↓ α←␈↓representing␈α~the␈α~formal␈α~parameter␈α→list␈α~and␈α~the␈α~function␈α~body.␈α→This
␈↓ α←␈↓representation␈αgives␈α
the␈αexpected␈α
result␈αin␈α
most␈αcases,␈α
but␈αinvolves␈α
one␈αof␈α
the
␈↓ α←␈↓more␈α⊃problematic␈α⊃areas␈α⊃of␈α⊃programming␈α⊂languages:␈α⊃how␈α⊃do␈α⊃you␈α⊃find␈α⊂the
␈↓ α←␈↓␈↓2.7␈↓ πFEvaluation of Polynomials 89␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Finally,␈α∂our␈α∂decisions␈α∂on␈α∂the␈α∞data␈α∂structures␈α∂and␈α∂the␈α∂algorithms␈α∞were
␈↓ α←␈↓not␈αmade␈α
independently.␈αFor␈α
example,␈αthere␈α
is␈αstrong␈α
interaction␈αbetween␈α
our
␈↓ α←␈↓representation␈α_of␈α→tables␈α_and␈α→the␈α_algorithms,␈α→␈↓αlocate␈↓␈α_and␈α→␈↓αnewtbl␈↓␈α_which
␈↓ α←␈↓manipulate␈α⊃those␈α∩tables.␈α⊃We␈α∩should␈α⊃ask␈α∩how␈α⊃much␈α∩of␈α⊃this␈α∩interaction␈α⊃is
␈↓ α←␈↓inherent␈α
and␈α
how␈α
much␈α
is␈α
gratuitous.␈α
For␈α
example,␈α
we␈α
have␈α
remarked␈α
that
␈↓ α←␈↓our␈α∂representation␈α∞can␈α∂contain␈α∞pairs␈α∂with␈α∞duplicate␈α∂first␈α∞elements.␈α∂It␈α∂is␈α∞the
␈↓ α←␈↓responsibility␈α⊂of␈α⊂␈↓αlocate␈↓␈α⊂to␈α⊂see␈α⊂that␈α∂we␈α⊂find␈α⊂the␈α⊂expected␈α⊂pair.␈α⊂If␈α⊂we␈α∂wrote
␈↓ α←␈↓␈↓αlocate␈↓␈α∂to␈α∂search␈α∂from␈α∂right␈α∂to␈α∂left,␈α∂we␈α∂could␈α∂get␈α∂the␈α∂wrong␈α∂pair.␈α∂We␈α∂␈↓¬could␈↓
␈↓ α←␈↓write␈α
␈↓αnewtbl␈↓␈α
to␈α
be␈αmore␈α
selective;␈α
it␈α
could␈αmanufacture␈α
a␈α
table␈α
without␈αsuch
␈↓ α←␈↓duplications:
␈↓"∀␈↓ α←␈↓αnewtbl[vars;vals;tbl] <=␈↓ ¬↔[null[tbl] →␈↓ ε'[null[vars] → ();
␈↓"β␈↓ α←␈↓α␈↓ ¬↔␈↓ ε' ␈↓
t␈↓α → concat[␈↓ π7mkent[first[vars];first[vals]];
␈↓"β␈↓ α←␈↓α␈↓ ¬↔␈↓ ε'␈↓ π7newtbl[␈↓ λ↔rest[vars];
␈↓"β␈↓ α←␈↓α␈↓ ¬↔␈↓ ε'␈↓ π7␈↓ λ↔rest[vals];
␈↓"β␈↓ α←␈↓α␈↓ ¬↔␈↓ ε'␈↓ π7␈↓ λ↔( )]]];
␈↓"β␈↓ α←␈↓α␈↓ ¬↔ member[name[first[tbl]];vars] → newtbl[␈↓ ∂vars;
␈↓"β␈↓ α←␈↓α␈↓ ¬↔␈↓ ε'␈↓ π7␈↓ λ↔␈↓ ∂vals;
␈↓"β␈↓ α←␈↓α␈↓ ¬↔␈↓ ε'␈↓ π7␈↓ λ↔␈↓ ∂rest[tbl]];
␈↓"β␈↓ α←␈↓α␈↓ ¬↔ ␈↓
t␈↓α → concat[first[tbl];
␈↓"β␈↓ α←␈↓α␈↓ ¬↔␈↓ ε'newtbl[vars;vals;rest[tbl]]] ]
␈↓"∀␈↓ α←␈↓This␈αversion␈αof␈α
␈↓αnewtbl␈↓␈αrequires␈αmuch␈α
more␈αcomputation␈αthan␈αthe␈α
alternative.
␈↓ α←␈↓Its␈α∃advantage␈α∃is␈α⊗that␈α∃the␈α∃"set"-ness␈α∃of␈α⊗symbol␈α∃tables␈α∃is␈α⊗maintained.␈α∃A
␈↓ α←␈↓disadvantage␈αis␈α
that␈αthe␈αrebinding␈α
process␈αimplies␈αa␈α
rebuilding␈αof␈α
the␈αtable.
␈↓ α←␈↓The␈α"set"␈α
property␈αis␈α
one␈αwhich␈α
we␈αneed␈α
not␈αdepend␈α
on␈αfor␈α
our␈αalgorithms;
␈↓ α←␈↓in␈α∞fact,␈α∂we␈α∞will␈α∂frequently␈α∞expect␈α∂that␈α∞a␈α∂table␈α∞is␈α∂represented␈α∞as␈α∂a␈α∞sequence
␈↓ α←␈↓with the previous values of variables found further along in the sequence.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊃main␈α⊃point␈α⊃of␈α⊃this␈α⊃example␈α⊂however␈α⊃is␈α⊃to␈α⊃impress␈α⊃on␈α⊃you␈α⊂the
␈↓ α←␈↓importance␈α∂of␈α∂writing␈α⊂at␈α∂a␈α∂sufficiently␈α∂high␈α⊂level␈α∂of␈α∂abstraction.␈α⊂We␈α∂have
␈↓ α←␈↓produced␈α∪a␈α∪non-trivial␈α∪algorithm␈α∪which␈α∪is␈α∪clear␈α∪and␈α∪concise.␈α∪If␈α∀it␈α∪were
␈↓ α←␈↓desirable␈αto␈αhave␈αthis␈αalgorithm␈αrunning␈αon␈αa␈αmachine␈αwe␈αcould␈αcode␈αit␈αand
␈↓ α←␈↓its␈α
associated␈α
data␈α∞structure␈α
representations␈α
in␈α
a␈α∞very␈α
short␈α
time.␈α
In␈α∞a␈α
very
␈↓ α←␈↓short time ␈↓¬we␈↓ will be able to run this algorithm on a LISP machine.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓bindings␈α
of␈α
variables␈α
which␈α
do␈α
not␈α
appear␈α
in␈α
the␈α
current␈α
variable␈α
list?␈α
For
␈↓ α←␈↓example,␈α∞function␈α
names␈α∞belong␈α
in␈α∞this␈α
category.␈α∞ Such␈α
variables␈α∞are␈α
called
␈↓ α←␈↓non-local␈α∞variables.␈α∞The␈α∞scheme␈α∂proposed␈α∞in␈α∞this␈α∞section␈α∞finds␈α∂the␈α∞binding
␈↓ α←␈↓which␈α∂is␈α∂current␈α∂when␈α∂the␈α∂function␈α∂was␈α∂applied.␈α∂ This␈α∂corresponds␈α⊂to␈α∂the
␈↓ α←␈↓"latest␈αactive"␈αbinding␈αmade␈αfor␈α
the␈αvariable␈αin␈αquestion.␈αSome␈α
programming
␈↓ α←␈↓languages,␈α∩in␈α∪particular␈α∩LISP,␈α∩follow␈α∪this␈α∩strategy;␈α∩some␈α∪other␈α∩languages
␈↓ α←␈↓follow␈α∞Algol 60␈α∞and␈α∞use␈α∞the␈α∂binding␈α∞which␈α∞was␈α∞current␈α∞when␈α∂the␈α∞function
␈↓ α←␈↓was␈αdefined,␈αand␈αsome␈αlanguages␈αallow␈α
both.␈α The␈αnext␈αtwo␈αchapters␈αbegin␈α
a
␈↓ α←␈↓study of binding strategies.
␈↓ α←␈↓␈↓90 Applications␈↓
(2.7␈↓
␈↓"β␈↓ α←␈↓␈↓ ε≡␈↓↓Problem␈↓
␈↓"→␈↓ α←␈↓␈↓↓1.␈↓ On␈α∞page 81␈α∂we␈α∞mentioned␈α∞the␈α∂possibility␈α∞of␈α∞writing␈α∂the␈α∞new␈α∞␈↓αvalue␈↓␈α∂as␈α∞a
␈↓ α←␈↓␈↓ β∂combination␈α∞of␈α∞old␈α∞␈↓αvalue␈↓␈α∂and␈α∞␈↓αinstantiate␈↓.␈α∞We␈α∞rejected␈α∞that␈α∂scheme.␈α∞On
␈↓ α←␈↓␈↓ β∂page 85␈α
we␈αhad␈α
to␈α
save␈αan␈α
old␈αtable␈α
since␈α
we␈αmight␈α
need␈αsome␈α
previously
␈↓ α←␈↓␈↓ β∂defined␈α∩functions.␈α∩We␈α∩might␈α∩not␈α∩have␈α∩had␈α∩this␈α∩difficulty␈α∩if␈α∩we␈α∩had
␈↓ α←␈↓␈↓ β∂substituted␈αdirectly.␈αWrite␈αa␈αsubstitution-type␈α␈↓αvalue␈↓␈αand␈αuse␈αit␈αto␈αevaluate
␈↓ α←␈↓␈↓ β∂the ␈↓αg[2]␈↓ example.
␈↓ α←␈↓␈↓2.8␈↓ π}The Great Progenitors 91␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬%␈↓↓2.8 The Great Progenitors␈↓
␈↓"β␈↓ α←␈↓The␈α∞following␈α∞problems␈α∞are␈α∞written␈α∞(intentionally)␈α∞with␈α∞a␈α∞great␈α∞deal␈α∂of␈α∞the
␈↓ α←␈↓representation built into them.
␈↓"β␈↓ α←␈↓␈↓ ∧#␈↓↓1. The Great Mother of All Functions (␈↓αtgmoaf␈↓↓)
␈↓"β␈↓ α←␈↓α␈↓ α←tgmoaf[x] <=␈↓ ∧∪[isindiv[x] →␈↓ ¬;[eq[x;T] → ␈↓
t␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪␈↓ ¬; eq[x;NIL] → ␈↓
f␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪␈↓ ¬; ␈↓
t␈↓α → TRYAGAINNEXTWEEK];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪ eq[first[x];QUOTE] → second[x];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪ eq[first[x];CAR] → car[tgmoaf[second[x]]];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪ eq[first[x];CDR] → cdr[tgmoaf[second[x]]];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪ eq[first[x];CONS] → cons[␈↓ εctgmoaf[second[x]];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪␈↓ ¬;␈↓ εctgmoaf[third[x]]];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪ eq[first[x];ATOM] → atom[tgmoaf[second[x]]];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪ eq[first[x];EQ] → eq[tgmoaf[second[x]];tgmoaf[third[x]]];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪ ␈↓
t␈↓α → TRYAGAINNEXTWEEK]
␈↓"β␈↓ α←␈↓Evaluate the following:
␈↓"β␈↓ α←␈↓␈↓↓a.␈↓α tgmoaf[T]
␈↓"β␈↓ α←␈↓α␈↓↓b.␈↓α tgmoaf[A]
␈↓"β␈↓ α←␈↓α␈↓↓c.␈↓α tgmoaf[(CAR (QUOTE (A . B)))]
␈↓"β␈↓ α←␈↓α␈↓↓d.␈↓α tgmoaf[(CDR (QUOTE (A B)))]
␈↓"β␈↓ α←␈↓α␈↓↓e.␈↓α tgmoaf[(EQ (CAR (QUOTE (A . B))) (QUOTE A))]
␈↓"β␈↓ α←␈↓α␈↓↓f.␈↓α tgmoaf[(EQ (CAR (QUOTE (A . B))) A)]
␈↓"β␈↓ α←␈↓α␈↓↓g.␈↓α tgmoaf[(ATOM (CAR (QUOTE (A B))))]
␈↓"β␈↓ α←␈↓α␈↓ βZ␈↓↓2. The Great Mother of All Functions Revisited (␈↓αtgmoafr␈↓↓)
␈↓"β␈↓ α←␈↓α␈↓ α←tgmoafr[x] <=␈↓ ∧∪[isindiv[x] →␈↓ ¬;[eq[x;T] → ␈↓
t␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪␈↓ ¬; eq[x;NIL] → ␈↓
f␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪␈↓ ¬; ␈↓
t␈↓α → TRYAGAINNEXTWEEK];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪ eq[first[x];QUOTE] → second[x];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪ eq[first[x];CAR] → car[tgmoafr[second[x]]];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪ eq[first[x];CDR] → cdr[tgmoafr[second[x]]];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪ eq[first[x];CONS] → cons[␈↓ εctgmoafr[second[x]];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪␈↓ ¬;␈↓ εctgmoafr[third[x]]];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪ eq[first[x];ATOM] → atom[tgmoafr[second[x]]];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪ eq[first[x];EQ] → eq[tgmoafr[second[x]];tgmoafr[third[x]]];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪ eq[first[x];COND] → evcond[rest[x]];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪ ␈↓
t␈↓α → TRYAGAINNEXTWEEK]
␈↓ α←␈↓␈↓92 Applications␈↓
(2.8␈↓
␈↓"β␈↓ α←␈↓α␈↓ α←evcond[x] <=␈↓ ∧∪[tgmoafr[first[first[x]]] → tgmoafr[second[first[x]]];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧∪ ␈↓
t␈↓α → evcond[rest[x]] ]
␈↓"β␈↓ α←␈↓Evaluate the following:
␈↓"β␈↓ α←␈↓␈↓↓a.␈↓α tgmoafr[T]
␈↓"β␈↓ α←␈↓α␈↓↓b.␈↓α tgmoafr[(CDR (QUOTE (A B)))]
␈↓"β␈↓ α←␈↓α␈↓↓c.␈↓α tgmoafr[(EQ (CAR (QUOTE (A . B))) (QUOTE A))]
␈↓"β␈↓ α←␈↓α␈↓↓d.␈↓α tgmoafr[(COND (␈↓ ∧[(EQ (CAR (QUOTE (A . B))) (QUOTE A))
␈↓"β␈↓ α←␈↓α␈↓ ∧[␈↓ ∧g(QUOTE FOO)))]
␈↓"β␈↓ α←␈↓α␈↓↓5.␈↓α tgmoafr[(COND␈↓ ∧[((ATOM (QUOTE (A))) (QUOTE FOO))
␈↓"β␈↓ α←␈↓α␈↓ ∧[(T (QUOTE BAZ)))]
␈↓"β␈↓ α←␈↓␈↓ ∧3Coming soon: ␈↓↓Son of the Great Progenitor !!␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬N␈↓↓2.9 Another Respite␈↓
␈↓"β␈↓ α←␈↓We␈αhave␈α
again␈αreached␈α
a␈αpoint␈αwhere␈α
a␈αcertain␈α
amount␈αof␈α
reflection␈αwould
␈↓ α←␈↓be␈α∩beneficial.␈α∩ Though␈α∩this␈α∩is␈α⊃not␈α∩a␈α∩programming␈α∩manual␈α∩we␈α∩would␈α⊃be
␈↓ α←␈↓remiss␈α⊂if␈α⊂we␈α⊂did␈α⊂not␈α⊂analyze␈α∂the␈α⊂programming␈α⊂style␈α⊂which␈α⊂we␈α⊂have␈α∂been
␈↓ α←␈↓advocating.
␈↓"λ␈↓ α←␈↓␈↓↓1.␈↓ Write␈α⊂the␈α⊂algorithm␈α⊂in␈α⊂an␈α∂abstract␈α⊂setting;␈α⊂do␈α⊂not␈α⊂muddle␈α⊂the␈α∂abstract
␈↓ α←␈↓␈↓ β∂algorithm␈αwith␈αthe␈αchosen␈αrepresentation.␈α If␈αyou␈αfollow␈αthis␈αdictum␈αyour
␈↓ α←␈↓␈↓ β∂LISP␈αprograms␈αwill␈αnever␈αuse␈α␈↓αcar,␈αcdr,␈αcons␈↓,␈αand␈α␈↓αatom␈↓,␈αand␈αrarely␈αuse␈α␈↓αeq␈↓.
␈↓ α←␈↓␈↓ β∂All␈α_instances␈α_of␈α↔these␈α_LISP␈α_primitives␈α↔will␈α_be␈α_relegated␈α_to␈α↔small
␈↓ α←␈↓␈↓ β∂subfunctions which manipulate representations.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ When␈αwriting␈αthe␈α
abstract␈αprogram,␈αdo␈αnot␈α
be␈αafraid␈αto␈αcast␈α
off␈αdifficult
␈↓ α←␈↓␈↓ β∂parts␈αof␈αthe␈αimplementation␈αto␈αsubfunctions.␈αRemember␈αthat␈αif␈αyou␈αhave
␈↓ α←␈↓␈↓ β∂trouble␈α
keeping␈αthe␈α
details␈α
in␈αmind␈α
when␈α
␈↓¬writing␈↓␈αthe␈α
program,␈α
then␈αthe
␈↓ α←␈↓␈↓ β∂confusion␈α⊂involved␈α⊂in␈α⊂␈↓¬reading␈↓␈α⊂the␈α⊂program␈α⊂at␈α⊂some␈α⊂later␈α⊂time␈α⊂will␈α∂be
␈↓ α←␈↓␈↓ β∂overwhelming.␈α∂Once␈α∂you␈α∂have␈α∂convinced␈α∂yourself␈α∂of␈α∂the␈α⊂correctness␈α∂of
␈↓ α←␈↓␈↓ β∂the␈α↔current␈α↔composition,␈α⊗then␈α↔worry␈α↔about␈α⊗the␈α↔construction␈α↔of␈α⊗the
␈↓ α←␈↓␈↓ β∂subfunctions.␈α∞Seldom␈α∞does␈α∂the␈α∞process␈α∞of␈α∂composing␈α∞a␈α∞program␈α∂flow␈α∞so
␈↓ α←␈↓␈↓ β∂gently␈α
from␈α
top-level␈αto␈α
specific␈α
representation.␈αOnly␈α
the␈α
toy␈αprograms␈α
are
␈↓ α←␈↓␈↓ β∂easy;␈αthe␈αconstruction␈αof␈αthe␈αpractical␈αprogram␈αwill␈αbe␈αconfusing,␈αand␈α
will
␈↓ α←␈↓␈↓ β∂require␈αmuch␈αrethinking.␈α But␈αbring␈αas␈αmuch␈αstructure␈αas␈αyou␈αcan␈αto␈αthe
␈↓ α←␈↓␈↓ β∂process.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ From␈α∂the␈α∂other␈α∂side␈α∞of␈α∂the␈α∂question,␈α∂don't␈α∞be␈α∂afraid␈α∂to␈α∂look␈α∂at␈α∞specific
␈↓ α←␈↓␈↓ β∂implementations,␈α∃or␈α∃specific␈α∃data-structure␈α∃representations␈α∃before␈α∃you
␈↓ α←␈↓␈↓2.9␈↓ λFAnother Respite 93␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂begin␈α
to␈α∞write.␈α
There␈α
is␈α∞something␈α
quite␈α
comforting␈α∞about␈α
a␈α∞"real"␈α
data
␈↓ α←␈↓␈↓ β∂structure.␈α
Essentially␈α
data␈α
structures␈α
are␈α
static␈α
objects,␈↓π 15␈↓␈α∞while␈α
programs
␈↓ α←␈↓␈↓ β∂are␈α
dynamic␈α
objects.␈αA␈α
close␈α
look␈α
at␈αa␈α
possible␈α
representation␈α
may␈αget␈α
you
␈↓ α←␈↓␈↓ β∂a␈α
starting␈α∞point␈α
and␈α∞as␈α
you␈α
write␈α∞the␈α
program␈α∞a␈α
distinction␈α∞will␈α
emerge
␈↓ α←␈↓␈↓ β∂between␈α∪a␈α∪dependence␈α∪on␈α∪the␈α∪specific␈α∪representation␈α∪and␈α∪the␈α∪use␈α∩of
␈↓ α←␈↓␈↓ β∂properties of an abstract data structure.
␈↓"β␈↓ α←␈↓␈↓ β'Perhaps␈α⊃the␈α∩more␈α⊃practical␈α∩reader␈α⊃is␈α∩overcome␈α⊃by␈α∩the␈α⊃inefficiencies
␈↓ α←␈↓inherent␈α∀in␈α∀these␈α∀proposals.␈α∀Two␈α∀answers:␈α∀first,␈α∀"inefficiency"␈α∀is␈α∀a␈α∀very
␈↓ α←␈↓ethereal␈αconcept.␈α Like␈α"structured␈αprogramming",␈αit␈αis␈αdifficult␈αto␈αdefine␈αbut
␈↓ α←␈↓recognizable␈α∀when␈α∀it␈α∀occurs.␈α∪ Hardware␈α∀development␈α∀has␈α∀enabled␈α∀us␈α∪to
␈↓ α←␈↓efficiently␈α∂execute␈α⊂many␈α∂operations␈α⊂which␈α∂were␈α⊂quite␈α∂inefficient␈α⊂on␈α∂earlier
␈↓ α←␈↓machines.␈α
But␈αeven␈α
at␈αa␈α
more␈αtopical␈α
level,␈αmuch␈α
of␈αwhat␈α
seems␈αinefficient
␈↓ α←␈↓can␈α∪now␈α∪be␈α∪straightened␈α∪out␈α∪by␈α∪a␈α∪compiler␈α∪(see␈α∪Chapter 6).␈α∪Frequently,
␈↓ α←␈↓compilers␈α∂can␈α∞do␈α∂very␈α∞clever␈α∂optimizations␈α∞to␈α∂generate␈α∞efficient␈α∂code.␈α∂ It␈α∞is
␈↓ α←␈↓better␈α⊗to␈α⊗leave␈α⊗the␈α⊗cleverness␈α↔to␈α⊗the␈α⊗compiler,␈α⊗and␈α⊗the␈α⊗clarity␈α↔to␈α⊗the
␈↓ α←␈↓programmer.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αcurrent␈αproblems␈αin␈αprogramming␈αare␈αnot␈αthose␈αof␈αefficiency;␈αthey
␈↓ α←␈↓are␈αproblems␈αof␈α␈↓¬correctness␈↓.␈α
That␈αis,␈αwe␈αhave␈α
a␈αbetter␈αgrasp␈αof␈αtechniques␈α
for
␈↓ α←␈↓improving␈α
efficiency␈α
of␈α
programs␈α
than␈αwe␈α
do␈α
of␈α
techniques␈α
for␈α
guiding␈αthe
␈↓ α←␈↓construction␈α
of␈α
programs␈α
which␈α
work.␈α
How␈α
do␈α
you␈α
write␈α
a␈α
program␈αwhich
␈↓ α←␈↓works?␈α Until␈αpractical␈αtools␈αare␈αdeveloped␈αfor␈αproving␈αcorrectness␈αit␈αis␈αup␈αto
␈↓ α←␈↓the␈αprogrammer␈αto␈αcertify␈αhis␈α
programs.␈αAny␈αmethodology␈αwhich␈αcan␈αaid␈α
the
␈↓ α←␈↓programmer␈α⊃will␈α⊃be␈α⊃most␈α⊃welcome.␈α∩ Clearly,␈α⊃the␈α⊃closer␈α⊃you␈α⊃can␈α∩write␈α⊃the
␈↓ α←␈↓program␈αto␈αyour␈αintuition,␈αthe␈αless␈αchance␈αthere␈αis␈αfor␈αerror.␈αThis␈αwas␈αone␈αof
␈↓ α←␈↓the␈αreasons␈αfor␈αdeveloping␈αhigh-level␈αlanguages.␈αThe␈αoriginal␈αmotivation␈αfor
␈↓ α←␈↓such␈α
languages␈αwas␈α
a␈αconvenient␈α
notation␈αfor␈α
expressing␈αnumerical␈α
problems.
␈↓ α←␈↓With␈α
data␈α
structures,␈α
we␈α
are␈α
able␈α
to␈α
formalize␈α
a␈α
broader␈α
range␈α∞of␈α
domains,
␈↓ α←␈↓expressing␈α→our␈α→ideas␈α→as␈α_data␈α→structure␈α→manipulations␈α→rather␈α→than␈α_as
␈↓ α←␈↓numerical relationships.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α⊃are␈α⊃at␈α⊂least␈α⊃two␈α⊃kinds␈α⊂of␈α⊃errors␈α⊃which␈α⊂are␈α⊃prevalent␈α⊃in␈α⊂data
␈↓ α←␈↓structure␈α∂programming:␈α∂errors␈α∂of␈α∂omission -- misunderstanding␈α∂of␈α∂the␈α∂basic
␈↓ α←␈↓algorithm;␈αand␈αerrors␈αof␈αcommission -- errors␈αdue␈αto␈αmisapplied␈αcleverness␈αin
␈↓ α←␈↓attempting to be efficient.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
occurrences␈αof␈α
errors␈αof␈α
omission␈αcan␈α
be␈αminimized␈α
by␈αpresenting
␈↓ α←␈↓the␈α∃user␈α∀with␈α∃programming␈α∃constructs␈α∀which␈α∃are␈α∀close␈α∃to␈α∃the␈α∀informal
␈↓ α←␈↓algorithm.␈α⊃ Such␈α⊃constructs␈α⊃include␈α⊂control␈α⊃structures,␈α⊃data␈α⊃structures,␈α⊂and
␈↓ α←␈↓representations for operations.
␈↓"β␈↓ α←␈↓␈↓ β'Errors␈α∂of␈α∂commission␈α∂comprise␈α∂the␈α∂great␈α∂majority␈α∂of␈α∂the␈α⊂present␈α∂day
␈↓ α←␈↓headaches.␈α⊃It␈α⊃is␈α⊃here␈α⊃that␈α∩programming␈α⊃␈↓¬style␈↓␈α⊃can␈α⊃be␈α⊃beneficial:␈α∩keep␈α⊃the
␈↓ α←␈↓representation␈α∃of␈α∃the␈α∀data␈α∃structures␈α∃away␈α∀from␈α∃the␈α∃description␈α∃of␈α∀the
␈↓ α←␈↓algorithm;␈α∪write␈α∪concise␈α∪abstract␈α∪programs,␈α∪passing␈α∪off␈α∪responsibilities␈α∩to
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 15␈↓At least within the program presently being constructed.
␈↓ α←␈↓␈↓94 Applications␈↓
(2.9␈↓
␈↓"β␈↓ α←␈↓subfunctions.␈αWhenever␈αa␈αdefinition␈αof␈α"structured␈αprogramming"␈α
is␈αarrived
␈↓ α←␈↓at, this advice on programming style will no doubt be included.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αrealization␈αthat␈αprograms␈α␈↓¬will␈↓␈αhave␈αerrors␈αor␈αrequire␈αmodification
␈↓ α←␈↓raises␈αsome␈αdifficulties␈αfor␈αhighly␈αstructured␈αlanguages.␈αA␈αrealistic␈αdebugging
␈↓ α←␈↓system␈α
must␈α∞allow␈α
program␈α∞modification␈α
and␈α∞data␈α
structure␈α∞modification;␈α
if
␈↓ α←␈↓the␈α→language␈α→system␈α→imposes␈α~rigid␈α→restrictions␈α→on␈α→such␈α~activities␈α→the
␈↓ α←␈↓programmer's␈α∪productivity␈α∪will␈α∪suffer.␈α∪Most␈α∪language␈α∪systems␈α∀have␈α∪been
␈↓ α←␈↓designed␈αfor␈αthe␈α␈↓¬execution␈↓␈αof␈αprograms.␈αLISP␈αsystems␈αput␈αa␈αhigher␈αpremium
␈↓ α←␈↓on␈α⊗␈↓¬debugging␈↓,␈α⊗perhaps␈α⊗because␈α↔of␈α⊗the␈α⊗nature␈α⊗of␈α↔Artificial␈α⊗Intelligence
␈↓ α←␈↓research:␈αthe␈αoriginal␈α
motivation␈αfor␈αLISP.␈α
LISP␈αprogramming␈αsystems␈α
have
␈↓ α←␈↓a␈αhigh␈αdegree␈αof␈αinteractiveness;␈αthe␈αresult␈αis␈αan␈αeffective␈α
programming␈αtool.
␈↓ α←␈↓It␈αis␈αa␈αtool␈αwith␈αsharp␈αedges;␈αone␈αcan␈αeither␈αbuild␈αmediocre␈αtools␈αwhich␈αcan't
␈↓ α←␈↓hurt␈α⊂anyone,␈α⊂or␈α⊂can␈α⊂build␈α⊂a␈α⊂sharp␈α⊂tool␈α⊂and␈α⊂expect␈α⊂that␈α⊂it␈α⊂be␈α⊃applied␈α⊂by
␈↓ α←␈↓knowledgeable␈α
users.␈α∞LISP␈α
programmers␈α
belong␈α∞in␈α
the␈α∞second␈α
classification.
␈↓ α←␈↓Our␈α⊃discussions␈α∩of␈α⊃LISP␈α∩programming␈α⊃style␈α⊃should␈α∩develop␈α⊃some␈α∩of␈α⊃the
␈↓ α←␈↓requisite knowledge.
␈↓"β␈↓ α←␈↓␈↓ β'Before␈αclosing␈αthis␈αdiscussion␈αof␈α
LISP␈αprogramming␈αstyle,␈αwe␈αcan't␈α
help
␈↓ α←␈↓but␈α↔note␈α_that␈α↔in␈α_the␈α↔preceding␈α↔section,␈α_␈↓↓The␈α↔Great␈α_Progenitors␈↓␈α↔have
␈↓ α←␈↓completely␈α⊃ignored␈α⊃our␈α⊃good␈α⊃advice.␈α⊃This␈α⊃would␈α⊃be␈α⊃a␈α⊃good␈α⊃time␈α⊃for␈α⊂the
␈↓ α←␈↓interested␈α∞reader␈α∞to␈α∂abstract␈α∞the␈α∞␈↓αtgmoaf␈↓␈α∂algorithm␈α∞from␈α∞the␈α∂particular␈α∞data
␈↓ α←␈↓representation. This detective work will be most rewarding.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Write an abstract version of ␈↓αtgmoaf␈↓.
␈↓"β␈↓ α←␈↓␈↓ ∧]␈↓↓2.10 Proving Properties of Programs␈↓
␈↓"β␈↓ α←␈↓People␈α_are␈α_becoming␈α_increasingly␈α_aware␈α_of␈α_the␈α_importance␈α_of␈α↔giving
␈↓ α←␈↓convincing␈α
arguments␈αfor␈α
such␈α
concepts␈αas␈α
the␈αcorrectness␈α
or␈α
equivalence␈αof
␈↓ α←␈↓programs.␈α
These␈αare␈α
both␈α
very␈αdifficult␈α
enterprises.␈↓π 16␈↓␈α
We␈αwill␈α
sketch␈αa␈α
proof
␈↓ α←␈↓of␈α
a␈α
simple␈α
property␈α
of␈α
two␈α
programs␈α
and␈α
leave␈α
others␈α
as␈α
problems␈α∞for␈α
the
␈↓ α←␈↓interested␈αreader.␈α How␈αdo␈αyou␈α
go␈αabout␈αproving␈αproperties␈αof␈αprograms?␈α
In
␈↓ α←␈↓Section 1.9␈α↔we␈α_noted␈α↔certain␈α_benefits␈α↔of␈α_defining␈α↔sets␈α_using␈α↔inductive
␈↓ α←␈↓definitions.␈α
There␈α
was␈α
a␈α
natural␈αway␈α
of␈α
thinking␈α
about␈α
the␈α
construction␈αof
␈↓ α←␈↓an␈αalgorithm␈αover␈αthat␈αset.␈αWe␈αhave␈αexploited␈αthat␈αobservation␈αin␈αour␈αstudy
␈↓ α←␈↓of␈αLISP␈αprogramming.␈αWe␈αneed␈αto␈αrecall␈αthe␈αobservation␈αthat␈αinductive␈αstyle
␈↓ α←␈↓proofs␈α
(see␈α
␈↓↓PRF␈↓␈α
on␈α
page 43)␈α
are␈α
valid␈α
forms␈α
of␈α
reasoning␈α
over␈αsuch␈α
domains.
␈↓ α←␈↓Since␈αwe␈αin␈αfact␈αdefined␈αour␈αdata␈αstructure␈αdomains␈αin␈αan␈αinductive␈αmanner,
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 16␈↓Question␈αof␈α"correctness"␈αreduce␈α
to␈α"equivalence"␈αnotions␈αin␈α
a␈αbroad
␈↓ α←␈↓sense,␈α∨relating␈α≡perhaps␈α∨a␈α∨declarative␈α≡specification␈α∨to␈α∨a␈α≡procedural
␈↓ α←␈↓specification.
␈↓ α←␈↓␈↓2.10␈↓ π Proving Properties of Programs 95␈↓
␈↓"β␈↓ α←␈↓it␈αseems␈αnatural␈αto␈αlook␈αfor␈αinductive␈αarguments␈αwhen␈αproving␈αproperties␈αof
␈↓ α←␈↓programs.␈αThis␈αis␈αindeed␈αwhat␈αwe␈αdo;␈αwe␈αperform␈αinduction␈αon␈αthe␈αstructure
␈↓ α←␈↓of the elements in the data domain.
␈↓"β␈↓ α←␈↓␈↓ β'For␈αexample,␈αgiven␈αthe␈αdefinition␈αof␈α␈↓αappend␈↓␈αgiven␈αon␈αpage 48␈αand␈αthe
␈↓ α←␈↓definition of ␈↓αreverse␈↓ given on page 49,
␈↓"∀␈↓ α←␈↓αappend[x;y] <= [null[x] → y; ␈↓
t␈↓α → concat[first[x];append[rest[x];y]]]
␈↓"∀␈↓ α←␈↓αreverse[x] <=␈↓ ∧∨[null[x] → ( );
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ ␈↓
t␈↓α → append[reverse[rest[x]];concat[first[x];( )]]]
␈↓"∀␈↓ α←␈↓we wish to show that:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ∧␈↓αappend[reverse[y];reverse[x]] = reverse[append[x;y]]␈↓
␈↓"∀␈↓ α←␈↓for any lists, ␈↓αx␈↓, and ␈↓αy␈↓. The induction will be on the structure of ␈↓αx␈↓.
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓↓Basis␈↓: ␈↓αx␈↓ is ␈↓α( )␈↓.
␈↓"β␈↓ α←␈↓␈↓ β∂We must thus show: ␈↓αappend[reverse[y];( )] = reverse[append[( );y]]␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂But: ␈↓αreverse[append[( );y]] = reverse[y]␈↓ by the def. of ␈↓αappend␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂We now establish the stronger result: ␈↓αappend[z;( )] = z␈↓ ␈↓π 17␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ βK␈↓↓Basis:␈↓ ␈↓αz␈↓ is ␈↓α( )␈↓.
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ βKShow ␈↓αappend[( );( )] = ( )␈↓. Easy.
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ βK␈↓↓Induction step:␈↓ Assume the lemma for lists, ␈↓αz␈↓, of length n;
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ βKProve: ␈↓αappend[concat[x;z];( )] = concat[x;z]␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ βKSince ␈↓αconcat[x;z]␈↓ is not ␈↓α( )␈↓, then applying the definition of ␈↓αappend␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ βKsays we must prove: ␈↓αconcat[x;append[z;( )]] = concat[x;z]␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ βKBut our induction hypothesis is applicable since ␈↓αz␈↓ is shorter than
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ βK␈↓αconcat[x;z]␈↓.
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ βKOur result follows.
␈↓"β␈↓ α←␈↓␈↓ β∂So the Basis for our main result is established.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 17␈↓In the following proof several intermediate steps have been omitted.
␈↓ α←␈↓␈↓96 Applications␈↓
_2.10␈↓
␈↓"∀␈↓ α←␈↓␈↓ β∂␈↓↓Induction step:␈↓ Assume the result for lists, ␈↓αz␈↓, of length n;
␈↓"β␈↓ α←␈↓␈↓ β∂Prove:
␈↓"β␈↓ α←␈↓␈↓ β∂(1) ␈↓αappend[reverse[y];reverse[concat[x;z]]]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βK = reverse[append[concat[x;z];y]]␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂ Applying the definition of ␈↓αreverse␈↓ to the LHS of (1) yields:
␈↓" ␈↓ α←␈↓␈↓ β∂␈↓ βh (2) ␈↓αappend[reverse[y];append[reverse[z];concat[x;( )]]]␈↓
␈↓" ␈↓ α←␈↓␈↓ β∂ Applying the definition of ␈↓αappend␈↓ to the RHS of (1) yields:
␈↓" ␈↓ α←␈↓␈↓ β∂␈↓ ∧l (3) ␈↓αreverse[concat[x;append[z;y]]]␈↓
␈↓" ␈↓ α←␈↓␈↓ β∂ Applying the definition of ␈↓αreverse␈↓ to (3) yields:
␈↓" ␈↓ α←␈↓␈↓ β∂␈↓ ∧≥ (4) ␈↓αappend[reverse[append[z;y]];concat[x;( )]]␈↓
␈↓" ␈↓ α←␈↓␈↓ β∂ Using our induction hypothesis on (4) gives:
␈↓" ␈↓ α←␈↓␈↓ β∂␈↓ βh (5) ␈↓αappend[append[reverse[y];reverse[z]];concat[x;( )]]␈↓
␈↓" ␈↓ α←␈↓␈↓ β∂ At this point we must establish that (2) = (5).
␈↓"β␈↓ α←␈↓␈↓ β∂ But this is just an instance of the associativity of ␈↓αappend␈↓:
␈↓" ␈↓ α←␈↓␈↓ β∂␈↓ ∧!␈↓αappend[x;append[y;z]] = append[append[x;y];z]␈↓
␈↓" ␈↓ α←␈↓␈↓ β'The␈α∩structure␈α∩of␈α∩the␈α∩proof␈α∩is␈α∩analogous␈α∩to␈α∩proofs␈α∩by␈α⊃mathematical
␈↓ α←␈↓induction␈α
in␈α
elementary␈αnumber␈α
theory.␈α
The␈α
ability␈αto␈α
perform␈α
such␈αproofs␈α
is
␈↓ α←␈↓a␈α≡direct␈α≡consequence␈α≡of␈α≡our␈α≡careful␈α≡definition␈α≡of␈α≡data␈α≥structures.
␈↓ α←␈↓Examination␈αof␈αthe␈α
proof␈αwill␈αshow␈α
that␈αthere␈αis␈α
a␈αclose␈αrelationship␈α
between
␈↓ α←␈↓what␈αwe␈αare␈αinducting␈αon␈αin␈αthe␈αproof␈αand␈αwhat␈αwe␈αare␈αrecurring␈αon␈αduring
␈↓ α←␈↓the␈α∞evaluation␈α∞of␈α
the␈α∞expressions.␈α∞A␈α∞program␈α
written␈α∞by␈α∞Boyer␈α∞and␈α
Moore
␈↓ α←␈↓has␈α∃been␈α∃reasonably␈α∃successful␈α∃in␈α∀generating␈α∃proofs␈α∃like␈α∃the␈α∃above␈α∀by
␈↓ α←␈↓exploiting this relationship. See [Boy 75] or [Moor 75b].␈↓π 18␈↓
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'1. Prove the associativity of ␈↓αappend␈↓.
␈↓" ␈↓ α←␈↓2. Analysis␈αof␈αthe␈αabove␈αproof␈αshows␈αfrequent␈αuse␈αof␈αother␈αresults␈αfor␈αLISP
␈↓ α←␈↓␈↓ β∂functions.␈αFill␈αin␈αthe␈αdetails.␈αInvestigate␈αthe␈αpossibility␈αof␈αformalizing␈αthis
␈↓ α←␈↓␈↓ β∂proof, showing what axioms are needed.
␈↓" ␈↓ α←␈↓3. Show the equivalence of ␈↓αfact␈↓ (page 44) and ␈↓αfact␈↓β1␈↓ (page 47).
␈↓" ␈↓ α←␈↓4. Show the equivalence of ␈↓αlength␈↓ and ␈↓αlength␈↓β1␈↓ (page 47).
␈↓" ␈↓ α←␈↓5. Using the definition of ␈↓αreverse␈↓, given on page 48, prove:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ¬K␈↓αreverse[reverse[x]] = x␈↓
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 18␈↓There␈αis␈αalso␈αa␈αformal␈αsystem␈αbased␈αon␈αa␈αtyped␈α␈↓λλ␈↓-calculus␈αwhich␈αhas
␈↓ α←␈↓had␈α0significant␈α0success␈α0in␈α0proving␈α0properies␈α1of␈α0programs.
␈↓ α←␈↓[LCF 72], [New 75].␈α∂More␈α∞recently␈α∂[Car 76]␈α∞has␈α∂developed␈α∞a␈α∂formal␈α∞system
␈↓ α←␈↓including␈α⊃rules␈α⊃of␈α⊃inference,␈α∩a␈α⊃proof␈α⊃checker,␈α⊃and␈α⊃a␈α∩viable␈α⊃programming
␈↓ α←␈↓language which is based on a "typed LISP".
␈↓ α←␈↓␈↓3.␈↓ ∧Evaluation 97␈↓
␈↓"β␈↓ α←␈↓␈↓ ␈↓↓CHAPTER 3␈↓
␈↓"β␈↓ α←␈↓␈↓ Evaluation of LISP Expressions␈↓
␈↓"β␈↓ α←␈↓¬␈↓ β'"...␈αI␈αalways␈αworked␈αwith␈αprogramming␈αlanguages␈αbecause␈αit␈αseemed
␈↓ α←␈↓¬␈↓ β'to␈α⊃me␈α⊃that␈α⊂until␈α⊃you␈α⊃could␈α⊂understand␈α⊃those,␈α⊃you␈α⊃really␈α⊂couldn't
␈↓ α←␈↓¬␈↓ β'understand␈α∪computers.␈α∀Understanding␈α∪them␈α∪doesn't␈α∀really␈α∪mean
␈↓ α←␈↓¬␈↓ β'only␈α
being␈α
able␈α
to␈α
use␈α
them.␈α
A␈α
lot␈α
of␈α
people␈α
can␈α
use␈α
them␈α
without
␈↓ α←␈↓¬␈↓ β'understanding them. ..."
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ π∪Christopher Strachey[Str 74]
␈↓"λ␈↓ α←␈↓␈↓ ¬g␈↓↓3.1 Introduction␈↓
␈↓"β␈↓ α←␈↓In␈α⊃the␈α⊃previous␈α⊃chapters␈α∩of␈α⊃this␈α⊃text␈α⊃we␈α∩have␈α⊃talked␈α⊃about␈α⊃some␈α∩of␈α⊃the
␈↓ α←␈↓schemes␈α∂for␈α∂evaluation.␈α∂ We␈α∂have␈α∂done␈α∂so␈α∂rather␈α∂informally␈α∂for␈α∂LISP;␈α∞we
␈↓ α←␈↓have␈α∂been␈α∂more␈α∂precise␈α∂about␈α∂evaluation␈α∂of␈α∂simple␈α⊂arithmetic␈α∂expressions.
␈↓ α←␈↓Section 2.7␈αdiscussed␈αthat␈αin␈αsome␈αdetail.␈α We␈αshall␈αnow␈αlook␈αmore␈α
closely␈αat
␈↓ α←␈↓the␈α∞informal␈α∞process␈α∞which␈α∞we␈α∞have␈α∞been␈α∞using␈α∞in␈α∞the␈α∞evaluation␈α∞of␈α
LISP
␈↓ α←␈↓expressions. This is motivated by at least two desires.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αwant␈αto␈αrun␈αour␈αLISP␈αprograms␈αon␈αa␈αmachine.␈α To␈αdo␈αso␈αrequires
␈↓ α←␈↓the␈α∞implementation␈α∞of␈α∞a␈α∞translator␈α∞to␈α∞turn␈α∞LISP␈α∞programs␈α∞into␈α
instructions
␈↓ α←␈↓which␈αcan␈α
be␈αcarried␈αout␈α
by␈αa␈αconventional␈α
machine.␈α We␈αwill␈α
be␈αinterested
␈↓ α←␈↓in␈αthe␈αstructure␈αof␈αsuch␈αimplementations.␈α Any␈αimplementation␈αof␈αLISP␈αmust
␈↓ α←␈↓␈↓98 Evaluation␈↓
(3.1␈↓
␈↓"β␈↓ α←␈↓be␈αgrounded␈αon␈αa␈αprecise,␈αand␈αclear␈αunderstanding␈αof␈αwhat␈αLISP-evaluation
␈↓ α←␈↓entails.␈α∩Indeed,␈α∩a␈α∩deep␈α∩understanding␈α∩of␈α∩evaluation␈α∩is␈α∩a␈α∩prerequisite␈α⊃for
␈↓ α←␈↓implementation of ␈↓¬any␈↓ language.␈↓π 1␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Our␈α⊂second␈α⊂reason␈α⊂for␈α⊂pursuing␈α⊂evaluation␈α⊂involves␈α⊂the␈α⊂question␈α⊂of
␈↓ α←␈↓programming␈α∂language␈α∞specification.␈α∂ At␈α∞a␈α∂practical␈α∞level␈α∂we␈α∞want␈α∂a␈α∞clean,
␈↓ α←␈↓machine␈α∃independent,␈↓π 2␈↓␈α∃"self-evident"␈α∃language␈α∃specification,␈α∃so␈α⊗that␈α∃the
␈↓ α←␈↓agony␈α∞involved␈α∂in␈α∞implementing␈α∂the␈α∞design␈α∂can␈α∞be␈α∂minimized.␈α∞ At␈α∂a␈α∞more
␈↓ α←␈↓abstract␈α∞level,␈α∂we␈α∞should␈α∞try␈α∂to␈α∞understand␈α∞just␈α∂what␈α∞␈↓¬is␈↓␈α∞specified␈α∂when␈α∞we
␈↓ α←␈↓design␈α
a␈α
language.␈α
Are␈α
we␈α
specifying␈αa␈α
single␈α
machine,␈α
a␈α
class␈α
of␈αmachines,
␈↓ α←␈↓or␈αa␈αclass␈αof␈αmathematical␈αfunctions?␈αJust␈αwhat␈αis␈αa␈αprogramming␈αlanguage?
␈↓ α←␈↓The␈α∂syntactic␈α⊂specification␈α∂of␈α∂languages␈α⊂is␈α∂reasonably␈α∂well␈α⊂established,␈α∂but
␈↓ α←␈↓syntax␈αis␈αonly␈αthe␈αtip␈αof␈αthe␈αiceberg.␈αOur␈αstudy␈αof␈αLISP␈αwill␈αaddress␈αitself␈αto
␈↓ α←␈↓the deeper problems of semantics, or meaning, of languages.
␈↓"β␈↓ α←␈↓␈↓ β'Before␈α∂we␈α∞address␈α∂the␈α∞direct␈α∂question␈α∞of␈α∂LISP␈α∞evaluation,␈α∂we␈α∞should
␈↓ α←␈↓perhaps␈αwonder␈αaloud␈αabout␈αthe␈αefficacy␈αof␈αstudying␈αlanguages␈αin␈αthe␈αdetail
␈↓ α←␈↓which␈αwe␈αare␈αproposing.␈α As␈αcomputer␈αscientists␈αwe␈αshould␈αbe␈αcurious␈αabout
␈↓ α←␈↓the␈α⊂structure␈α⊃of␈α⊂programming␈α⊂languages␈α⊃because␈α⊂we␈α⊂must␈α⊃understand␈α⊂our
␈↓ α←␈↓tools␈α≠-- our␈α≠programming languages.␈α≠People␈α~who␈α≠simply␈α≠wish␈α≠to␈α~␈↓¬use␈↓
␈↓ α←␈↓computers␈α∂as␈α⊂tools␈α∂need␈α⊂not␈α∂care␈α⊂about␈α∂the␈α⊂structure␈α∂of␈α⊂languages.␈α∂Indeed
␈↓ α←␈↓they␈αusually␈αcouldn't␈αcare␈αless␈αabout␈α
the␈αinner␈αworkings␈αof␈αthe␈αlanguage;␈α
they
␈↓ α←␈↓only␈α
want␈αlanguages␈α
in␈αwhich␈α
they␈α
can␈αstate␈α
their␈αproblems␈α
in␈α
a␈αreasonably
␈↓ α←␈↓natural␈αmanner.␈α They␈αwant␈αtheir␈αprograms␈αto␈αrun␈αand␈αget␈αresults.␈αThey␈αare
␈↓ α←␈↓interested␈α
in␈αthe␈α
output␈αand␈α
seldom␈α
are␈αinterested␈α
in␈αthe␈α
detailed␈α
process␈αof
␈↓ α←␈↓computation.␈α
For␈α
a␈αsimple␈α
analogy,␈α
consider␈αthe␈α
field␈α
of␈α
mathematics.␈α The
␈↓ α←␈↓practicing␈αmathematician␈α
uses␈αhis␈α
tools␈α-- proofs --␈α
in␈αa␈α
similar␈αmanner␈αto␈α
the
␈↓ α←␈↓person␈α⊃interested␈α∩in␈α⊃computer␈α⊃applications.␈α∩ He␈α⊃seldom␈α⊃needs␈α∩to␈α⊃examine
␈↓ α←␈↓questions␈αlike␈α"what␈αis␈αa␈αproof?"␈αHe␈αdoes␈αnot␈αanalyze␈αhis␈αtools.␈α However␈αnot
␈↓ α←␈↓so␈α∞many␈α∞years␈α∞ago␈α∞such␈α∞questions␈α∞␈↓¬were␈↓␈α∞raised,␈α∞and␈α∞for␈α∞good␈α∂reason.␈α∞ Some
␈↓ α←␈↓common␈αforms␈αof␈αreasoning␈αwere␈αshown␈αto␈αlead␈αto␈αcontradictions␈αunless␈αcare
␈↓ α←␈↓was taken.
␈↓"β␈↓ α←␈↓␈↓ β'Our␈αposition␈αis␈αmore␈αlike␈αthat␈αof␈αthe␈αfoundations␈αof␈αmathematics;␈αthere
␈↓ α←␈↓the␈α↔tools␈α↔of␈α↔mathematics␈α↔␈↓¬are␈↓␈α↔studied␈α↔and␈α↔analyzed.␈α_ Mathematics␈α↔has
␈↓ α←␈↓flourished␈α↔because␈α⊗of␈α↔it.␈α⊗Though␈α↔our␈α⊗expectations␈α↔are␈α⊗not␈α↔quite␈α⊗that
␈↓ α←␈↓presumptuous,␈α
we␈α
␈↓¬do␈↓␈α
expect␈αthat␈α
programming␈α
language␈α
design␈α
cannot␈αhelp
␈↓ α←␈↓but be improved.
␈↓"β␈↓ α←␈↓␈↓ β'Our␈α
study␈α
of␈α
language␈αimplementation␈α
will␈α
proceed␈α
from␈α
the␈αabstract␈α
to
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 1␈↓The␈α⊗question␈α⊗of␈α⊗evaluation␈α↔cannot␈α⊗be␈α⊗sidestepped␈α⊗by␈α↔basing␈α⊗a
␈↓ α←␈↓language␈α
on␈α
a␈α
compiler.␈α
A␈α
compiler␈α
must␈α
produce␈α
code␈α
which␈αwhen␈α
executed,
␈↓ α←␈↓simulates the evaluation process.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 2␈↓By␈α∀"machine␈α∀independent"␈α∃we␈α∀mean␈α∀independent␈α∀of␈α∃any␈α∀specific
␈↓ α←␈↓hardware␈αimplementation.␈αA␈αprogramming␈αlanguage,␈αalmost␈αby␈αdefinition,␈αis
␈↓ α←␈↓a␈α⊂machine␈α∂specification.␈α⊂What␈α⊂we␈α∂would␈α⊂like␈α⊂is␈α∂a␈α⊂"sufficiently␈α⊂high␈α∂level"
␈↓ α←␈↓machine.
␈↓ α←␈↓␈↓3.1␈↓ λmIntroduction 99␈↓
␈↓"β␈↓ α←␈↓the␈α
concrete.␈α∞Each␈α
level␈α
will␈α∞intimately␈α
involve␈α
the␈α∞study␈α
of␈α∞data␈α
structures.
␈↓ α←␈↓The␈αnext␈α
two␈αchapters␈α
will␈αbe␈α
the␈αmost␈α
abstract,␈αbuilding␈α
a␈αprecise␈α
high-level
␈↓ α←␈↓description␈αof␈αan␈αevaluation␈αscheme␈αfor␈α
LISP.␈αIn␈αfact,␈αthe␈αdiscussion␈αis␈α
much
␈↓ α←␈↓more␈αgeneral␈αthan␈αthat␈αof␈αLISP;␈αthe␈αtext␈αaddresses␈αitself␈αto␈αproblem␈αareas␈αin
␈↓ α←␈↓the␈α
design␈α
of␈αany␈α
reasonably␈α
sophisticated␈αlanguage.␈α
In␈α
subsequent␈αchapters
␈↓ α←␈↓we␈α∪probe␈α∪beneath␈α∪the␈α∪surface␈α∩of␈α∪this␈α∪high-level␈α∪description␈α∪and␈α∩discuss
␈↓ α←␈↓common␈α∩ways␈α∪of␈α∩implementing␈α∩the␈α∪necessary␈α∩data␈α∩structures␈α∪and␈α∩control
␈↓ α←␈↓structures.␈α In␈αthe␈αprocess␈αwe␈αwill␈αnot␈αonly␈αunderstand␈αLISP␈αbut␈αwill␈αdevelop
␈↓ α←␈↓a firm understanding of virtually any other language.
␈↓"β␈↓ α←␈↓␈↓ β'But␈α
how␈αcan␈α
we␈α
begin␈αto␈α
understand␈αLISP␈α
evaluation?␈α
In␈αSection 2.7
␈↓ α←␈↓we␈αmade␈α
a␈αbeginning,␈α
giving␈αan␈αalgorithm␈α
for␈αa␈α
subset␈αof␈α
the␈αcomputations
␈↓ α←␈↓expressible␈α_in␈α_LISP.␈α↔ This␈α_subset␈α_covered␈α↔evaluation␈α_of␈α_some␈α↔simple
␈↓ α←␈↓arithmetic␈αexpressions.␈α From␈αour␈αearliest␈α
grade␈αschool␈αdays␈αwe␈αhave␈α
had␈αto
␈↓ α←␈↓evaluate␈αsimple␈αarithmetic␈αexpressions.␈αLater,␈αin␈αalgebra␈αwe␈αmanaged␈αto␈αcope
␈↓ α←␈↓with␈α⊃expressions␈α⊃involving␈α⊃function␈α⊃application.␈α⊃Most␈α⊃of␈α⊃us␈α∩survived␈α⊃the
␈↓ α←␈↓experience.␈αWe␈αshould␈αnow␈αtry␈αto␈αunderstand␈αthe␈αprocesses␈αwe␈αused␈αin␈αthese
␈↓ α←␈↓simple␈αarithmetic␈αcases,␈αdoing␈αour␈αexamination␈αat␈αthe␈αmost␈αmechanical␈αlevel.
␈↓ α←␈↓The␈α∩basic␈α∩intent␈α∪of␈α∩the␈α∩algorithm␈α∩is␈α∪fixed:␈α∩evaluate␈α∩the␈α∪expression;␈α∩but
␈↓ α←␈↓within␈α⊃that␈α∩general␈α⊃constraint␈α∩we␈α⊃often␈α∩have␈α⊃several␈α∩distinct␈α⊃alternatives.
␈↓ α←␈↓Those␈α
places␈αat␈α
which␈αwe␈α
have␈α
choices␈αshould␈α
be␈αremembered.␈α
We␈αwill␈α
make
␈↓ α←␈↓reasonable␈α⊗choices␈α⊗so␈α⊗that␈α⊗the␈α⊗process␈α⊗becomes␈α⊗deterministic␈α↔and␈α⊗then
␈↓ α←␈↓proceed.␈α∩Later,␈α⊃we␈α∩should␈α⊃reflect␈α∩on␈α⊃what␈α∩effect␈α⊃our␈α∩choices␈α⊃had␈α∩on␈α⊃the
␈↓ α←␈↓resulting␈αscheme.␈α For␈αexample,␈αrecall␈αthe␈αdiscussion␈αof␈αthe␈α
representation␈αof
␈↓ α←␈↓symbol␈α⊂tables␈α⊂on␈α⊂page 89.␈α⊂We␈α⊂had␈α⊂several␈α⊂options,␈α⊂but␈α⊂picked␈α⊂one␈α⊂which
␈↓ α←␈↓seemed␈αto␈α
satisfy␈αour␈αintuitions␈α
and␈αwas␈αreasonably␈α
efficient.␈αBut␈α
we␈αshould
␈↓ α←␈↓subject␈α
that␈α
decision␈α∞to␈α
close␈α
scrutiny:␈α
does␈α∞it␈α
really␈α
fulfill␈α∞our␈α
expectations?
␈↓ α←␈↓In␈α⊂absence␈α⊂of␈α⊂absolute␈α⊂standards,␈α∂these␈α⊂questions␈α⊂are␈α⊂usually␈α⊂answered␈α∂by
␈↓ α←␈↓examining the behavior of the algorithm.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∞first␈α∞thing␈α∂to␈α∞note␈α∞in␈α∞reflecting␈α∂on␈α∞simple␈α∞arithmetic␈α∂examples␈α∞is
␈↓ α←␈↓that␈α∞␈↓¬nothing␈↓␈α
is␈α∞really␈α
said␈α∞about␈α∞the␈α
process␈α∞of␈α
evaluation.␈α∞ When␈α∞asked␈α
to
␈↓ α←␈↓evaluate␈α↔␈↓α(2*3) + (5*6)␈↓␈α↔we␈α↔never␈α⊗specified␈α↔which␈α↔summand␈α↔was␈α↔to␈α⊗be
␈↓ α←␈↓evaluated␈α∂first.␈α∞ Indeed␈α∂it␈α∂didn't␈α∞matter␈α∂here.␈α∞␈↓α6 + (5*6)␈↓␈α∂or␈α∂␈↓α(2*3) + 30␈↓␈α∞both
␈↓ α←␈↓yield␈α␈↓α36␈↓.␈α Does␈αit␈α␈↓¬ever␈↓␈αmatter?␈αSums␈αand␈αproducts␈αare␈αexamples␈αof␈α
arithmetic
␈↓ α←␈↓operations;␈α∪can␈α∪we␈α∩always␈α∪leave␈α∪the␈α∩order␈α∪of␈α∪evaluation␈α∪unspecified␈α∩for
␈↓ α←␈↓arithmetic␈α~operations?␈α~What␈α~about␈α~evaluation␈α~of␈α~arbitrary␈α~functional
␈↓ α←␈↓expressions?␈α∃ If␈α∃the␈α∃order␈α⊗doesn't␈α∃matter,␈α∃then␈α∃the␈α∃specification␈α⊗of␈α∃the
␈↓ α←␈↓evaluation␈α⊂process␈α⊃becomes␈α⊂much␈α⊂simpler.␈α⊃If␈α⊂it␈α⊂␈↓¬does␈↓␈α⊃matter␈α⊂then␈α⊃we␈α⊂must
␈↓ α←␈↓know why and where.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∂have␈α∞seen␈α∂that␈α∞the␈α∂order␈α∞of␈α∂evaluation␈α∞␈↓¬can␈↓␈α∂make␈α∞a␈α∂difference␈α∞in
␈↓ α←␈↓LISP.␈α On␈αpage 15␈αwe␈αsaw␈αthat␈α␈↓↓CBV␈↓,␈αLISP's␈αcomputational␈αinterpretation␈αof
␈↓ α←␈↓function␈α
application,␈α∞requires␈α
some␈α
care.␈α∞ On␈α
page 21␈α
we␈α∞saw␈α
that␈α∞order␈α
of
␈↓ α←␈↓evaluation␈α⊂in␈α∂conditional␈α⊂expressions␈α∂can␈α⊂make␈α∂a␈α⊂difference.␈α∂Since␈α⊂we␈α∂are
␈↓ α←␈↓using␈α␈↓↓CBV␈↓␈αwe␈αmust␈αmake␈α␈↓¬some␈↓␈αdecision␈αregarding␈αthe␈αorder␈αof␈αevaluation␈α
of
␈↓ α←␈↓the␈α∞arguments␈α∞to␈α
a␈α∞function␈α∞call,␈α
say␈α∞␈↓αf[t␈↓β1␈↓α;t␈↓β2␈↓α; ...;t␈↓βn␈↓].␈α∞ We␈α
will␈α∞assume␈α∞that␈α
we
␈↓ α←␈↓␈↓100 Evaluation␈↓
(3.1␈↓
␈↓"β␈↓ α←␈↓will␈α∞evaluate␈α∞the␈α∞arguments␈α∞from␈α∞left␈α∞to␈α∞right.␈α∞ This␈α∞second␈α∞decision␈α∞about
␈↓ α←␈↓the order of evaluation can also effect the computation.
␈↓"β␈↓ α←␈↓␈↓ β'Consider the example due to J. Morris:
␈↓"∀␈↓ α←␈↓␈↓ ∧X␈↓αf[x;y] <= [x = 0 → 0; ␈↓
t␈↓α → f[x-1;f[y-2;x]]]␈↓
␈↓"∀␈↓ α←␈↓Evaluation␈α∪of␈α∪␈↓αf[2;1]␈↓␈α∀will␈α∪terminate␈α∪if␈α∀we␈α∪always␈α∪evaluate␈α∀the␈α∪outermost
␈↓ α←␈↓occurrence of ␈↓αf␈↓. Thus:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ∧D␈↓αf[2;1] = f[1;f[-1;2]] = f[0;f[f[-1;2]-2;1]] = 0␈↓
␈↓"∀␈↓ α←␈↓However␈α⊂if␈α∂we␈α⊂evaluate␈α∂the␈α⊂innermost␈α∂occurrences␈↓π 3␈↓␈α⊂first,␈α⊂the␈α∂computation
␈↓ α←␈↓will not terminate:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ βp␈↓αf[2;1] = f[1;f[-1;2]] = f[1;f[-2;f[0;-1]]] = f[1;f[-2;0]] = ... ␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'The␈αchoice␈αof␈αevaluation␈αschemes␈αhas␈αfar␈αreaching␈α
consequences.␈α The
␈↓ α←␈↓evaluation␈αscheme,␈α
␈↓↓CBV␈↓,␈αwhich␈αwe␈α
chose␈αis␈αcalled␈α
␈↓↓call-by-value␈↓.␈α It␈α
is␈αcalled
␈↓ α←␈↓applicative␈α∞order␈α
evaluation␈α∞or␈α
␈↓↓inside-out␈↓␈α∞style␈α
of␈α∞evaluation,␈α∞meaning␈α
that
␈↓ α←␈↓we␈α⊗evaluate␈α⊗the␈α⊗subexpressions␈α∃before␈α⊗evaluating␈α⊗the␈α⊗main␈α∃expression.
␈↓ α←␈↓Alternative␈αproposals␈αexist;␈αcall-by-name␈αevaluation,␈αalso␈αcalled␈αnormal␈αorder
␈↓ α←␈↓evaluation,␈αis␈αanother␈αcommon␈αscheme.␈αWe␈αintroduced␈αthis␈αoutside-in␈α
scheme
␈↓ α←␈↓on␈α⊂page 16␈α∂as␈α⊂␈↓↓CBN␈↓.␈α∂ From␈α⊂an␈α∂implementation␈α⊂perspective,␈α⊂call-by-value␈α∂is
␈↓ α←␈↓favored;␈αthese␈αissues␈αwill␈αbe␈αdiscussed␈αsoon.␈α However␈αthose␈αadvantages␈αmust
␈↓ α←␈↓be␈α≥weighed␈α≥against␈α≥the␈α≡knowledge␈α≥that␈α≥call-by-value␈α≥may␈α≡lead␈α≥to
␈↓ α←␈↓non-terminating computations when call-by-name would terminate.␈↓π 4␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Informally,␈α∪call-by-value␈α∪says:␈α∪evaluate␈α∩the␈α∪arguments␈α∪to␈α∪a␈α∩function
␈↓ α←␈↓before␈α∞you␈α∂apply␈α∞the␈α∞function␈α∂definition␈α∞to␈α∞the␈α∂arguments.␈α∞ Let's␈α∞look␈α∂at␈α∞a
␈↓ α←␈↓simple␈α∩arithmetic␈α∩example.␈α∩ Let␈α∩␈↓αf[x;y]␈↓␈α∩be␈α∩␈↓αx␈↓π2␈↓α + y␈↓␈α∩and␈α∩consider␈α∩␈↓αf[3+4;2*2]␈↓.
␈↓ α←␈↓Then␈α⊂call-by-value␈α⊂says␈α∂evaluate␈α⊂the␈α⊂arguments,␈α∂getting␈α⊂␈↓α7␈↓␈α⊂and␈α⊂␈↓α4␈↓;␈α∂associate
␈↓ α←␈↓those␈αvalues␈αwith␈αthe␈αformal␈αparameters␈αof␈α␈↓αf␈↓␈α(i.e.␈α␈↓α7␈↓␈αwith␈α␈↓αx␈↓␈αand␈α␈↓α4␈↓␈αwith␈α␈↓αy␈↓)␈αand
␈↓ α←␈↓then␈αevaluate␈αthe␈αbody␈αof␈α␈↓αf␈↓␈αresulting␈αin␈α ␈↓α7␈↓π2␈↓α + 4 = 53␈↓.␈αThis␈αis␈αthe␈αscheme␈αwe
␈↓ α←␈↓captured in Section 2.7.
␈↓"β␈↓ α←␈↓␈↓ β'Call-by-name␈α↔says␈α↔pass␈α↔the␈α↔␈↓¬unevaluated␈↓␈α↔actual␈α↔parameters␈α_to␈α↔the
␈↓ α←␈↓function,␈α⊗giving␈α⊗␈↓α(3+4)␈↓π2␈↓α + 2*2␈↓.␈α⊗ This␈α⊗expression␈α⊗will␈α⊗simplify␈α⊗to␈α⊗␈↓α53␈↓.␈α∃In
␈↓ α←␈↓general,␈α evaluation␈α∨can␈α be␈α described␈α∨as␈α "substitution␈α followed␈α∨by
␈↓ α←␈↓simplification";␈α∩the␈α∪different␈α∩evaluation␈α∪schemes␈α∩involve␈α∪different␈α∩choices
␈↓ α←␈↓about␈αthe␈αorder␈αin␈αwhich␈αthose␈αoperations␈αare␈αperformed.␈α We␈αwill␈αsay␈αmore
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 3␈↓The␈α⊂notions␈α⊃of␈α⊂"innermost"␈α⊃and␈α⊂"outermost"␈α⊃evaluation␈α⊂need␈α⊃to␈α⊂be
␈↓ α←␈↓slightly␈α~embellished␈α~for␈α~multiple-argument␈α~applications.␈α~If␈α≠the␈α~chosen
␈↓ α←␈↓application␈α∞has␈α∞several␈α∞arguments,␈α∞then␈α∞we␈α∞must␈α∞specify␈α∞an␈α∞order␈α∞for␈α∞their
␈↓ α←␈↓evaluation.␈α∂ Thus␈α∂terms␈α∞like␈α∂"leftmost-outermost"␈α∂and␈α∞"rightmost-innermost"
␈↓ α←␈↓occur.␈α∞For␈α∞example,␈α∞the␈α∞LISP␈α
scheme␈α∞is␈α∞an␈α∞instance␈α∞of␈α
"leftmost-innermost"
␈↓ α←␈↓evaluation.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 4␈↓There␈α↔are␈α⊗also␈α↔examples␈α⊗where␈α↔call-by-value␈α⊗will␈α↔terminate␈α⊗but
␈↓ α←␈↓call-by-name will not. See page 227.
␈↓ α←␈↓␈↓3.1␈↓ λ↑Introduction 101␈↓
␈↓"β␈↓ α←␈↓about␈α↔call-by-name␈α⊗and␈α↔other␈α⊗styles␈α↔of␈α⊗evaluation␈α↔in␈α↔Section 3.13␈α⊗and
␈↓ α←␈↓Section 4.9. Most of this chapter will be restricted to call-by-value.
␈↓"β␈↓ α←␈↓␈↓ β'If␈αyou␈αlook␈αat␈α
the␈αstructure␈αof␈α␈↓αvalue␈↓λ''␈↓␈α
and␈α␈↓αapply␈↓λ'␈↓␈αbeginning␈αon␈α
page 86
␈↓ α←␈↓you␈α
will␈αsee␈α
that␈αthey␈α
encode␈αa␈α
call-by-value␈αstrategy␈α
and␈αhave␈α
the␈αfollowng
␈↓ α←␈↓interpretation:
␈↓"λ␈↓ α←␈↓␈↓↓1.␈↓ If␈α∂the␈α∂expression␈α∂is␈α∂a␈α∂constant␈α∞then␈α∂the␈α∂value␈α∂of␈α∂the␈α∂expression␈α∂is␈α∞that
␈↓ α←␈↓␈↓ β∂constant. (The value of ␈↓α3␈↓ is ␈↓α3␈↓).␈↓π 5␈↓
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ If␈αthe␈αexpression␈αis␈αa␈αvariable␈αthen␈αsee␈αwhat␈αthe␈αcurrent␈αvalue␈αassociated
␈↓ α←␈↓␈↓ β∂with␈α⊗that␈α⊗variable␈α↔is.␈α⊗Within␈α⊗the␈α↔evaluation␈α⊗of,␈α⊗say,␈α↔␈↓αf[3;4]␈↓␈α⊗where
␈↓ α←␈↓␈↓ β∂␈↓αf[x;y] <= x␈↓π2␈↓α + y ␈↓the current value of the variable ␈↓αx␈↓ is ␈↓α3␈↓.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ The␈α∩only␈α∩other␈α∩kind␈α∩of␈α∩arithmetic␈α∩expression␈α∩that␈α∩we␈α∩can␈α∩have␈α∩is␈α⊃a
␈↓ α←␈↓␈↓ β∂function␈α
name␈α
followed␈α
by␈α
arguments,␈α
for␈α
example␈α
␈↓αf[3;4]␈↓.␈α
In␈α
this␈αcase␈α
we
␈↓ α←␈↓␈↓ β∂first␈α∩evaluate␈α∩the␈α∩arguments␈α∩␈↓π 6␈↓␈α∩and␈α∩then␈α∩apply␈α∩the␈α∩definition␈α∩of␈α∩the
␈↓ α←␈↓␈↓ β∂function␈α∩to␈α∩those␈α⊃evaluated␈α∩arguments.␈α∩ When␈α⊃we␈α∩apply␈α∩the␈α⊃function
␈↓ α←␈↓␈↓ β∂definition␈αto␈αthe␈αevaluated␈αarguments␈αwe␈αassociate␈αthe␈αformal␈αparameters
␈↓ α←␈↓␈↓ β∂of␈α
the␈αdefinition␈α
with␈α
the␈αvalues␈α
of␈α
the␈αactual␈α
parameters.␈α
This␈αprocess
␈↓ α←␈↓␈↓ β∂of␈α∞associating␈α∞parameters␈α∞is␈α∞called␈α∞␈↓↓binding␈↓␈α∞and␈α∞simulates␈α∞some␈α∞form␈α
of
␈↓ α←␈↓␈↓ β∂substitution.␈α
We␈α
then␈α
evaluate␈α
the␈α
body␈α
of␈α
the␈α
function␈α
using␈α
this␈α
new
␈↓ α←␈↓␈↓ β∂environment.␈α∂ Notice␈α∞that␈α∂we␈α∂do␈α∞␈↓¬not␈↓␈α∂explicitly␈α∞substitute␈α∂the␈α∂values␈α∞for
␈↓ α←␈↓␈↓ β∂the␈α∞variables␈α∞which␈α∞appear␈α∞in␈α∞an␈α∞expression.␈α∞We␈α∞␈↓¬simulate␈↓␈α∞substitutions
␈↓ α←␈↓␈↓ β∂by table lookup.
␈↓"∀␈↓ α←␈↓␈↓ β'We␈αwant␈αto␈αapply␈αthis␈αtreatment␈αof␈αevaluation␈αto␈αLISP␈αexpressions.␈α If
␈↓ α←␈↓the␈α∞LISP␈α∞expression␈α∞is␈α∞a␈α∞constant,␈α
then␈α∞the␈α∞value␈α∞of␈α∞the␈α∞expression␈α∞is␈α
that
␈↓ α←␈↓constant.␈α The␈α
constants␈αof␈αLISP␈α
are␈αthe␈αS-exprs.␈α
Thus␈αthe␈αvalue␈α
of␈α␈↓α(A . B)␈↓
␈↓ α←␈↓is␈α
␈↓α(A . B)␈↓,␈α
just␈αlike␈α
the␈α
value␈αof␈α
␈↓α3␈↓␈α
is␈α␈↓α3␈↓.␈α
Variables␈α
and␈αfunctional␈α
applications
␈↓ α←␈↓appear␈αin␈αLISP␈αand␈αare␈αhandled␈αsimilarly␈αto␈α␈↓↓2␈↓␈αand␈α␈↓↓3␈↓␈αabove.␈α The␈α
additional
␈↓ α←␈↓artifact␈αof␈αLISP␈αis␈αthe␈αconditional␈αexpression.␈αBut␈αits␈αevaluation␈αcan␈αalso␈αbe
␈↓ α←␈↓precisely specified. We did so on page 19.
␈↓"β␈↓ α←␈↓␈↓ β'In␈α∪more␈α∩specific␈α∪detail,␈α∩here␈α∪is␈α∩some␈α∪of␈α∩the␈α∪structure␈α∩of␈α∪the␈α∩LISP
␈↓ α←␈↓evaluation mechanism:
␈↓"λ␈↓ α←␈↓␈↓↓1.␈↓ If␈α⊃the␈α⊂expression␈α⊃to␈α⊃be␈α⊂evaluated␈α⊃is␈α⊂a␈α⊃constant␈α⊃then␈α⊂the␈α⊃value␈α⊃is␈α⊂that
␈↓ α←␈↓␈↓ β∂constant.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ If the expression is a variable find its value in the current environment.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ If␈α∀the␈α∪expression␈α∀is␈α∪a␈α∀conditional␈α∪expression␈α∀then␈α∪it␈α∀is␈α∪of␈α∀the␈α∪form
␈↓ α←␈↓␈↓ β∂␈↓α[␈↓↓p␈↓β1␈↓α → ␈↓↓e␈↓β1␈↓α; ␈↓↓p␈↓β2␈↓α → ␈↓↓e␈↓β2␈↓α; ... ;␈↓↓p␈↓βn␈↓α → ␈↓↓e␈↓βn␈↓α]␈↓.␈α Evaluate␈α
it␈αusing␈α
the␈αsemantics␈αdefined␈α
on
␈↓ α←␈↓␈↓ β∂page 19.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 5␈↓We␈α∪are␈α∀ignoring␈α∪the␈α∀distinction␈α∪between␈α∪the␈α∀␈↓¬numeral␈↓␈α∪␈↓α3␈↓␈α∀and␈α∪the
␈↓ α←␈↓␈↓¬number␈↓α 3.␈↓
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 6␈↓Here we are using the evaluation process recursively.
␈↓ α←␈↓␈↓102 Evaluation␈↓
(3.1␈↓
␈↓" ␈↓ α←␈↓␈↓↓4.␈↓ If the expression is of the form: ␈↓αf␈↓[␈↓αt␈↓β1␈↓α;t␈↓β2␈↓α; ... ;t␈↓βn␈↓α]␈↓ then:
␈↓"β␈↓ α←␈↓␈↓ β3␈↓↓a.␈↓␈↓ βcEvaluate the arguments ␈↓αt␈↓β1␈↓α, t␈↓β2␈↓α, ... , t␈↓βn␈↓ from left to right.
␈↓" ␈↓ α←␈↓␈↓ β3␈↓↓b.␈↓␈↓ βcFind the definition of the function, ␈↓αf␈↓.
␈↓" ␈↓ α←␈↓␈↓ β3␈↓↓c.␈↓␈↓ βcAssociate␈α∞the␈α∞evaluated␈α∞arguments␈α∞with␈α∞the␈α∞formal␈α∞parameters␈α
in
␈↓ α←␈↓␈↓ βothe function definition.
␈↓" ␈↓ α←␈↓␈↓ β3␈↓↓d.␈↓␈↓ βcEvaluate␈α
the␈α∞body␈α
of␈α∞the␈α
function,␈α∞while␈α
remembering␈α∞the␈α
values
␈↓ α←␈↓␈↓ βoof the variables.
␈↓"∀␈↓ α←␈↓␈↓ β'We␈αsaw␈αin␈α(Section 2.7)␈αthat␈αa␈αsimple␈αkind␈αof␈αarithmetic␈αevaluation␈αcan
␈↓ α←␈↓be␈αtranscribed␈αinto␈αa␈αrecursive␈αLISP␈αalgorithm.␈α That␈αalgorithm␈αoperates␈αon
␈↓ α←␈↓a␈αrepresentation␈αof␈αthe␈αexpression␈αand␈αproduces␈αthe␈αvalue.␈αMost␈αof␈αour␈αwork
␈↓ α←␈↓in␈α≥that␈α≥example␈α≡was␈α≥done␈α≥without␈α≡giving␈α≥explicit␈α≥details␈α≡of␈α≥the
␈↓ α←␈↓representation.␈α_ We␈α↔had␈α_previously␈α↔given␈α_a␈α↔detailed␈α_representation␈α↔in
␈↓ α←␈↓Section 2.3.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α
have␈αdemonstrated␈α
an␈αinformal,␈α
but␈αreasonably␈α
precise,␈αevaluation
␈↓ α←␈↓scheme␈α∞for␈α∞LISP;␈α∞our␈α∞discussion␈α∞is␈α∞ready␈α∞for␈α∞more␈α∞formal␈α∂development.␈α∞ It
␈↓ α←␈↓should␈α∃be␈α∀clear␈α∃that␈α∀we␈α∃could␈α∀write␈α∃a␈α∀LISP␈α∃function␈α∃representing␈α∀the
␈↓ α←␈↓evaluation␈α∩process␈α∩provided␈α∩that␈α∩we␈α∩can␈α∩find␈α∩a␈α∩representation␈α∩for␈α⊃LISP
␈↓ α←␈↓expressions␈α∀as␈α∀S-expressions.␈α∀This␈α∃mapping,␈α∀␈↓
R␈↓,␈α∀of␈α∀LISP␈α∃expressions␈α∀to
␈↓ α←␈↓S-exprs␈α∞is␈α∞our␈α∞first␈α∞order␈α∞of␈α
business.␈α∞We␈α∞will␈α∞accomplish␈α∞this␈α∞mapping␈α
by
␈↓ α←␈↓using an extension of the scheme introduced in Section 2.3.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αrationale␈αfor␈αmapping␈α
LISP␈αexpressions␈αonto␈αS-exprs␈α
and␈αwriting
␈↓ α←␈↓a␈α⊂LISP␈α∂function␈α⊂to␈α⊂act␈α∂as␈α⊂an␈α∂evaluator␈α⊂may␈α⊂seem␈α∂overly␈α⊂opaque,␈α⊂but␈α∂the
␈↓ α←␈↓mapping␈α∩is␈α∩no␈α∪more␈α∩obscure␈α∩than␈α∩that␈α∪in␈α∩the␈α∩polynomial␈α∪evaluation␈α∩or
␈↓ α←␈↓differentiation␈α∀examples.␈α∀ It␈α∀is␈α∀just␈α∀another␈α∀instance␈α∀of␈α∀the␈α∀diagram␈α∪of
␈↓ α←␈↓page 56,␈α⊃only␈α⊃now␈α⊃we␈α⊂are␈α⊃applying␈α⊃the␈α⊃process␈α⊂to␈α⊃LISP␈α⊃itself.␈α⊃ Once␈α⊂the
␈↓ α←␈↓representation␈α
is␈α
given␈α
we␈α
will␈α
produce␈α
a␈α
LISP␈α
algorithm␈α
which␈αdescribes␈α
the
␈↓ α←␈↓evaluation␈α∞process␈α
used␈α∞in␈α∞LISP.␈α
The␈α∞effect␈α
is␈α∞to␈α∞force␈α
us␈α∞to␈α∞make␈α
precise
␈↓ α←␈↓exactly␈α∂what␈α∂is␈α∂meant␈α∂by␈α∂LISP␈α∂evaluation.␈α∂This␈α∂precision␈α∂will␈α∂have␈α∞many
␈↓ α←␈↓important␈αramifications.␈αThe␈αfirst␈αdividend␈α
is␈αan␈αabstract,␈αcompact,␈αand␈α
high
␈↓ α←␈↓level description of a LISP machine.
␈↓ α←␈↓␈↓3.1␈↓ λ↑Introduction 103␈↓
␈↓"β␈↓ α←␈↓␈↓ β'In terms of the diagrams on page 56 we have:
␈↓"∀␈↓ α←␈↓LISP evaluation => LISP evaluation algorithm
␈↓"β␈↓ α←␈↓ Call-by-value ␈↓αeval␈↓␈↓ ¬S|
␈↓"β␈↓ α←␈↓␈↓ ¬S| LISP evaluation
␈↓"β␈↓ α←␈↓␈↓ ¬S|=====> ␈↓
R␈↓∞(␈↓αA␈↓∞)␈↓ interpret this output
␈↓"β␈↓ α←␈↓␈↓ ¬S| as answer ␈↓αA␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬S|
␈↓"β␈↓ α←␈↓expression => Representation
␈↓"β␈↓ α←␈↓ ␈↓αcar[(A . B)] ␈↓
R␈↓∞(␈↓αcar[(A . B)]␈↓∞)␈↓α
␈↓"∀␈↓ α←␈↓The␈αdiagram␈αis␈α␈↓¬almost␈↓␈αcircular.␈αWe␈αevaluate␈αan␈αevaluation␈αalgorithm␈αnamed
␈↓ α←␈↓␈↓αeval␈↓.␈α∞We␈α∞break␈α∞the␈α∞circle␈α∂by␈α∞supplying␈α∞a␈α∞lower-level␈α∞implementation␈α∂of␈α∞the
␈↓ α←␈↓original␈α⊃evaluator.␈α⊃That␈α⊂will␈α⊃be␈α⊃the␈α⊂subject␈α⊃of␈α⊃Chapter 5␈α⊃and␈α⊂Chapter 6.
␈↓ α←␈↓With that, our diagram reduces to:
␈↓"∀␈↓ α←␈↓LISP expression => =>␈↓ ¬kRepresentation
␈↓"β␈↓ α←␈↓ ␈↓αcar[(A . B)]␈↓ ¬k ␈↓
R␈↓∞(␈↓αcar[(A . B)]␈↓∞)␈↓
␈↓"β␈↓ α←␈↓␈↓ βc␈↓ ¬k␈↓ ε3↓↓
␈↓"β␈↓ α←␈↓␈↓ βc␈↓ ¬kLISP evaluation
␈↓"β␈↓ α←␈↓␈↓ βc␈↓ ¬k␈↓ ε3␈↓αeval␈↓
␈↓"β␈↓ α←␈↓␈↓ βc␈↓ ¬k␈↓ ε3↓↓
␈↓"β␈↓ α←␈↓␈↓ βc␈↓ ¬kRepresentation of answer
␈↓"β␈↓ α←␈↓␈↓ βc␈↓ ¬k ␈↓
R␈↓∞(␈↓αA␈↓∞)␈↓
␈↓"∀␈↓ α←␈↓This␈α
picture␈α
reflects␈α
two␈α
points:␈α
we␈α
should␈α
pick␈α
a␈α
representation␈α
such␈α
that␈α
the
␈↓ α←␈↓reinterpretation␈α
of␈α
the␈α
answer␈α
is␈α
easy.␈α
We␈α
should␈α
also␈α
pick␈α
a␈α
representation
␈↓ α←␈↓such␈αthat␈αthe␈αrepresentation␈αof␈αthe␈αexpression␈αis␈αeasy.␈αIf␈αthose␈αtwo␈αconditions
␈↓ α←␈↓are␈αsatisfied,␈αthen␈αwe␈αmight␈αas␈αwell␈αwrite␈αour␈αprograms␈αin␈αthe␈αrepresentation
␈↓ α←␈↓and␈α∞do␈α∞the␈α∞input␈α∞and␈α∞output␈α∞transformations␈α∞ourselves.␈α∞With␈α∞this␈α∞in␈α
mind
␈↓ α←␈↓we can simplify further to:
␈↓"∀␈↓ α←␈↓␈↓ β=␈↓
R␈↓∞(␈↓αcar[(A . B)]␈↓∞)␈↓ ==a LISP evaluation algorithm=> ␈↓
R␈↓∞(␈↓αA␈↓∞)␈↓
␈↓"∀␈↓ α←␈↓This␈α∀last␈α∪diagram␈α∀reflects␈α∪the␈α∀typical␈α∪LISP␈α∀programming␈α∀language.␈α∪We
␈↓ α←␈↓program using the data structure representation.
␈↓"β␈↓ α←␈↓␈↓ β'We've␈α≤already␈α≠seen␈α≤the␈α≠evaluation␈α≤of␈α≠representations␈α≤of␈α≠LISP
␈↓ α←␈↓expressions.␈α↔ The␈α↔␈↓↓great␈α↔progenitor␈α⊗of␈α↔all␈α↔functions␈↓␈α↔is␈α↔an␈α⊗evaluation
␈↓ α←␈↓algorithm␈α
for␈α
the␈α
LISP␈α
primitive␈α
functions␈α
and␈α
predicates,␈α
␈↓αcar,␈α
cdr,␈αcons,␈α
atom␈↓
␈↓ α←␈↓and␈α⊂␈↓αeq␈↓␈α⊃when␈α⊂restricted␈α⊃to␈α⊂functional␈α⊃composition␈α⊂and␈α⊃constant␈α⊂arguments.
␈↓ α←␈↓The␈α∂representation␈α∂used␈α⊂there␈α∂was␈α∂a␈α⊂list␈α∂representation,␈α∂and␈α⊂exemplifies␈α∂a
␈↓ α←␈↓notation which we will develop further.
␈↓"β␈↓ α←␈↓␈↓ β'In␈αthe␈αnext␈αsection␈α
we␈αwill␈αgive␈αa␈α
specific␈αmapping␈αof␈αLISP␈α
expressions
␈↓ α←␈↓onto␈α∂lists␈α∂and␈α∂S-exprs.␈α∞ But␈α∂remember␈α∂that␈α∂we␈α∞should␈α∂attempt␈α∂to␈α∂keep␈α∞the
␈↓ α←␈↓knowledge␈α
of␈α
the␈αrepresentation␈α
out␈α
of␈α
the␈αstructure␈α
of␈α
the␈α
algorithm.␈α Let's
␈↓ α←␈↓stop␈αfor␈αa␈αdescription␈αof␈αthe␈αrepresentation␈αand␈αsome␈αexamples␈αof␈αtranslating
␈↓ α←␈↓LISP functions into that representation.
␈↓ α←␈↓␈↓104 Evaluation␈↓
(3.2␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧,␈↓↓3.2 S-expr Translation of LISP Expressions␈↓
␈↓"β␈↓ α←␈↓We␈α
will␈α∞go␈α
through␈α∞the␈α
list␈α∞of␈α
LISP␈α∞constructs,␈α
describing␈α∞the␈α
effect␈α∞of␈α
the
␈↓ α←␈↓representational␈α∂map,␈α⊂␈↓
R␈↓,␈α∂and␈α⊂give␈α∂a␈α⊂few␈α∂examples␈α⊂applying␈α∂␈↓
R␈↓.␈α⊂ The␈α∂first
␈↓ α←␈↓class␈α⊃of␈α⊃LISP␈α⊃objects␈α⊃we␈α∩represent␈α⊃are␈α⊃the␈α⊃numerical␈α⊃constants.␈α∩ We␈α⊃will
␈↓ α←␈↓represent numerals just as numerals, e.g.:
␈↓"⊂␈↓ α←␈↓␈↓ ∧␈␈↓
R␈↓∞(␈↓ <numeral> ␈↓∞)␈↓α = ␈↓<numeral>
␈↓"⊃␈↓ α←␈↓␈↓ ∧␈␈↓
R␈↓∞(␈↓α 2 ␈↓∞)␈↓α = 2
␈↓"∀␈↓ α←␈↓␈↓ β'Other␈α
simple␈α
components␈α
of␈αLISP␈α
syntax␈α
include␈α
the␈α
identifiers␈αused␈α
as
␈↓ α←␈↓variable␈α∩names␈α⊃and␈α∩function␈α∩names;␈α⊃and␈α∩of␈α∩course␈α⊃the␈α∩LISP␈α∩atoms␈α⊃and
␈↓ α←␈↓S-exprs␈α↔themselves.␈α_ We␈α↔want␈α_to␈α↔represent␈α_identifiers␈α↔and␈α_S-exprs␈α↔as
␈↓ α←␈↓S-expressions.␈α∂The␈α∂first␈α∂request␈α⊂is␈α∂understandable,␈α∂but␈α∂perhaps␈α⊂the␈α∂second
␈↓ α←␈↓request␈α→seems␈α→vacuous:␈α_LISP␈α→S-exprs␈α→␈↓¬are␈↓␈α_S-exprs.␈α→Both␈α→requests␈α_are
␈↓ α←␈↓justifiable as we shall now see.
␈↓"β␈↓ α←␈↓␈↓ β'In␈α∞the␈α∞evaluator,␈α∞identifiers␈α∞are␈α∞used␈α∞as␈α∞variables;␈α∞therefore␈α∞we␈α
might
␈↓ α←␈↓represent a variable ␈↓εi␈↓ as:
␈↓"∀␈↓ α←␈↓␈↓ ¬e␈↓
R␈↓∞( ␈↓εi␈↓∞ )␈↓ = ␈↓α(VAR ␈↓εi␈↓α)␈↓
␈↓"∀␈↓ α←␈↓For example ␈↓αx␈↓ could be represented as ␈↓α(VAR X)␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Every␈α∞LISP␈α∞expression␈α∞must␈α∞have␈α∞a␈α∞representation;␈α∞and␈α∂the␈α∞mapping
␈↓ α←␈↓function␈α∩must␈α∪be␈α∩such␈α∪that␈α∩we␈α∩can␈α∪recover␈α∩the␈α∪original␈α∩object␈α∪from␈α∩its
␈↓ α←␈↓representation.␈α∞From␈α∞␈↓α(VAR X)␈↓␈α∞we␈α∞can␈α∞tell␈α∞that␈α∞it␈α∞is␈α∞a␈α∞representation␈α∞of␈α
the
␈↓ α←␈↓variable␈α∩␈↓αx␈↓.␈α∩ Now␈α∩consider␈α∪the␈α∩representation␈α∩of␈α∩the␈α∪non-numerical␈α∩LISP
␈↓ α←␈↓constant:␈αatoms␈αand␈αS-exprs.␈αSince␈α␈↓α(VAR X)␈↓␈αis␈αa␈αLISP␈αconstant,␈αit␈αmust␈αhave
␈↓ α←␈↓a␈αrepresentation␈α
under␈αour␈α
mapping.␈αWe␈αcannot␈α
represent␈αthe␈α
expression␈αas
␈↓ α←␈↓itself␈α∞since␈α
that␈α∞would␈α
violate␈α∞our␈α
inverse␈α∞mapping␈α
property.␈α∞Following␈α
our
␈↓ α←␈↓discussion of variable representation, we could represent a constant ␈↓εa␈↓ as:
␈↓"∀␈↓ α←␈↓␈↓ ¬E␈↓
R␈↓∞( ␈↓εa␈↓∞ )␈↓ = ␈↓α(CONST ␈↓εa␈↓α)␈↓
␈↓"∀␈↓ α←␈↓This mapping will solve the problems; we can
␈↓"∀␈↓ α←␈↓map the list ␈↓α(VAR X)␈↓ to ␈↓α(CONST (VAR X))␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈␈↓
R␈↓∞( ␈↓αx␈↓∞ )␈↓ = ␈↓α(VAR X)␈↓
␈↓" ␈↓ α←␈↓␈↓ ∧␈␈↓
R␈↓∞( ␈↓αX␈↓∞ )␈↓ = ␈↓α(CONST X)␈↓ ␈↓π 7␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'When␈α∂this␈α∂maping␈α∂is␈α∂extended␈α∂to␈α∂represent␈α∂␈↓¬all␈↓␈α∂LISP␈α∂expressions␈α∞the
␈↓ α←␈↓resulting␈αexpressions␈αbecome␈αvery␈αcomplex.␈αSince␈αwe␈αwish␈αto␈αuse␈αthe␈α
mapped
␈↓ α←␈↓expressions␈αas␈αthe␈αprogramming␈αlanguage,␈αhuman␈αengineering␈αconsiderations
␈↓ α←␈↓beg for a simplification. Therefore we use the following map:
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 7␈↓To be consistent, we should represent numerals in this format too.
␈↓ α←␈↓␈↓3.2␈↓ ε≤S-expr Translation of LISP Expressions 105␈↓
␈↓"∀␈↓ α←␈↓␈↓ ∧␈␈↓
R␈↓∞( ␈↓αx␈↓∞ )␈↓ = ␈↓αX␈↓
␈↓" ␈↓ α←␈↓␈↓ ∧␈␈↓
R␈↓∞( ␈↓αX␈↓∞ )␈↓ = ␈↓α(QUOTE X)␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'That is, we will translate identifiers to their upper-case counterpart.
␈↓"⊂␈↓ α←␈↓Thus:␈↓ ∧g␈↓
R␈↓∞( ␈↓<identifier> ␈↓∞)␈↓α = ␈↓<literal atom>
␈↓""␈↓ α←␈↓Examples:␈↓ ∧g␈↓
R␈↓∞( ␈↓αx ␈↓∞)␈↓α = X
␈↓"⊃␈↓ α←␈↓α␈↓ ∧g␈↓
R␈↓∞( ␈↓αy2 ␈↓∞)␈↓α = Y2
␈↓"⊃␈↓ α←␈↓α␈↓ ∧g␈↓
R␈↓∞( ␈↓αcar ␈↓∞)␈↓α = CAR
␈↓"∀␈↓ α←␈↓The mapping for LISP constants is:
␈↓"∀␈↓ α←␈↓␈↓ ∧q␈↓
R␈↓∞( ␈↓<sexpr> ␈↓∞)␈↓α = (QUOTE ␈↓<sexpr>␈↓α)␈↓
␈↓"!␈↓ α←␈↓For example:␈↓α␈↓ ∧g␈↓
R␈↓∞( ␈↓αX ␈↓∞)␈↓α = (QUOTE X)␈↓
␈↓"⊃␈↓ α←␈↓␈↓ ∧g␈↓
R␈↓∞( ␈↓α(A . B) ␈↓∞)␈↓α = (QUOTE (A . B))␈↓
␈↓"⊃␈↓ α←␈↓␈↓ ∧g␈↓
R␈↓∞( ␈↓αQUOTE ␈↓∞)␈↓α = (QUOTE QUOTE)␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'We␈α↔must␈α↔extend␈α⊗the␈α↔mapping␈α↔to␈α⊗the␈α↔other␈α↔constitutients␈α↔of␈α⊗the
␈↓ α←␈↓language.␈α⊃ We␈α⊃must␈α⊃map␈α∩applicative␈α⊃expressions␈α⊃of␈α⊃the␈α∩form␈α⊃␈↓αf[e␈↓β1␈↓α ; ... ;e␈↓βn␈↓α]␈↓
␈↓ α←␈↓onto␈αS-exprs.␈α Following␈αthe␈αstyle␈αof␈αour␈αinitial␈αmapping,␈αwe␈αmight␈αmap␈α␈↓αf[x]␈↓
␈↓ α←␈↓onto␈αsomething␈αlike␈α␈↓α(APP (VAR F) (VAR X))␈↓␈αor␈α␈↓α(APP (FUN F) (VAR X))␈↓,
␈↓ α←␈↓signifying␈α⊂that␈α⊃the␈α⊂list␈α⊃represents␈α⊂an␈α⊃applicative␈α⊂expression.␈α⊃However␈α⊂this
␈↓ α←␈↓leads␈α∩to␈α⊃cumbersome␈α∩expressions.␈α⊃ We␈α∩have␈α⊃seen␈α∩one␈α⊃other␈α∩mapping␈α⊃for
␈↓ α←␈↓functions␈α∂in␈α∂prefix␈α⊂form␈α∂in␈α∂Section 2.3.␈α∂ We␈α⊂will␈α∂use␈α∂that␈α⊂mapping,␈α∂called
␈↓ α←␈↓Cambridge Polish,␈↓π 8␈↓ here. That is:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ β:␈↓
R␈↓∞( ␈↓αf[e␈↓β1␈↓α;e␈↓β2␈↓α; ...;e␈↓βn␈↓α] ␈↓∞)␈↓α = ( ␈↓
R␈↓∞( ␈↓αf ␈↓∞)␈↓α ␈↓
R␈↓∞(␈↓α e␈↓β1␈↓α ␈↓∞)␈↓α ␈↓
R␈↓∞(␈↓α e␈↓β2␈↓α ␈↓∞)␈↓α ... ␈↓
R␈↓∞(␈↓α e␈↓βn␈↓α ␈↓∞)␈↓α )␈↓
␈↓"∀␈↓ α←␈↓Examples:␈↓α␈↓ ∧'␈↓
R␈↓∞( ␈↓αcar[x] ␈↓∞)␈↓α = (␈↓
R␈↓∞( ␈↓αcar ␈↓∞)␈↓α ␈↓
R␈↓∞( ␈↓αx ␈↓∞)␈↓α ) = (CAR X)
␈↓""␈↓ α←␈↓␈↓ β'␈↓
R␈↓∞( ␈↓αcar[X] ␈↓∞)␈↓α = (␈↓
R␈↓∞( ␈↓αcar ␈↓∞)␈↓α ␈↓
R␈↓∞( ␈↓αX ␈↓∞)␈↓α ) = (CAR (QUOTE X))
␈↓"⊃␈↓ α←␈↓α␈↓ β'␈↓
R␈↓∞( ␈↓αcons[cdr[(A . B)];x] ␈↓∞)␈↓α = (CONS (CDR (QUOTE (A . B))) X)
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 8␈↓The␈α≥name,␈α≥Cambridge␈α≥Polish,␈α≥is␈α≥derived␈α≥from␈α≡two␈α≥sources:
␈↓ α←␈↓Cambridge,␈αsince␈α
M.I.T.␈αis␈αin␈α
Cambridge␈αMassachusetts,␈αand␈α
McCarthy␈αwas
␈↓ α←␈↓at␈α⊂M.I.T.␈α⊂while␈α⊂developing␈α⊂his␈α⊃ideas;␈α⊂Polish,␈α⊂since␈α⊂the␈α⊂representation␈α⊃is␈α⊂a
␈↓ α←␈↓dialect of a notation developed by a school of Polish logicians.
␈↓ α←␈↓␈↓106 Evaluation␈↓
(3.2␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α∨␈↓
R␈↓-mapping␈α∨must␈α∨also␈α∨handle␈α∨conditional␈α∨expressions.␈α∨A
␈↓ α←␈↓conditional␈α
is␈α
represented␈α
as␈α
a␈α
list␈α
whose␈α
first␈α
element␈α
is␈α
␈↓αCOND␈↓␈α∞and␈α
whose
␈↓ α←␈↓next␈α∂␈↓αn␈↓␈α∂elements␈α∂are␈α∂representations␈α∂of␈α∂the␈α∂␈↓αp␈↓βi␈↓α-e␈↓βi␈↓␈α∂pairs.␈α∂The␈α∂␈↓
R␈↓-map␈α⊂of␈α∂such
␈↓ α←␈↓pairs is a list of the ␈↓
R␈↓-maps of the two elements:
␈↓"∀␈↓ α←␈↓␈↓ αv␈↓
R␈↓∞( ␈↓α[p␈↓β1␈↓α → e␈↓β1␈↓α; ... ;p␈↓βn␈↓α → e␈↓βn␈↓α] ␈↓∞)␈↓α = (COND␈↓ εo(␈↓
R␈↓∞(␈↓α p␈↓β1 ␈↓∞)␈↓α
␈↓"β␈↓ α←␈↓α␈↓ εo␈↓
R␈↓∞(␈↓α e␈↓β1␈↓∞)␈↓α )
␈↓"β␈↓ α←␈↓α␈↓ εo . . .
␈↓"β␈↓ α←␈↓α␈↓ εo(␈↓
R␈↓∞(␈↓α p␈↓βn␈↓α ␈↓∞)␈↓α ␈↓
R␈↓∞(␈↓α e␈↓βn ␈↓∞)␈↓α))
␈↓"∀␈↓ α←␈↓An example:␈↓α
␈↓" ␈↓ α←␈↓α␈↓ αf␈↓
R␈↓∞( ␈↓α[atom[x] →1; q[y] → X] ␈↓∞)␈↓α = (COND␈↓ εc((ATOM X) 1)
␈↓"β␈↓ α←␈↓α␈↓ εc((Q Y) (QUOTE X)))
␈↓"∀␈↓ α←␈↓Notice␈α∂that␈α⊂␈↓α(COND ... )␈↓␈α∂and␈α∂␈↓α(QUOTE ... )␈↓␈α⊂␈↓¬look␈↓␈α∂like␈α∂translations␈α⊂of␈α∂function
␈↓ α←␈↓applications␈α
of␈α
the␈α
form␈α
␈↓αcond[ ... ]␈α
␈↓␈α
and␈α
␈↓αquote[ ... ]␈↓.␈α
However␈α
since␈α
we␈αexpect
␈↓ α←␈↓application␈α∃to␈α⊗be␈α∃performed␈α⊗using␈α∃call-by-value,␈α⊗we␈α∃must␈α⊗handle␈α∃these
␈↓ α←␈↓constructs␈αin␈αa␈αspecial␈αmanner.␈α Indeed,␈α␈↓αquote[␈↓λα␈↓α]␈↓␈αstands␈αfor␈α␈↓
R␈↓∞(␈↓λα␈↓∞)␈↓.␈αSimilarly
␈↓ α←␈↓the␈α"arguments"␈αto␈α␈↓αcond␈↓␈αare␈αnot␈αto␈αbe␈αinterpreted␈αas␈αin␈αfunction␈αapplications;
␈↓ α←␈↓for␈α?example,␈α?␈↓αCOND ((ATOM X) 1) ...)␈↓␈α?does␈α?not␈α?represent
␈↓ α←␈↓␈↓αcond[ atom[x][1]; ... ]␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Finally,␈αthe␈αtranslations␈αof␈αthe␈αtruth␈αvalues␈α␈↓
t␈↓␈αand␈α␈↓
f␈↓␈αwill␈αbe␈α␈↓αT␈↓␈αand␈α␈↓αNIL␈↓,
␈↓ α←␈↓respectively.
␈↓"⊂␈↓ α←␈↓␈↓ ε∂␈↓
R␈↓∞( ␈↓
t␈↓∞ )␈↓α = T
␈↓"⊃␈↓ α←␈↓α␈↓ ε∂␈↓
R␈↓∞( ␈↓
f␈↓∞ )␈↓α = NIL
␈↓"∀␈↓ α←␈↓You␈α∀might␈α∪have␈α∀noticed␈α∪that␈α∀these␈α∪last␈α∀two␈α∪applications␈α∀of␈α∀the␈α∪chosen
␈↓ α←␈↓␈↓
R␈↓-mapping␈α∩have␈α∪the␈α∩potential␈α∪to␈α∩cause␈α∪trouble.␈α∩They␈α∪will␈α∩spoil␈α∪the␈α∩1-1
␈↓ α←␈↓property of ␈↓
R␈↓:
␈↓"⊂␈↓ α←␈↓␈↓ ε∂␈↓
R␈↓∞( ␈↓αt␈↓∞ )␈↓α = T
␈↓"⊃␈↓ α←␈↓α␈↓ ε∂␈↓
R␈↓∞( ␈↓αnil␈↓∞ )␈↓α = NIL
␈↓"∀␈↓ α←␈↓The␈α
usual␈αway␈α
to␈αescape␈α
from␈αthis␈α
difficulty␈αis␈α
to␈αoutlaw␈α
␈↓αt␈↓␈αand␈α
␈↓αnil␈↓␈α
as␈αLISP
␈↓ α←␈↓variables.␈↓π 9␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Perhaps␈α∨our␈α∨concern␈α∨for␈α∨the␈α∨␈↓
R␈↓-mapping's␈α∨properties␈α∨appears
␈↓ α←␈↓heavy-handed␈αwhere␈αa␈αsimple␈α
solution␈αseems␈αapparent:␈α␈↓
t␈↓␈αis␈α
␈↓
t␈↓␈αand␈α␈↓αt␈↓␈αis␈α␈↓αt␈↓;␈α
when
␈↓ α←␈↓we␈αwant␈αthe␈αtruth␈αvalue␈αwe␈αwrite␈α␈↓
t␈↓␈αand␈αwhen␈αwe␈αwant␈αthe␈αvariable␈αwe␈αwrite
␈↓ α←␈↓␈↓αt␈↓.␈α⊂The␈α⊂answer␈α⊂is␈α⊂that␈α⊂when␈α⊂we␈α⊂write␈α⊂programs␈α⊂for␈α⊂a␈α⊂machine␈α⊃version␈α⊂of
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 9␈↓In␈αLISP 1.5␈α␈↓αT␈↓␈αand␈α␈↓αF␈↓␈αwere␈αused␈αas␈αthe␈αrepresentations␈αof␈α␈↓
t␈↓␈αand␈α␈↓
f␈↓;␈αthe
␈↓ α←␈↓atoms␈α␈↓αT␈↓␈αand␈α␈↓αF␈↓␈αwere␈α(permanently)␈αbound␈αto␈αvalues␈α␈↓α*T*␈↓␈αand␈α␈↓αNIL␈↓.␈αNote␈αtoo,
␈↓ α←␈↓that␈α
our␈αinitial␈α
mapping␈αcould␈α
solve␈α
the␈αproblem␈α
by␈αmapping␈α
␈↓αt␈↓␈α
to␈α␈↓α(VAR T)␈↓
␈↓ α←␈↓and mapping ␈↓
t␈↓ to ␈↓α(BOOL T)␈↓.
␈↓ α←␈↓␈↓3.2␈↓ ε≤S-expr Translation of LISP Expressions 107␈↓
␈↓"β␈↓ α←␈↓LISP,␈α∩we␈α∩will␈α∩be␈α∩writing␈α∩the␈α∩␈↓
R␈↓-image,␈α∩rather␈α∩than␈α∩the␈α∪more␈α∩traditional
␈↓ α←␈↓syntax.␈α∞Thus␈α∂to␈α∞ask␈α∞a␈α∂LISP␈α∞machine␈α∞to␈α∂evaluate␈α∞␈↓αcar[(A . B)]␈↓␈α∞we␈α∂present␈α∞it
␈↓ α←␈↓with␈α⊂␈↓α(CAR (QUOTE (A . B)))␈↓.␈α∂ What␈α⊂this␈α∂means␈α⊂is␈α∂that␈α⊂we␈α⊂are␈α∂presenting
␈↓ α←␈↓our␈αprograms␈αto␈α
the␈αmachine␈αas␈αdata␈α
structures␈αof␈αthe␈αlanguage.␈↓π 10␈↓␈α
It␈αwould
␈↓ α←␈↓be␈αlike␈αexpressing␈αprograms␈αin␈αFortran␈αor␈αAlgol␈αas␈αarrays␈αof␈αintegers;␈αthat␈α
is,
␈↓ α←␈↓the␈α∞data␈α∞structures␈α∞of␈α∞␈↓¬those␈↓␈α∞languages.␈α∞ We␈α∞will␈α∞explore␈α∞the␈α∞implications␈α∞of
␈↓ α←␈↓this approach to programming in later sections.
␈↓"β␈↓ α←␈↓␈↓ β'In␈αessence,␈αthen,␈αthere␈αare␈α␈↓¬two␈↓␈αLISP's:␈αthere␈αis␈αthe␈αalgorithmic␈αlanguage
␈↓ α←␈↓and␈α⊂there␈α⊂is␈α⊂the␈α⊂programming␈α⊂language.␈α⊂The␈α⊂programming␈α⊂language␈α⊂is␈α∂a
␈↓ α←␈↓data␈α∞structure␈α∂representation␈α∞of␈α∞the␈α∂algorithmic␈α∞language.␈α∂ The␈α∞algorithmic
␈↓ α←␈↓language␈α⊂is␈α⊂called␈α⊂the␈α⊂␈↓↓meta-language␈↓␈α⊂or␈α⊂␈↓↓M-expr␈α⊂LISP␈↓,␈α⊂and␈α⊂for␈α∂historical
␈↓ α←␈↓purposes, the programming language is called ␈↓↓S-expr LISP␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Review␈α∞the␈α∞␈↓αtgm␈↓'s␈α∞(Section 2.8)␈α∞now␈α∞that␈α∞you␈α∞understand␈α∞that␈α∞they␈α∞are
␈↓ α←␈↓evaluators␈α∀for␈α∃simple␈α∀subsets␈α∃of␈α∀LISP␈α∃expressions;␈α∀discover␈α∃what␈α∀LISP
␈↓ α←␈↓expressions␈α
were␈α
encoded␈α∞in␈α
arguments␈α
to␈α
the␈α∞␈↓αtgm␈↓'s␈α
and␈α
verify␈α∞the␈α
answers
␈↓ α←␈↓you␈α⊃obtained␈α⊃earlier.␈α⊃ Note␈α⊃that␈α⊃the␈α⊃only␈α⊃atoms␈α⊃which␈α⊃the␈α∩great␈α⊃mothers
␈↓ α←␈↓recognize␈αare␈α␈↓αT␈↓␈α
and␈α␈↓αNIL␈↓.␈αAny␈α
other␈αatoms␈αelicit␈α
an␈αerror␈αmessage.␈α What␈α
do
␈↓ α←␈↓other␈αatoms␈αrepresent?␈αNumerals␈αare␈αatoms␈αand␈αare␈αthe␈α␈↓
R␈↓-maps␈αof␈αnumerals.
␈↓ α←␈↓We␈α
could␈αextend␈α
␈↓αtgmoaf␈↓␈αto␈α
handle␈αthis␈α
case.␈α Atoms␈α
are␈αalso␈α
translations␈αof
␈↓ α←␈↓variables␈αand␈αfunction␈αnames.␈α So␈αone␈αtask␈αis␈αto␈αinclude␈αa␈αmechanism␈αin␈αour
␈↓ α←␈↓LISP␈α∞evaluator␈α∞to␈α∂handle␈α∞evaluation␈α∞of␈α∂variables␈α∞and␈α∞function␈α∂names.␈α∞We
␈↓ α←␈↓have␈α
already␈α
seen␈α
the␈α
necessary␈αmechanism␈α
in␈α
Section 2.7␈α
where␈α
we␈αstudied
␈↓ α←␈↓tables␈α⊂as␈α⊂abstract␈α⊃data␈α⊂stuctures.␈α⊂The␈α⊂other␈α⊃piece␈α⊂of␈α⊂LISP␈α⊂which␈α⊃did␈α⊂not
␈↓ α←␈↓appear␈α→in␈α→the␈α→evaluator␈α→for␈α→polynomials␈α→was␈α→conditional␈α_expressions.
␈↓ α←␈↓Conditional␈α
expressions␈α
␈↓¬were␈↓␈αhandled␈α
in␈α
␈↓αtgmoafr␈↓.␈αThe␈α
"progenitors"␈α
did␈α␈↓¬not␈↓
␈↓ α←␈↓handle␈α⊗variable␈α⊗references,␈α⊗however.␈α↔In␈α⊗preparation␈α⊗for␈α⊗that␈α↔work␈α⊗we
␈↓ α←␈↓reexamine the issues of symbol tables.
␈↓"β␈↓ α←␈↓␈↓ ¬W␈↓↓3.3 Symbol Tables␈↓
␈↓"β␈↓ α←␈↓One␈αdistinguishing␈α
feature␈αof␈αcomputer␈α
science␈αis␈αthe␈α
ubiquity␈αof␈α
devices␈αto
␈↓ α←␈↓store␈α
and␈αrecover␈α
information.␈α A␈α
notation␈α
which␈αaddresses␈α
itself␈αto␈α
computer
␈↓ α←␈↓science␈α⊃must␈α⊃treat␈α⊃this␈α⊃aspect.␈α⊂ In␈α⊃hardware␈α⊃oriented␈α⊃languages␈α⊃and␈α⊂some
␈↓ α←␈↓high␈α
level␈α
programming␈αlanguages␈α
we␈α
find␈α
the␈αnotion␈α
of␈α
"cell"␈α
or␈α"location"
␈↓ α←␈↓and␈α∞find␈α∞operations␈α∞to␈α∞explicitly␈α∞deposit␈α∞and␈α∞examine␈α∞information␈α∞in␈α
those
␈↓ α←␈↓cells.␈α∃ Our␈α⊗LISP␈α∃subset␈α⊗has␈α∃no␈α∃such␈α⊗explict␈α∃features;␈α⊗it␈α∃relies␈α⊗on␈α∃the
␈↓ α←␈↓implementation␈α⊗of␈α⊗binding␈α⊗and␈α⊗variable␈α⊗evaluation␈α⊗to␈α⊗perform␈α⊗similar
␈↓ α←␈↓notions.␈α
As␈α
part␈α
of␈αour␈α
examination␈α
of␈α
evaluation␈αwe␈α
wish␈α
to␈α
expose␈αthese
␈↓ α←␈↓details␈α
to␈α
close␈αscrutiny␈α
and␈α
understand␈αhow␈α
binding␈α
and␈αvariable␈α
evaluation
␈↓ α←␈↓can␈α∪be␈α∩mechanized.␈α∪ The␈α∩most␈α∪common␈α∩notion␈α∪used␈α∩to␈α∪implement␈α∩these
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 10␈↓Compare␈α
this␈α
with␈α
the␈α
technique␈α
of␈α
Godel␈α
numbering␈α
in␈αformal␈α
logic
␈↓ α←␈↓[Men 64].
␈↓ α←␈↓␈↓108 Evaluation␈↓
(3.3␈↓
␈↓"β␈↓ α←␈↓operations␈α∞is␈α∞the␈α∂symbol␈α∞table.␈↓π 11␈↓␈α∞This␈α∞is␈α∂the␈α∞device␈α∞we␈α∞used␈α∂informally␈α∞in
␈↓ α←␈↓Section 2.7; we will review some of that discussion here.
␈↓"β␈↓ α←␈↓␈↓ β'In␈αits␈αabstract␈αform,␈αa␈αsymbol␈αtable␈αis␈αa␈αset␈αof␈αordered␈αpairs␈αof␈αobjects;
␈↓ α←␈↓one␈α∞of␈α
the␈α∞elements␈α
of␈α∞each␈α
pair␈α∞is␈α
a␈α∞name;␈α
the␈α∞other␈α
is␈α∞a␈α∞value␈α
associated
␈↓ α←␈↓with␈α⊃that␈α∩name.␈α⊃ This␈α⊃means␈α∩that␈α⊃symbol␈α⊃tables␈α∩can␈α⊃be␈α∩characterized␈α⊃as
␈↓ α←␈↓relations␈αor␈αperhaps␈αeven␈αas␈αfunctions.␈α This␈αlatter␈αcharacterization␈αis␈αindeed
␈↓ α←␈↓viable.␈α∃On␈α∃page 89␈α∃we␈α∃showed␈α∃that␈α∃a␈α∃table␈α∃could␈α∃be␈α∃constructed␈α∃and
␈↓ α←␈↓maintained␈α
in␈α
a␈α
manner␈αpreserving␈α
functionality.␈α
As␈α
an␈α
abstract␈αoperation,
␈↓ α←␈↓finding␈α⊂an␈α⊂element␈α⊂in␈α⊂a␈α⊂symbol␈α⊂table␈α⊂is␈α⊂also␈α⊂quite␈α⊂simple:␈α⊂given␈α⊂a␈α⊃set␈α⊂of
␈↓ α←␈↓ordered␈αpairs␈αand␈αa␈αname,␈αfind␈αa␈αpair␈αwhose␈αfirst␈αelement␈αis␈αthe␈αsame␈αas␈αthe
␈↓ α←␈↓given␈αname.␈α This␈αoperation␈αcan␈αbe␈αdescribed␈αas␈αfunction␈α
application␈αwhere
␈↓ α←␈↓the␈α∪function␈α∀being␈α∪applied␈α∪is␈α∀the␈α∪table␈α∪and␈α∀the␈α∪argument␈α∪is␈α∀the␈α∪name
␈↓ α←␈↓component. That is: ␈↓αlocate[x;tbl] = tbl(x)␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∂maintenance␈α∂of␈α∂symbol␈α∂tables␈α∂as␈α∂sets␈α∂was␈α∂a␈α∂bit␈α∂too␈α⊂abstract;␈α∂the
␈↓ α←␈↓level␈α
of␈α
abstraction␈α
we␈α
implemented␈α
viewed␈α
a␈α
symbol␈α
table␈α
as␈α
a␈α
␈↓¬sequence␈↓␈αof
␈↓ α←␈↓pairs,␈α∂each␈α∂pair␈α∂representing␈α∂a␈α∞variable␈α∂and␈α∂its␈α∂corresponding␈α∂value.␈α∞ The
␈↓ α←␈↓table␈αmanipulating␈αalgorithms,␈αgiven␈αin␈αSection 2.7,␈αdepended␈αheavily␈αon␈αthe
␈↓ α←␈↓implied␈α
sequencing␈α
of␈αcall-by-value␈α
and␈α
recursion.␈α Since␈α
this␈α
was␈αconsistent
␈↓ α←␈↓with␈α∃the␈α∃explicit␈α∀sequencing␈α∃used␈α∃in␈α∃adding␈α∀elements␈α∃to␈α∃the␈α∃table,␈α∀we
␈↓ α←␈↓achieved␈α∂the␈α∞desired␈α∂effect.␈α∞We␈α∂found␈α∞the␈α∂expected␈α∞bindings,␈α∂even␈α∞though
␈↓ α←␈↓there␈α
may␈αhave␈α
been␈α
other␈αcandidates␈α
in␈α
the␈αtables.␈α
In␈α
the␈αremaining␈α
sections
␈↓ α←␈↓of␈α∃this␈α∀chapter␈α∃we␈α∀will␈α∃utilize␈α∀more␈α∃features␈α∀of␈α∃this␈α∃interplay␈α∀between
␈↓ α←␈↓representation␈α
of␈α
data␈α
and␈α
calling␈α
style␈α
of␈α
algorithm.␈α
Symbol␈α
tables␈α
are␈α
just
␈↓ α←␈↓one manifestation of this phenomenon.
␈↓"β␈↓ α←␈↓␈↓ β'Symbol␈αtables␈αare␈αalso␈αknown␈αas␈αassociation␈αlists␈αor␈α␈↓↓a-lists␈↓;␈αthus␈α␈↓αassoc␈↓␈αis
␈↓ α←␈↓the␈α⊃traditional␈α⊃name␈α⊃of␈α⊃a␈α⊃LISP␈α⊂function␈α⊃to␈α⊃search␈α⊃a␈α⊃symbol␈α⊃table.␈α⊂More
␈↓ α←␈↓recently␈α⊂symbol␈α⊂tables␈α⊂have␈α⊂been␈α⊂called␈α⊂␈↓↓environments␈↓;␈α⊂thus␈α⊂we␈α⊂frequently
␈↓ α←␈↓will␈α
use␈α
the␈αidentifer␈α
␈↓αenv␈↓␈α
as␈αa␈α
variable␈α
which␈αis␈α
an␈α
environment.␈α The␈α
binary
␈↓ α←␈↓function␈α⊃␈↓αassoc␈↓␈α⊃expects␈α∩a␈α⊃name␈α⊃and␈α⊃a␈α∩symbol␈α⊃table␈α⊃as␈α⊃arguments.␈α∩ It␈α⊃will
␈↓ α←␈↓examine␈α∃the␈α∀table␈α∃from␈α∀left␈α∃to␈α∀right,␈α∃looking␈α∀for␈α∃the␈α∀first␈α∃pair␈α∀whose
␈↓ α←␈↓name-component␈αmatches␈αthe␈αgiven␈αname.␈α If␈αa␈αpair␈αis␈αfound,␈αthen␈αthat␈αpair
␈↓ α←␈↓is␈α
returned;␈αif␈α
no␈α
such␈αpair␈α
is␈α
found,␈αthe␈α
result␈α
is␈αundefined.␈α
We␈α
will␈αneed␈α
to
␈↓ α←␈↓designate␈α∩a␈α∩selector,␈α∪␈↓αname␈↓,␈α∩to␈α∩locate␈α∩the␈α∪name-component␈α∩of␈α∩a␈α∪pair,␈α∩and
␈↓ α←␈↓another selector, ␈↓αvalue␈↓, to retrieve the value-component.
␈↓"∀␈↓ α←␈↓α␈↓ βKassoc[x;env] <=␈↓ ¬#[eq[name[first[env]];x] → first[env];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ¬# ␈↓
t␈↓α → assoc[x;rest[env]]]
␈↓"∀␈↓ α←␈↓␈↓ β'If␈αthe␈αtable␈αis␈αvery␈αlong␈αand␈αthe␈αdesired␈αpair␈αis␈αclose␈αto␈αthe␈αend␈αof␈αthe
␈↓ α←␈↓table,␈αthen␈αwe␈αwill␈αbe␈αin␈αfor␈αa␈αvery␈αlong␈αsearch.␈αThe␈αsearch␈αscheme␈αencoded
␈↓ α←␈↓in␈α
␈↓αassoc␈↓␈α
is␈α
called␈α
␈↓↓linear␈α
search␈↓,␈α
and␈α
is␈α
unnecessarily␈α
inefficient␈α
for␈α
tables␈α
of
␈↓ α←␈↓substantial␈αlength.␈αHowever␈αthe␈αphenomemona␈αwe␈αwish␈αto␈αstudy␈αnow␈αare␈αnot
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 11␈↓Recall, we are ␈↓¬simulating␈↓ substitution; see [Ber 75] for an alternative.
␈↓ α←␈↓␈↓3.3␈↓ λLSymbol Tables 109␈↓
␈↓"β␈↓ α←␈↓directly␈α∞related␈α
to␈α∞efficiency␈α
of␈α∞searching␈α
methods.␈↓π 12␈↓␈α∞We␈α
will␈α∞come␈α∞back␈α
to
␈↓ α←␈↓symbol␈α∂tables␈α∂in␈α∂Section 5.6␈α∂to␈α∂study␈α∂the␈α∂problems␈α∂of␈α∂efficient␈α∂storage␈α∞and
␈↓ α←␈↓retrieval␈αof␈αinformation.␈αIt␈αwill␈αsuffice␈αnow␈αsimply␈αto␈αthink␈αof␈αa␈αsymbol␈αtable
␈↓ α←␈↓as␈α
represented␈α
in␈α
LISP␈α∞by␈α
a␈α
list␈α
of␈α
dotted␈α∞pairs:␈α
a␈α
name␈α
dotted␈α∞with␈α
value.
␈↓ α←␈↓In␈α∞this␈α∂representation,␈α∞then,␈α∂␈↓αname[x]␈α∞<=␈α∂car[x]␈↓,␈α∞and␈α∂␈↓αvalue[x] <= cdr[x]␈↓.␈α∞ For
␈↓ α←␈↓completeness,␈α
we␈α
should␈αalso␈α
specify␈α
a␈α
constructor.␈αThough␈α
we␈α
won't␈αneed␈α
the
␈↓ α←␈↓function␈α
for␈α
a␈αwhile,␈α
we␈α
will␈α
name␈αit␈α
␈↓αmkent␈↓;␈α
it␈α
will␈αtake␈α
an␈α
identifier␈α
and␈αa
␈↓ α←␈↓value␈α∃and␈α∃return␈α∃a␈α∃new␈α∃symbol␈α∃table␈α∃entry.␈α∃Its␈α∃representation␈α⊗here␈α∃is
␈↓ α←␈↓␈↓αmkent[x;y] <= cons[x;y]␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'To␈α∩illustrate␈α∪the␈α∩representation␈α∪and␈α∩algorithms,␈α∪assume␈α∩we␈α∪wish␈α∩to
␈↓ α←␈↓represent␈αthree␈α
variables␈α␈↓αx,␈αy,␈α
␈↓and␈α␈↓αz␈↓␈αwhich␈α
were␈αto␈αhave␈α
values␈α␈↓α2,␈α3,␈α
␈↓and␈α␈↓α4␈↓.
␈↓ α←␈↓That fact could be encoded as:
␈↓"∀␈↓ α←␈↓␈↓ ¬B␈↓α((X . 2) (Y . 3) (Z . 4))␈↓
␈↓"∀␈↓ α←␈↓Then the retrieval of ␈↓αy␈↓ and ␈↓αu␈↓ could be encoded as:
␈↓""␈↓ α←␈↓␈↓ ∧7␈↓αassoc[Y; ((X . 2) (Y . 3) (Z . 4))] = (Y . 3)
␈↓"⊃␈↓ α←␈↓α␈↓ ∧7assoc[U; ((X . 2) (Y . 3) (Z . 4))] ␈↓ = ␈↓λB␈↓
␈↓"∀␈↓ α←␈↓The␈αretrieval␈α
of␈α␈↓λB␈↓␈α
for␈α␈↓αu␈↓␈αcould␈α
be␈αimplemented␈α
as␈αan␈α
error␈αmessage␈αor,␈α
better
␈↓ α←␈↓yet,␈α∞could␈α
interact␈α∞with␈α
the␈α∞user␈α∞to␈α
isolate␈α∞the␈α
misconception,␈α∞correct␈α∞it,␈α
and
␈↓ α←␈↓continue.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∞must␈α
also␈α∞represent␈α
bindings␈α∞of␈α
variables␈α∞to␈α∞non-numeric␈α
S-exprs.
␈↓ α←␈↓For␈α
example,␈α
we␈α∞must␈α
represent␈α
information␈α
like:␈α∞"the␈α
current␈α
value␈α
of␈α∞␈↓αx␈↓␈α
is
␈↓ α←␈↓␈↓αA␈↓".␈α_ We␈α_will␈α_place␈α_the␈α_dotted-pair␈α_␈↓α(X␈α_.␈α_A)␈↓␈α_in␈α_the␈α_table.␈α_Now␈α_this
␈↓ α←␈↓representation␈α
is␈α
certainly␈α
open␈α
to␈α
question:␈α
why␈α
not␈α
add␈α
␈↓α(X . (QUOTE A))␈↓?
␈↓ α←␈↓The␈α
latter␈α
notation␈α
is␈α
more␈α
consistent␈α
with␈α
our␈α
conception␈α
of␈α
representation
␈↓ α←␈↓espoused␈α∂on␈α∂page 56.␈α∂ That␈α∂is,␈α∂we␈α∂map␈α∂LISP␈α∂expressions␈α∂to␈α∞S-expressions;
␈↓ α←␈↓perform␈α⊂the␈α⊂calculations␈α⊂on␈α∂this␈α⊂representation,␈α⊂and␈α⊂finally␈α⊂␈↓¬reinterpret␈↓␈α∂the
␈↓ α←␈↓result␈α
of␈α
this␈α
calculation␈α
as␈α∞a␈α
LISP␈α
expression.␈α
The␈α
representation␈α∞we␈α
have
␈↓ α←␈↓chosen␈α⊃for␈α⊃symbol␈α⊃tables␈α⊃obviates␈α⊂the␈α⊃last␈α⊃reinterpretation␈α⊃step;␈α⊃recall␈α⊂the
␈↓ α←␈↓diagram␈α⊂on␈α⊂page 103.␈α∂Now␈α⊂it␈α⊂will␈α∂turn␈α⊂out␈α⊂that␈α∂for␈α⊂our␈α⊂initial␈α⊂subsets␈α∂of
␈↓ α←␈↓LISP␈α~this␈α~reinterpretation␈α~step␈α~simply␈α~would␈α~involve␈α~"stripping"␈α→the
␈↓ α←␈↓␈↓αQUOTE␈↓s.␈α∩ The␈α∩only␈α∪"values"␈α∩which␈α∩a␈α∪LISP␈α∩computation␈α∩can␈α∪return␈α∩are
␈↓ α←␈↓constants;␈α
however␈α
more␈α
general␈α
evaluation␈α
schemes␈α
are␈α
conceivable;␈α
partial
␈↓ α←␈↓evaluation␈α⊃may␈α⊃be␈α⊃useful,␈α⊃simplifying␈α∩␈↓αx+y+2␈↓␈α⊃to␈α⊃␈↓αx+6␈↓␈α⊃when␈α⊃␈↓αy␈↓␈α⊃has␈α∩value␈α⊃␈↓α4␈↓.
␈↓ α←␈↓Perhaps␈α
the␈α
LISP␈αrepresentation␈α
of␈α
table␈αentries␈α
is␈α
a␈α
poor␈αone;␈α
we␈α
will␈αsee.␈α
In
␈↓ α←␈↓studying␈αany␈αexisting␈αlanguage,␈αor␈αcontemplating␈αthe␈αdesign␈αof␈αany␈αnew␈αone,
␈↓ α←␈↓we␈αmust␈αquestion␈αeach␈αdetail␈αof␈αrepresentation.␈αDecisions␈αmade␈αtoo␈αearly␈αcan
␈↓ α←␈↓have serious consequences.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 12␈↓At␈αleast␈αindirectly␈αthe␈αdiscussion␈α␈↓¬is␈↓␈αrelated␈αto␈αsearch␈αefficiency.␈αLISP
␈↓ α←␈↓implements␈αa␈αdynamic␈αbinding␈αor␈α"latest␈αactive"␈αbinding␈αstrategy.␈αA␈αcase␈αcan
␈↓ α←␈↓be made for static binding on the basis of shorter symbol table searches.
␈↓ α←␈↓␈↓110 Evaluation␈↓
(3.3␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Before␈αcontinuing␈αwe␈αshould␈αtake␈αstock␈αof␈αour␈αcurrent␈αposition.␈αIn␈αthis
␈↓ α←␈↓section␈αwe␈α
have␈αrecreated␈α
the␈αtable-lookup␈α
mechanism␈αwe␈α
used␈αin␈α
Section 2.7,
␈↓ α←␈↓but␈αnow␈αwe␈αare␈αpaying␈αa␈αbit␈αmore␈αattention␈αto␈αrepresentation.␈αWe␈αcan␈αlocate
␈↓ α←␈↓things␈α
in␈αa␈α
table␈αand␈α
we␈αhave␈α
seen␈αhow␈α
calling␈αfunctions␈α
can␈αadd␈α
values␈αto␈α
a
␈↓ α←␈↓table.␈αWe␈αhave␈αsaid␈αnothing␈αabout␈αadding␈αfunction␈αdefinitions␈αto␈αthe␈αtables.
␈↓ α←␈↓Abstractly␈α
we␈αknow␈α
how␈α
to␈αextract␈α
the␈αdefinition␈α
from␈α
the␈αtable␈α
and␈αapply␈α
it.
␈↓ α←␈↓We␈α∞must␈α∞give␈α∞an␈α∞explicit␈α∂representation␈α∞of␈α∞the␈α∞storage␈α∞of␈α∞a␈α∂function.␈α∞This
␈↓ α←␈↓turns␈α∞out␈α∞to␈α∂be␈α∞a␈α∞reasonably␈α∂non-trivial␈α∞problem.␈α∞ We␈α∂have␈α∞seen␈α∞that␈α∂it␈α∞is
␈↓ α←␈↓possible␈α∩to␈α∩mechanize␈α∩at␈α∩least␈α∩one␈α∩scheme␈α∩for␈α∩evaluation␈α∩of␈α∩functions␈α∩--
␈↓ α←␈↓call-by-value,␈αevaluating␈α
arguments␈αfrom␈αleft␈α
to␈αright.␈α We␈α
have␈αseen␈α
that␈αit
␈↓ α←␈↓is␈αpossible␈αto␈α
translate␈αLISP␈αexpressions␈α
into␈αS-exprs␈αin␈α
such␈αa␈αway␈α
that␈αwe
␈↓ α←␈↓can␈α↔write␈α↔a␈α↔LISP␈α↔function␈α↔which␈α↔will␈α↔act␈α↔as␈α↔an␈α↔evaluator␈α↔for␈α⊗such
␈↓ α←␈↓translations.␈α
In␈αthe␈α
process␈α
we␈αhave␈α
had␈α
to␈αmechanize␈α
the␈α
intuitive␈αdevices
␈↓ α←␈↓we␈α
might␈α
mentally␈α
use␈α
to␈α
recall␈αthe␈α
definition␈α
of␈α
functions␈α
and␈α
to␈α
recall␈αthe
␈↓ α←␈↓current␈α∞values␈α∞of␈α∞variables.␈α∞ It␈α∞became␈α∞clear␈α∞that␈α∞the␈α∞mechanism␈α∞of␈α
symbol
␈↓ α←␈↓tables␈α∂could␈α∂be␈α∂used.␈α∂ To␈α∂associate␈α⊂a␈α∂variable␈α∂with␈α∂a␈α∂value␈α∂was␈α⊂easy.␈α∂ To
␈↓ α←␈↓associate␈α∂a␈α∂function␈α∂name␈α∂with␈α∞its␈α∂definition␈α∂required␈α∂some␈α∂care.␈α∂ That␈α∞is,
␈↓ α←␈↓part␈αof␈αthe␈αdefinition␈αof␈αa␈αfunction␈αinvolves␈αthe␈αproper␈αassociation␈αof␈αformal
␈↓ α←␈↓parameters␈α∂with␈α∂the␈α∂body␈α∂of␈α∂the␈α∂definition.␈α∂The␈α∂next␈α∂section␈α⊂introduces␈α∂a
␈↓ α←␈↓notation for describing function definitions.
␈↓"β␈↓ α←␈↓␈↓ ¬u␈↓↓3.4 ␈↓αλ␈↓↓-notation␈↓α
␈↓"β␈↓ α←␈↓Recall␈α~our␈α~discussion␈α~of␈α~the␈α~problems␈α~of␈α~representation␈α~of␈α→function
␈↓ α←␈↓definitions.␈αThis␈αdiscussion␈α
began␈αon␈αpage 84␈αand␈α
our␈αconclusion␈αwas␈αthat␈α
to
␈↓ α←␈↓represent␈α∞a␈α∞definition␈α∞like␈α∞␈↓αf[x;y] <= ␈↓λx␈↓␈α∂we␈α∞needed␈α∞a␈α∞symbol␈α∞table␈α∂entry␈α∞with
␈↓ α←␈↓name␈α␈↓αf␈↓␈αand␈αa␈αvalue␈αpart␈αwhich␈αcontained␈αthe␈αbody␈αof␈αthe␈αdefinition,␈α␈↓λx␈↓,␈αand
␈↓ α←␈↓the␈αlist␈αof␈αformal␈αparameters,␈α␈↓α[x;y]␈↓.␈α This␈αview␈αof␈αthe␈αcontent␈αof␈αa␈αdefinition
␈↓ α←␈↓will␈αhave␈αto␈αbe␈αrevised,␈αbut␈αits␈αimplementation␈αcontains␈αsufficient␈αcomplexity
␈↓ α←␈↓to␈α⊂support␈α⊂a␈α⊂lively␈α⊂and␈α⊂fruitful␈α⊂discussion.␈α⊂ LISP␈α⊂uses␈α⊂a␈α⊃unique␈α⊂notation,
␈↓ α←␈↓called␈αthe␈αλ-␈↓↓notation␈↓␈αto␈αlend␈αprecision␈αto␈αour␈αinformal␈αdiscussion␈αof␈αfunction
␈↓ α←␈↓representation.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αλ-notation␈αis␈αderived␈αfrom␈αthe␈α␈↓λλ␈↓-calculus,␈αa␈αformalism␈αinvented␈αby
␈↓ α←␈↓the␈α↔logician␈α↔Alonzo␈α↔Church␈α⊗([Chu 41])␈α↔to␈α↔model␈α↔functions␈α↔which␈α⊗are
␈↓ α←␈↓describable␈α⊗by␈α⊗algorithms.␈α⊗ The␈α⊗␈↓λλ␈↓-calculus␈α⊗is␈α⊗useful␈α⊗for␈α⊗discussing␈α∃the
␈↓ α←␈↓concepts␈α_of␈α_function␈α_and␈α↔function␈α_application.␈α_Since␈α_many␈α↔algorithms
␈↓ α←␈↓compute␈α
functions␈α∞and␈α
since␈α
function␈α∞application␈α
is␈α
simulated␈α∞by␈α
procedure
␈↓ α←␈↓calls,␈α∂the␈α⊂calculus␈α∂is␈α∂well␈α⊂suited␈α∂for␈α∂a␈α⊂purified␈α∂discussion␈α∂of␈α⊂procedures␈α∂in
␈↓ α←␈↓programming␈α∀languages.␈α∀We␈α∀shall␈α∪outline␈α∀the␈α∀␈↓λλ␈↓-calculus␈α∀in␈α∪Section 3.13.
␈↓ α←␈↓The␈α_λ-notation␈α_was␈α_introduced␈α_into␈α_programming␈α_languages␈α_by␈α↔John
␈↓ α←␈↓McCarthy␈α↔in␈α_the␈α↔description␈α_of␈α↔LISP␈α_([McC 60]).␈α↔ There␈α_are␈α↔several
␈↓ α←␈↓important␈α∩distinctions␈α∩between␈α∩Church's␈α∩␈↓λλ␈↓-calculus␈α∩and␈α∩the␈α∪λ-notation␈α∩of
␈↓ α←␈↓McCarthy; we will point out the differences in Section 3.13.
␈↓ α←␈↓␈↓3.4␈↓ λv␈↓αλ␈↓-notation 111␈↓α
␈↓"β␈↓ α←␈↓␈↓ β'We␈α⊃begin␈α⊂the␈α⊃discussion␈α⊂by␈α⊃exemplifying␈α⊂the␈α⊃need␈α⊂for␈α⊃more␈α⊂precise
␈↓ α←␈↓terminology.␈α→ We␈α→have␈α→been␈α_informally␈α→writing␈α→␈↓αf[x;y] <= x*y + y␈↓␈α→as␈α_a
␈↓ α←␈↓definition␈α∃of␈α∃the␈α⊗function␈α∃␈↓αf␈↓.␈α∃This␈α∃notation␈α⊗is␈α∃supposed␈α∃to␈α⊗convey␈α∃the
␈↓ α←␈↓following␈αintent:␈α␈↓αf␈↓␈α
is␈αthe␈αname␈α
of␈αa␈αfunction␈αor␈α
rule;␈αwhenever␈α␈↓αf␈↓␈α
is␈αsupplied
␈↓ α←␈↓with␈αtwo␈αnumeric␈α
arguments␈αit␈αis␈αsupposed␈α
to␈αmultiply␈αthose␈α
arguments␈αand
␈↓ α←␈↓add␈αthe␈αresult␈αto␈αthe␈αsecond.␈αThe␈αresulting␈αsum␈αis␈αthe␈αdesired␈αanswer.␈α Since
␈↓ α←␈↓informality␈α∞is␈α∞susceptible␈α∞to␈α∞ambiguity,␈α∞we␈α∞should␈α∞analyze␈α∞the␈α
"<="-notation
␈↓ α←␈↓more␈αclosely.␈αThough␈α
we␈αsay␈α␈↓αf␈↓␈αis␈α
being␈αdefined,␈αit␈αis␈α
not␈α␈↓αf␈↓,␈αbut␈α
␈↓αf[x;y]␈↓␈αwhich
␈↓ α←␈↓appears␈α∀to␈α∀the␈α∀left␈α∀of␈α∀the␈α∀"<="-symbol.␈α∀ First,␈α∀␈↓αf[x;y]␈↓␈α∀does␈α∀␈↓¬not␈↓␈α∃denote␈α∀a
␈↓ α←␈↓function,␈α∀␈↓αf␈↓␈α∀denotes␈α∪a␈α∀function.␈α∀ To␈α∀see␈α∪what␈α∀␈↓αf[x;y]␈↓␈α∀means␈α∀consider␈α∪the
␈↓ α←␈↓following␈α∞example.␈α
When␈α∞we␈α∞are␈α
asked␈α∞to␈α∞evaluate␈α
␈↓αcar[(A . B)]␈↓␈α∞we␈α∞say␈α
the
␈↓ α←␈↓value␈α∂is␈α∞␈↓αA␈↓.␈α∂ ␈↓αcar[(A . B)]␈↓␈α∞is␈α∂an␈α∂expression␈α∞to␈α∂be␈α∞evaluated;␈α∂we␈α∂have␈α∞called
␈↓ α←␈↓such␈αexpressions␈αLISP␈α
␈↓↓forms␈↓.␈α If␈α␈↓αcar[(A . B)]␈↓␈α
is␈αa␈αform␈α
then␈αso␈αis␈α␈↓αcar[x]␈↓;␈α
only
␈↓ α←␈↓now␈αthe␈αform␈αreferences␈αa␈αvariable␈αinstead␈αof␈αa␈αconstant;␈αtherefore␈αthe␈αvalue
␈↓ α←␈↓of␈αthe␈αform␈αdepends␈αon␈αthe␈αcurrent␈αvalue␈αassigned␈αto␈αthe␈αvariable␈α␈↓αx␈↓.␈α So␈αthe
␈↓ α←␈↓␈↓↓function␈↓␈α
is␈α
␈↓αcar␈↓;␈α
the␈α∞␈↓↓form␈↓␈α
is␈α
␈↓αcar[x]␈↓.␈α
Therefore,␈α
the␈α∞function␈α
is␈α
␈↓αf␈↓;␈α
␈↓αf[x;y]␈↓␈α∞is␈α
a
␈↓ α←␈↓form,␈αand␈αso␈αis␈α
␈↓αx*y + y␈↓.␈α The␈αinformal␈αnotation␈αhas␈α
a␈αform␈αon␈αboth␈α
sides␈αof
␈↓ α←␈↓the␈α
"<=".␈α
We␈α
would␈α
like␈α
a␈αnotation␈α
which␈α
clearly␈α
shows␈α
what␈α
is␈αbeing␈α
defined
␈↓ α←␈↓and what is given.
␈↓"β␈↓ α←␈↓␈↓ β'Further,␈α⊃our␈α⊃notation␈α⊃has␈α⊃really␈α⊂been␈α⊃specifying␈α⊃more␈α⊃than␈α⊃just␈α⊂the
␈↓ α←␈↓name.␈α The␈αnotation␈αspecifies␈αthe␈αformal␈αparameters␈α(␈↓αx␈↓␈αand␈α␈↓αy␈↓)␈αand␈αthe␈αorder
␈↓ α←␈↓in␈α⊂which␈α⊃we␈α⊂are␈α⊂to␈α⊃associate␈α⊂actual␈α⊃parameters␈α⊂in␈α⊂a␈α⊃call␈α⊂with␈α⊃the␈α⊂formal
␈↓ α←␈↓parameters␈α⊃of␈α⊃the␈α∩definition␈α⊃(␈↓αx␈↓␈α⊃with␈α⊃the␈α∩first,␈α⊃␈↓αy␈↓␈α⊃with␈α⊃the␈α∩second).␈α⊃ More
␈↓ α←␈↓subtly,␈α∩the␈α∩notation␈α∩tells␈α∩␈↓¬which␈↓␈α∩variables␈α⊃in␈α∩the␈α∩function␈α∩body␈α∩are␈α∩to␈α⊃be
␈↓ α←␈↓supplied␈α≡values␈α≡when␈α≡the␈α≡function␈α≡is␈α≡called.␈α≡For␈α∨example␈α≡define
␈↓ α←␈↓␈↓αg[x] <= x*y + y␈↓;␈αthen␈αthe␈αexpression␈α␈↓αg[2]␈↓␈αspecifies␈αthat␈α␈↓αx␈↓␈αis␈αto␈αreceive␈αa␈αvalue
␈↓ α←␈↓␈↓α2␈↓, but leaves unspecified what the value of ␈↓αy␈↓ should be.␈↓π 13␈↓
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∂also␈α∂wish␈α∂to␈α∂have␈α∂a␈α∂notation␈α∂so␈α∂that␈α∂function␈α∂definitions␈α⊂can␈α∂be
␈↓ α←␈↓inserted␈α∂into␈α∞the␈α∂symbol␈α∞table␈α∂as␈α∂"values"␈α∞assigned␈α∂to␈α∞names.␈α∂They␈α∂will␈α∞be
␈↓ α←␈↓parametric␈α
values,␈αbut␈α
they␈αwill␈α
be␈αvalues.␈α
The␈αλ-notation␈α
performs␈αthis␈α
task
␈↓ α←␈↓by␈α∞preceding␈α∞the␈α
function␈α∞body␈α∞with␈α
a␈α∞list␈α∞of␈α
variables,␈α∞called␈α∞␈↓↓lambda␈α
list␈↓.
␈↓ α←␈↓The␈α
lambda␈αlist␈α
has␈α
been␈αpreviously␈α
called␈α
the␈αformal␈α
parameter␈α
list;␈αeither
␈↓ α←␈↓term␈α∂is␈α⊂acceptable.␈α∂ Each␈α⊂parameter␈α∂in␈α∂the␈α⊂lambda␈α∂list␈α⊂is␈α∂called␈α⊂a␈α∂lambda
␈↓ α←␈↓variable␈α(or␈αa␈αformal␈αparameter).␈α
The␈αresulting␈αconstruct␈αis␈αpreceded␈αby␈α
"λ["
␈↓ α←␈↓and␈α∩followed␈α∩by␈α∩"]".␈α∩ Using␈α∪the␈α∩above␈α∩example,␈α∩the␈α∩identifier␈α∪␈↓αf␈↓␈α∩denotes
␈↓ α←␈↓exactly␈αthe␈αsame␈αLISP␈αfunction␈αas␈α␈↓αλ[[x;y] x*y + y]␈↓.␈αThe␈αλ-notation␈αintroduces
␈↓ α←␈↓nothing␈α⊂new␈α⊂as␈α⊂far␈α⊂as␈α∂our␈α⊂intuitive␈α⊂binding␈α⊂and␈α⊂evaluation␈α⊂processes␈α∂are
␈↓ α←␈↓concerned;␈αit␈αonly␈αmakes␈αthese␈αoperations␈αmore␈αclear.␈α To␈αanalyze␈αthese␈αideas
␈↓ α←␈↓a␈α≠bit␈α~further,␈α≠notice␈α~that␈α≠␈↓αλ[[x;y] x*y + y]␈↓␈α~is␈α≠the␈α~"same"␈α≠function␈α~as
␈↓ α←␈↓␈↓αλ[[u;v] u*v + v]␈↓.␈α∞This␈α
means␈α∞in␈α
effect␈α∞that␈α
the␈α∞formal␈α
parameters␈α∞are␈α
"place
␈↓ α←␈↓holders"␈αand␈αcan␈αbe␈αuniformly␈α
replaced␈αwith␈αother␈αidentifiers.␈α Notice␈αto␈α
that
␈↓ α←␈↓function names are also place holders.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 13␈↓Note also, that the "values" for + and * are also unspecified.
␈↓ α←␈↓␈↓112 Evaluation␈↓
(3.4␈↓
␈↓"∀␈↓ α←␈↓α␈↓ ¬αone[x] <= [x=0 → 1; ␈↓
t␈↓α → one[x-1]]
␈↓" ␈↓ α←␈↓is the same function as:
␈↓" ␈↓ α←␈↓α␈↓ ¬βfxy[x] <= [x=0 → 1; ␈↓
t␈↓α → fxy[x-1]]
␈↓"∀␈↓ α←␈↓␈↓ β'There␈α∩are␈α∩certain␈α∩restrictions␈α∪on␈α∩the␈α∩replacement␈α∩of␈α∪identifiers;␈α∩the
␈↓ α←␈↓precise␈α∂description␈α∂of␈α∂that␈α⊂algorithm␈α∂requires␈α∂care.␈α∂The␈α⊂implementation␈α∂of
␈↓ α←␈↓that algorithm will be part of this chapter.
␈↓"β␈↓ α←␈↓␈↓ β'One␈α
benefit␈αof␈α
the␈αλ-notation␈α
is␈αthat␈α
we␈αneed␈α
not␈αgive␈α
explicit␈αnames␈α
to
␈↓ α←␈↓functions␈α∪in␈α∪order␈α∪to␈α∪perform␈α∪the␈α∪evaluation.␈α∪Evaluation␈α∀of␈α∪expressions
␈↓ α←␈↓involving␈αsuch␈αanonymous␈αfunctions,␈αalso␈αcalled␈α␈↓↓open␈αlambdas␈↓,␈αis␈αwithin␈αthe
␈↓ α←␈↓province␈αof␈αLISP.␈α Currently,␈αwe␈αwill␈αrestrict␈αour␈αdiscussion␈α
to␈αλ-expressions
␈↓ α←␈↓which␈α∩are␈α∩function␈α∩constants,␈α∩just␈α∩like␈α∩␈↓αA␈↓␈α∩is␈α∩an␈α∩S-expr␈α∩constant.␈α∩Since␈α∩a
␈↓ α←␈↓λ-expression␈α↔is␈α↔a␈α↔constant,␈α↔its␈α↔value␈α↔is␈α↔itself.␈α↔ LISP␈α↔will␈α↔evaluate␈α↔an
␈↓ α←␈↓application␈α∩involving␈α⊃a␈α∩λ-expression␈α⊃in␈α∩two␈α⊃stages;␈α∩first,␈α⊃it␈α∩will␈α∩bind␈α⊃the
␈↓ α←␈↓evaluated␈α
actual␈αparameters␈α
to␈αthe␈α
λ-variables,␈αand␈α
then␈αit␈α
will␈α
evaluate␈αthe
␈↓ α←␈↓function body.
␈↓"β␈↓ α←␈↓␈↓ β'Consider, for example:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ¬Y␈↓αλ[[x;y] x␈↓π2␈↓α + y][2;3] ␈↓
␈↓"∀␈↓ α←␈↓We associate ␈↓α2␈↓ with ␈↓αx␈↓ and ␈↓α3␈↓ with ␈↓αy␈↓ and evaluate the expression:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ε2␈↓αx␈↓π2␈↓α + y␈↓
␈↓"∀␈↓ α←␈↓This calculation will give ␈↓α7␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'To evaluate the more complex:
␈↓"∀␈↓ α←␈↓␈↓ ¬∃␈↓αλ[[x] cdr[car[x]]][((A . B) . C)]␈↓
␈↓"∀␈↓ α←␈↓we␈α∞bind␈α∞␈↓αx␈↓␈α∂to␈α∞the␈α∞S-expression␈α∞␈↓α((A . B). C)␈↓␈α∂and␈α∞evaluate␈α∞the␈α∂function␈α∞body.
␈↓ α←␈↓The␈α
evaluation␈α
procedure␈α
first␈α
evaluates␈α
␈↓αcar[x]␈↓␈α
with␈α
the␈α
current␈α
binding␈αof␈α
␈↓αx␈↓;
␈↓ α←␈↓this result, ␈↓α(A . B)␈↓, is passed to ␈↓αcdr␈↓; and that calculation finally returns ␈↓αB␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αλ-notation␈αcan␈αbe␈αused␈αanywhere␈αLISP␈αexpects␈αto␈αfind␈αa␈αfunction,
␈↓ α←␈↓for example:
␈↓"∀␈↓ α←␈↓␈↓ ∧[␈↓αλ[[x] first[x]]
␈↓"β␈↓ α←␈↓α␈↓ ∧[ [λ[[y] rest[y]][(A B)]]␈↓
␈↓"∀␈↓ α←␈↓This expression equivalent to writing:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ βa␈↓αf[g[(A B)]]␈↓ where ␈↓αf[x] <= first[x] ␈↓and ␈↓αg[y] <= rest[y]␈↓
␈↓"∀␈↓ α←␈↓Though␈α∞the␈α∂second␈α∞form␈α∂is␈α∞perhaps␈α∞easier␈α∂for␈α∞us␈α∂to␈α∞comprehend,␈α∂the␈α∞first
␈↓ α←␈↓form␈α∩␈↓¬is␈↓␈α∩equivalent␈α⊃and␈α∩will␈α∩be␈α⊃acceptable␈α∩to␈α∩the␈α⊃evaluator.␈α∩ In␈α∩fact,␈α⊃the
␈↓ α←␈↓evaluation␈α∪of␈α∀the␈α∪second␈α∀formulation␈α∪will␈α∪effectively␈α∀reduce␈α∪to␈α∀the␈α∪first
␈↓ α←␈↓formulation on its way to final evaluation.
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ βV␈↓αλ[[x] first[x]][λ[[y] rest[y]][(A B)]] = λ[[x] first[x]][(B)] = B␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'LISP␈α
evaluation␈α
requires␈α
care.␈α
For␈α
example␈α
the␈α
LISP␈α
function␈α␈↓αλ[[x]2]␈↓
␈↓ α←␈↓is␈α
␈↓¬not␈↓␈α
the␈αconstant␈α
function␈α
which␈αalways␈α
gives␈α
value␈α␈↓α2␈↓.␈α
The␈α
evaluation␈αof
␈↓ α←␈↓␈↓3.4␈↓ λv␈↓αλ␈↓-notation 113␈↓α
␈↓"β␈↓ α←␈↓an␈α∞expression␈α∞involving␈α∞this␈α∂function␈α∞requires␈α∞the␈α∞evaluation␈α∞of␈α∂the␈α∞actual
␈↓ α←␈↓parameter␈α∪associated␈α∪with␈α∪␈↓αx␈↓.␈α∀ That␈α∪computation␈α∪may␈α∪not␈α∀terminate.␈α∪For
␈↓ α←␈↓example,␈α
consider␈α
␈↓αλ[[x]2][fact[-1]]␈↓␈αwhere␈α
␈↓αfact␈↓␈α
is␈αthe␈α
LISP␈α
implementation␈αof
␈↓ α←␈↓the factorial function given on page 44.
␈↓"β␈↓ α←␈↓␈↓ β'Since␈α∩we␈α∪intend␈α∩to␈α∪include␈α∩λ-expressions␈α∩in␈α∪our␈α∩language␈α∪we␈α∩must
␈↓ α←␈↓include␈α
an␈α
␈↓
R␈↓-mapping␈α
into␈α
S-expression␈α
form␈α
for␈α
them.␈α
The␈α
character␈α
λ␈α
will
␈↓ α←␈↓be␈αtranslated␈αto␈α␈↓αLAMBDA␈↓␈αand␈αthe␈αformal␈αparameters␈αwill␈αbe␈αtranslated␈αinto
␈↓ α←␈↓a list:
␈↓"∀␈↓ α←␈↓␈↓ βo␈↓
R␈↓∞( ␈↓αλ[[x␈↓β1␈↓α; ...; x␈↓βn␈↓α] ␈↓λx␈↓α] ␈↓∞)␈↓α = (LAMBDA (X␈↓β1␈↓α ... X␈↓βn␈↓α) ␈↓
R␈↓∞( ␈↓λx␈↓α ␈↓∞)␈↓α)␈↓
␈↓"∀␈↓ α←␈↓Here are some examples of ␈↓αλ␈↓-expressions and their ␈↓
R␈↓-translations:
␈↓"∀␈↓ α←␈↓␈↓ β%␈↓
R␈↓∞( ␈↓αλ[[x;y] x␈↓π2␈↓α + y] ␈↓∞)␈↓α = (LAMBDA (X Y) (PLUS (EXPT X 2) Y))
␈↓""␈↓ α←␈↓α␈↓ β∂␈↓
R␈↓∞( ␈↓αλ[[x;y] cons[car[x];y]] ␈↓∞)␈↓α = (LAMBDA (X Y) (CONS (CAR X) Y))
␈↓"∀␈↓ α←␈↓␈↓ β'To␈α↔complete␈α_our␈α↔introduction␈α↔of␈α_λ-expressions,␈α↔our␈α_LISP␈α↔syntax
␈↓ α←␈↓equations will be augmented to include:
␈↓"∀␈↓ α←␈↓<function>␈↓ β{::= λ[<varlist><form>]
␈↓" ␈↓ α←␈↓<varlist>␈↓ β{::= [<variable>; ... ; <variable>] ␈↓π 14␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Besides␈α
giving␈α
a␈α
clear␈α
notation␈α
for␈α
function␈α
definitions,␈α∞the␈α
λ-notation
␈↓ α←␈↓is␈α
a␈αuseful␈α
computational␈αdevice.␈α
Consider␈αthe␈α
following␈αsketch␈α
of␈αa␈α
function
␈↓ α←␈↓definition:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ∧o␈↓αg <= λ[[x][␈↓λp␈↓α[lic[x]] → lic[x]; .... x ...]]␈↓
␈↓"∀␈↓ α←␈↓where ␈↓αlic␈↓ may be a ␈↓αl␈↓ong ␈↓αi␈↓nvolved ␈↓αc␈↓alculation, and ␈↓λp␈↓ is a predicate.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∞certainly␈α∞must␈α∞compute␈α∞␈↓αlic[x]␈↓␈α∞␈↓¬once␈↓.␈α∞But␈α∞as␈α∞␈↓αg␈↓␈α∞is␈α∞defined,␈α∞we␈α∞would
␈↓ α←␈↓compute␈α
␈↓αlic[x]␈↓␈α
␈↓¬twice␈↓␈α
if␈α
␈↓↓p␈↓β1␈↓␈α
is␈α
true:␈α
once␈α
in␈α
the␈α
calculation␈α
of␈α
␈↓↓p␈↓β1␈↓,␈α
and␈α
once␈α
as␈α
␈↓↓e␈↓β1␈↓.
␈↓ α←␈↓Since␈α∩both␈α∩calculations␈α∩of␈α∩␈↓αlic[x]␈↓␈α∩will␈α∩give␈α∩the␈α∩same␈α∩value,␈↓π 15␈↓␈α∩this␈α∩second
␈↓ α←␈↓calculation is unnecessary. Instead, we could write:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ¬P␈↓αg <= λ[[x] f[lic[x];x]]␈↓
␈↓" ␈↓ α←␈↓where:␈↓ ¬∀␈↓αf <= λ[[u;v][␈↓λp␈↓α[u] → u; .... v ...]]␈↓
␈↓"∀␈↓ α←␈↓In␈αthis␈αscheme␈α␈↓αlic␈↓␈αwill␈αonly␈αbe␈αevaluated␈αonce;␈αits␈αvalue␈αwill␈αbe␈αpassed␈αinto␈α␈↓αf␈↓.
␈↓ α←␈↓This␈α→solution␈α→requires␈α→introduction␈α→of␈α→a␈α→new␈α→function␈α→name.␈α_ Using
␈↓ α←␈↓λ-expressions,␈α
in␈α
a␈α∞style␈α
called␈α
␈↓↓internal␈α
lambdas␈↓␈α∞we␈α
can␈α
improve␈α∞␈↓αg␈↓␈α
without
␈↓ α←␈↓adding any new function names to our symbol tables.
␈↓"β␈↓ α←␈↓␈↓ β'Replace the body of ␈↓αg␈↓ with:
␈↓"∀␈↓ α←␈↓␈↓↓LAM␈↓ ¬~ ␈↓αλ[[y][␈↓λp␈↓α[y] → y; ... x ...]][lic[x]]␈↓
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 14␈↓Recall␈α∂that␈α∂this␈α∞use␈α∂of␈α∂ellipses␈α∂means␈α∞"zero␈α∂or␈α∂more␈α∂occurrences␈α∞of
␈↓ α←␈↓<variable>".
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 15␈↓Our␈αcurrent␈αLISP␈αsubset␈αhas␈αno␈αside␈αeffects.␈αThat␈αmeans␈αthere␈αis␈αno
␈↓ α←␈↓way␈α∩for␈α∩a␈α∩computation␈α∩to␈α∩affect␈α∩its␈α∩surrounding␈α∩environment.␈α∪The␈α∩most
␈↓ α←␈↓common construct which has a side-effect is the assignment statement.
␈↓ α←␈↓␈↓114 Evaluation␈↓
(3.4␈↓
␈↓"β␈↓ α←␈↓Call this new function ␈↓αg␈↓λ'␈↓:
␈↓"∀␈↓ α←␈↓␈↓ ∧?␈↓αg␈↓λ'␈↓α <= λ[[x] λ[[y][␈↓λp␈↓α[y] → y; ... x ... ]][lic[x]] ] ␈↓
␈↓"∀␈↓ α←␈↓Now␈αwhen␈α␈↓αg␈↓λ'␈↓␈αis␈αcalled␈αwe␈αevaluate␈αthe␈αactual␈αparameter,␈αbinding␈αit␈αto␈α␈↓αx␈↓,␈α
and
␈↓ α←␈↓evaluate␈α∂␈↓↓LAM␈↓.␈α∂Evaluation␈α∂of␈α⊂␈↓↓LAM␈↓␈α∂involves␈α∂only␈α∂one␈α∂calculation␈α⊂of␈α∂␈↓αlic[x]␈↓,
␈↓ α←␈↓binding␈α∀the␈α∪result␈α∀to␈α∪␈↓αy␈↓.␈α∀We␈α∪then␈α∀evaluate␈α∪the␈α∀body␈α∪of␈α∀the␈α∪conditional
␈↓ α←␈↓expression␈α∞as␈α∞before.␈α∞ If␈α∞␈↓↓p␈↓β1␈↓␈α∞␈↓¬is␈↓␈α∂true,␈α∞then␈α∞this␈α∞definition␈α∞of␈α∞␈↓αg␈↓λ'␈↓␈α∂involves␈α∞one
␈↓ α←␈↓calculation␈α
of␈α
␈↓αlic[x]␈↓␈α
and␈α
two␈α
table␈α
look-ups␈α
(for␈α
the␈α
value␈α
of␈α
␈↓αy␈↓),␈α
rather␈αthan
␈↓ α←␈↓the␈α_two␈α→calculations␈α_of␈α_␈↓αlic[x]␈↓␈α→in␈α_␈↓αg␈↓.␈α_ More␈α→conventional␈α_programming
␈↓ α←␈↓languages␈α⊃can␈α⊃obtain␈α⊃the␈α⊃same␈α∩effect␈α⊃as␈α⊃this␈α⊃use␈α⊃of␈α⊃internal␈α∩lambdas␈α⊃by
␈↓ α←␈↓assignment␈α∞of␈α∞␈↓αlic[x]␈↓␈α∂to␈α∞a␈α∞temporary␈α∂variable.␈α∞We␈α∞will␈α∂introduce␈α∞assignment
␈↓ α←␈↓statements in LISP in Section 4.2.␈↓π 16␈↓
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. What is the difference between ␈↓αλ[[ ] x*y + y]␈↓ and ␈↓αx*y + y␈↓ ?
␈↓"β␈↓ α←␈↓␈↓ ∧y␈↓↓3.5 Mechanization of Evaluation␈↓
␈↓"β␈↓ α←␈↓We␈α∂first␈α∂gave␈α∂plausibility␈α∂arguments␈α⊂for␈α∂the␈α∂existence␈α∂of␈α∂an␈α⊂evaluator␈α∂for
␈↓ α←␈↓LISP;␈α⊃and␈α⊃then␈α∩picked␈α⊃a␈α⊃representation␈α∩for␈α⊃LISP␈α⊃expressions;␈α∩finally␈α⊃we
␈↓ α←␈↓introduced␈α
a␈αprecise␈α
notation␈αfor␈α
discussing␈α
functions.␈α It␈α
is␈αnow␈α
time␈αto␈α
write
␈↓ α←␈↓an␈αevaluator␈αfor␈αrepresentations␈αof␈αLISP␈αexpressions.␈α The␈αevaluator␈αwill␈αbe
␈↓ α←␈↓the␈α∞final␈α∞arbiter␈α∞on␈α∞the␈α∞question␈α∞of␈α∞the␈α∞meaning␈α∞of␈α∞a␈α∞LISP␈α∞construct.␈α∞The
␈↓ α←␈↓evaluator␈α⊃is␈α⊃thus␈α⊃a␈α⊃very␈α⊃important␈α⊃algorithm.␈α⊃We␈α⊃will␈α⊃express␈α⊃it␈α⊃and␈α⊂its
␈↓ α←␈↓related␈α↔functions␈α↔in␈α↔a␈α↔representation-free␈α⊗form,␈α↔but␈α↔we␈α↔will␈α↔keep␈α⊗our
␈↓ α←␈↓Cambridge Polish representation in mind.
␈↓"β␈↓ α←␈↓␈↓ β'As␈α≠we␈α≠have␈α≠discovered,␈α≠the␈α≠great␈α≠progenitors␈α≤(Section 2.8)␈α≠are
␈↓ α←␈↓evaluators␈αfor␈αsubsets␈αof␈α
LISP.␈α With␈αour␈αsymbol-table␈αmechanism␈α
we␈αcould
␈↓ α←␈↓now␈α∂extend␈α∞those␈α∂algorithms␈α∞to␈α∂handle␈α∞variable␈α∂look-ups.␈α∞ Rather␈α∂than␈α∞do
␈↓ α←␈↓this␈α∩we␈α⊃will␈α∩make␈α∩a␈α⊃total␈α∩revision␈α⊃of␈α∩the␈α∩structure␈α⊃of␈α∩the␈α∩evaluators.␈α⊃In
␈↓ α←␈↓making the revision, the following points should be remembered:
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 16␈↓This␈αtechnique␈αis␈αalso␈αrelated␈αto␈αthe␈αideas␈αof␈αcommon␈α
sub-expression
␈↓ α←␈↓recognition in compiling algorithms (Section 6.16).
␈↓ α←␈↓␈↓3.5␈↓ π↔Mechanization of Evaluation 115␈↓
␈↓"λ␈↓ α←␈↓␈↓↓1.␈↓ Expressions␈α
to␈α
be␈α
evaluated␈α∞can␈α
contain␈α
variables,␈α
both␈α∞simple␈α
variables
␈↓ α←␈↓␈↓ β∂and␈αvariables␈αnaming␈αλ-expressions.␈αTherefore,␈αevaluation␈αmust␈αbe␈αdone
␈↓ α←␈↓␈↓ β∂with␈α∂respect␈α∂to␈α∂an␈α∂environment␈α⊂or␈α∂symbol␈α∂table.␈α∂We␈α∂wish␈α⊂to␈α∂recognize
␈↓ α←␈↓␈↓ β∂other␈αfunction␈αnames␈αbesides␈α␈↓αCAR,␈αCDR,␈αCONS,␈αEQ,␈↓␈αand␈↓α␈αATOM␈↓␈αin␈αour
␈↓ α←␈↓␈↓ β∂evaluator,␈α∂but␈α∞explicitly␈α∂adding␈α∞new␈α∂definitions␈α∞to␈α∂the␈α∞evaluator␈α∂in␈α∞the
␈↓ α←␈↓␈↓ β∂style␈α∃of␈α∀the␈α∃recognizers␈α∀for␈α∃the␈α∀five␈α∃primitives␈α∀is␈α∃not␈α∃an␈α∀attractive
␈↓ α←␈↓␈↓ β∂approach.␈α That␈α
scheme␈αwould␈α
require␈αrewriting␈α
sections␈αof␈αthe␈α
evaluator
␈↓ α←␈↓␈↓ β∂every␈αtime␈αa␈αnew␈αdefinition␈αwas␈αintroduced.␈αAn␈αalternative␈αsolution␈αis␈αto
␈↓ α←␈↓␈↓ β∂hold␈αthe␈αdefinitions␈αin␈αa␈αsymbol␈αtable.␈αOur␈αsymbol␈αtable␈αshould␈αhold␈αthe
␈↓ α←␈↓␈↓ β∂function␈α
definitions␈α
and␈α
the␈α
evaluator␈α
should␈α
contain␈α
the␈αgeneral␈α
schemes
␈↓ α←␈↓␈↓ β∂for␈α∂finding␈α∞the␈α∂definitions,␈α∞binding␈α∂variables␈α∞to␈α∂values,␈α∂and␈α∞evaluating
␈↓ α←␈↓␈↓ β∂the function body.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ All␈α
␈↓↓function␈↓␈α
calls␈α
are␈α
to␈α
be␈α
evaluated␈α
"by-value."␈α
However,␈α
there␈α
are␈α
some
␈↓ α←␈↓␈↓ β∂␈↓↓special␈α≠forms␈↓␈α≠which␈α≠are␈α≤not␈α≠evaluated␈α≠in␈α≠the␈α≤normal␈α≠manner.
␈↓ α←␈↓␈↓ β∂Conditional␈αexpressions,␈αquoted␈αexpressions,␈αand␈αlambda␈αexpressions␈αare
␈↓ α←␈↓␈↓ β∂handled␈α∪differently,␈α∪and␈α∪the␈α∪evaluator␈α∪will␈α∪recognize␈α∪these␈α∩constructs
␈↓ α←␈↓␈↓ β∂specially.
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α
primary␈α
algorithm␈α
in␈α
the␈α
evaluator␈α
will␈α
be␈α
named␈α
␈↓αeval␈↓.␈α
It␈α
will␈α
take
␈↓ α←␈↓two␈α∩arguments;␈α∩the␈α⊃first␈α∩will␈α∩be␈α⊃a␈α∩representation␈α∩of␈α⊃an␈α∩expression␈α∩to␈α⊃be
␈↓ α←␈↓evaluated,␈α∂and␈α∂the␈α∂second␈α∂will␈α∂be␈α∂a␈α∂representation␈α∂of␈α∂a␈α∂symbol␈α∂table.␈α∞The
␈↓ α←␈↓evaluator␈α∩will␈α∩recognize␈α∩numbers,␈α∩and␈α⊃the␈α∩constants␈α∩␈↓αT␈↓␈α∩and␈α∩␈↓αNIL␈↓,␈α∩and␈α⊃if
␈↓ α←␈↓presented␈α
with␈α∞a␈α
variable,␈α
will␈α∞attempt␈α
to␈α∞find␈α
the␈α
value␈α∞of␈α
the␈α∞variable␈α
in
␈↓ α←␈↓the symbol table using ␈↓αassoc␈↓ (Section 3.3).
␈↓"β␈↓ α←␈↓␈↓ β'␈↓αeval␈↓␈α∂will␈α⊂also␈α∂recognize␈α⊂the␈α∂special␈α⊂forms␈α∂␈↓αcond␈↓␈α⊂and␈α∂␈↓αquote␈↓.␈α⊂When␈α∂␈↓αeval␈↓
␈↓ α←␈↓recognizes␈αa␈αconditional␈αexpression␈α(represented␈αby␈α␈↓α(COND␈α...)␈↓␈α),␈αthe␈αbody␈αof
␈↓ α←␈↓the␈α∞␈↓αCOND␈↓␈α∞will␈α∞be␈α∞passed␈α∞to␈α∞a␈α∞subfunction␈α∞named␈α∞␈↓αevcond␈↓.␈α∞␈↓αevcond␈↓␈α
embodies
␈↓ α←␈↓the␈α≤conditional␈α≠expression␈α≤semantics␈α≠as␈α≤described␈α≠on␈α≤page 19.␈α≠ The
␈↓ α←␈↓representation,␈α␈↓α(QUOTE ␈↓λα␈↓α)␈↓,␈α
signifies␈αthe␈α
occurrence␈αof␈α
a␈αconstant,␈α
␈↓λα␈↓,␈αwhich␈α
is
␈↓ α←␈↓simply␈α∂returned.␈α∂ Any␈α∞other␈α∂expression␈α∂is␈α∞a␈α∂call-by-value␈α∂application.␈α∞ The
␈↓ α←␈↓argument-list␈α⊂evaluation␈α⊂is␈α⊂handled␈α⊂by␈α⊂␈↓αevlis␈↓␈α⊂in␈α⊂the␈α⊂authorized␈α∂left-to-right
␈↓ α←␈↓ordering.␈αThis␈αcalculation␈α
is␈αperformed␈αby␈α
recurring␈αon␈αthe␈α
list␈αrepresenting
␈↓ α←␈↓the␈α∀arguments.␈α∪ Finally,␈α∀we␈α∪␈↓↓apply␈↓␈α∀the␈α∪function␈α∀to␈α∪the␈α∀list␈α∀of␈α∪evaluated
␈↓ α←␈↓arguments. This is done by the function ␈↓αapply␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'With␈α⊃this␈α∩introduction␈α⊃we␈α∩will␈α⊃now␈α⊃write␈α∩a␈α⊃more␈α∩general␈α⊃evaluator
␈↓ α←␈↓which will handle a larger subset of LISP than the ␈↓αtgm␈↓s.
␈↓"β␈↓ α←␈↓α␈↓Here's the new ␈↓αeval␈↓:␈↓α
␈↓"∀␈↓ α←␈↓αeval <= λ[[exp;environ]
␈↓"β␈↓ α←␈↓α␈↓ βW[isconst[exp] → denote[exp];
␈↓"β␈↓ α←␈↓α␈↓ βW isvar[exp] → lookup[exp;environ];
␈↓"β␈↓ α←␈↓α␈↓ βW iscond[exp] → evcond[arg␈↓βc␈↓α[exp];environ];
␈↓"β␈↓ α←␈↓α␈↓ βW isfunc+args[exp] → apply[␈↓ ε3func[exp];
␈↓"β␈↓ α←␈↓α␈↓ βW␈↓ ε3evlis[arglist[exp];environ];
␈↓"β␈↓ α←␈↓α␈↓ βW␈↓ ε3environ] ]]
␈↓ α←␈↓␈↓116 Evaluation␈↓
(3.5␈↓
␈↓"β␈↓ α←␈↓α␈↓and:␈↓α
␈↓"β␈↓ α←␈↓αlookup <=λ[[var;env] value[assoc[var;env]]]
␈↓"β␈↓ α←␈↓αdenote <= λ[[exp]␈↓ ∧C[isnumber[exp] → exp;
␈↓"β␈↓ α←␈↓α␈↓ ∧C istruth[exp] → exp;
␈↓"β␈↓ α←␈↓α␈↓ ∧C isfalse[exp] → exp;
␈↓"β␈↓ α←␈↓α␈↓ ∧C issexpr[exp] → rep[exp];
␈↓"β␈↓ α←␈↓α␈↓ ∧C islambda[exp] → exp ]]
␈↓"∀␈↓ α←␈↓where:
␈↓"∀␈↓ α←␈↓␈↓αrep␈↓␈αknows␈αhow␈αto␈αextract␈αthe␈αS-expr␈αfrom␈αthe␈αrepresentation.␈αIn␈αour␈αscheme
␈↓ α←␈↓␈↓ β'the selector ␈↓αrep␈↓ is given by ␈↓αcadr␈↓.
␈↓"β␈↓ α←␈↓The␈α∂other␈α⊂selectors,␈α∂constructors␈α⊂and␈α∂recognizers␈α⊂which␈α∂relate␈α⊂this␈α∂abstract
␈↓ α←␈↓definition␈α∃to␈α∃our␈α∃particular␈α∃S-expression␈α∃representation␈α∃are␈α∃grouped␈α∀on
␈↓ α←␈↓page 117.
␈↓"∀␈↓ α←␈↓αevcond <= λ[␈↓ ∧π[e;environ]
␈↓"β␈↓ α←␈↓α␈↓ ∧π[eval[ante[first[e]];environ] → eval[conseq[first[e]];environ];
␈↓"β␈↓ α←␈↓α␈↓ ∧π ␈↓
t␈↓α → evcond[rest[e];environ] ]]
␈↓"β␈↓ α←␈↓α␈↓and,␈↓α
␈↓"β␈↓ α←␈↓αevlis <= λ[[e;environ]␈↓ ∧s[null[e] → ( );
␈↓"β␈↓ α←␈↓α␈↓ ∧s ␈↓
t␈↓α → concat[␈↓ εβeval[first[e];environ];
␈↓"β␈↓ α←␈↓α␈↓ ∧s␈↓ εβevlis[rest[e];environ]] ]]
␈↓"∀␈↓ α←␈↓The␈α∂subfunctions,␈α∞␈↓αevcond␈↓␈α∂and␈α∂␈↓αevlis␈↓,␈α∞are␈α∂simple.␈α∞ ␈↓αevcond␈↓␈α∂appeared␈α∂before␈α∞in
␈↓ α←␈↓␈↓αtgmoafr␈↓␈α∞in␈α∞a␈α∞less␈α∞abstract␈α∞form;␈α∞␈↓αevlis␈↓␈α∞constructs␈α∞a␈α∞new␈α∞list␈α∞consisting␈α∞of␈α∞the
␈↓ α←␈↓results␈α∞of␈α∞evaluating␈α∂the␈α∞elements␈α∞of␈α∂␈↓αe␈↓␈α∞from␈α∞left␈α∂to␈α∞right,␈α∞using␈α∂the␈α∞symbol
␈↓ α←␈↓table,␈α∞␈↓αenviron␈↓,␈α∞where␈α∂necessary.␈α∞ Since␈α∞␈↓αevcond␈↓␈α∂and␈α∞␈↓αevlis␈↓␈α∞are␈α∂LISP␈α∞functions,
␈↓ α←␈↓␈↓¬they␈↓␈α
are␈α
subject␈α
to␈α
the␈α
left-to-right␈α
evaluation␈α
rule.␈α
Thus␈α
␈↓αevlis␈↓␈α
embodies␈α
the
␈↓ α←␈↓left-to-right␈αrule.␈α If␈α
␈↓αevlis␈↓␈αwere␈αevaluated␈αunder␈α
a␈αright-to-left␈αrule␈α
then␈α␈↓αevlis␈↓
␈↓ α←␈↓would␈α⊂evaluate␈α⊂expressions␈α⊂in␈α⊂right-to-left␈α⊂order.␈α⊂ It␈α⊂is␈α⊂possible␈α⊂to␈α⊂write␈α⊂a
␈↓ α←␈↓version␈α⊂of␈α∂␈↓αevlis␈↓␈α⊂which␈α⊂only␈α∂depends␈α⊂on␈α⊂being␈α∂evaluated␈α⊂␈↓↓CBV␈↓,␈α⊂and␈α∂which
␈↓ α←␈↓does embody the left-to-right rule:
␈↓"∀␈↓ α←␈↓αevlis <= λ[[e;environ]␈↓ ∧s[null[e] → ( );
␈↓"β␈↓ α←␈↓α␈↓ ∧s ␈↓
t␈↓α → λ[[x] concat[x;evlis[rest[e];environ]]]
␈↓"β␈↓ α←␈↓α␈↓ ∧s␈↓ ε∂[eval[first[e];environ]] ]]
␈↓"∀␈↓ α←␈↓␈↓ β'To␈αcontinue,␈αthe␈αfunction␈α␈↓αapply␈↓␈αtakes␈αthree␈αarguments:␈αa␈α
representation
␈↓ α←␈↓of␈α~a␈α≠function,␈α~a␈α≠representation␈α~of␈α~the␈α≠evaluated␈α~arguments,␈α≠and␈α~a
␈↓ α←␈↓representation␈α"of␈α!a␈α"symbol␈α"table.␈α!␈↓αapply␈↓␈α"explicitly␈α"recognizes␈α!the
␈↓ α←␈↓representations␈α∞of␈α∞the␈α∞five␈α∞primitive␈α
functions␈α∞␈↓αCAR,␈α∞CDR,␈α∞CONS,␈α∞EQ,␈α
␈↓and␈↓α
␈↓ α←␈↓αATOM␈↓.␈α∞If␈α∞the␈α∞function␈α∂name␈α∞is␈α∞a␈α∞variable,␈α∂the␈α∞definition␈α∞is␈α∞located␈α∂in␈α∞the
␈↓ α←␈↓␈↓3.5␈↓ π↔Mechanization of Evaluation 117␈↓
␈↓"β␈↓ α←␈↓symbol␈α
table␈αby␈α
␈↓αeval␈↓␈α
and␈αapplied␈α
to␈α
the␈αarguments.␈α
Otherwise␈α
the␈αfunction
␈↓ α←␈↓must␈α∂be␈α∞a␈α∂λ-expression.␈α∂ Things␈α∞now␈α∂get␈α∂interesting;␈α∞we␈α∂must␈α∂evaluate␈α∞the
␈↓ α←␈↓body␈α↔of␈α⊗the␈α↔λ-expression␈α↔after␈α⊗binding␈α↔the␈α⊗formal␈α↔parameters␈α↔of␈α⊗the
␈↓ α←␈↓λ-expression␈αto␈α
the␈αevaluated␈α
arguments.␈α We␈α
add␈αvariable-value␈α
pairs␈αto␈α
the
␈↓ α←␈↓front␈α∂of␈α∞the␈α∂current␈α∞symbol␈α∂table.␈α∂We␈α∞will␈α∂define␈α∞a␈α∂subfunction,␈α∂␈↓αmkenv␈↓,␈α∞to
␈↓ α←␈↓perform␈αthe␈αbinding;␈αthen␈αpass␈αthe␈αfunction␈αbody␈αand␈αthe␈αnew␈α
symbol␈αtable
␈↓ α←␈↓to ␈↓αeval␈↓.
␈↓"β␈↓ α←␈↓α␈↓Here is ␈↓αapply␈↓:␈↓α
␈↓"∀␈↓ α←␈↓αapply <= λ[␈↓ β{[fn;args,environ]
␈↓"β␈↓ α←␈↓α␈↓ β{[iscar[fn] → car[arg␈↓β1␈↓α[args]];
␈↓"β␈↓ α←␈↓α␈↓ β{ iscons[fn] → cons[arg␈↓β1␈↓α[args];arg␈↓β2␈↓α[args]];
␈↓"β␈↓ α←␈↓α␈↓ β{ ... ...
␈↓"β␈↓ α←␈↓α␈↓ β{ isvar[fn] → apply[eval[fn;environ];args;environ];
␈↓"β␈↓ α←␈↓α␈↓ β{ islambda[fn] → eval[␈↓ ε∂body[fn];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ε∂mkenv[vars[fn];args;environ]] ]]
␈↓"∀␈↓ α←␈↓αmkenv <= λ[[vars;vals;environ] pairlis[vars;vals;environ]]
␈↓"∀␈↓ α←␈↓αpairlis <= λ[[vars;vals;environ]
␈↓"β␈↓ α←␈↓α␈↓ ∧π[null[vars] → environ;
␈↓"β␈↓ α←␈↓α␈↓ ∧π ␈↓
t␈↓α → concat[␈↓ ¬↔mkent[first[vars];first[vals]];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ¬↔pairlis[␈↓ ¬wrest[vars];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ¬↔␈↓ ¬wrest[vals];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ¬↔␈↓ ¬wenviron]] ]]
␈↓"∀␈↓ α←␈↓␈↓ β'Some␈α∂of␈α∂the␈α∂functions␈α∂and␈α∞predicates␈α∂which␈α∂will␈α∂relate␈α∂these␈α∞abstract
␈↓ α←␈↓definitions␈αto␈αour␈αspecific␈αS-expression␈αrepresentation␈αof␈αLISP␈αconstructs␈αare
␈↓ α←␈↓given below.
␈↓"∀␈↓ α←␈↓↓␈↓ β∂Recognizers␈↓ ¬←␈↓ π∪Selectors
␈↓"β␈↓ α←␈↓αiscar <= λ[[x] eq[x;CAR]]␈↓ ¬←␈↓ π∪func <= λ[[x] first[x]]
␈↓"β␈↓ α←␈↓αisSexpr <= λ[[x] eq[first[x];QUOTE]]␈↓ π∪arglist <= λ[[x] rest[x]]
␈↓"β␈↓ α←␈↓αistruth <= λ[[x] eq[x;T]]␈↓ ¬←␈↓ π∪body <= λ[[x] third[x]]
␈↓"β␈↓ α←␈↓αislambda <= λ[[x] eq[first[x];LAMBDA]]␈↓ π∪vars <= λ[[x] second[x]]
␈↓"β␈↓ α←␈↓αisfun+args <= λ[[x] ␈↓
t␈↓α]␈↓ ¬←␈↓ π∪args␈↓βc␈↓α <= λ[[x] rest[x]]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ¬←␈↓ π∪arg␈↓β1␈↓α <= λ[[x] first[x]]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ¬←␈↓ π∪arg␈↓β2␈↓α <= λ[[x] second[x]]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ¬←␈↓ π∪ante <= λ[[x] first[x]]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ¬←␈↓ π∪conseq <= λ[[x] second[x]]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ¬←␈↓ π∪rep <= λ[[x] second[x]]
␈↓"β␈↓ α←␈↓α␈↓ ελ␈↓↓Constructor␈↓
␈↓"∀␈↓ α←␈↓α␈↓ ¬,mkent <= λ[[x;y] cons[x;y]]␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Another␈αapplication␈αof␈αthe␈α
left-to-right␈αproperty␈αoccurs␈αwithin␈α␈↓αapply␈↓,␈α
in
␈↓ α←␈↓the␈α⊃symbol␈α⊂table␈α⊃search␈α⊃and␈α⊂construction␈α⊃process.␈α⊂Notice␈α⊃that␈α⊃␈↓αlookup␈↓␈α⊂uses
␈↓ α←␈↓␈↓118 Evaluation␈↓
(3.5␈↓
␈↓"β␈↓ α←␈↓␈↓αassoc␈↓␈αto␈αlook␈αfrom␈αleft␈αto␈αright␈αfor␈αthe␈αlatest␈αbinding␈αof␈αa␈αvariable.␈αThus␈αthe
␈↓ α←␈↓function␈α
which␈α␈↓¬augments␈↓␈α
the␈αtable␈α
must␈α
add␈αthe␈α
latest␈αbinding␈α
to␈α
the␈α␈↓¬front␈↓.
␈↓ α←␈↓New␈α∀bindings␈α∀occur␈α∪when␈α∀the␈α∀function␈α∪␈↓αmkenv␈↓,␈α∀using␈α∀␈↓αpairlis␈↓,␈α∀builds␈α∪an
␈↓ α←␈↓augmented␈α∃symbol␈α∃table␈α∀with␈α∃the␈α∃λ-variables␈α∀bound␈α∃to␈α∃their␈α∀evaluated
␈↓ α←␈↓arguments.␈α∂The␈α∂functions␈α∂␈↓αlookup␈↓␈α⊂and␈α∂␈↓αmkenv␈↓␈α∂operate␈α∂together.␈α∂We␈α⊂will␈α∂see
␈↓ α←␈↓representations␈α∂of␈α∂these␈α∂functions␈α∂other␈α∂than␈α∂␈↓αassoc␈↓␈α∂and␈α∂␈↓αpairlis␈↓.␈α∂The␈α∂actual
␈↓ α←␈↓search␈α∞and␈α∞construction␈α∞operations␈α∞will␈α∞change,␈α∞but␈α∞the␈α∞critical␈α∞relationship
␈↓ α←␈↓that␈α∩␈↓αmkenv␈↓␈α∩always␈α⊃builds␈α∩a␈α∩table␈α∩compatible␈α⊃with␈α∩the␈α∩search␈α∩strategy␈α⊃of
␈↓ α←␈↓␈↓αlookup␈↓ will be maintained.
␈↓"β␈↓ α←␈↓␈↓ β'To␈αsummarize␈αthen:␈αthe␈αevaluation␈αof␈αan␈αexpression␈α␈↓αf[a␈↓β1␈↓α; ... ;a␈↓βn␈↓α]␈↓,␈αwhere
␈↓ α←␈↓the␈α≠␈↓αa␈↓βi␈↓'s␈α~are␈α≠S-exprs,␈α~consists␈α≠in␈α~applying␈α≠␈↓αeval␈↓␈α~to␈α≠the␈α~␈↓
R␈↓-translation,
␈↓ α←␈↓␈↓α(␈↓
R␈↓∞(␈↓α f ␈↓∞)␈↓α ␈↓
R␈↓∞(␈↓α a␈↓β1 ␈↓∞)␈↓α ... ␈↓
R␈↓∞(␈↓α a␈↓βn ␈↓∞)␈↓α).␈↓␈α∂This␈α∂behavior␈α∂is␈α∞again␈α∂an␈α∂example␈α∂of␈α∞the
␈↓ α←␈↓diagrams␈α
of␈α
page 56.␈α
In␈α
its␈α∞most␈α
simple␈α
terms,␈α
we␈α
mapped␈α∞LISP␈α
evaluation
␈↓ α←␈↓onto␈α∞the␈α∂LISP␈α∞␈↓αeval␈↓␈α∂function;␈α∞mapped␈α∂LISP␈α∞expressions␈α∂onto␈α∞S-expressions;
␈↓ α←␈↓and␈α
executed␈α
␈↓αeval␈↓.␈α
Notice␈α
that␈α
in␈α
this␈α
case␈α
we␈α
do␈α
not␈α
reinterpret␈α
the␈αoutput
␈↓ α←␈↓since␈α∃the␈α∃structure␈α∃of␈α∃the␈α∃representation␈α∃does␈α∃this␈α∃implicitly.␈α⊗We␈α∃have
␈↓ α←␈↓commented on the efficacy of this already on page 109.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αspecification␈αof␈αthe␈αevaluation␈αof␈αLISP␈αexpressions␈αusing␈α␈↓αeval␈↓␈αand
␈↓ α←␈↓␈↓αapply␈↓ is one of the most interesting developments of computer science.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Compare␈α∀our␈α∃version␈α∀of␈α∃␈↓αeval␈↓␈α∀and␈α∃␈↓αapply␈↓␈α∀with␈α∃the␈α∀version␈α∃given␈α∀in
␈↓ α←␈↓␈↓ β∂[McC 65].␈α
Though␈α
the␈αcurrent␈α
version␈α
is␈αmuch␈α
more␈α
readable,␈αhow␈α
much
␈↓ α←␈↓␈↓ β∂of␈αit␈α
␈↓¬still␈↓␈αdepends␈α
on␈αthe␈αrepresentation␈α
we␈αchose?␈α
That␈αis,␈α
how␈αabstract
␈↓ α←␈↓␈↓ β∂is it really?
␈↓" ␈↓ α←␈↓2. Complete the specification of the selectors, constructors, and recognizers.
␈↓"β␈↓ α←␈↓␈↓ ¬H␈↓↓3.6 Examples of ␈↓αeval␈↓↓␈↓α
␈↓"β␈↓ α←␈↓We␈α∂will␈α∂demonstrate␈α∂the␈α∂inner␈α∂workings␈α∂of␈α∂the␈α∂evaluation␈α∂algorithm␈α⊂on␈α∂a
␈↓ α←␈↓couple␈αof␈αsamples␈αand␈αwill␈αdescribe␈αthe␈αflow␈αof␈αcontrol␈αin␈αthe␈αexecution␈αin␈αa
␈↓ α←␈↓couple␈αof␈α
different␈αways.␈α
The␈αexamples␈αwill␈α
be␈αdone␈α
in␈αterms␈α
of␈αthe␈αimage␈α
of
␈↓ α←␈↓the␈α∩␈↓
R␈↓-mapping␈α∩rather␈α∩than␈α∩being␈α⊃done␈α∩abstractly.␈α∩We␈α∩do␈α∩this␈α∩since␈α⊃the
␈↓ α←␈↓structure␈α⊂of␈α⊂an␈α⊂actual␈α⊂LISP␈α⊂evaluator␈α⊂will␈α⊂use␈α⊂this␈α⊂representation.␈↓π 17␈↓␈α⊃It␈α⊂is
␈↓ α←␈↓important␈α
that␈αyou␈α
diligently␈α
study␈αthe␈α
sequence␈α
of␈αevents␈α
in␈α
the␈αexecution␈α
of
␈↓ α←␈↓the evaluator. The process is detailed, but it must be done at least once.
␈↓"β␈↓ α←␈↓␈↓ β'Let's evaluate ␈↓αf[2;3]␈↓ where ␈↓αf <= λ[[x;y] x␈↓π2␈↓α + y].␈↓ That is, evaluate:
␈↓"∀␈↓ α←␈↓␈↓ β␈␈↓αeval[ ␈↓
R␈↓∞(␈↓α f[2;3] ␈↓∞)␈↓α; ␈↓
R␈↓∞(␈↓α{ <f, λ[[x;y] +[↑[x;2]; y]]> }␈↓∞)␈↓α]␈↓
␈↓"∀␈↓ α←␈↓After appropriate translation this is equivalent to evaluating:
␈↓"∀␈↓ α←␈↓␈↓ β2␈↓αeval[(F 2 3); ((F . (LAMBDA (X Y) (PLUS (EXPT X 2) Y))))]␈↓
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 17␈↓Recall that we will be programming in the ␈↓
R␈↓-image.
␈↓ α←␈↓␈↓3.6␈↓ λ-Examples of ␈↓αeval␈↓ 119␈↓α
␈↓"β␈↓ α←␈↓␈↓↓Notes:␈↓
␈↓"λ␈↓ α←␈↓␈↓↓1.␈↓␈α∩␈↓α((F␈α⊃.␈α∩(LAMBDA␈α∩(X␈α⊃Y)␈α∩...␈α∩)))␈α⊃=␈α∩((F␈α∩LAMBDA␈α⊃(X␈α∩Y)␈α∩...␈α⊃))␈α∩␈↓␈α∩This␈α⊃is
␈↓ α←␈↓␈↓ β∂mentioned␈α
because␈α
most␈α
LISP␈αimplementations␈α
will␈α
print␈α
the␈α
latter␈αeven
␈↓ α←␈↓␈↓ β∂if you write the former.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓␈αSince␈αthe␈αsymbol␈αtable␈α␈↓α((F␈α...))␈↓␈αoccurs␈αso␈αfrequently␈αin␈αthe␈αfollowing␈αtrace,
␈↓ α←␈↓␈↓ β∂we␈α
will␈α
abbreviate␈α
it␈α
as␈α
␈↓↓st␈↓.␈α
We␈α
have␈α
no␈α
mechanism␈α
yet␈α
for␈αpermanently
␈↓ α←␈↓␈↓ β∂increasing␈α
the␈α
repertoire␈α
of␈αknown␈α
functions.␈α
We␈α
must␈α
therefore␈αresort␈α
to
␈↓ α←␈↓␈↓ β∂subterfuge and initialize the symbol table to get ␈↓αf␈↓ defined.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓␈αFor␈αthis␈α
example␈αwe␈αmust␈α
assume␈αthat␈α+␈α
and␈α↑␈α(exponentiation)␈αare␈α
known
␈↓ α←␈↓␈↓ β∂functions.␈α Thus␈α␈↓αapply␈↓␈αwould␈αhave␈αto␈αcontain␈αrecognizers␈αfor␈α␈↓αPLUS␈↓␈αand
␈↓ α←␈↓␈↓ β∂␈↓αTIMES␈↓:
␈↓"∀␈↓ α←␈↓α␈↓ βK. . . atom[fn] → [␈↓ ¬#isplus[fn] → +[arg␈↓β1␈↓α[args];arg␈↓β2␈↓α[args]];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ¬#isexpt[fn] → ↑[arg␈↓β1␈↓α[args];arg␈↓β2␈↓α[args]];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ¬# . . . ]
␈↓"β␈↓ α←␈↓α␈↓ βK . . .
␈↓ α←␈↓␈↓120 Evaluation␈↓
(3.6␈↓
␈↓"β␈↓ α←␈↓So ␈↓αeval[(F 2 3);␈↓↓st␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ β∂= apply[␈↓ ∧πfunc[(F 2 3)];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧πevlis[arglist[(F 2 3)];␈↓↓st␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓↓st␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ β∂= apply[F;evlis[(2 3);␈↓↓st␈↓α];␈↓↓st␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ β∂= apply[F;(2 3);␈↓↓st␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ β∂= apply[␈↓ ∧πeval[F;␈↓↓st␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π(2 3);
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓↓st␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ β∂= apply[␈↓ ∧π(LAMBDA (X Y) (PLUS (EXPT X 2) Y));
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π(2 3);
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓↓st␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ β∂= eval[␈↓ βobody[(LAMBDA (X Y) (PLUS (EXPT X 2) Y))];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βomkenv[␈↓ ∧Ovars[(LAMBDA (X Y) (PLUS (EXPT X 2) Y))];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O(2 3);
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓↓st␈↓α]]
␈↓"β␈↓ α←␈↓α␈↓ β∂= eval[␈↓ βo(PLUS (EXPT X 2) Y);
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βopairlis[(X Y);(2 3);␈↓↓st␈↓α]]
␈↓"β␈↓ α←␈↓α␈↓ β∂= eval[␈↓ βo(PLUS (EXPT X 2) Y);
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo((X . 2)(Y . 3)(F LAMBDA (X Y) ...))]
␈↓"β␈↓ α←␈↓α␈↓ β∂= apply[␈↓ ∧πPLUS;
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧πevlis[((EXPT X 2) Y);((X . 2)(Y . 3)..)];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π((X . 2)...)]
␈↓ α←␈↓␈↓3.6␈↓ λ-Examples of ␈↓αeval␈↓ 121␈↓α
␈↓"β␈↓ α←␈↓α␈↓Let's do a little of:␈↓α evlis[((EXPT X 2) Y);((X . 2)(Y . 3)...)]
␈↓"∞␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈= concat[␈↓ ¬weval[(EXPT X 2);((X . 2)(Y . 3) ...)];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈␈↓ ¬wevlis[(Y);((X . 2) ...)]]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈= concat[␈↓ ¬wapply[␈↓ εWEXPT;
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈␈↓ ¬w␈↓ εWevlis[(X 2);((X . 2)...)];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈␈↓ ¬w␈↓ εW((X . 2) ...]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈␈↓ ¬wevlis[(Y); ...]]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈= concat[␈↓ ¬wapply[␈↓ εWEXPT;
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈␈↓ ¬w␈↓ εW(2 2);
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈␈↓ ¬w␈↓ εW((X . 2);
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈␈↓ ¬w␈↓ εW...];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈␈↓ ¬wevlis[(Y); ...]]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈= concat[␈↓ ¬w↑[arg␈↓β1␈↓α[(2 2)];arg␈↓β2␈↓α[(2 2)]];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈␈↓ ¬wevlis[(Y); ... ]]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈= concat[↑[2;2];evlis[(Y); ... ]]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈= concat[4;evlis[(Y);((X . 2)(Y . 3) ....)]]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈= concat[4;concat[eval[Y;((X .2) ...)]; evlis[( );(( ...))]]]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈= concat[4;concat[3;( )]]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π␈↓ ∧O␈↓ ∧␈= (4 3)␈↓
␈↓"β␈↓ α←␈↓Now back to ␈↓αapply␈↓:
␈↓"∀␈↓ α←␈↓α␈↓ β∂= apply[␈↓ ∧πPLUS;
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π(4 3);
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo␈↓ ∧π((X . 2) (Y . 3) ... )]
␈↓"β␈↓ α←␈↓α␈↓ β∂= +[4;3]
␈↓"β␈↓ α←␈↓α␈↓ β∂= 7␈↓
␈↓"∀␈↓ α←␈↓It␈α
should␈α
now␈α
be␈α
clear␈α
that␈α
␈↓αeval␈↓␈α
does␈α
perform␈α
as␈α
you␈α
would␈α
expect,␈α
at␈αleast
␈↓ α←␈↓for␈αthis␈αexample.␈α It␈αis␈αnot␈αclear␈αthat␈αa␈αsimpler␈αscheme␈αmight␈αnot␈αdo␈αas␈αwell.
␈↓ α←␈↓In␈α∀particular,␈α∀the␈α∀complexity␈α∀of␈α∪the␈α∀symbol␈α∀table␈α∀mechanism␈α∀which␈α∪we
␈↓ α←␈↓claimed␈α⊂was␈α⊂so␈α⊂important␈α⊂has␈α⊃not␈α⊂been␈α⊂exploited.␈α⊂The␈α⊂next␈α⊃example␈α⊂will
␈↓ α←␈↓show that a scheme like ours is necessary to keep track of variable bindings.
␈↓ α←␈↓␈↓122 Evaluation␈↓
(3.6␈↓
␈↓"β␈↓ α←␈↓Let's sketch the evaluation of ␈↓αfact[3]␈↓ where:
␈↓"∀␈↓ α←␈↓␈↓ ∧G␈↓αfact <= λ[[x][x = 0 → 1; ␈↓
t␈↓α → *[x;fact[x-1]]]]␈↓
␈↓"∀␈↓ α←␈↓that is, ␈↓αeval[(FACT 3);␈↓↓st␈↓α] ␈↓where ␈↓↓st␈↓ names the initial symbol table:
␈↓"∀␈↓ α←␈↓α((FACT .␈↓ βo(LAMBDA (X) (COND␈↓ ε'((ZEROP X) 1)
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ ε'(T (TIMES␈↓ πOX
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ ε'␈↓ πO(FACT (SUB1 X))))))))␈↓␈↓π 18␈↓
␈↓"∀␈↓ α←␈↓In␈α∀this␈α∀example␈α∀we␈α∀will␈α∀assume␈α∀that␈α∀the␈α∀binary␈α∀function␈α∀*,␈α∀the␈α∀unary
␈↓ α←␈↓predicate␈α∩␈↓αzerop <= λ[[x] x = 0]␈↓␈α∩and␈α∩unary␈α∩function␈α∩␈↓α␈α∩sub1 <= λ[[x] x-1]␈↓␈α∩are
␈↓ α←␈↓known␈α∞and␈α
are␈α∞recognized␈α∞in␈α
the␈α∞evaluator␈α∞as␈α
␈↓αTIMES,␈α∞ZEROP␈↓␈α∞and␈α
␈↓αSUB1␈↓
␈↓ α←␈↓respectively.
␈↓"∀␈↓ α←␈↓Then␈↓α eval[(FACT 3);␈↓↓st␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ βK= apply[␈↓ ∧7FACT;
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7evlis[(3);␈↓↓st␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7␈↓↓st␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ βK= apply[␈↓ ∧7(LAMBDA (X) (COND ...));
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7(3);
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7␈↓↓st␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ βK= eval[(COND ((ZEROP X) 1) (T ( ...)));((X . 3) . ␈↓↓st␈↓α)]
␈↓"β␈↓ α←␈↓α␈↓ βK= evcond[␈↓ ∧C(((ZEROP X) 1) (T (TIMES X (FACT (SUB1 X)))));
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7␈↓ ∧C((X . 3) . ␈↓↓st␈↓α)]
␈↓" ␈↓ α←␈↓α␈↓Now, let ␈↓↓st1␈↓ be␈↓α ((X . 3) . ␈↓↓st␈↓α)
␈↓" ␈↓ α←␈↓α␈↓ βK= eval[(TIMES X (FACT (SUB1 X))); ␈↓↓st1␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ βK= apply[␈↓ ∧7TIMES;
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7evlis[(X (FACT (SUB1 X))); ␈↓↓st1␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7␈↓↓st1␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ βK= apply[␈↓ ∧7TIMES;
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7concat[␈↓ ¬3;
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7␈↓ ∧C␈↓ ¬evlis[((FACT (SUB1 X))); ␈↓↓st1␈↓α]];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7␈↓↓st1␈↓α]
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 18␈↓We␈αhave␈αsplit␈αthe␈α␈↓αCOND␈↓␈αacross␈αseveral␈αlines␈αin␈αan␈αindented␈αfashion
␈↓ α←␈↓to␈α∂improve␈α∂readibility.␈α∂Such␈α∂techniques␈α∂are␈α∂common␈α∂in␈α∂LISP.␈α∂The␈α∂idea␈α∞is
␈↓ α←␈↓called␈α→"pretty printing"␈α→and␈α→is␈α_discussed␈α→further␈α→on␈α→page 274␈α→and␈α_in
␈↓ α←␈↓Section 9.2.
␈↓ α←␈↓␈↓3.6␈↓ λ-Examples of ␈↓αeval␈↓ 123␈↓α
␈↓"∀␈↓ α←␈↓Now things get a little interesting inside ␈↓αevlis␈↓:
␈↓" ␈↓ α←␈↓α␈↓ β∂evlis[((FACT (SUB1 X)));␈↓↓st1␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧π= concat[␈↓ ∧␈eval[(FACT (SUB1 X)); ␈↓↓st1␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧π␈↓ ∧␈( )]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧π␈↓ and ␈↓α eval[(FACT (SUB1 X));␈↓↓st1␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧π␈↓ ∧␈␈↓ ¬↔= apply[␈↓ εβFACT;
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧π␈↓ ∧␈␈↓ ¬↔␈↓ εβevlis[((SUB1 X));␈↓↓st1␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧π␈↓ ∧␈␈↓ ¬↔␈↓ εβ␈↓↓st1␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧π␈↓ ∧␈␈↓ ¬↔= apply[FACT; (2);␈↓↓st1␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧π␈↓ ∧␈␈↓ ¬↔= apply[␈↓ εβ(LAMBDA (X) (COND ...));
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧π␈↓ ∧␈␈↓ ¬↔␈↓ εβ(2);
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧π␈↓ ∧␈␈↓ ¬↔␈↓ εβ␈↓↓st1␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧π␈↓ ∧␈␈↓ ¬↔= eval[(COND ((ZEROP X) 1) ...));((X . 2) . ␈↓↓st1␈↓α)]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧π␈↓ ∧␈␈↓ ¬↔ . . .
␈↓"∀␈↓ α←␈↓Within␈α⊂this␈α⊂latest␈α⊂call␈α⊂on␈α⊂␈↓αeval␈↓␈α⊂the␈α⊂symbol-table-searching␈α⊂function,␈α⊂␈↓αlookup␈↓,
␈↓ α←␈↓will␈α⊂find␈α⊂the␈α⊂pair␈α⊃␈↓α(X . 2)␈↓␈α⊂when␈α⊂looking␈α⊂for␈α⊂the␈α⊃value␈α⊂of␈α⊂␈↓αx␈↓.␈α⊂This␈α⊂is␈α⊃as␈α⊂it
␈↓ α←␈↓should␈αbe.␈α
But␈αnotice␈α
also␈αthat␈α
the␈αolder␈α
binding,␈α␈↓α(X . 3)␈↓,␈α
is␈αstill␈α
around␈αin
␈↓ α←␈↓the␈α
symbol␈α
table␈α␈↓↓st1␈↓,␈α
and␈α
will␈αbecome␈α
accessible␈α
once␈αwe␈α
complete␈α
this␈αlatest
␈↓ α←␈↓call␈α∞on␈α∂␈↓αeval␈↓.␈α∞ It␈α∂will␈α∞become␈α∂accessible␈α∞because␈α∂this␈α∞earlier␈α∂manifestation␈α∞of
␈↓ α←␈↓the␈αtable␈αwas␈αsaved␈αby␈αthe␈αλ-binding␈αprocess␈αas␈αwe␈αentered␈αthe␈αinner␈αcall␈αon
␈↓ α←␈↓␈↓αeval␈↓;␈αas␈αwe␈αleave␈αthis␈αinner␈αevaluation,␈αthe␈αprevious␈αincarnation␈αof␈αthe␈αtable
␈↓ α←␈↓is restored.
␈↓"β␈↓ α←␈↓␈↓ β'As␈α∪the␈α∩computation␈α∪continues,␈α∩the␈α∪current␈α∩symbol␈α∪table␈α∪appears␈α∩as
␈↓ α←␈↓follows:
␈↓"∀␈↓ α←␈↓α␈↓ ∧L((FACT LAMBDA (X) (COND ...))) = ␈↓↓st␈↓α
␈↓"β␈↓ α←␈↓α␈↓ ¬b((X . 3) . ␈↓↓st␈↓α) = ␈↓↓st1␈↓α
␈↓"β␈↓ α←␈↓α␈↓ ¬\((X . 2) . ␈↓↓st1␈↓α) = ␈↓↓st2␈↓α
␈↓"β␈↓ α←␈↓α␈↓ ¬]((X . 1) . ␈↓↓st2␈↓α) = ␈↓↓st3␈↓α
␈↓"β␈↓ α←␈↓α␈↓ ε((X . 0) . ␈↓↓st3␈↓α)
␈↓"∀␈↓ α←␈↓Thus␈α
each␈α
new␈αlevel␈α
of␈α
the␈α
table␈αbuilds␈α
on␈α
the␈α
prior␈αtable;␈α
each␈α
prior␈αtable␈α
is
␈↓ α←␈↓saved by the following line from ␈↓αapply␈↓ (page 117):
␈↓"∀␈↓ α←␈↓α␈↓ βfislambda[fn] → eval[body[fn];mkenv[vars[fn];args;environ]
␈↓"∀␈↓ α←␈↓The␈αcall␈αon␈α␈↓αeval␈↓␈αis␈αperformed␈αwith␈αthe␈αaugmented␈αtable;␈αwhen␈αwe␈αleave␈αthat
␈↓ α←␈↓inner ␈↓αeval␈↓ we return to an environment which contains the prior table.
␈↓"β␈↓ α←␈↓␈↓ β'Using␈α⊂␈↓αmkenv␈↓␈α⊂to␈α⊃concatenate␈α⊂the␈α⊂new␈α⊂bindings␈α⊃onto␈α⊂the␈α⊂front␈α⊃of␈α⊂the
␈↓ α←␈↓symbol␈αtable␈αas␈αwe␈αcall␈α␈↓αeval␈↓,␈αgenerates␈αthe␈αrequired␈αenvironment.␈α The␈αtricky
␈↓ α←␈↓part␈α∩occurs␈α∩when␈α∩we␈α∪leave␈α∩that␈α∩particular␈α∩call␈α∪on␈α∩␈↓αeval␈↓;␈α∩the␈α∩old␈α∪table␈α∩is
␈↓ α←␈↓automatically␈α∂restored␈α∂by␈α⊂the␈α∂recursion␈α∂mechanism.␈α∂ That␈α⊂is,␈α∂concatenating
␈↓ α←␈↓things␈α
onto␈α
the␈α
front␈α
of␈αa␈α
table␈α
doesn't␈α
change␈α
the␈αtable,␈α
but␈α
if␈α
we␈α
call␈α␈↓αeval␈↓␈α
or
␈↓ α←␈↓␈↓αapply␈↓ with a symbol table of say:
␈↓ α←␈↓␈↓124 Evaluation␈↓
(3.6␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬␈↓αconcat[(X . 2);concat[(X . 3); ␈↓↓st␈↓α]] ␈↓
␈↓"∀␈↓ α←␈↓then␈α∂in␈α∂␈↓¬that␈↓␈α∂call␈α∂on␈α∂␈↓αeval␈↓␈α∂or␈α∂␈↓αapply␈↓␈α∂we␈α∂have␈α∂access␈α∂to␈α∂␈↓α2␈↓␈α∂as␈α∂the␈α∂value␈α∂of␈α∂␈↓αx␈↓,
␈↓ α←␈↓rather than ␈↓α3␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'In␈αthis␈αrepresentation,␈αthe␈αsearch␈αfunction␈α␈↓αlookup␈↓␈αalways␈αproceeds␈αfrom
␈↓ α←␈↓left␈α⊃to␈α⊃right␈α⊃through␈α⊃the␈α⊃table␈α⊂and,␈α⊃since␈α⊃the␈α⊃table␈α⊃entry␈α⊃function␈α⊂␈↓αmkenv␈↓
␈↓ α←␈↓always␈α
adds␈αpairs␈α
onto␈αthe␈α
left␈αof␈α
the␈αtable␈α
before␈α␈↓αeval␈↓␈α
is␈αcalled,␈α
we␈α
will␈αget
␈↓ α←␈↓the expected binding of the variables.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∞structure␈α∞of␈α∞␈↓αmkenv␈↓␈α∞should␈α∞be␈α∞analyzed␈α∞further:␈α∞it␈α∞takes␈α∂a␈α∞formal
␈↓ α←␈↓parameter␈αlist,␈αan␈αevaluated␈αactual␈αparameter␈αlist,␈αand␈αan␈αenvironment,␈αas␈αits
␈↓ α←␈↓arguments;␈α⊃it␈α∩allocates␈α⊃a␈α∩new␈α⊃block␈α∩to␈α⊃contain␈α∩the␈α⊃name-value␈α∩pairs␈α⊃and
␈↓ α←␈↓proceeds␈α∞to␈α∞send␈α∞each␈α∞name-value␈α∞pair␈α∞to␈α∞its␈α∞proper␈α∞slot␈α∞in␈α∞the␈α∞block.␈α
The
␈↓ α←␈↓value␈αof␈α
␈↓αmkenv␈↓␈αis␈α
the␈αnewly␈α
constructed␈αenvironment␈α
formed␈αby␈α
linking␈αthe
␈↓ α←␈↓new␈αblock␈αonto␈α
the␈αfront␈αof␈α
the␈αold␈αenvironment.␈αIt␈α
turns␈αout␈αthat␈α
␈↓αpairlis␈↓␈αis
␈↓ α←␈↓able to combine the action of making the new block and filling the slots.
␈↓"β␈↓ α←␈↓␈↓ β'A more accurate picture of the abstract behavior of ␈↓αmkenv␈↓ is:
␈↓"∀␈↓ α←␈↓αmkenv <= λ[[vars;vals;env] mkenv␈↓λ'␈↓α[vars;vals;alloc[vars];env]]
␈↓"∀␈↓ α←␈↓αmkenv␈↓λ'␈↓α <= λ[[vars;vals;block;env]␈↓ ε∂[null[vars] → link[block;env];
␈↓"β␈↓ α←␈↓α␈↓ ε∂ ␈↓
t␈↓α → mkenv␈↓λ'␈↓α[␈↓ π7rest[vars];
␈↓"β␈↓ α←␈↓α␈↓ ε∂␈↓ π7rest[vals];
␈↓"β␈↓ α←␈↓α␈↓ ε∂␈↓ π7send[␈↓ π␈first[vars];
␈↓"β␈↓ α←␈↓α␈↓ ε∂␈↓ π7␈↓ π␈first[vals];
␈↓"β␈↓ α←␈↓α␈↓ ε∂␈↓ π7␈↓ π␈block];
␈↓"β␈↓ α←␈↓α␈↓ ε∂␈↓ π7env] ]]]
␈↓"∀␈↓ α←␈↓Our current implementation of ␈↓αpairlis␈↓ is equivalent to:
␈↓"β␈↓ α←␈↓␈↓ ¬W␈↓αalloc <=λ[[x] ( )] ␈↓π 19␈↓α
␈↓"∀␈↓ α←␈↓α␈↓ ∧↓send <= λ[[var;val;block] concat[mkent[var;val];block]]
␈↓"∀␈↓ α←␈↓α␈↓ ∧Xlink <= λ[[block;env] append[block;env]]
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α⊂computational␈α⊃behavior␈α⊂of␈α⊃␈↓αpairlis␈↓␈α⊂is␈α⊃slightly␈α⊂different:␈α⊃here␈α⊂the
␈↓ α←␈↓name-value␈α
pairs␈α
are␈α
added␈α
to␈α
the␈α
environment␈α
in␈α
an␈α
order␈α
reverse␈α∞to␈α
that
␈↓ α←␈↓used␈α∂in␈α∞␈↓αpairlis␈↓.␈α∂Since␈α∂the␈α∞variables␈α∂in␈α∞the␈α∂λ-list␈α∂must␈α∞be␈α∂distinct␈α∂from␈α∞one
␈↓ α←␈↓another, this alternative environment is equivalent to the previous one.
␈↓"β␈↓ α←␈↓␈↓ β'Symbol␈αtable␈αmanipulation␈αis␈αvery␈αimportant,␈αso␈αlet's␈αlook␈αat␈αit␈αagain␈αin
␈↓ α←␈↓a␈α∂slightly␈α∞different␈α∂manner.␈α∂ In␈α∞this␈α∂example,␈α∞expressions␈α∂and␈α∂table␈α∞entries
␈↓ α←␈↓will␈α
be␈α
written␈α∞more␈α
informally.␈α
Since␈α
the␈α∞evaluator␈α
is␈α
operating␈α
on␈α∞the␈α
list
␈↓ α←␈↓representation␈αof␈α
expressions␈αwe␈α
should␈αcontinue␈α
to␈αpresent␈α
these␈αarguments
␈↓ α←␈↓to␈α∀␈↓αeval␈↓␈α∀as␈α∀lists.␈α∀ However,␈α∪the␈α∀object␈α∀being␈α∀represented␈α∀might␈α∀be␈α∪more
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 19␈↓␈↓αalloc␈↓␈α⊃is␈α⊂defined␈α⊃as␈α⊃a␈α⊂unary␈α⊃function␈α⊃even␈α⊂though␈α⊃its␈α⊃argument␈α⊂is
␈↓ α←␈↓ignored␈α≥here.␈α≤This␈α≥generality␈α≤is␈α≥in␈α≤anticipation␈α≥of␈α≥future␈α≤binding
␈↓ α←␈↓implementations.
␈↓ α←␈↓␈↓3.6␈↓ λ-Examples of ␈↓αeval␈↓ 125␈↓α
␈↓"β␈↓ α←␈↓understandable␈αand␈αreadable␈↓π 20␈↓␈αthan␈αthe␈αrepresentation␈αof␈αthat␈αobject.␈αThus,
␈↓ α←␈↓initially,␈α⊃we␈α⊃will␈α∩write␈α⊃␈↓
R␈↓∞(␈↓λx␈↓∞)␈↓␈α⊃rather␈α⊃than␈α∩the␈α⊃explicit␈α⊃␈↓
R␈↓-image␈α⊃of␈α∩␈↓λx␈↓;␈α⊃for
␈↓ α←␈↓example,␈α⊃write␈α⊃␈↓
R␈↓∞(␈↓αfact[3]␈↓∞)␈↓␈α⊃rather␈α⊃than␈α⊃␈↓α(FACT 3)␈↓.␈α⊃ Later␈α⊃we␈α∩will␈α⊃simply
␈↓ α←␈↓write␈α
␈↓λx␈↓␈α
where␈α
no␈α
confusion␈α
is␈α
likely.␈α
With␈α
similar␈α
motivation,␈α
we␈αrepresent
␈↓ α←␈↓the symbol table between vertical bars, "|", in such a way that if a table, t␈↓β1␈↓, is:
␈↓"∀␈↓ α←␈↓␈↓ βK| b␈↓βn␈↓␈↓ ∧∨|
␈↓"β␈↓ α←␈↓␈↓ βK| ...␈↓ ∧∨| then ␈↓αconcat␈↓ing a new element, b␈↓βn+1␈↓ onto t␈↓β1␈↓ gives:
␈↓"β␈↓ α←␈↓␈↓ βK| b␈↓β1␈↓␈↓ ∧∨|
␈↓"β␈↓ α←␈↓␈↓ βK| b␈↓βn+1␈↓␈↓ ∧∨|
␈↓"β␈↓ α←␈↓␈↓ βK| b␈↓βn␈↓␈↓ ∧∨|
␈↓"β␈↓ α←␈↓␈↓ βK| ...␈↓ ∧∨|
␈↓"β␈↓ α←␈↓␈↓ βK| b␈↓β1␈↓␈↓ ∧∨|
␈↓"∀␈↓ α←␈↓The␈αelements␈αof␈αthe␈αtable␈αshould␈αalso␈αbe␈αpresented␈αas␈α␈↓
R␈↓-images,␈αbut␈αwe␈αwill
␈↓ α←␈↓represent the entries in a more transparent form. For example:
␈↓"∀␈↓ α←␈↓αeval[␈↓
R␈↓∞(␈↓αfact[3]␈↓∞)␈↓α; | fact : λ[[x][x=0 → 1;␈↓
t␈↓α → *[x;fact[x-1]]]] | ]
␈↓"β␈↓ α←␈↓α␈↓ α←= eval[␈↓
R␈↓∞(␈↓α[x=0 → 1; ␈↓
t␈↓α → *[x;fact[x-1]]]␈↓∞)␈↓α;␈↓ ε{|x : 3␈↓ λ;| ]
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε{| fact : λ[ ... ]␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ α←= *[3;eval[␈↓
R␈↓∞(␈↓α[x=0 → ...]␈↓∞)␈↓α;␈↓ ε{| x : 2␈↓ λ;| ]
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε{| x : 3␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε{| fact : λ[ ... ]␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ α←= *[3; *[2;eval[␈↓
R␈↓∞(␈↓α[x=0 → ...]␈↓∞)␈↓α;␈↓ ε{| x : 1␈↓ λ;| ]
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε{| x : 2␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε{| x : 3␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε{| fact : λ[ ... ]␈↓ λ;|
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 20␈↓Readability␈αof␈αLISP␈α
expressions␈αis␈αa␈α
subject␈αof␈αheated␈αbetween␈α
LISP
␈↓ α←␈↓users␈α
and␈α
non-users.␈α
Since␈α
we␈α
program␈α
using␈α
the␈α
list␈α
representation␈α
there␈αis
␈↓ α←␈↓an␈α
initial␈α
period␈α
in␈α
which␈α
the␈α
representation␈α
is␈α
"difficult␈α
to␈α
read".␈α
However
␈↓ α←␈↓that␈α∩phemononon␈α∪is␈α∩short␈α∪lived;␈α∩the␈α∩regularity␈α∪of␈α∩LISP␈α∪expressions,␈α∩the
␈↓ α←␈↓minimality␈αof␈αsyntax,␈αthe␈αuse␈αof␈αformatting␈αprograms␈αcalled␈α"pretty␈αprinters",
␈↓ α←␈↓and␈α#several␈α#abbreviational␈α$devices␈α#soon␈α#overcome␈α$any␈α#supposed
␈↓ α←␈↓disadvantages.␈α∩This␈α∩text␈α∩presents␈α∩LISP␈α∩expressions␈α∩in␈α∩the␈α∩meta-language
␈↓ α←␈↓since␈α⊂we␈α⊂wish␈α⊂to␈α⊂stress␈α⊂the␈α⊂notions␈α⊂of␈α⊂representation␈α⊃independence,␈α⊂rather
␈↓ α←␈↓than LISP's programming behavior.
␈↓ α←␈↓␈↓126 Evaluation␈↓
(3.6␈↓
␈↓"β␈↓ α←␈↓α␈↓ α←= *[3; *[2; *[1;eval[␈↓
R␈↓∞(␈↓α[x=0 → ...]␈↓∞)␈↓α;␈↓ ε{| x : 0␈↓ λ;| ]
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε{| x : 1␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε{| x : 2␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε{| x : 3␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε{| fact : λ[ ... ]␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ α←= *[3; *[2; *[1;1]]] ␈↓with:␈↓α␈↓ ε{| x : 1␈↓ λ;| ]
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε{| x : 2␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε{| ...␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ α←= *[3; *[2;1]] ␈↓with:␈↓α␈↓ ε{| x : 2␈↓ λ;| ]
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε{| ...␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ α←= *[3;2] ␈↓with:␈↓α␈↓ ε{| x : 3␈↓ λ;| ]
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε{| ...␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ α←= 6 ␈↓with:␈↓α␈↓ ε{| fact : λ[ ... ]␈↓ λ;|
␈↓"β␈↓ α←␈↓α= 6
␈↓"∀␈↓ α←␈↓Notice␈α∞that␈α∞after␈α
we␈α∞went␈α∞to␈α
all␈α∞the␈α∞trouble␈α
to␈α∞save␈α∞the␈α
old␈α∞values␈α∞of␈α∞␈↓αx␈↓␈α
we
␈↓ α←␈↓never␈αhad␈αto␈αuse␈αthem.␈α However,␈αin␈αthe␈αgeneral␈αcase␈αof␈αrecursive␈αevaluation
␈↓ α←␈↓we␈α∪must␈α∪be␈α∀able␈α∪to␈α∪save␈α∀and␈α∪restore␈α∪the␈α∀old␈α∪values␈α∪of␈α∀variables.␈α∪ For
␈↓ α←␈↓example, if we had defined ␈↓αfact␈↓ as:
␈↓"∀␈↓ α←␈↓α␈↓ ∧I fact <= λ[[x][x=0 → 1; ␈↓
t␈↓α → *[fact[x-1];x]]],
␈↓"∀␈↓ α←␈↓then we ␈↓¬would␈↓ have to access the old binding of ␈↓αx␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'For further example, recall the definition of ␈↓αequal:
␈↓"∀␈↓ α←␈↓αequal <= λ[[x;y]␈↓ ∧+[atom[x] → [atom[y] → eq[x;y]; ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ ∧+ atom[y] → ␈↓
f␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ∧+ equal[car[x];car[y]] → equal[cdr[x];cdr[y]];
␈↓"β␈↓ α←␈↓α␈↓ ∧+ ␈↓
t␈↓α → ␈↓
f␈↓α]]
␈↓"∀␈↓ α←␈↓α␈↓If we were evaluating:␈↓α
␈↓"∀␈↓ α←␈↓α␈↓ ¬πequal[((A . B) . C);((A . B) . D)],
␈↓"∀␈↓ α←␈↓then,␈α∂reading␈α∂across␈α∂the␈α∂page,␈α∂our␈α∂symbol␈α∂table␈α∂structure␈α∂would␈α∂change␈α∞as
␈↓ α←␈↓follows:
␈↓"β␈↓ α←␈↓α␈↓ βK|equal : λ[[x;y] ...]␈↓ ¬;| ==>␈↓ εK|x : ((A . B) . C)␈↓ λ;| ==>
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ¬;␈↓ εK|y : ((A . B) . D)␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ¬;␈↓ εK|equal : λ[[x;y] ... ]␈↓ λ;|
␈↓ α←␈↓␈↓3.6␈↓ λ-Examples of ␈↓αeval␈↓ 127␈↓α
␈↓"β␈↓ α←␈↓α␈↓ βK| x : (A . B)␈↓ ¬;|␈↓ εK| x : A␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ βK| y : (A . B)␈↓ ¬;|␈↓ εK| y : A␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ βK| x : ((A . B) . C)␈↓ ¬;| ==>␈↓ εK| x : (A . B)␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ βK| y : ((A . B) . D)␈↓ ¬;|␈↓ εK| y : (A . B)␈↓ λ;| ==>
␈↓"β␈↓ α←␈↓α␈↓ βK|equal : λ[[x;y] ... ]␈↓ ¬;|␈↓ εK| x : ((A . B) . C)␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ¬;␈↓ εK| y : ((A . B) . D)␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ¬;␈↓ εK|equal : λ[[x;y] ... ]␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ βK| x : B␈↓ ¬;|␈↓ εK| x : C␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ βK| y : B␈↓ ¬;|␈↓ εK| y : D␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ βK| x : (A . B)␈↓ ¬;|␈↓ εK| x : ((A . B) . C)␈↓ λ;| ==>
␈↓"β␈↓ α←␈↓α␈↓ βK| y : (A . B)␈↓ ¬;| ==>␈↓ εK| y : ((A . B) . D)␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ βK| x : ((A . B) . C)␈↓ ¬;|␈↓ εK|equal : λ[[x;y] ... ]␈↓ λ;|
␈↓"β␈↓ α←␈↓α␈↓ βK| y : ((A . B). D)␈↓ ¬;|
␈↓"β␈↓ α←␈↓α␈↓ βK|equal : λ[[x;y] ... ]␈↓ ¬;|
␈↓"β␈↓ α←␈↓α␈↓ ¬[|equal : λ[[x;y] ... ] |
␈↓"∀␈↓ α←␈↓This␈α_degree␈α_of␈α_complexity␈α_is␈α_necessary,␈α_for␈α_while␈α_we␈α_are␈α_evaluating
␈↓ α←␈↓␈↓αequal[car[x];car[y]]␈↓,␈αwe␈αrebind␈α␈↓αx␈↓␈α
and␈α␈↓αy␈↓␈αbut␈αwe␈αmust␈α
save␈αthe␈αold␈αvalues␈α
of␈α␈↓αx␈↓
␈↓ α←␈↓and␈α∞␈↓αy␈↓␈α∞for␈α∞the␈α∞possible␈α∞evaluation␈α∞of␈α∞␈↓αequal[cdr[x];cdr[y]].␈↓␈α∞It␈α∞is␈α∞␈↓¬not␈↓␈α∂clear␈α∞that
␈↓ α←␈↓this␈αimplementation␈αis␈αoptimal.␈α
The␈αsearch␈αfor␈αthe␈αvalues␈α
of␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓␈αis␈α
short,
␈↓ α←␈↓but␈α∞the␈α∞evaluation␈α∞of␈α∞any␈α
subexpressions␈α∞involving␈α∞␈↓αequal␈↓␈α∞must␈α∞retrieve␈α
the
␈↓ α←␈↓definition␈α∂of␈α∂␈↓αequal␈↓.␈α∂That␈α∞search␈α∂is␈α∂proportional␈α∂to␈α∞the␈α∂depth␈α∂of␈α∂the␈α∞initial
␈↓ α←␈↓arguments to ␈↓αequal␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Before␈α
continuing,␈αwe␈α
should␈αexamine␈α
␈↓αeval␈↓␈αand␈α
␈↓αapply␈↓␈αto␈α
see␈α
how␈αthey
␈↓ α←␈↓compare␈α⊂with␈α∂our␈α⊂previous␈α⊂discussions␈α∂of␈α⊂LISP␈α∂evaluation.␈α⊂ The␈α⊂spirit␈α∂of
␈↓ α←␈↓call-by-value␈αand␈αconditional␈αexpression␈αevaluation␈αis␈αmaintained.␈α λ-binding
␈↓ α←␈↓seems␈α∂correct,␈α∂though␈α∂our␈α∂current␈α∂discussion␈α∂is␈α∂not␈α∂complete.␈α∂ At␈α⊂least␈α∂one
␈↓ α←␈↓preconception␈α
is␈α
not␈α∞maintained␈α
here.␈α
Recall␈α∞the␈α
discussion␈α
on␈α∞page 17.␈α
We
␈↓ α←␈↓wanted␈α
n-ary␈α
functions␈α
called␈α
with␈α
exactly␈α
n␈α
arguments.␈α
An␈α
examination␈α
of
␈↓ α←␈↓the␈α∃structure␈α∃of␈α∃␈↓αeval␈↓␈α∃and␈α∃␈↓αapply␈↓␈α∀shows␈α∃that␈α∃if␈α∃a␈α∃function␈α∃expecting␈α∀␈↓¬n␈↓
␈↓ α←␈↓arguments␈α
is␈α∞presented␈α
with␈α∞fewer,␈α
then␈α∞the␈α
result␈α∞is␈α
undefined;␈α∞but␈α
if␈α∞it␈α
is
␈↓ α←␈↓given␈α
␈↓¬more␈↓␈α
arguments␈α
than␈α
necessary␈α
then␈α
the␈α
calculation␈α
is␈α
performed.␈α
For
␈↓ α←␈↓example:
␈↓"∀␈↓ α←␈↓αeval[(CONS (QUOTE A) (QUOTE B) (QUOTE C));NIL]
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓reduces to ␈↓α eval[(CONS (QUOTE A) (QUOTE B));NIL]
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓reduces to ␈↓α (A . B)
␈↓"∀␈↓ α←␈↓␈↓ β'This␈α∞example␈α∞shows␈α∞one␈α∞of␈α∞the␈α
pitfalls␈α∞in␈α∞defining␈α∞a␈α∞language␈α∞by␈α
an
␈↓ α←␈↓evaluator.␈α
If␈α
the␈α
intuitions␈α
of␈α
the␈α
language␈α
specifiers␈α
are␈α
faulty␈α
or␈α
incomplete
␈↓ α←␈↓then␈αeither␈αwe␈αmust␈αmaintain␈αthat␈αfaulty␈αjudgement,␈αor␈αwe␈αmust␈αlobby␈αfor␈αa
␈↓ α←␈↓␈↓128 Evaluation␈↓
(3.6␈↓
␈↓"β␈↓ α←␈↓"revised report".␈↓π 21␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈αdefinition␈αof␈αa␈αlanguage␈α
by␈αan␈αevaluator␈αwritten␈αin␈α
that␈αlanguage
␈↓ α←␈↓is␈α⊂subject␈α∂to␈α⊂other␈α⊂criticisms.␈α∂The␈α⊂troublesome␈α⊂areas␈α∂of␈α⊂our␈α⊂description␈α∂of
␈↓ α←␈↓LISP's␈α≥evaluation␈α≤included␈α≥λ-binding,␈α≤calling␈α≥styles␈α≤in␈α≥general␈α≤and
␈↓ α←␈↓call-by-value␈α⊂in␈α⊂particular,␈α⊂and␈α⊂left-to-right␈α⊂order␈α⊂of␈α⊂evaluation.␈α⊂We␈α∂wrote
␈↓ α←␈↓␈↓αeval␈↓␈α∂to␈α∂explicate␈α∂the␈α∂meaning␈α⊂of␈α∂these␈α∂constructs,␈α∂yet␈α∂within␈α∂␈↓αeval␈↓␈α⊂we␈α∂often
␈↓ α←␈↓relied␈α
on␈α
exactly␈αthese␈α
constructs␈α
to␈α
convey␈αour␈α
intent.␈α
Now,␈α
our␈αdescription
␈↓ α←␈↓in␈αnot␈αentirely␈αcircular;␈α␈↓αeval␈↓␈αdoes␈α
convey␈αmuch␈αof␈αour␈αintention␈αto␈αthe␈α
reader,
␈↓ α←␈↓but␈α⊃the␈α⊃discussion␈α⊃of␈α⊃␈↓¬how␈↓␈α⊃these␈α⊂constructs␈α⊃operate␈α⊃is␈α⊃either␈α⊃implicit␈α⊃or␈α⊂is
␈↓ α←␈↓explained␈α∃by␈α∃using␈α∀the␈α∃same␈α∃kind␈α∃of␈α∀constructs.␈α∃ In␈α∃gaining␈α∃a␈α∀clearer
␈↓ α←␈↓understanding␈α∪of␈α∪what␈α∩LISP␈α∪constructs␈α∪mean,␈α∩␈↓αeval␈↓␈α∪is␈α∪exemplary.␈α∩Indeed
␈↓ α←␈↓many␈α∞of␈α∞the␈α
details␈α∞of␈α∞how␈α
these␈α∞constructs␈α∞work␈α
are␈α∞irrelevant␈α∞to␈α∞such␈α
an
␈↓ α←␈↓understanding.␈αWhen␈αwe␈αattempt␈αto␈αimplement␈αa␈αlanguage␈αfeature␈αwe␈αcannot
␈↓ α←␈↓assume␈α∞the␈α∂existence␈α∞of␈α∂that␈α∞feature;␈α∞the␈α∂implementation␈α∞must␈α∂be␈α∞prepared
␈↓ α←␈↓from␈α∞a␈α
combination␈α∞of␈α
more␈α∞primitive␈α
components.␈α∞As␈α
we␈α∞proceed␈α
through
␈↓ α←␈↓the␈αtext␈αwe␈αwill␈αintroduce␈αthe␈αmechanisms␈αwhich␈αare␈αnecessary␈αto␈αimplement
␈↓ α←␈↓LISP␈α∩and,␈α∩indeed,␈α∩implement␈α∪the␈α∩constructs␈α∩of␈α∩most␈α∩other␈α∪languages.␈α∩In
␈↓ α←␈↓Section 4.4␈α∂we␈α∂give␈α∂several␈α∂alternative␈α∂algorithms␈α∂for␈α∂␈↓αeval␈↓.␈α⊂The␈α∂algorithms
␈↓ α←␈↓will␈αevolve␈αto␈αan␈α␈↓αeval␈↓␈αwhich␈αmakes␈αexplicit␈αmost␈αof␈αthe␈αmechanisms␈αwe␈αneed.
␈↓ α←␈↓In␈α⊃Chapter 5␈α⊂we␈α⊃will␈α⊃begin␈α⊂to␈α⊃discuss␈α⊂efficient␈α⊃representations␈α⊃for␈α⊂LISP's
␈↓ α←␈↓data␈α∞structures,␈α∞control␈α∞structures,␈α∞and␈α∞primitive␈α∞operations.␈α∂The␈α∞remainder
␈↓ α←␈↓of␈αthe␈αcurrent␈αchapter␈αwill␈αexplicate␈αfurther␈αfeatures␈αof␈αLISP␈αin␈αpreparation
␈↓ α←␈↓for that discussion.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Which␈αparts␈αof␈αthe␈αevaluator␈αallow␈αthe␈αevaluation␈αof␈αfunctions␈αapplied␈αto
␈↓ α←␈↓␈↓ β∂too many arguments?
␈↓" ␈↓ α←␈↓2. Find␈α⊗other␈α⊗anomalies␈α⊗in␈α⊗the␈α⊗evaluator.␈α⊗That␈α⊗is,␈α⊗find␈α↔places␈α⊗where
␈↓ α←␈↓␈↓ β∂unexpected results are obtained?
␈↓"β␈↓ α←␈↓␈↓ ¬|␈↓↓3.7 Variables␈↓
␈↓"β␈↓ α←␈↓Let's␈α⊂look␈α⊂more␈α⊃closely␈α⊂at␈α⊂λ-binding␈α⊂in␈α⊃␈↓αeval␈↓.␈α⊂The␈α⊂scheme␈α⊃presented␈α⊂seems
␈↓ α←␈↓reasonable,␈α
but␈α
as␈α
with␈α
"␈↓αcons[A;B;C]␈↓",␈αthere␈α
may␈α
be␈α
more␈α
expressed␈αhere␈α
than
␈↓ α←␈↓we anticipated.
␈↓"β␈↓ α←␈↓␈↓ β'If␈α≠we␈α~asked␈α≠␈↓αeval␈↓␈α≠to␈α~compute␈α≠␈↓αf[2]␈↓,␈α~given␈α≠a␈α≠representation␈α~for
␈↓ α←␈↓␈↓αf <= λ[[x] x + y]␈↓␈α
but␈αno␈α
representation␈αfor␈α
the␈αvalue␈α
of␈α␈↓αy␈↓␈α
it␈α
would␈αcomplain.
␈↓ α←␈↓It␈α
would␈α∞find␈α
␈↓αf␈↓,␈α∞bind␈α
␈↓α2␈↓␈α
to␈α∞␈↓αx␈↓,␈α
and␈α∞begin␈α
the␈α
evaluation␈α∞of␈α
the␈α∞body␈α
of␈α∞␈↓αf␈↓.␈α
It
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 21␈↓For␈α
example␈α
the␈α
LISP 1.6␈α
system␈α
([Qua 72])␈α
gives␈α
␈↓α(A . A)␈↓␈α
for␈α
␈↓αcons[A]␈↓;
␈↓ α←␈↓the␈α∀MacLISP␈α∀system␈α∪([Moo 74])␈α∀gives␈α∀␈↓α(A . ␈↓"missing-arg"␈↓α)␈↓;␈α∀and␈α∪InterLISP
␈↓ α←␈↓([Int 75]) gives ␈↓α(A)␈↓.
␈↓ α←␈↓␈↓3.7␈↓ πVariables 129␈↓
␈↓"β␈↓ α←␈↓would␈αfind␈α␈↓αx␈↓'s␈αvalue,␈αbut␈αit␈αwould␈αfind␈αno␈αvalue␈αfor␈α␈↓αy␈↓.␈αHowever,␈αif␈αwe␈αasked
␈↓ α←␈↓it␈αto␈α
evaluate␈αthe␈α
form␈α␈↓αλ[[y] f[2]][1]␈↓␈α
it␈α␈↓¬would␈↓␈α
work.␈αIt␈α
would␈αfind␈α
the␈αvalue␈α
of
␈↓ α←␈↓␈↓αy␈↓␈αto␈αbe␈α␈↓α1␈↓␈α
and␈αwould␈αget␈αa␈α
final␈αanswer␈αof␈α␈↓α3␈↓.␈α
You␈αshould␈αconvince␈αyourself␈α
of
␈↓ α←␈↓this assertion.
␈↓"β␈↓ α←␈↓␈↓ β'Within␈α⊃the␈α⊃evaluation␈α⊃of␈α⊃␈↓αf[2]␈↓␈α∩in␈α⊃␈↓αλ[[y] f[2]][1]␈↓␈α⊃the␈α⊃variable␈α⊃␈↓αy␈↓␈α∩has␈α⊃a
␈↓ α←␈↓different␈αcharacter␈αfrom␈αthat␈αof␈α␈↓αx␈↓.␈α The␈αvalue␈αof␈α␈↓αx␈↓␈αis␈αfound␈αwithin␈αthe␈αlatest
␈↓ α←␈↓λ-binding,␈α⊂whereas␈α⊂␈↓αy␈↓␈α⊂was␈α⊂bound␈α⊂in␈α⊂a␈α⊂dynamically␈α⊃surrounding␈α⊂λ-binding.
␈↓ α←␈↓That␈αis,␈αthe␈αλ-expression␈αwhich␈αbound␈α␈↓αy␈↓␈αtook␈αeffect␈αbefore␈αthe␈αbinding␈αof␈α␈↓αx␈↓
␈↓ α←␈↓and␈αis␈αstill␈αin␈αeffect␈αwhen␈αthe␈αbinding␈αof␈α␈↓αx␈↓␈αis␈αmade.␈αWe␈αdo␈αhave␈αaccess␈αto␈α
␈↓αy␈↓'s
␈↓ α←␈↓binding␈αin␈α
this␈αcase;␈α
the␈α␈↓αlookup␈↓␈αroutine␈α
will␈αlocate␈α
␈↓αy␈↓'s␈αvalue.␈α
There␈αis␈αa␈α
third
␈↓ α←␈↓kind␈αof␈αname-value␈αassociation␈αpresent␈αin␈αthese␈αexamples:␈αwe␈αexpect␈αthat␈αthe
␈↓ α←␈↓symbol␈α
"+"␈α∞is␈α
recognized␈α
during␈α∞the␈α
evaluation␈α
as␈α∞denoting␈α
a␈α∞procedure␈α
for
␈↓ α←␈↓computing␈α∃the␈α∃sum␈α∀of␈α∃two␈α∃numbers.␈α∀ In␈α∃previous␈α∃discussions␈α∃we␈α∀have
␈↓ α←␈↓assumed␈α∃that␈α∃"+"␈α∃was␈α∃pre-defined␈α∃inside␈α∃␈↓αapply␈↓␈α∃and␈α∃therefore␈α∃explicitly
␈↓ α←␈↓recognized.␈α⊂Finally,␈α⊃in␈α⊂the␈α⊃first␈α⊂example,␈α⊃a␈α⊂fourth␈α⊃kind␈α⊂of␈α⊃variable␈α⊂usage
␈↓ α←␈↓occurred.␈α⊃The␈α⊃variable␈α⊃␈↓αy␈↓␈α∩had␈α⊃no␈α⊃associated␈α⊃value␈α⊃when␈α∩the␈α⊃computation
␈↓ α←␈↓expected␈α⊗one.␈α⊗In␈α⊗this␈α⊗section␈α⊗we␈α⊗wish␈α⊗to␈α⊗examine␈α⊗these␈α⊗properties␈α⊗of
␈↓ α←␈↓variables.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊃implementation␈α⊃of␈α⊃λ-bindings␈α⊃described␈α⊃in␈α⊃␈↓αpairlis␈↓␈α⊃(page 116)␈α⊃is
␈↓ α←␈↓slightly␈αmisleading.␈αThere,␈α
the␈αnew␈αλ-bindings␈αare␈α
␈↓αconcat␈↓-ed␈αonto␈αthe␈αfront␈α
of
␈↓ α←␈↓the␈α
existing␈α∞table.␈α
They␈α∞go␈α
on␈α
in␈α∞a␈α
one-at-a-time␈α∞fashion␈α
even␈α∞though␈α
they
␈↓ α←␈↓are␈α∞to␈α∞be␈α∞thought␈α∞of␈α
as␈α∞a␈α∞logical␈α∞unit:␈α∞at␈α
the␈α∞language␈α∞level␈α∞they␈α∞all␈α∞go␈α
on
␈↓ α←␈↓together,␈αand␈α
they␈αall␈α
come␈αoff␈αtogether.␈α
It␈αis␈α
the␈αstructure␈α
of␈αthis␈αtable␈α
which
␈↓ α←␈↓we should also examine. To these ends we now introduce some terminology.
␈↓"β␈↓ α←␈↓␈↓ β'Consider the evaluation of the expression:
␈↓"∀␈↓ α←␈↓α␈↓ ∧Oλ[[y] equal[λ[[x] cons[x;y]][(A . B)];x]][A]
␈↓"∀␈↓ α←␈↓in an environment where the definition of ␈↓αequal␈↓ is known.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αevaluate␈αthe␈αmain␈αargument␈α␈↓αA␈↓,␈αand␈αperform␈αthe␈αλ-binding␈αof␈α␈↓αA␈↓␈αto
␈↓ α←␈↓␈↓αy␈↓.␈αThis␈αoperation␈αof␈αλ-binding␈αcreates␈α
what␈αwe␈αcall␈αa␈α␈↓↓local␈αsymbol␈αtable␈↓␈α
and
␈↓ α←␈↓the␈αvariables␈α
bound␈αin␈α
that␈αlocal␈α
table␈αare␈α
called␈α␈↓↓local␈α
bindings␈↓␈αfor␈αthe␈α
body
␈↓ α←␈↓of␈αthe␈αλ-expression.␈αWe␈αnow␈αbegin␈αthe␈αevaluation␈αof␈αthe␈αarguments␈αto␈α␈↓αequal␈↓.
␈↓ α←␈↓The␈αfirst␈αargument␈αis␈αitself␈αan␈αexpression␈αrequiring␈αλ-binding.␈α We␈αevaluate
␈↓ α←␈↓it's␈αargument␈αand␈αbind␈α␈↓α(A . B)␈↓␈α
to␈α␈↓αx␈↓.␈αThis␈αcreates␈αa␈α
local␈αbinding␈αfor␈α␈↓αx␈↓.␈αIn␈α
the
␈↓ α←␈↓process␈α∩of␈α∩making␈α∪␈↓αx␈↓␈α∩local␈α∩what␈α∩happens␈α∪to␈α∩␈↓αy␈↓?␈α∩ Notice␈α∩that␈α∪the␈α∩binding
␈↓ α←␈↓process␈αhas␈αnot␈αmade␈α␈↓αy␈↓␈αinaccessible:␈αwe␈αcan␈αcompute␈α␈↓αcons[x;y]␈↓␈αeven␈αthough␈α␈↓αy␈↓
␈↓ α←␈↓is␈α∩not␈α⊃local.␈α∩Variables␈α∩like␈α⊃␈↓αy␈↓␈α∩which␈α⊃are␈α∩accessible,␈α∩but␈α⊃not␈α∩local,␈α∩we␈α⊃call
␈↓ α←␈↓␈↓↓non-local␈↓␈α∂variables.␈α∂ Thus␈α⊂both␈α∂␈↓αy␈↓␈α∂and␈α∂␈↓αcons␈↓␈α⊂are␈α∂non-local␈α∂variables␈α⊂in␈α∂our
␈↓ α←␈↓evaluation␈α
of␈α␈↓αcons[x;y]␈↓.␈α
There␈αis␈α
a␈α
further␈αdistinction␈α
between␈α␈↓αy␈↓␈α
and␈α␈↓αcons␈↓:␈α
We
␈↓ α←␈↓expect␈α∞␈↓αcons␈↓␈α
to␈α∞be␈α
a␈α∞predefined␈α∞function;␈α
indeed␈α∞␈↓αcons␈↓␈α
has␈α∞not␈α∞been␈α
λ-bound
␈↓ α←␈↓any␈α∀where␈α∀in␈α∀our␈α∀computation.␈α∃Variables␈α∀like␈α∀␈↓αcons␈↓␈α∀we␈α∀will␈α∃call␈α∀␈↓↓global
␈↓ α←␈↓↓variables␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Global␈α∞variables␈α∂include␈α∞predefined␈α∞function␈α∂names,␈α∞␈↓αcar␈↓,␈α∞␈↓αcdr␈↓,␈α∂etc,␈α∞and
␈↓ α←␈↓variables␈α∞like␈α∞␈↓αt␈↓␈α∞and␈α∞␈↓αnil␈↓.␈α∞ A␈α
useful␈α∞interpretation␈α∞of␈α∞global␈α∞variables␈α∞is␈α
that
␈↓ α←␈↓␈↓130 Evaluation␈↓
(3.7␈↓
␈↓"β␈↓ α←␈↓they␈α∂are␈α∂bound␈α⊂in␈α∂the␈α∂initial␈α∂symbol␈α⊂table,␈α∂also␈α∂called␈α∂the␈α⊂␈↓↓global␈α∂table␈↓.␈↓π 22␈↓
␈↓ α←␈↓Non-local␈α∂variables␈α∂which␈α∂are␈α∂λ-bound␈α∂somewhere␈α∂in␈α∂the␈α∂symbol␈α⊂table␈α∂we
␈↓ α←␈↓call␈α␈↓↓free␈αvariables␈↓,␈αand␈αvariables␈αwhich␈αhave␈α␈↓↓some␈↓␈αaccessible␈αbinding␈αat␈αthe
␈↓ α←␈↓current point in the computation are called ␈↓↓bound variables␈↓.␈↓π 23␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Finally␈αthe␈αfirst␈αargument␈αto␈α␈↓αequal␈↓␈αis␈αevaluated␈αgiving␈α␈↓α((A . B) . A)␈↓.␈α As
␈↓ α←␈↓we␈αcomplete␈αthat␈α
evaluation␈αthe␈αlocal␈αbinding␈α
for␈α␈↓αx␈↓␈αbecomes␈αinaccessible,␈α
and
␈↓ α←␈↓␈↓αy␈↓␈αbecomes␈αlocal␈αagain.␈αWe␈αexamine␈α
the␈αsecond␈αargument␈αto␈α␈↓αequal␈↓,␈αwhich␈αis␈α
␈↓αx␈↓,
␈↓ α←␈↓and␈α
now␈αfind␈α
there␈α
is␈αno␈α
binding␈αfor␈α
that␈α
variable.␈αVariables␈α
which␈αhave␈α
no
␈↓ α←␈↓binding␈α⊂of␈α⊂any␈α⊂kind␈α⊃at␈α⊂the␈α⊂time␈α⊂we␈α⊂ask␈α⊃for␈α⊂a␈α⊂value␈α⊂are␈α⊃called␈α⊂␈↓↓unbound
␈↓ α←␈↓↓variables␈↓.␈α
The␈α
local,␈α∞free,␈α
and␈α
global␈α
variables␈α∞make␈α
up␈α
the␈α
class␈α∞of␈α
␈↓↓bound
␈↓ α←␈↓↓variables␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'For␈α_a␈α_computation␈α_to␈α→be␈α_meaningful,␈α_each␈α_variable␈α→which␈α_that
␈↓ α←␈↓computation␈α∪references␈α∪must␈α∩be␈α∪bound␈α∪when␈α∪we␈α∩ask␈α∪for␈α∪its␈α∪value.␈α∩The
␈↓ α←␈↓computation␈αof␈αour␈αcurrent␈αexample␈αwould␈αfail;␈αit␈αwould␈αfail␈αeven␈αbefore␈αwe
␈↓ α←␈↓asked␈αfor␈α
the␈αdefinition␈α
of␈α␈↓αequal␈↓␈α
since␈αwe␈α
are␈αdoing␈α
call-by-value.␈αOne␈αof␈α
our
␈↓ α←␈↓tasks␈α∂will␈α∞be␈α∂to␈α∞discuss␈α∂where␈α∞definitions␈α∂such␈α∞as␈α∂that␈α∞for␈α∂␈↓αequal␈↓␈α∂should␈α∞be
␈↓ α←␈↓kept.
␈↓"β␈↓ α←␈↓␈↓ β'Here is a diagram of our characterization of variables:
␈↓"⊃␈↓ α←␈↓∂ ␈↓variables␈↓∂
␈↓"␈↓ α←␈↓∂ ~
␈↓"␈↓ α←␈↓∂ ⊂ααααααα∀ααααααα⊃
␈↓"␈↓ α←␈↓∂ ~ ~
␈↓"␈↓ α←␈↓∂ ␈↓non-local␈↓∂ ␈↓local␈↓∂
␈↓"␈↓ α←␈↓∂ ~
␈↓"␈↓ α←␈↓∂ ⊂αα∀ααααααπααααααααααα⊃
␈↓"␈↓ α←␈↓∂ ~ ~ ~
␈↓"␈↓ α←␈↓∂ ␈↓free␈↓∂ ␈↓global␈↓∂ ␈↓unbound␈↓∂
␈↓"∀␈↓ α←␈↓Notice␈α∞that␈α∞a␈α∞variable␈α∞which␈α∞is␈α∞initially␈α∞global␈α∞may␈α∞become␈α∞local␈α∂and␈α∞then
␈↓ α←␈↓free by virtue of λ-bindings.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∀binding␈α∀strategy␈α∀for␈α∃local␈α∀variables␈α∀is␈α∀reasonably␈α∃uniform␈α∀in
␈↓ α←␈↓programming␈α∞languages:␈α
bind␈α∞some␈α
form␈α∞of␈α
the␈α∞actual␈α
parameters␈↓π 24␈↓␈α∞to␈α
the
␈↓ α←␈↓formal␈α∩parameters␈α⊃and␈α∩evaluate␈α⊃the␈α∩body␈α⊃of␈α∩the␈α⊃definition.␈α∩ One␈α∩of␈α⊃the
␈↓ α←␈↓difficulties␈α⊃in␈α⊂programming␈α⊃languages␈α⊂is␈α⊃deciding␈α⊂what␈α⊃value␈α⊃to␈α⊂associate
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 22␈↓This␈α
analogy␈α∞breaks␈α
down␈α
somewhat␈α∞in␈α
that␈α∞usual␈α
implementations
␈↓ α←␈↓of␈α∞LISP␈α∞allow␈α∂this␈α∞global␈α∞table␈α∞to␈α∂be␈α∞augmented;␈α∞for␈α∞example,␈α∂by␈α∞function
␈↓ α←␈↓definitions␈α∂using␈α∞a␈α∂version␈α∞of␈α∂"<=".␈α∞Thus␈α∂the␈α∞global␈α∂table␈α∞can␈α∂be␈α∞enlarged
␈↓ α←␈↓whereas a true λ-binding involves a fixed number of variables.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 23␈↓Our␈α≥notion␈α≥of␈α≥free␈α≥and␈α≥bound␈α≥variables␈α≥has␈α≡a␈α≥decidedly
␈↓ α←␈↓computational␈α⊂flavor,␈α⊃in␈α⊂contrast␈α⊂to␈α⊃the␈α⊂mathematical␈α⊂definitions␈α⊃of␈α⊂"free"
␈↓ α←␈↓and␈α∞"bound"␈α∞given␈α∞on␈α∞page 170.␈α∞For␈α
example␈α∞a␈α∞variable␈α∞may␈α∞be␈α∞both␈α
free
␈↓ α←␈↓and bound in our terminology.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 24␈↓The parameters may either be evaluated or unevaluated, however.
␈↓ α←␈↓␈↓3.7␈↓ πVariables 131␈↓
␈↓"β␈↓ α←␈↓with␈α∞a␈α∞non-local␈α∞variable.␈α∞ In␈α∞LISP,␈α∞it␈α∞is␈α∞clear␈α∞␈↓¬how␈↓␈α∞values␈α∞get␈α∞associated;␈α∞it
␈↓ α←␈↓happens␈α∂through␈α∂λ-binding␈α∂or␈α∂by␈α∂virtue␈α∂of␈α∂an␈α∂initial␈α∂entry␈α∂in␈α⊂the␈α∂symbol
␈↓ α←␈↓table.␈α∀ The␈α∀scheme␈α∀which␈α∀LISP␈α∀uses␈α∀for␈α∀discovering␈α∀the␈α∀value␈α∀of␈α∀any
␈↓ α←␈↓variable␈α
is␈α
to␈α
proceed␈α
linearly␈α
down␈α
the␈α
symbol␈α
table,␈α
looking␈α
for␈α
the␈α␈↓¬latest
␈↓ α←␈↓¬active␈↓␈αbinding.␈α This␈αscheme␈αis␈αcalled␈α␈↓↓dynamic␈αbinding␈↓.␈αIt␈α␈↓¬usually␈↓␈αresults␈αin
␈↓ α←␈↓uncovering␈α∩the␈α⊃value␈α∩that␈α∩is␈α⊃expected;␈α∩but␈α⊃not␈α∩always␈α∩as␈α⊃we␈α∩will␈α∩see␈α⊃in
␈↓ α←␈↓Section 3.10.␈α∞ Conceptually,␈α∞the␈α∞dynamic␈α
binding␈α∞scheme␈α∞corresponds␈α∞to␈α
the
␈↓ α←␈↓physical␈αreplacement␈αof␈αthe␈αfunction␈αcall␈α
with␈αthe␈αfunction␈αbody␈αand␈αthen␈α
an
␈↓ α←␈↓evaluation␈α⊃of␈α⊃the␈α⊃resulting␈α⊃expression.␈α⊃Free␈α⊃variables␈α⊃whose␈α⊃bindings␈α⊂are
␈↓ α←␈↓determined dynamically are called ␈↓↓fluid variables␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'In␈α∩review,␈α⊃the␈α∩evaluation␈α∩of␈α⊃a␈α∩typical␈α⊃function-call␈α∩will␈α∩involve␈α⊃the
␈↓ α←␈↓evaluation␈α
of␈α
the␈α
arguments,␈α
the␈α
binding␈α
of␈α
the␈α
λ-variables␈α
to␈α
those␈αvalues,
␈↓ α←␈↓the␈α⊂addition␈α⊂of␈α⊂these␈α⊂new␈α⊂bindings␈α⊂to␈α⊂the␈α⊂front␈α⊂of␈α⊂the␈α⊂symbol␈α⊂table,␈α∂and
␈↓ α←␈↓finally␈α⊃the␈α⊃evaluation␈α⊂of␈α⊃the␈α⊃body␈α⊂of␈α⊃the␈α⊃function.␈α⊂ That␈α⊃segment␈α⊃of␈α⊂the
␈↓ α←␈↓symbol␈α
table␈α
which␈α
we␈α
have␈α
just␈α
added␈α
by␈α
the␈α
λ-binding␈α
will␈α
be␈α
called␈α
the
␈↓ α←␈↓␈↓↓local␈α
symbol␈αtable␈↓␈α
or␈αlocal␈α
environment.␈αThe␈α
variables␈αwhich␈α
appear␈αin␈α
that
␈↓ α←␈↓segment␈α∞are␈α∞the␈α∞local␈α∞variables.␈α∞ The␈α∞remainder␈α∞of␈α∞the␈α∞symbol␈α∞table␈α
makes
␈↓ α←␈↓up␈αthe␈α
␈↓↓non-local␈αtable␈↓.␈α
Variables␈αwhich␈αappear␈α
in␈αthe␈α
global␈αtable␈α
but␈αnot
␈↓ α←␈↓in␈α∀any␈α∃local␈α∀table␈α∀are␈α∃the␈α∀␈↓↓global␈α∀variables␈↓.␈α∃ Free␈α∀variables␈α∃are␈α∀bound
␈↓ α←␈↓somewhere␈αbetween␈α
the␈αlocal␈αtable␈α
and␈αthe␈αglobal␈α
table.␈α Variables␈αwhich␈α
are
␈↓ α←␈↓local␈α⊂to␈α∂a␈α⊂form-evaluation␈α⊂are␈α∂those␈α⊂which␈α⊂were␈α∂present␈α⊂in␈α⊂the␈α∂λ-binding.
␈↓ α←␈↓We␈α∂first␈α∂wish␈α⊂to␈α∂develop␈α∂a␈α∂useful␈α⊂notation␈α∂for␈α∂describing␈α⊂bindings␈α∂before
␈↓ α←␈↓delving␈α
further␈α
into␈α
the␈αintricacies␈α
of␈α
binding␈α
strategies.␈α
That␈αdiscussion␈α
will
␈↓ α←␈↓be the content of Section 3.11.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Write␈α
a␈αLISP␈α
predicate,␈α
␈↓αnon␈α<=␈α
λ[[x;e]␈α...␈α
]␈↓,␈α
which␈αwill␈α
give␈α␈↓
t␈↓␈α
just␈α
in␈αthe
␈↓ α←␈↓␈↓ β∂case␈α∂that␈α∂␈↓αx␈↓␈α∂and␈α∂␈↓αe␈↓␈α∂represent␈α∂a␈α∂variable␈α∂and␈α∂a␈α⊂λ-expression␈α∂respectively,
␈↓ α←␈↓␈↓ β∂and ␈↓αx␈↓ is non-local to ␈↓αe␈↓.
␈↓" ␈↓ α←␈↓2. Give␈αan␈αexample␈αshowing␈αthat␈αthe␈αphrase␈α"latest␈αbinding"␈αis␈αnot␈αa␈αproper
␈↓ α←␈↓␈↓ β∂characterization of dynamic binding.
␈↓"β␈↓ α←␈↓␈↓ ∧}␈↓↓3.8 Environments and Bindings␈↓
␈↓"β␈↓ α←␈↓This␈αsection␈αwill␈αintroduce␈αone␈αmore␈αnotation␈αfor␈αdescribing␈αsymbol␈αtables␈α
or
␈↓ α←␈↓environments.␈α∂This␈α∞notation,␈α∂due␈α∂to␈α∞J.␈α∂Weizenbaum ([Wei 68]),␈α∂only␈α∞shows
␈↓ α←␈↓the␈α∞abstract␈α∞structure␈α∞of␈α∂the␈α∞symbol␈α∞table␈α∞manipulations␈α∂during␈α∞evaluation.
␈↓ α←␈↓Its␈α∞simplicity␈α∞will␈α∞be␈α∞of␈α∞great␈α
benefit␈α∞when␈α∞we␈α∞introduce␈α∞the␈α∞more␈α
complex
␈↓ α←␈↓binding schemes necessary for function-valued functions in Section 3.10.
␈↓"β␈↓ α←␈↓␈↓ β'In␈α
the␈α
previous␈α∞discussions␈α
it␈α
has␈α
been␈α∞sufficient␈α
to␈α
simply␈α
think␈α∞of␈α
a
␈↓ α←␈↓symbol␈α∪table␈α∪as␈α∪a␈α∀sequence␈α∪of␈α∪pairs;␈α∪each␈α∀pair␈α∪was␈α∪a␈α∪variable␈α∀and␈α∪its
␈↓ α←␈↓associated␈α⊂value.␈α∂This␈α⊂sufficed␈α⊂because␈α∂we␈α⊂dealt␈α∂only␈α⊂with␈α⊂λ-variables;␈α∂we
␈↓ α←␈↓␈↓132 Evaluation␈↓
(3.8␈↓
␈↓"β␈↓ α←␈↓ignored␈αthe␈αpossibility␈αof␈αfree␈αvariables.␈αAs␈αlong␈αas␈αwe␈αadded␈αthe␈αλ-bindings
␈↓ α←␈↓to␈α∞the␈α∂␈↓¬front␈↓␈α∞of␈α∂the␈α∞sequence␈α∞representing␈α∂the␈α∞symbol␈α∂table␈α∞we␈α∂showed␈α∞that
␈↓ α←␈↓expected␈α∪evaluation␈α∪would␈α∩result.␈α∪Local␈α∪values␈α∩were␈α∪found␈α∪in␈α∪the␈α∩table;
␈↓ α←␈↓global␈α
values␈αwere␈α
found␈αby␈α
explicit␈α
recognizers␈αin␈α
␈↓αeval␈↓␈αand␈α
␈↓αapply␈↓.␈α With␈α
the
␈↓ α←␈↓advent␈α∀of␈α∀free␈α∃variables,␈α∀however,␈α∀it␈α∀will␈α∃be␈α∀necessary␈α∀to␈α∃examine␈α∀the
␈↓ α←␈↓structure␈α∞of␈α∞environments␈α
more␈α∞closely.␈α∞ We␈α
will␈α∞describe␈α∞our␈α
environments
␈↓ α←␈↓in␈αterms␈αof␈αa␈αlocal␈αsymbol␈αtable␈αaugmented␈αby␈αa␈αdescription␈αof␈αwhere␈αto␈αlook
␈↓ α←␈↓for the non-local values.
␈↓"β␈↓ α←␈↓␈↓ β'Instead␈αof␈αhaving␈αone␈αamorphous␈αsequential␈αsymbol␈αtable,␈α
we␈αenvision
␈↓ α←␈↓a␈αsequence␈αof␈αtables.␈αOne␈αis␈αthe␈αlocal␈αtable,␈αand␈αits␈αsuccessor␈αin␈αthe␈αsequence
␈↓ α←␈↓is␈α∀the␈α∀previous␈α∀local␈α∀table.␈α∀ The␈α∀information␈α∀telling␈α∀where␈α∀to␈α∀find␈α∀the
␈↓ α←␈↓previous␈α
table␈α
is␈α
called␈α
the␈α
␈↓↓access␈α
chain␈↓␈α
or␈α
␈↓↓access␈α
link␈↓.␈α
Thus␈α
if␈α
tables␈α
are
␈↓ α←␈↓represented␈αby␈αE␈↓βi␈↓␈αand␈αthe␈αaccess␈αlink␈α
by␈α␈↓→␈↓␈αthen␈αwe␈αmight␈αrepresent␈αa␈α
symbol
␈↓ α←␈↓table as:
␈↓"∀␈↓ α←␈↓␈↓ ¬∨␈↓α(␈↓E␈↓βn␈↓ → ␈↓E␈↓βn-1␈↓ → ␈↓ ...␈↓→ ␈↓E␈↓β1 ␈↓→␈↓ E␈↓β0␈↓α)
␈↓"∀␈↓ α←␈↓where␈α
E␈↓βn␈↓␈αis␈α
the␈αlocal␈α
or␈α
current␈αsegment␈α
of␈αthe␈α
table.␈α
We␈αreserve␈α
E␈↓β0␈↓␈αto␈α
name
␈↓ α←␈↓the global table.
␈↓"β␈↓ α←␈↓␈↓ β'LISP␈αfinds␈α
local␈αbindings␈α
in␈αthe␈α
local␈αtable␈α
and␈αuses␈α
the␈αaccess␈αchain␈α
to
␈↓ α←␈↓find␈αbindings␈α
of␈αnon-local␈α
variables.␈α If␈αa␈α
variable␈αis␈α
not␈αfound␈α
in␈αany␈αof␈α
the
␈↓ α←␈↓tables, then it is unbound.
␈↓"β␈↓ α←␈↓An environment will be described as:
␈↓"∀␈↓ α←␈↓␈↓ εβ␈↓ ε3␈↓↓Form␈↓
␈↓"β␈↓ α←␈↓␈↓ εβ␈↓ ε3E␈↓βlocal␈↓
␈↓"β␈↓ α←␈↓␈↓ εβ␈↓ ε3| E␈↓βi␈↓
␈↓"β␈↓ α←␈↓␈↓ εβ_________
␈↓"β␈↓ α←␈↓␈↓ εβvar␈↓ ε3| value
␈↓"β␈↓ α←␈↓␈↓ εβ␈↓αv␈↓β1␈↓␈↓ ε3| ␈↓↓val␈↓β1␈↓
␈↓"β␈↓ α←␈↓␈↓ εβ␈↓αv␈↓β2␈↓␈↓ ε3| ␈↓↓val␈↓β2␈↓
␈↓"β␈↓ α←␈↓␈↓ εβ . . .
␈↓"β␈↓ α←␈↓␈↓ εβ␈↓αv␈↓βn␈↓␈↓ ε3| ␈↓↓val␈↓βn␈↓
␈↓"β␈↓ α←␈↓␈↓ εβ␈↓ ε3|
␈↓"∀␈↓ α←␈↓␈↓↓Form␈↓␈α
is␈αthe␈α
current␈αform␈α
being␈αevaluated.␈α
E␈↓βlocal␈↓␈αis␈α
the␈αname␈α
of␈α
the␈αcurrent
␈↓ α←␈↓environment␈αor␈αsymbol␈αtable.␈αLet␈α␈↓αx␈↓␈αbe␈αa␈αvariable␈αappearing␈αin␈α␈↓↓Form␈↓.␈αIf␈α␈↓αx␈↓␈αis
␈↓ α←␈↓not␈α
found␈α
among␈α
the␈α
␈↓αv␈↓βj␈↓'s,␈α
then␈α
entries␈α
in␈α
the␈α
table␈α
named␈α
E␈↓βi␈↓␈α∞are␈α
examined.
␈↓ α←␈↓If␈α∪␈↓αx␈↓␈α∪is␈α∪not␈α∪found␈α∪in␈α∪E␈↓βi␈↓␈α∪then␈α∪the␈α∪environment␈α∪mentioned␈α∪in␈α∪the␈α∩upper
␈↓ α←␈↓right-hand␈α⊃quadrant␈α⊃of␈α⊃E␈↓βi␈↓␈α⊃is␈α⊃searched.␈α⊃The␈α⊃search␈α⊃will␈α⊃terminate␈α⊃if␈α∩␈↓αx␈↓␈α⊃is
␈↓ α←␈↓found␈αas␈αa␈α␈↓αv␈↓βj␈↓;␈α
the␈αvalue␈αof␈α␈↓αx␈↓␈αis␈α
the␈αcorresponding␈α␈↓↓val␈↓βj␈↓.␈α If␈α
␈↓αx␈↓␈αis␈αnot␈αfound␈αin␈α
a
␈↓ α←␈↓local␈αtable,␈αand␈αthe␈αsymbol␈α"/"␈αappears␈αin␈αthe␈αright-hand␈αquadrant,␈αthen␈α␈↓αx␈↓␈αis
␈↓ α←␈↓unbound.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊂notation␈α∂is␈α⊂used␈α∂as␈α⊂follows:␈α⊂when␈α∂we␈α⊂begin␈α∂the␈α⊂evaluation␈α⊂of␈α∂a
␈↓ α←␈↓form,␈αthe␈αinitial␈α
table␈αE␈↓β0␈↓␈αis␈αset␈α
up␈αwith␈α"/"␈αin␈α
its␈αaccess␈αfield.␈α
The␈αexecution
␈↓ α←␈↓␈↓3.8␈↓ π"Environments and Bindings 133␈↓
␈↓"β␈↓ α←␈↓of␈α∩a␈α∩function␈α∩definition,␈α∩say␈α∩␈↓αf <= λ[[x;y] x␈↓π2␈↓α + y]␈↓,␈α∩will␈α∩add␈α∩an␈α∩appropriate
␈↓ α←␈↓entry␈α∞to␈α∞the␈α∂table,␈α∞binding␈α∞␈↓αf␈↓␈α∞to␈α∂its␈α∞lambda␈α∞definition.␈↓π 25␈↓␈α∞Now,␈α∂consider␈α∞the
␈↓ α←␈↓evaluation␈α
of␈α
the␈α
form␈α
␈↓αf[2;3]␈↓.␈α∞ When␈α
the␈α
λ-expression␈α
is␈α
entered,␈α∞i.e.,␈α
when
␈↓ α←␈↓we␈α∞bind␈α∞the␈α∞evaluated␈α
arguments␈α∞(␈↓α2␈↓␈α∞and␈α∞␈↓α3␈↓)␈α∞to␈α
the␈α∞λ-variables␈α∞(␈↓αx␈↓␈α∞and␈α∞␈↓αy␈↓),␈α
a
␈↓ α←␈↓new␈αlocal␈αtable␈α(E␈↓β1␈↓)␈αis␈αset␈αup␈α
with␈αan␈αaccess␈αlink␈αto␈αE␈↓β0␈↓.␈α Entries␈αreflecting␈α
the
␈↓ α←␈↓binding␈α
of␈α
the␈α∞λ-variables␈α
are␈α
made␈α∞in␈α
E␈↓β1␈↓␈α
and␈α∞evaluation␈α
of␈α
the␈α∞λ-body␈α
is
␈↓ α←␈↓begun.
␈↓"β␈↓ α←␈↓The flow of symbol table creation is:
␈↓"∀␈↓ α←␈↓␈↓ β∂␈↓ β3␈↓αf[2;3]␈↓ ¬/␈↓ ¬Sx␈↓π2␈↓α + y␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ β3E␈↓β0␈↓␈↓ βW␈↓ ¬/␈↓ ¬SE␈↓β1␈↓␈↓ ππ␈↓ π∨␈↓ πCE␈↓β0␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ β3| /␈↓ βW␈↓ ¬/␈↓ ¬S| E␈↓β0␈↓␈↓ ππ␈↓ π∨␈↓ πC| /
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ β3______ =>␈↓ ¬/______ =>␈↓ ππ______ return with value ␈↓α7␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂ ␈↓αf␈↓ β3| λ[[x;y] x␈↓π2␈↓α + y]␈↓␈↓ ¬/␈↓α x␈↓ ¬S| 2␈↓ ππ␈↓ π∨f␈↓ πC| λ[[x;y] ... ]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ β3␈↓ βW␈↓ ¬/ y␈↓ ¬S| 3␈↓
␈↓"∀␈↓ α←␈↓Compare this sequence to the example on page 120.
␈↓"β␈↓ α←␈↓␈↓ β'The sequence of tables corresponds to the evaluation sequence:
␈↓"∀␈↓ α←␈↓␈↓ ∧1␈↓αeval[␈↓
R␈↓∞( ␈↓αf[2;3] ␈↓∞)␈↓α; ␈↓
R␈↓∞( ␈↓α{<f , λ[[x;y] x␈↓π2␈↓α +y]>} ␈↓∞)␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ εO↓
␈↓"β␈↓ α←␈↓α␈↓ βTeval[␈↓
R␈↓∞( ␈↓αx␈↓π2␈↓α + y ␈↓∞)␈↓α; ␈↓
R␈↓∞( ␈↓α{<x, 2>, <y, 3>, <f , λ[[x;y] x␈↓π2␈↓α +y]>} ␈↓∞)␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ εO↓
␈↓"β␈↓ α←␈↓α␈↓ εP7␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'You␈α
should␈α
realize␈α
that␈αthe␈α
Weizenbaum␈α
environments␈α
are␈αjust␈α
another
␈↓ α←␈↓abstract␈αdata␈αstructure␈αwith␈αassociated␈αconstructors,␈αselectors,␈αand␈αrecognizers.
␈↓ α←␈↓They␈α
may␈αbe␈α
expressed␈α
as␈αLISP␈α
data␈α
structures␈αwithout␈α
much␈αdifficulty.␈α
The
␈↓ α←␈↓only␈αdifference␈αhere␈αis␈αthat␈αthe␈αenvironments␈αhappen␈αto␈αbe␈αmore␈αmeaningful
␈↓ α←␈↓when␈αdescribed␈αgraphically␈α
than␈αif␈αthey␈α
were␈αspecified␈αby␈αtheir␈α
manipulating
␈↓ α←␈↓functions.␈α∀See␈α∀the␈α∀problem␈α∀on␈α∀page 135.␈α∀ Graphical␈α∀representations␈α∀and
␈↓ α←␈↓languages␈αare␈α
an␈αimportant␈α
tool␈αin␈α
data␈αstructure␈α
programming;␈αwe␈α
will␈αsay␈α
a
␈↓ α←␈↓bit more about this in Section 5.4.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 25␈↓Note that we really mean "representation of lambda definition".
␈↓ α←␈↓␈↓134 Evaluation␈↓
(3.8␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∩execution␈α∩of␈α∩␈↓αfact[3]␈↓␈α∩on␈α∩page 122␈α∩results␈α∩in␈α∩a␈α∪more␈α∩interesting
␈↓ α←␈↓example.␈α∞The␈α
following␈α∞discussion␈α
should␈α∞be␈α
read␈α∞in␈α
conjunction␈α∞with␈α
that
␈↓ α←␈↓description.␈↓π 26␈↓
␈↓"␈↓ α←␈↓␈↓ β∂␈↓ βW␈↓ ∧[␈↓ ¬G␈↓ ε∂␈↓ π∨␈↓ πg␈↓ λ/␈↓ 3␈↓
⊃␈↓
␈↓"␈↓ α←␈↓␈↓ β∂␈↓ βW␈↓ ∧[␈↓ ¬G␈↓ ε∂␈↓ π∨␈↓ πg␈↓ λ/␈↓ 3␈↓
εα→ ␈↓α6␈↓
␈↓"␈↓ α←␈↓
␈↓ β∂␈↓ βW␈↓αfact[3]␈↓ ∧[␈↓ ¬G␈↓ ε∂[x=0→ ...]␈↓ π∨␈↓ πg*[x;fact[x-1]]␈↓ 3␈↓
ε←⊃␈↓α
␈↓"␈↓ α←␈↓α␈↓ β∂␈↓ βW␈↓E␈↓β0␈↓␈↓ ∧[␈↓ ¬G␈↓ ε∂E␈↓β1␈↓␈↓ π∨␈↓ πg␈↓ λ/E␈↓β1␈↓␈↓ 3␈↓
~ ~␈↓
␈↓"␈↓ α←␈↓␈↓ β∂␈↓ βW| /␈↓ ∧[␈↓ ¬G␈↓ ε∂| E␈↓β0␈↓␈↓ π∨␈↓ πg␈↓ λ/| E␈↓β0␈↓␈↓ 3␈↓
$ ↑␈↓
␈↓"␈↓ α←␈↓␈↓ β∂_______␈↓ ∧[=>␈↓ ¬G_______␈↓ π∨=>␈↓ πg_______ =>␈↓ 3␈↓ K␈↓ W␈↓α2␈↓
␈↓"␈↓ α←␈↓␈↓ β∂␈↓αfact␈↓ βW| λ[[x][x=0→1;...]␈↓ ¬G x␈↓ ε∂| 3␈↓ π∨␈↓ πg x␈↓ λ/| 3␈↓ 3␈↓ K␈↓ W␈↓
↑␈↓α
␈↓"␈↓ α←␈↓α␈↓ β∂␈↓ βW␈↓ ∧[␈↓ ¬G␈↓ ε∂␈↓ π∨␈↓ πg␈↓ λ/␈↓ 3␈↓
⊃ ~␈↓α
␈↓"␈↓ α←␈↓α␈↓ β∂␈↓ βW␈↓ ∧[␈↓ ¬G␈↓ ε∂␈↓ π∨␈↓ πg␈↓ λ/␈↓ 3␈↓
ε→$␈↓α
␈↓"␈↓ α←␈↓α␈↓ β∂␈↓ βWfact[2]␈↓ ∧[␈↓ ¬G␈↓ ε∂[x=0→ ...]␈↓ π∨␈↓ πg*[x;fact[x-1]]␈↓ 3␈↓
~␈↓α
␈↓"␈↓ α←␈↓α␈↓ β∂␈↓ βW␈↓E␈↓β1␈↓␈↓ ∧[␈↓ ¬G␈↓ ε∂E␈↓β2␈↓␈↓ π∨␈↓ πg␈↓ λ/E␈↓β2␈↓␈↓ 3␈↓
ε←⊃␈↓
␈↓"␈↓ α←␈↓␈↓ β∂␈↓ βW| E␈↓β0␈↓␈↓ ∧[␈↓ ¬G␈↓ ε∂| E␈↓β1␈↓␈↓ π∨␈↓ πg␈↓ λ/| E␈↓β1␈↓␈↓ 3␈↓
$ ↑␈↓
␈↓"␈↓ α←␈↓=>␈↓ β∂_______␈↓ ∧[=>␈↓ ¬G_______␈↓ π∨=>␈↓ πg_______ =>␈↓ 3␈↓ K␈↓ W␈↓α1␈↓
␈↓"␈↓ α←␈↓␈↓ β∂␈↓α x␈↓ βW| 3␈↓ ∧[␈↓ ¬G x␈↓ ε∂| 2␈↓ π∨␈↓ πg x␈↓ λ/| 2␈↓ 3␈↓ K␈↓ W␈↓
↑␈↓α
␈↓"␈↓ α←␈↓α␈↓ β∂␈↓ βW␈↓ ∧[␈↓ ¬G␈↓ ε∂␈↓ π∨␈↓ πg␈↓ λ/␈↓ 3␈↓ K␈↓ W␈↓
~␈↓α
␈↓"␈↓ α←␈↓α␈↓ β∂␈↓ βW␈↓ ∧[␈↓ ¬G␈↓ ε∂␈↓ π∨␈↓ πg␈↓ λ/␈↓ 3␈↓
⊃ ~␈↓α
␈↓"␈↓ α←␈↓α␈↓ β∂␈↓ βW␈↓ ∧[␈↓ ¬G␈↓ ε∂␈↓ π∨␈↓ πg␈↓ λ/␈↓ 3␈↓
ε→$␈↓α
␈↓"␈↓ α←␈↓α␈↓ β∂␈↓ βWfact[1]␈↓ ∧[␈↓ ¬G␈↓ ε∂[x=0→ ...]␈↓ π∨␈↓ πg*[x;fact[x-1]]␈↓ 3␈↓
~␈↓α
␈↓"␈↓ α←␈↓α␈↓ β∂␈↓ βW␈↓E␈↓β2␈↓␈↓ ∧[␈↓ ¬G␈↓ ε∂E␈↓β3␈↓␈↓ π∨␈↓ πg␈↓ λ/E␈↓β3␈↓␈↓ 3␈↓
ε←⊃␈↓
␈↓"␈↓ α←␈↓␈↓ β∂␈↓ βW| E␈↓β1␈↓␈↓ ∧[␈↓ ¬G␈↓ ε∂| E␈↓β2␈↓␈↓ π∨␈↓ πg␈↓ λ/| E␈↓β2␈↓␈↓ 3␈↓
$ ~␈↓
␈↓"␈↓ α←␈↓=>␈↓ β∂_______␈↓ ∧[=>␈↓ ¬G_______␈↓ π∨=>␈↓ πg_______ =>␈↓ 3␈↓ K␈↓ W␈↓
~␈↓
␈↓"␈↓ α←␈↓␈↓ β∂␈↓α x␈↓ βW| 2␈↓ ∧[␈↓ ¬G x␈↓ ε∂| 1␈↓ π∨␈↓ πg x␈↓ λ/| 1␈↓ 3␈↓ K␈↓ W␈↓
↑␈↓α
␈↓"␈↓ α←␈↓α␈↓ β∂␈↓ βW␈↓ ∧[␈↓ ¬G␈↓ ε∂␈↓ π∨␈↓ πg␈↓ λ/␈↓ 3␈↓ K␈↓ W1
␈↓"␈↓ α←␈↓α␈↓ β∂␈↓ βW␈↓ ∧[␈↓ ¬G␈↓ ε∂␈↓ π∨␈↓ πg␈↓ λ/␈↓ 3␈↓ K␈↓ W␈↓
↑␈↓α
␈↓"␈↓ α←␈↓α␈↓ β∂␈↓ βW␈↓ ∧[␈↓ ¬G␈↓ ε∂␈↓ π∨␈↓ πg␈↓ λ/␈↓ 3␈↓ K␈↓ W␈↓
~␈↓α
␈↓"␈↓ α←␈↓α␈↓ β∂␈↓ βWfact[0]␈↓ ∧[␈↓ ¬G␈↓ ε∂[x=0→1; ...]␈↓ π∨␈↓ πg␈↓ λ/␈↓ 3␈↓
⊃ ~␈↓α
␈↓"␈↓ α←␈↓α␈↓ β∂␈↓ βWE␈↓β3␈↓α␈↓ ∧[␈↓ ¬G␈↓ ε∂E␈↓β4␈↓α␈↓ π∨␈↓ πg␈↓ λ/␈↓ 3␈↓
~ ↑␈↓α
␈↓"␈↓ α←␈↓α␈↓ β∂␈↓ βW| E␈↓β2␈↓α␈↓ ∧[␈↓ ¬G␈↓ ε∂| E␈↓β3␈↓α␈↓ π∨␈↓ πg␈↓ λ/␈↓send␈↓α␈↓ 3␈↓
~ ~␈↓α
␈↓"␈↓ α←␈↓α=>␈↓ β∂_______␈↓ ∧[=>␈↓ ¬G_______␈↓ π∨=> 1␈↓ πg␈↓ λ/ 1␈↓ 3␈↓
ε→$␈↓α
␈↓"␈↓ α←␈↓α␈↓ β∂ x␈↓ βW| 1␈↓ ∧[␈↓ ¬G x␈↓ ε∂| 0␈↓ π∨␈↓ πg␈↓ λ/␈↓back up␈↓ 3␈↓
$␈↓
␈↓"∀␈↓ α←␈↓At␈αthe␈αend␈αof␈αthe␈αfirst␈αline␈αwe␈αare␈αfaced␈αwith␈αthe␈αevaluation␈αof␈α␈↓α*[x;fact[x-1]]␈↓.
␈↓ α←␈↓This␈α
requires␈αthe␈α
evaluation␈αof␈α
the␈αarguments␈α
to␈α
*;␈αthis␈α
is␈αdone␈α
by␈α␈↓αevlis␈↓.␈α
First
␈↓ α←␈↓␈↓αx␈↓␈α
is␈α
evaluated␈α
and␈α
saved,␈↓π 27␈↓␈α
then␈α
the␈α
evaluation␈α
of␈α
␈↓αfact[x-1]␈↓␈α
is␈α∞begun␈α
using
␈↓ α←␈↓environment␈αE␈↓β1␈↓.␈αIn␈αE␈↓β1␈↓,␈α␈↓αx-1␈↓␈αgives␈α␈↓α2␈↓␈αand␈αwe␈αfind␈αthe␈αdefinition␈αof␈α␈↓αfact␈↓␈αin␈αE␈↓β0␈↓.
␈↓ α←␈↓In␈α∞the␈α∞second␈α∞line␈α∞we␈α∞set␈α∞up␈α∞E␈↓β2␈↓␈α∞and␈α∞evaluate␈α∞␈↓αfact[2]␈↓.␈α∞Analogous␈α∞situations
␈↓ α←␈↓occur␈α
until␈α
the␈αfourth␈α
line;␈α
at␈αthis␈α
time␈α
we␈α
suddenly␈αfind␈α
ourselves␈α
in␈αE␈↓β4␈↓␈α
with
␈↓ α←␈↓␈↓αx␈↓␈αbound␈αto␈α␈↓α0␈↓.␈αThe␈αexpression␈α␈↓αx=0␈↓␈α
is␈αsatisfied␈αand␈αwe␈αstart␈αback␈αup␈αthe␈α
right
␈↓ α←␈↓margin␈α∪to␈α∪conclude␈α∪the␈α∩nested␈α∪evaluations␈α∪of␈α∪␈↓α*[x;fact[x-1]]␈↓.␈α∪This␈α∩process
␈↓ α←␈↓finally␈αterminates␈αat␈αthe␈αtop,␈αreturning␈αa␈αvalue␈α␈↓α6␈↓.␈α Notice␈αthat␈αwe␈αwill␈αget␈αthe
␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 26␈↓The layout of this example is due to R. Davis.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 27␈↓This␈αsaved␈αinformation␈αis␈αnot␈αexplicitly␈αrepresented␈αin␈αthese␈αpictures
␈↓ α←␈↓or in the Weizenbaum diagrams.
␈↓ α←␈↓␈↓3.8␈↓ π"Environments and Bindings 135␈↓
␈↓"β␈↓ α←␈↓correct␈α
binding␈α
of␈α
␈↓αx␈↓␈α
locally.␈α
It␈α
is␈α
important␈α
to␈α
note␈α
that␈α
the␈α
occurrence␈α
of␈α
␈↓αfact␈↓
␈↓ α←␈↓within␈α∂the␈α⊂body␈α∂of␈α∂the␈α⊂definition␈α∂of␈α⊂␈↓αfact␈↓␈α∂is␈α∂␈↓¬global␈↓.␈↓π 28␈↓␈α⊂We␈α∂find␈α⊂the␈α∂correct
␈↓ α←␈↓binding␈α∞for␈α∞␈↓αfact␈↓␈α∞by␈α∞searching␈α∞the␈α∞access␈α∞chain.␈α∞ We␈α∞must␈α∞search␈α∞the␈α
access
␈↓ α←␈↓chain␈α
even␈α
though␈α
␈↓αfact␈↓␈α
is␈α
global.␈α
We␈α
cannot␈α
shortcut␈α
the␈α
search␈α
by␈α
simply
␈↓ α←␈↓looking␈α⊗in␈α∃E␈↓β0␈↓.␈α⊗A␈α∃variable␈α⊗might␈α∃have␈α⊗been␈α∃rebound␈α⊗in␈α⊗an␈α∃enclosing
␈↓ α←␈↓environment and it would be that binding we should discover.
␈↓"β␈↓ α←␈↓␈↓ β'As␈α∀a␈α∀final␈α∀example␈α∀showing␈α∀access␈α∀to␈α∀non-local␈α∃variable␈α∀bindings
␈↓ α←␈↓consider ␈↓αf[3]␈↓ where ␈↓αf <= λ[[x] g[2]]␈↓ and ␈↓αg <= λ[[y] x+y]␈↓.
␈↓"∀␈↓ α←␈↓␈↓ βc␈↓ ∧π␈↓αf[3]␈↓ ∧C␈↓ ε≠␈↓ ε3g[2]␈↓ πs␈↓ λ/␈↓ λSx + y
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧πE␈↓β0␈↓α␈↓ ∧C␈↓ ε≠␈↓ ε3E␈↓β1␈↓α␈↓ πs␈↓ λ/␈↓ λSE␈↓β2␈↓α
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧π| /␈↓ ∧C␈↓ ε≠␈↓ ε3| E␈↓β0␈↓α␈↓ πs␈↓ λ/␈↓ λS| E␈↓β1␈↓α
␈↓"β␈↓ α←␈↓α␈↓ βc______␈↓ ∧C =>␈↓ ε≠______␈↓ πs=>␈↓ λ/______␈↓ ∂
␈↓"β␈↓ α←␈↓α␈↓ βc f␈↓ ∧π| λ[[x] g[2]]␈↓ ε≠x␈↓ ε3| 3␈↓ πs␈↓ λ/y␈↓ λS| 2
␈↓"β␈↓ α←␈↓α␈↓ βc g␈↓ ∧π| λ[[y] x+y]
␈↓"∀␈↓ α←␈↓Notice␈αthat␈αwhen␈αwe␈αevaluate␈α␈↓αx␈α+␈αy␈↓␈αwe␈αfind␈α␈↓αy␈↓␈αhas␈αa␈αlocal␈αvalue,␈αbut␈αwe␈αmust
␈↓ α←␈↓look down the access chain to find a binding for ␈↓αx␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αscheme␈αfor␈αusing␈αWeizenbaum␈αenvironments␈αfor␈αthe␈αcurrent␈αLISP
␈↓ α←␈↓subset is:
␈↓"β␈↓ α←␈↓␈↓ β≠When␈α→preparing␈α→a␈α→λ-binding,␈α→set␈α_up␈α→a␈α→new␈α→E␈↓βnew␈↓␈α→with␈α_the
␈↓ α←␈↓␈↓ β≠λ-variables␈α
as␈α∞the␈α
local␈α
variable␈α∞entries␈α
and␈α
add␈α∞the␈α
values␈α∞of␈α
the
␈↓ α←␈↓␈↓ β≠arguments␈α
as␈α
the␈α
corresponding␈α
value␈α
entries.␈α
The␈α
access␈α
slot␈α
of␈α
the
␈↓ α←␈↓␈↓ β≠new␈α≤E␈↓βnew␈↓␈α≠points␈α≤to␈α≠the␈α≤previous␈α≠access␈α≤environment.␈α≠ The
␈↓ α←␈↓␈↓ β≠evaluation␈α⊂of␈α⊂the␈α⊂body␈α⊂of␈α⊂the␈α⊂λ-expression␈α⊂takes␈α⊂place␈α⊂using␈α⊂the
␈↓ α←␈↓␈↓ β≠new␈α
table;␈α
when␈α
a␈αlocal␈α
variable␈α
is␈α
accessed␈αwe␈α
find␈α
it␈α
in␈αE␈↓βnew␈↓;␈α
when
␈↓ α←␈↓␈↓ β≠a␈α⊂non-local␈α∂variable␈α⊂occurs,␈α∂we␈α⊂chase␈α∂the␈α⊂access␈α∂chain␈α⊂to␈α⊂find␈α∂its
␈↓ α←␈↓␈↓ β≠value.
␈↓" ␈↓ α←␈↓␈↓ β≠When␈αthe␈αevaluation␈αof␈αthe␈α
body␈αis␈αcompleted,␈αE␈↓βnew␈↓␈αdisappears␈α
and
␈↓ α←␈↓␈↓ β≠the previous environment is restored.
␈↓"β␈↓ α←␈↓␈↓ β'You␈α
should␈α
verify␈α
that␈α
the␈α
current␈α
access-␈α
and␈α
binding-scheme␈α
espoused
␈↓ α←␈↓by LISP is faithfully described in these diagrams.
␈↓"β␈↓ α←␈↓␈↓ ε≡␈↓↓Problem␈↓
␈↓"→␈↓ α←␈↓1. Environments␈α∂really␈α∞are␈α∂a␈α∂class␈α∞of␈α∂abstract␈α∞data␈α∂structures:␈α∂they␈α∞include
␈↓ α←␈↓␈↓ β∂constructors,␈α∂selectors,␈α∂and␈α∂recognizers.␈α∂ To␈α∞help␈α∂discover␈α∂what␈α∂a␈α∂set␈α∞of
␈↓ α←␈↓␈↓ β∂such␈α≠functions␈α≠might␈α≠be,␈α≠give␈α≠a␈α≠representation␈α≠for␈α~Weizenbaum
␈↓ α←␈↓␈↓ β∂environments␈α∞and␈α∞write␈α∞new␈α∂versions␈α∞of␈α∞the␈α∞symbol␈α∂table␈α∞manipulating
␈↓ α←␈↓␈↓ β∂functions,␈α→␈↓αlookup␈↓␈α_and␈α→␈↓αmkenv␈↓,␈α→which␈α_will␈α→operate␈α→on␈α_Weizenbaum
␈↓ α←␈↓␈↓ β∂environments. See page 124.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 28␈↓Notice that ␈↓αeq␈↓, +, and * are also global.
␈↓ α←␈↓␈↓136 Evaluation␈↓
(3.9␈↓
␈↓"β␈↓ α←␈↓␈↓ ε∨␈↓↓3.9 ␈↓αlabel␈↓↓␈↓α
␈↓"β␈↓ α←␈↓Placing␈α"λ"␈α
and␈αa␈αlist␈α
of␈αλ-variables␈α
in␈αfront␈αof␈α
an␈αexpresson␈α
designates␈αthe
␈↓ α←␈↓variables␈α∂which␈α∂appear␈α∂in␈α∂the␈α∂λ-list␈α∂as␈α∂local␈α∂variables.␈α∂All␈α∂other␈α∂variables
␈↓ α←␈↓appearing␈αin␈αthe␈αexpression␈αare␈αnon-local.␈α For␈αexample,␈α␈↓αf␈↓␈αis␈αnon-local␈αin␈αthe
␈↓ α←␈↓following:
␈↓"∀␈↓ α←␈↓␈↓ ∧Q␈↓αf <= λ[[x][zerop[x] → 1; ␈↓
t␈↓α → *[x;f[x-1]]] ] ␈↓
␈↓"∀␈↓ α←␈↓Clearly␈αour␈αintention␈αis␈αthat␈αthe␈α␈↓αf␈↓␈αappearing␈αto␈αthe␈αright␈αof␈α"<="␈αis␈αthe␈αsame
␈↓ α←␈↓as the ␈↓αf␈↓ appearing to the left of "<=".
␈↓"β␈↓ α←␈↓␈↓ β'This␈α
has␈α∞not␈α
been␈α∞a␈α
problem␈α
for␈α∞us.␈α
We␈α∞have␈α
simply␈α∞pre-loaded␈α
the
␈↓ α←␈↓symbol␈α⊃table,␈α⊃binding␈α⊃␈↓αf␈↓␈α⊃to␈α⊃its␈α⊃definition;␈α⊃see␈α⊃page 122.␈α⊃LISP␈α⊃has␈α⊃a␈α⊃more
␈↓ α←␈↓elegant device for this binding. It is called the ␈↓αlabel␈↓ operator and is written:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ¬≤␈↓αlabel[␈↓<identifier>;<function>]
␈↓"∀␈↓ α←␈↓Its␈α∂evaluation␈α∂has␈α∞the␈α∂effect␈α∂of␈α∂binding␈α∞the␈α∂<identifier>␈α∂to␈α∂the␈α∞<function>.
␈↓ α←␈↓The␈α
value␈α
constructed␈αby␈α
executing␈α
a␈α
␈↓αlabel␈↓-expression␈αis␈α
a␈α
representation␈αof␈α
a
␈↓ α←␈↓function with name <identifier> and body <function>.
␈↓"β␈↓ α←␈↓␈↓ β'For example, a proper definition of ␈↓αfact␈↓ is:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ∧∂␈↓αlabel[fact; λ[[x][eq[x;0] → 1; ␈↓
t␈↓α → *[x;fact[sub1[x]]]]]]␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'To include ␈↓αlabel␈↓ in the LISP syntax add:
␈↓"∀␈↓ α←␈↓␈↓ ∧D<function>::= ␈↓αlabel␈↓[<identifier>;<function>]
␈↓"∀␈↓ α←␈↓and the S-expr translation of the ␈↓αlabel␈↓ construct should naturally be:
␈↓"∀␈↓ α←␈↓␈↓ ∧(␈↓
R␈↓∞( ␈↓αlabel[f;fn] ␈↓∞)␈↓α = (LABEL ␈↓
R␈↓∞( ␈↓αf ␈↓∞)␈↓α ␈↓
R␈↓∞( ␈↓αfn ␈↓∞)␈↓α)␈↓
␈↓"∀␈↓ α←␈↓Note that ␈↓αlabel␈↓ is a special form, not a call-by-value function.
␈↓"β␈↓ α←␈↓␈↓ β'Since␈α⊃the␈α∩␈↓αlabel␈↓␈α⊃operator␈α⊃creates␈α∩a␈α⊃function,␈α⊃it␈α∩should␈α⊃appear␈α∩in␈α⊃the
␈↓ α←␈↓function␈αposition␈αof␈α
a␈αfunction␈αapplication.␈α A␈α
typical␈αapplication␈αof␈αthe␈α
␈↓αlabel␈↓
␈↓ α←␈↓construct,␈α∩say␈α⊃␈↓αlabel[f;λ[[x]␈α∩␈↓λx␈↓α[x]]][␈↓εe␈↓α]␈↓,␈α∩results␈α⊃in␈α∩the␈α∩following␈α⊃environmental
␈↓ α←␈↓picture when we get ready to evaluate ␈↓λx␈↓α[x]␈↓:
␈↓"∀␈↓ α←␈↓␈↓ β∂␈↓αlabel[f;λ[[x] ␈↓λx␈↓α[x]]][␈↓εe␈↓α]␈↓␈↓ ε∂␈↓ ε3␈↓λx␈↓α[␈↓εe␈↓α]␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ ∧πE␈↓β0␈↓␈↓ ¬G␈↓ ε∂␈↓ ε3E␈↓β1␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ ∧π | /␈↓ ¬G␈↓ ε∂␈↓ ε3| E␈↓β0␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂ ___________␈↓ ¬G=>␈↓ ε∂______
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ ∧π |␈↓ ¬G␈↓ ε∂␈↓αf␈↓ ε3| λ[[x] ␈↓λx␈↓α[x]]
␈↓"∀␈↓ α←␈↓Notice␈α⊂that␈α⊃␈↓αlabel[f;λ[[x]␈↓λx␈↓α]][␈↓εe␈↓α]␈↓␈α⊂is␈α⊂equivalent␈α⊃to␈α⊂␈↓αλ[[x]␈↓λx␈↓α[␈↓εe␈↓α]][quote[␈↓εe␈↓α]]␈↓;␈α⊃notice␈α⊂too
␈↓ α←␈↓that␈α
the␈α
definition␈α
does␈α
not␈α
appear␈α∞in␈α
the␈α
global␈α
table␈α
E␈↓β0␈↓.␈α
We␈α
use␈α∞␈↓αlabel␈↓␈α
to
␈↓ α←␈↓create␈α
temporary␈α∞function␈α
definitions.␈α
Such␈α∞definitions␈α
disappear␈α∞when␈α
the
␈↓ α←␈↓environment␈α∞in␈α∞which␈α∞the␈α∞␈↓αlabel␈↓␈α∞was␈α∞executed␈α∞is␈α∞no␈α∞longer␈α∞accessible␈α∂to␈α∞the
␈↓ α←␈↓computation.␈α
Thus␈α
within␈α
the␈α
evaluation␈αof␈α
the␈α
body␈α
␈↓λx␈↓α[x]␈↓␈α
a␈α
recursive␈αcall␈α
on
␈↓ α←␈↓␈↓αf␈↓␈αwill␈αrefer␈αto␈αthe␈αdefinition␈αof␈α␈↓αf␈↓␈αlocated␈αin␈αE␈↓β1␈↓␈αso␈αlong␈αas␈α␈↓αf␈↓␈αis␈αnot␈αrebound␈αin
␈↓ α←␈↓␈↓3.9␈↓ A␈↓αlabel␈↓ 137␈↓α
␈↓"β␈↓ α←␈↓␈↓λx␈↓;␈αonce␈αwe␈α
have␈αcompleted␈αthe␈α
computation␈αinitialized␈αin␈α
E␈↓β0␈↓␈αthe␈αdefinition␈α
of
␈↓ α←␈↓␈↓αf␈↓␈αwill␈αdisappear.␈α
If␈α␈↓αf␈↓␈αis␈α
not␈αrecursive,␈αthen␈α
the␈αuse␈αof␈α
␈↓αlabel␈↓␈αis␈αunnecessary;␈α
an
␈↓ α←␈↓anonymous function application will suffice.
␈↓"β␈↓ α←␈↓␈↓ β'What␈α8about␈α7statements␈α8like␈α7"evaluate␈α8␈↓αg[A;B]␈↓␈α7where
␈↓ α←␈↓␈↓αg <= λ[[x;y] ... f[u;v] ...]␈↓␈α
and␈α
␈↓αf <= λ[[x;y] ... ]␈↓ ?"␈α
␈↓αlabel␈↓␈α
defines␈α
only␈αone␈α
function;
␈↓ α←␈↓we␈αmay␈α
not␈αsay␈α␈↓αlabel[f,g; ... ]␈↓.␈α
What␈αwe␈α␈↓¬can␈↓␈α
do␈αis␈αembed␈α
the␈α␈↓αlabel␈↓-definition
␈↓ α←␈↓for ␈↓αf␈↓ within the ␈↓αlabel␈↓-definition for ␈↓αg␈↓.␈↓π 29␈↓ Thus:
␈↓"∀␈↓ α←␈↓α␈↓ ∧:label[g; λ[[x;y] ... label[f; λ[[x;y] ... ]][u;v] ...]]␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Several␈α→languages␈α→allow␈α→a␈α→simpler␈α→notation␈α→for␈α~giving␈α→mutually
␈↓ α←␈↓recursive definitions; see [Rey 72], [Hew 74], or [Sus 75].
␈↓"β␈↓ α←␈↓␈↓ β'It␈α
can␈α
be␈α
shown␈α
that␈α
the␈α
␈↓αlabel␈↓␈α
operator␈α
is␈α
superfluous;␈α
the␈α∞same␈α
effect
␈↓ α←␈↓can␈αbe␈αobtained␈αby␈αa␈αcomplicated␈αλ-binding.␈α However␈αour␈αpoint␈αhere␈αis␈αnot
␈↓ α←␈↓to␈α⊂be␈α⊂"minimal",␈α∂but␈α⊂to␈α⊂be␈α⊂"useful".␈α∂ Implementations␈α⊂of␈α⊂LISP␈α⊂offer␈α∂other
␈↓ α←␈↓definitional␈αfacilities,␈αwith␈α"<="␈αhaving␈αthe␈αeffect␈αof␈αpermanently␈αestablishing
␈↓ α←␈↓the definition in E␈↓β0␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α→apparent␈α→simplicity␈α→of␈α→the␈α_␈↓αlabel␈↓␈α→operator␈α→is␈α→partly␈α→due␈α_to
␈↓ α←␈↓misconception␈αand␈αpartly␈αdue␈αto␈αthe␈αrestrictions␈αplaced␈αon␈αthe␈αcurrent␈αsubset
␈↓ α←␈↓of LISP. The following sections will illuminate some of these difficulties.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Show one way to change ␈↓αeval␈↓ to handle ␈↓αlabel␈↓.
␈↓" ␈↓ α←␈↓2. Express the definition of ␈↓αreverse␈↓ given on page 48 using ␈↓αlabel␈↓.
␈↓" ␈↓ α←␈↓3. Evaluate the following:
␈↓"∀␈↓ α←␈↓α␈↓ ¬=λ[[y] label[fn;fn␈↓β2␈↓α][␈↓
f␈↓α]] [␈↓
f␈↓α]
␈↓" ␈↓ α←␈↓α␈↓where:␈↓α␈↓ ∧kfn␈↓β2␈↓α <= λ[[x][y → 1; x → 2; ␈↓
t␈↓α → fn␈↓β1␈↓α[␈↓
t␈↓α]]
␈↓" ␈↓ α←␈↓α␈↓and:␈↓α␈↓ ¬afn␈↓β1␈↓α <= λ[[y] fn[y]]
␈↓"β␈↓ α←␈↓␈↓ ∧↓␈↓↓3.10 Functional Arguments and Functional Values␈↓
␈↓"β␈↓ α←␈↓Recall our discussion of :
␈↓"∀␈↓ α←␈↓α␈↓ β7eval[(F 2 3);((F . (LAMBDA (X Y) (PLUS (EXPT X 2) Y))))]
␈↓"∀␈↓ α←␈↓We now know this is equivalent to:
␈↓"∀␈↓ α←␈↓␈↓ β∞␈↓αeval[((LABEL F (LAMBDA (X Y) (PLUS (EXPT X 2) Y))) 2 3);( )]␈↓
␈↓"∀␈↓ α←␈↓In␈αeither␈αcase,␈αthe␈αeffect␈αis␈αto␈αbind␈αthe␈αname␈α␈↓αf␈↓␈αto␈αthe␈α
λ-expression.␈α Binding
␈↓ α←␈↓also␈αoccurs␈αwhen␈α␈↓αf␈↓␈α
is␈αcalled:␈αwe␈αbind␈α␈↓αx␈↓␈α
to␈α␈↓α2␈↓,␈αand␈α␈↓αy␈↓␈αto␈α
␈↓α3␈↓.␈α In␈αthe␈αlatter␈αcase␈α
we
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 29␈↓Indeed␈α∪␈↓¬every␈↓␈α∪occurrence␈α∀of␈α∪␈↓αf␈↓␈α∪must␈α∀be␈α∪replaced␈α∪by␈α∀the␈α∪␈↓αlabel[f;...]␈↓
␈↓ α←␈↓construct.
␈↓ α←␈↓␈↓138 Evaluation␈↓
_3.10␈↓
␈↓"β␈↓ α←␈↓are␈αbinding␈αsimple␈αvalues;␈αin␈αthe␈αformer␈αwe␈αare␈αbinding␈αfunctions␈αas␈αvalues.
␈↓ α←␈↓We␈α
have␈α
decided␈α
that␈α
the␈α
necessary␈α
ingredients␈α
to␈α
characterize␈α
a␈α
functional
␈↓ α←␈↓value␈↓π 30␈↓␈αare␈αa␈α
representation␈αof␈αthe␈α
formal␈αparameters,␈αand␈α
a␈αrepresentation
␈↓ α←␈↓of␈α
the␈α
expression␈α∞described␈α
in␈α
the␈α∞body␈α
of␈α
the␈α∞function.␈α
In␈α
this␈α∞section␈α
we
␈↓ α←␈↓will␈αexamine␈αthe␈αadequacy␈αof␈αthat␈αdecision.␈α We␈αbegin␈αinformally␈αwith␈αa␈αfew
␈↓ α←␈↓examples.
␈↓"β␈↓ α←␈↓␈↓ β'Assume␈αwe␈αhave␈αa␈αlist␈α␈↓αl␈↓␈αof␈αdotted-pairs␈α␈↓λα␈↓β1␈↓ ,..., ␈↓λα␈↓βn␈↓,␈αand␈αwe␈αwish␈αto␈αform
␈↓ α←␈↓a␈α
new␈α∞list␈α
of␈α∞the␈α
form␈α∞␈↓α(car[␈↓λα␈↓β1␈↓α] ... car[␈↓λα␈↓βn␈↓α])␈↓.␈α
That␈α
is␈α∞we␈α
wish␈α∞to␈α
apply␈α∞␈↓αcar␈↓␈α
to
␈↓ α←␈↓each of the elements of ␈↓αl␈↓. Such a function is easy to write:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ β≠␈↓αcarfirst <= λ[[l][null[l] → ( ); ␈↓
t␈↓α → concat[car[first[l]];carfirst[rest[l]]]]]␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Now␈α
suppose␈α
we␈αwish␈α
to␈α
write␈α
a␈αmore␈α
general␈α
function,␈α
which␈αinstead
␈↓ α←␈↓of␈α
being␈α
specific␈α
to␈α
␈↓αcar␈↓,␈α
will␈α
take␈αan␈α
␈↓¬arbitrary␈↓␈α
unary␈α
function␈α
␈↓αf␈↓␈α
and␈α
apply␈αit␈α
to
␈↓ α←␈↓each␈αof␈αthe␈αelements␈αof␈α␈↓αl␈↓,␈αgenerating␈α␈↓α(f[␈↓λα␈↓β1␈↓α], ..., f[␈↓λα␈↓βn␈↓α])␈↓.␈α Such␈αa␈αfunction␈αcould
␈↓ α←␈↓plausibly be defined as follows:
␈↓"∀␈↓ α←␈↓␈↓ β∂␈↓αmapfirst <= λ[[fn;l][␈↓ ¬↔null[l] → ( );
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ¬↔␈↓
t␈↓α → concat[fn[first[l]];mapfirst[fn;rest[l]]]]] ␈↓
␈↓"∀␈↓ α←␈↓Thus the first calculation we requested above could be expressed as:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ¬λ␈↓αmapfirst[car;l]␈↓ ....... or could it?
␈↓"∀␈↓ α←␈↓Recalling␈αLISP's␈α
penchant␈αfor␈α
call-by-value␈αevaluation,␈α
we␈αmight␈αbelieve␈α
that
␈↓ α←␈↓the␈α∀computation␈α∀would␈α∀not␈α∀be␈α∀done␈α∪as␈α∀expected.␈α∀We␈α∀do␈α∀␈↓¬not␈↓␈α∀want␈α∪the
␈↓ α←␈↓argument␈α∩␈↓αcar␈↓␈α⊃evaluated␈α∩to␈α∩produce␈α⊃an␈α∩S-expr␈α∩value;␈α⊃rather,␈α∩we␈α∩want␈α⊃its
␈↓ α←␈↓evaluation␈α∞to␈α∞produce␈α∞a␈α∞representation␈α∞of␈α∞a␈α∞primitive␈α∞function,␈α∞suitable␈α
for
␈↓ α←␈↓application.␈α⊂ There␈α⊂are␈α⊂two␈α⊂ways␈α⊂out␈α⊂of␈α⊂this␈α⊂dilemma.␈α⊂One␈α⊂solution␈α⊃is␈α⊂to
␈↓ α←␈↓suppress␈α∂the␈α⊂evaluation␈α∂of␈α⊂␈↓αcar␈↓,␈α∂postponing␈α⊂it␈α∂until␈α⊂the␈α∂␈↓αapply␈↓␈α⊂function␈α∂can
␈↓ α←␈↓recognize␈αthat␈αa␈αfunction␈αname␈αhas␈αbeen␈αseen.␈α We␈αhave␈αseen␈αone␈αartifact␈αin
␈↓ α←␈↓LISP␈α
to␈αsubdue␈α
evaluation:␈αwe␈α
can␈αmake␈α
it␈αa␈α
constant␈αby␈α
␈↓αquote␈↓-ing␈αit.␈α
Indeed,
␈↓ α←␈↓␈↓αmapfirst[quote[car];l]␈α∩␈↓or␈α⊃␈↓αmapfirst[CAR;l]␈↓␈α∩will␈α⊃work.␈α∩ You␈α∩should␈α⊃convince
␈↓ α←␈↓yourself␈α∞that␈α∞␈↓αmapfirst[CAR;l]␈↓␈α∞will␈α
compute␈α∞␈↓αcarfirst[l]␈↓;␈α∞that␈α∞exercise␈α
requires
␈↓ α←␈↓examining the details of ␈↓αeval␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'A␈αsecond␈αsolution␈αexists␈αand␈αis␈αthe␈αone␈αwe␈αwill␈αpursue.␈αWe␈αsay␈αthat␈αthe
␈↓ α←␈↓"value"␈α∞of␈α∞␈↓αcar␈↓␈α∞␈↓¬is␈↓␈α∞the␈α∞description␈α
of␈α∞the␈α∞program␈α∞which␈α∞computes␈α∞␈↓αcar␈↓.␈α
Since
␈↓ α←␈↓␈↓αcar␈↓␈α↔is␈α↔a␈α↔primitive,␈α↔that␈α↔description␈α↔is␈α↔machine␈α↔code␈α↔for␈α_this␈α↔specific
␈↓ α←␈↓implementation.
␈↓"β␈↓ α←␈↓␈↓ β'Before␈α
going␈α
on␈α
to␈α
more␈α
complex␈α
examples␈α
it␈α
would␈α
be␈α
well␈α
to␈α
note␈α
that
␈↓ α←␈↓␈↓αmapfirst␈↓␈α∩is␈α∩a␈α⊃different␈α∩kind␈α∩of␈α⊃LISP␈α∩function␈α∩from␈α⊃those␈α∩we␈α∩have␈α⊃seen
␈↓ α←␈↓before.␈α∞ The␈α
first␈α∞argument␈α
to␈α∞␈↓αmapfirst␈↓␈α
is␈α∞expected␈α
to␈α∞represent␈α∞a␈α
function.
␈↓ α←␈↓Notice␈α∞that␈α
the␈α∞argument␈α
␈↓αfn␈↓␈α∞appears␈α
in␈α∞the␈α
body␈α∞of␈α
␈↓αmapfirst␈↓␈α∞in␈α∞a␈α
position
␈↓ α←␈↓reserved␈α
for␈α
functions.␈α
Therefore␈α
any␈αparameter␈α
bound␈α
to␈α
␈↓αfn␈↓␈α
is␈α
expected␈αto
␈↓ α←␈↓be a function. Such a use of a function is called a ␈↓↓functional argument␈↓.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 30␈↓It␈αwould␈αbe␈α
better␈αto␈αcall␈αthese␈α
constructs␈α"procedure␈αvalues"␈αsince␈α
we
␈↓ α←␈↓will take a decidedly algorithmic interpretation of them.
␈↓ α←␈↓␈↓3.10␈↓ ¬NFunctional Arguments and Functional Values 139␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈αfirst␈αtrick␈αwe␈αused␈αabove,␈αrepresenting␈αthe␈αfunctional␈αargument␈α
␈↓αcar␈↓
␈↓ α←␈↓as a constant ␈↓αCAR␈↓, can be applied to other instances of functional arguments.
␈↓"β␈↓ α←␈↓␈↓ β'Thus the functional argument:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ε
␈↓αλ[[x] f[g[x]]␈↓
␈↓"∀␈↓ α←␈↓could be represented as, ␈↓α(LAMBDA (X) (F (G X))) ␈↓
␈↓"∀␈↓ α←␈↓The␈α⊃trick␈α⊃is␈α⊃called␈α⊃␈↓αQUOTE␈↓-ing␈α⊃the␈α⊃functional␈α⊃argument␈α⊃since␈α∩the␈α⊃S-expr
␈↓ α←␈↓representation␈αof␈αan␈αinstance␈αof␈α
such␈αa␈αconstruct␈αis␈αa␈α
␈↓αQUOTE␈↓-ed␈αexpression.
␈↓ α←␈↓␈↓αQUOTE␈↓-ing␈αis␈αnot␈αstrictly␈αnecessary␈αif␈αwe␈αfollow␈αthe␈αsecond␈αalternative␈αabove
␈↓ α←␈↓and␈αuse␈αthe␈αevaluator␈αdescribed␈αin␈αSection 3.5.␈αWorse␈αyet,␈α␈↓αQUOTE␈↓-ing␈αis␈α
also
␈↓ α←␈↓not␈α⊃sufficient␈α∩to␈α⊃capture␈α∩the␈α⊃intended␈α∩meaning␈α⊃in␈α∩all␈α⊃cases␈α∩of␈α⊃functional
␈↓ α←␈↓parameters.␈α∩To␈α∩understand␈α∪why␈α∩␈↓αQUOTE␈↓-ing␈α∩is␈α∪not␈α∩sufficient␈α∩we␈α∪need␈α∩a
␈↓ α←␈↓slightly more complex set of examples. First we try:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ∧0␈↓αmapfirst[ λ[[x] concat[x;( )]];(A B C D)]␈↓ ␈↓π 31␈↓
␈↓"∀␈↓ α←␈↓which we expect to evaluate to ␈↓α((A) (B) (C) (D))␈↓
␈↓"∀␈↓ α←␈↓ ␈↓αmapfirst[ λ[[x] concat[x;( )]]; ... ]␈↓ εK␈↓ εo[null[l] ... ]␈↓
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ ∧∪E␈↓β0␈↓␈↓ ¬S␈↓ εK␈↓ εoE␈↓β1␈↓
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ ∧∪| /␈↓ ¬S␈↓ εK␈↓ εo| E␈↓β0␈↓
␈↓"β␈↓ α←␈↓␈↓ β'______________␈↓ ¬S=>␈↓ εK__________␈↓ λ/=> . . .
␈↓"β␈↓ α←␈↓ ␈↓α mapfirst␈↓ ∧∪| λ[[fn;l][null[l] ... ]]␈↓ εKl␈↓ εo| (A B C D)
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧∪␈↓ ¬S␈↓ εKfn␈↓ εo| λ[[x] concat[x;( )]]␈↓
␈↓"∀␈↓ α←␈↓Since ␈↓αnull[l]␈↓ is false, the problem reduces to:
␈↓"β␈↓ α←␈↓␈↓ ∧␈␈↓αconcat[fn[first[l]];mapfirst[fn;rest[l]]].␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈␈↓ ¬←E␈↓β1␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈␈↓ ¬←| E␈↓β0␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈______________
␈↓"β␈↓ α←␈↓␈↓ ∧␈ ␈↓αl␈↓ ¬←| (A B C D)
␈↓"β␈↓ α←␈↓α␈↓ ∧␈ fn␈↓ ¬←| λ[[x] concat[x;( )]]␈↓
␈↓"∀␈↓ α←␈↓Since␈αwe␈αare␈α
using␈αcall-by-value␈αwe␈α
have␈αto␈αevaluate␈α
the␈αarguments␈αto␈α
␈↓αconcat␈↓;
␈↓ α←␈↓that␈α∩requires␈α∪evaluating␈α∩␈↓αfn[first[l]]␈↓.␈α∪The␈α∩value␈α∪of␈α∩␈↓αl␈↓␈α∪we␈α∩find␈α∪locally␈α∩and
␈↓ α←␈↓evaluate␈α
␈↓αfirst[l]␈↓,␈αgetting␈α
␈↓αA␈↓.␈αThe␈α
value␈αfor␈α
␈↓αfn␈↓␈α
is␈αalso␈α
found␈αlocally,␈α
and␈αsince␈α
it
␈↓ α←␈↓is␈α
the␈α
representation␈αof␈α
a␈α
λ-definition,␈αwe␈α
set␈α
up␈αa␈α
new␈α
environment␈αin␈α
which
␈↓ α←␈↓to evaluate the body of ␈↓αfn␈↓, binding the λ-variable ␈↓αx␈↓ to ␈↓αA␈↓:
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 31␈↓Note␈αthat␈αwe␈αdo␈αnot␈αuse␈α␈↓αquote␈↓.␈αSome␈αimplementations␈αdo␈αnot␈αsupport
␈↓ α←␈↓this␈α~notation.␈α≠Some␈α~require␈α≠␈↓αquote␈↓,␈α~and␈α~still␈α≠others␈α~give␈α≠a␈α~different
␈↓ α←␈↓interpretation to unembellished functions appearing as actual parameters.
␈↓ α←␈↓␈↓140 Evaluation␈↓
_3.10␈↓
␈↓"∀␈↓ α←␈↓␈↓ ∧␈ ␈↓αconcat[x;( )]␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈␈↓ ¬←E␈↓β2␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈␈↓ ¬←| E␈↓β1␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈______________
␈↓"β␈↓ α←␈↓␈↓ ∧␈ ␈↓αx␈↓ ¬←| A␈↓
␈↓"∀␈↓ α←␈↓The␈α∀expected␈α∃evaluation␈α∀takes␈α∃place:␈α∀␈↓α(A)␈↓␈α∀is␈α∃computed␈α∀and␈α∃returned␈α∀to
␈↓ α←␈↓environment E␈↓β1␈↓ so that we may continue the evaluation ␈↓αmapfirst[fn;rest[l]]␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'However, consider the following variant of this last example. Define:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ∧∪␈↓αfoo <= λ[[l] mapfirst[ λ[[x]concat[x;l]]; (A B C D)]␈↓
␈↓"∀␈↓ α←␈↓It␈α
would␈α
seem␈αthat␈α
␈↓αfoo[( )]␈↓␈α
should␈αalso␈α
give␈α
␈↓α((A) (B) (C) (D))␈↓␈αsince␈α
␈↓αl␈↓␈α
will␈αbe
␈↓ α←␈↓bound␈α
to␈α
␈↓α( )␈↓␈α
and␈α
therefore␈α
the␈α
␈↓αl␈↓␈α
in␈α
the␈α
functional␈α
argument␈α
will␈αeffectively␈α
be
␈↓ α←␈↓␈↓α( )␈↓.
␈↓"∀␈↓ α←␈↓␈↓ α← ␈↓αfoo[( )] mapfirst[ λ[[x] concat[x;l]];...]␈↓ π∪␈↓ π[[null[l] ... ]
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ βW␈↓E␈↓β0␈↓␈↓ ¬↔␈↓ ¬←␈↓ ε∂E␈↓β1␈↓␈↓ π∪␈↓ π[␈↓ λE␈↓β2␈↓
␈↓"β␈↓ α←␈↓␈↓ α←␈↓ βW | /␈↓ ¬↔␈↓ ¬←␈↓ ε∂| E␈↓β0␈↓␈↓ π∪␈↓ π[␈↓ λ| E␈↓β1␈↓
␈↓"β␈↓ α←␈↓␈↓ α←______________␈↓ ¬↔=>␈↓ ¬←______␈↓ π∪=>␈↓ π[______ => ...
␈↓"β␈↓ α←␈↓␈↓ α← ␈↓αfoo␈↓ βW| λ[[l]... ]␈↓ ¬↔␈↓ ¬←l␈↓ ε∂| ( )␈↓ π∪␈↓ π[l␈↓ λ| (A B C D)
␈↓"β␈↓ α←␈↓α␈↓ α←mapfirst␈↓ βW| λ[[fn;l][null[l]...]]␈↓ ¬←␈↓ ε∂␈↓ π∪␈↓ π[fn␈↓ λ| λ[[x] concat[x;l]]␈↓
␈↓"∀␈↓ α←␈↓␈↓αnull[l]␈↓␈αis␈α
false␈αsince␈α
␈↓αl␈↓␈αis␈α
␈↓α(A␈αB␈α
C␈αD)␈↓,␈α
so␈αwe␈α
evaluate␈α␈↓αconcat[fn[first[l]] ... ]␈↓.␈α
This
␈↓ α←␈↓involves␈α∩evaluating␈α∩␈↓αfirst[l]␈↓␈α∩in␈α∩E␈↓β2␈↓,␈α∩giving␈α∩␈↓αA␈↓.␈α∩We␈α∩evaluate␈α∩␈↓αfn␈↓␈α∩in␈α∩E␈↓β2␈↓␈α⊃and,
␈↓ α←␈↓finding␈αa␈αrepresentation␈α
of␈αa␈αλ-definition,␈α
we␈αmake␈αa␈α
new␈αenvironment␈αE␈↓β3␈↓␈α
in
␈↓ α←␈↓which to evaluate the body of ␈↓αfn␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'As␈αwe␈αmake␈αE␈↓β3␈↓,␈αwe␈αadd␈αan␈αentry␈αbinding␈α␈↓αx␈↓␈αto␈α␈↓αA␈↓␈αand␈αwe␈αsettle␈αdown␈αin
␈↓ α←␈↓E␈↓β3␈↓ to evaluate ␈↓αconcat[x;l]␈↓:
␈↓"∀␈↓ α←␈↓␈↓ ∧␈ ␈↓αconcat[x;l]␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈␈↓ ¬;E␈↓β3␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈␈↓ ¬;| E␈↓β2␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈__________
␈↓"β␈↓ α←␈↓␈↓ ∧␈ ␈↓α x␈↓ ¬;| A
␈↓"∀␈↓ α←␈↓Since␈α
␈↓αl␈↓␈α
is␈αnon-local␈α
to␈α
E␈↓β3␈↓,␈αwe␈α
follow␈α
the␈α
access␈αchain␈α
to␈α
find␈αits␈α
value␈α
in␈αE␈↓β2␈↓␈α
to
␈↓ α←␈↓be␈α∞␈↓α(A␈α∞B␈α∞C␈α∂D)␈↓.␈α∞But␈α∞that's␈α∞not␈α∞the␈α∂expected␈α∞value!␈α∞We␈α∞expected␈α∞to␈α∂find␈α∞␈↓α( )␈↓,
␈↓ α←␈↓which was hidden away in E␈↓β1␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αtrouble␈α
here␈αis␈αthat␈α
␈↓αl␈↓␈αwas␈α
rebound␈αin␈αthe␈α
interim.␈αThe␈α
first␈αthing
␈↓ α←␈↓to␈αnote␈αis␈αthat␈αthe␈αproblem␈αis␈αcaused␈αby␈αfree␈αvariables␈αand␈αdynamic␈αbinding:
␈↓ α←␈↓␈↓αl␈↓␈α∩is␈α∩free␈α∩in␈α∩the␈α∩functional␈α∩argument.␈α∩ Local␈α∩variables␈α∩aren't␈α∩problematic;
␈↓ α←␈↓neither␈αare␈αglobal␈αvariables.␈α The␈αdesired␈αbinding␈αfor␈α␈↓αl␈↓␈αis␈αthe␈αone␈αwhich␈α
was
␈↓ α←␈↓current␈α∃when␈α∃we␈α∃were␈α⊗binding␈α∃the␈α∃functional␈α∃argument␈α∃to␈α⊗the␈α∃formal
␈↓ α←␈↓parameter␈α
␈↓αfn␈↓.␈α A␈α
plausible␈αsolution␈α
then␈αis␈α
to␈αreplace␈α
all␈α
non-local␈αvariables
␈↓ α←␈↓with␈αtheir␈αvalues␈αat␈αthe␈αtime␈αwe␈αrecognize␈αthe␈αfunctional␈αargument.␈αThis␈αwill
␈↓ α←␈↓not␈α∞always␈α∞suffice.␈α
See␈α∞page 145␈α∞for␈α
a␈α∞counterexample.␈α∞ A␈α∞more␈α
promising
␈↓ α←␈↓solution␈α∂associates␈α∂the␈α∂name␈α⊂of␈α∂the␈α∂current␈α∂environment␈α∂with␈α⊂the␈α∂function
␈↓ α←␈↓␈↓3.10␈↓ ¬NFunctional Arguments and Functional Values 141␈↓
␈↓"β␈↓ α←␈↓and␈α
use␈αthat␈α
pair␈α
as␈αthe␈α
value␈α
to␈αbe␈α
given␈α
to␈αthe␈α
formal␈α
parameter.␈αWhen␈α
we
␈↓ α←␈↓want␈α∀to␈α∀apply␈α∃the␈α∀functional␈α∀argument␈α∀we␈α∃set␈α∀up␈α∀a␈α∃new␈α∀environment,
␈↓ α←␈↓introducing␈αa␈α
local␈αtable␈α
with␈αthe␈αλ-variables␈α
bound␈αto␈α
their␈αvalues;␈αonly␈α
␈↓¬now␈↓
␈↓ α←␈↓we␈α∂use␈α∂the␈α∂saved␈α∂environment␈α∂as␈α∂the␈α∂beginning␈α∂of␈α∂the␈α∂access␈α⊂chain.␈α∂ The
␈↓ α←␈↓values␈α∩of␈α∪any␈α∩non-local␈α∩variables␈α∪which␈α∩we␈α∩encounter␈α∪in␈α∩the␈α∪process␈α∩of
␈↓ α←␈↓applying␈α_the␈α_functional␈α_argument␈α_will␈α_be␈α_searched␈α_for␈α_in␈α_the␈α_saved
␈↓ α←␈↓environment.
␈↓"β␈↓ α←␈↓␈↓ β'To␈αinitialize␈α
this␈αprocess␈α
we␈αmust␈αbe␈α
able␈αto␈α
recognize␈αthe␈αoccurrence␈α
of
␈↓ α←␈↓a␈α⊂functional␈α⊃argument.␈α⊂ To␈α⊃that␈α⊂end,␈α⊃we␈α⊂introduce␈α⊃a␈α⊂new␈α⊃operator␈α⊂called
␈↓ α←␈↓␈↓αfunction␈↓.␈αThis␈αoperator␈αtakes␈αone␈αargument:␈αa␈αrepresentation␈αof␈αthe␈αfunction.
␈↓ α←␈↓The␈α
effect␈αof␈α
␈↓αfunction␈↓␈α
will␈αbe␈α
to␈α
construct␈αa␈α
value␈α
representing␈αthat␈α
argument
␈↓ α←␈↓and␈α∩the␈α∪environment␈α∩which␈α∪was␈α∩current␈α∪when␈α∩the␈α∪␈↓αfunction␈↓-instance␈α∩was
␈↓ α←␈↓evaluated.
␈↓"β␈↓ α←␈↓␈↓ β'In␈αthe␈α
current␈αexample,␈α
we␈αwould␈α
recognize␈αthe␈α␈↓αfunction␈↓-construct␈α
while
␈↓ α←␈↓evaluating␈α⊂the␈α∂arguments␈α⊂to␈α∂␈↓αmapfirst␈↓;␈α⊂the␈α∂environment␈α⊂which␈α⊂was␈α∂current
␈↓ α←␈↓then␈α∪was␈α∪E␈↓β1␈↓.␈α∀Therefore␈α∪as␈α∪we␈α∀build␈α∪E␈↓β2␈↓␈α∪we␈α∀want␈α∪to␈α∪associate␈α∀the␈α∪pair
␈↓ α←␈↓␈↓αλ[[x] concat[x;l]] - ␈↓E␈↓β1␈↓␈α
with␈α∞the␈α
formal␈α∞parameter␈α
␈↓αfn␈↓.␈α∞Whenever␈α
we␈α∞apply␈α
␈↓αfn␈↓
␈↓ α←␈↓we␈αwant␈αto␈αuse␈α␈↓αλ[[x] concat[x;l]]␈↓;␈αand␈αwithin␈αthat␈αcontext,␈αwhenever␈αwe␈αwant
␈↓ α←␈↓␈↓αl␈↓, we want the value of ␈↓αl␈↓ in E␈↓β1␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∞function-environment␈α∞pair␈α∞is␈α∂called␈α∞a␈α∞␈↓↓closure␈↓␈α∞or␈α∞␈↓↓funarg␈↓.␈α∂ In␈α∞our
␈↓ α←␈↓diagrams we will designate the pair as:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ¬*<function>:<environment>.
␈↓"∀␈↓ α←␈↓Therefore,␈α∞in␈α
our␈α∞example␈α∞we␈α
should␈α∞designate␈α∞the␈α
value␈α∞of␈α∞the␈α
functional
␈↓ α←␈↓argument as:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ¬Z␈↓αλ[[x] concat[x;l]]␈↓:E␈↓β1␈↓
␈↓"∀␈↓ α←␈↓We␈α∩must␈α∩also␈α∩extend␈α∩the␈α∩manipulation␈α∩of␈α∩Weizenbaum␈α∪environments␈α∩to
␈↓ α←␈↓handle␈α∂such␈α∞constructions.␈α∂ The␈α∞process␈α∂which␈α∞recognizes␈α∂λ-definitions␈α∞and
␈↓ α←␈↓sets␈α
up␈α
new␈α
environments␈αmust␈α
now␈α
watch␈α
for␈αfunargs.␈α
When␈α
it␈α
sees␈α
one␈αit
␈↓ α←␈↓uses␈α∩the␈α⊃associated␈α∩environment␈α∩as␈α⊃the␈α∩access␈α⊃environment.␈α∩ Let's␈α∩do␈α⊃the
␈↓ α←␈↓example again.
␈↓"∀␈↓ α←␈↓␈↓ β∂ ␈↓αfoo[( )] mapfirst[function[λ[[x] concat[x;l]]; ..] [null[l] ... ]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βoE␈↓β0␈↓α␈↓ ¬G␈↓ εβ␈↓ ε'E␈↓β1␈↓α␈↓ π7␈↓ πs␈↓ λ↔E␈↓β2␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βo| /␈↓ ¬G␈↓ εβ␈↓ ε'| E␈↓β0␈↓α␈↓ π7␈↓ πs␈↓ λ↔| E␈↓β1␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂______________␈↓ ¬G=>␈↓ εβ______␈↓ π7=>␈↓ πs______ => . . .
␈↓"β␈↓ α←␈↓α␈↓ β∂ foo␈↓ βo| λ[[l]...␈↓ ¬G␈↓ εβl␈↓ ε'| ( )␈↓ π7␈↓ πsl␈↓ λ↔| (A B C D)
␈↓"β␈↓ α←␈↓α mapfirst␈↓ βo| λ[[fn;l][null[l]...]]␈↓ εβ␈↓ ε'␈↓ π7␈↓ πsfn␈↓ λ↔| λ[[x] concat[x;l]]␈↓:E␈↓β1␈↓
␈↓"∀␈↓ α←␈↓Things␈αare␈αas␈αbefore␈αexcept␈αnow␈α␈↓αfn␈↓␈αis␈αbound␈αto␈αthe␈αfunarg␈αpair␈αin␈αE␈↓β2␈↓.␈α We
␈↓ α←␈↓look␈α
up␈α␈↓αfn␈↓␈α
in␈αE␈↓β2␈↓␈α
and,␈αfinding␈α
a␈α
λ-definition,␈αwe␈α
make␈αa␈α
new␈αenvironment␈α
E␈↓β3␈↓
␈↓ α←␈↓in␈α⊃which␈α⊃to␈α⊃evaluate␈α⊃the␈α⊃body␈α⊃of␈α⊂␈↓αfn␈↓.␈α⊃As␈α⊃we␈α⊃make␈α⊃E␈↓β3␈↓,␈α⊃we␈α⊃add␈α⊃an␈α⊂entry
␈↓ α←␈↓binding␈α∂␈↓αx␈↓␈α∂to␈α⊂␈↓αA␈↓.␈α∂ But␈α∂now␈α⊂since␈α∂the␈α∂λ-definition␈α∂is␈α⊂a␈α∂funarg␈α∂we␈α⊂make␈α∂the
␈↓ α←␈↓access␈α⊃environment␈α⊃E␈↓β1␈↓␈α⊃as␈α⊂saved␈α⊃with␈α⊃␈↓αfn␈↓.␈α⊃ Thus␈α⊂we␈α⊃settle␈α⊃down␈α⊃in␈α⊃E␈↓β3␈↓␈α⊂to
␈↓ α←␈↓evaluate ␈↓αconcat[x;l]␈↓:
␈↓ α←␈↓␈↓142 Evaluation␈↓
_3.10␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈␈↓ ¬;␈↓αconcat[x;l]␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈␈↓ ¬;E␈↓β3␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈␈↓ ¬;| E␈↓β1␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈__________
␈↓"β␈↓ α←␈↓␈↓ ∧␈ ␈↓α x␈↓ ¬;| A
␈↓"∀␈↓ α←␈↓Since␈α
␈↓αl␈↓␈α
is␈αnon-local␈α
to␈α
E␈↓β3␈↓,␈αwe␈α
follow␈α
the␈α
access␈αchain␈α
to␈α
find␈αits␈α
value␈α
in␈αE␈↓β1␈↓␈α
to
␈↓ α←␈↓be␈α∩␈↓α( )␈↓␈α∩as␈α∪desired.␈α∩ Thus␈α∩instead␈α∩of␈α∪simply␈α∩tracing␈α∩back␈α∩to␈α∪the␈α∩previous
␈↓ α←␈↓environment we detour around E␈↓β2␈↓:
␈↓"⊃␈↓ α←␈↓∂␈↓ ¬;␈↓E␈↓β0␈↓∂
␈↓"␈↓ α←␈↓∂␈↓ ¬;↑
␈↓"␈↓ α←␈↓∂␈↓ ¬;~
␈↓"␈↓ α←␈↓∂␈↓ ¬;␈↓E␈↓β1␈↓∂←␈↓ ¬k⊃
␈↓"␈↓ α←␈↓∂␈↓ ¬;↑␈↓ ¬←.
␈↓"␈↓ α←␈↓∂␈↓ ¬;~␈↓ ¬←.
␈↓"␈↓ α←␈↓∂␈↓ ¬;␈↓E␈↓β2␈↓∂␈↓ ¬←↑
␈↓"␈↓ α←␈↓∂␈↓ ¬;↑␈↓ ¬←.
␈↓"␈↓ α←␈↓∂␈↓ ¬;~␈↓ ¬←.
␈↓"␈↓ α←␈↓∂␈↓ ¬;␈↓E␈↓β3␈↓∂→␈↓ ¬k$
␈↓"∀␈↓ α←␈↓However,␈α
there␈α
is␈α
still␈α
some␈α
information␈α
which␈α
we␈α
must␈α
make␈α
explicit␈αif␈α
these
␈↓ α←␈↓Weizenbaum␈α∞diagrams␈α
are␈α∞to␈α
faithfully␈α∞represent␈α
the␈α∞process␈α∞of␈α
evaluation.
␈↓ α←␈↓Namely,␈αafter␈αwe␈αhave␈α
finished␈αthe␈αevaluation␈αof␈α
␈↓αconcat[x;l]␈↓␈αwe␈αare␈αto␈α
restore
␈↓ α←␈↓a␈α↔previous␈α⊗environment.␈α↔Which␈α⊗one␈α↔is␈α↔it?␈α⊗ It␈α↔isn't␈α⊗E␈↓β1␈↓,␈α↔it's␈α↔E␈↓β2␈↓!␈α⊗That
␈↓ α←␈↓information is not available in our diagram, so we must correct the situation.
␈↓"β␈↓ α←␈↓␈↓ β'In␈α∂the␈α∂left-hand␈α∂quadrant␈α∂of␈α∂our␈α∂diagram␈α∂we␈α∂place␈α∂the␈α∂name␈α⊂of␈α∂the
␈↓ α←␈↓environment␈α
which␈α
we␈α
wish␈αrestored␈α
when␈α
we␈α
leave␈α
the␈αcurrent␈α
environment.
␈↓ α←␈↓That␈α⊂environment␈α⊂name␈α∂will␈α⊂be␈α⊂called␈α∂the␈α⊂␈↓↓control␈α⊂environment␈↓,␈α⊂and␈α∂will
␈↓ α←␈↓head␈αa␈α
chain␈αof␈αenvironments,␈α
called␈αthe␈αcontrol␈α
chain.␈↓π 32␈↓␈αHere's␈α
the␈αcorrect
␈↓ α←␈↓picture:
␈↓"∀␈↓ α←␈↓␈↓ ∧␈␈↓ ¬;␈↓αconcat[x;l]␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈␈↓ ¬;E␈↓β3␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈E␈↓β2␈↓␈↓ ¬;| E␈↓β1␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈__________
␈↓"β␈↓ α←␈↓␈↓ ∧␈ ␈↓α x␈↓ ¬;| A
␈↓"∀␈↓ α←␈↓So␈α∂after␈α∂we␈α∞have␈α∂finished␈α∂the␈α∞computation␈α∂in␈α∂E␈↓β3␈↓␈α∞we␈α∂return␈α∂control␈α∂to␈α∞E␈↓β2␈↓.
␈↓ α←␈↓Thus the general structure of an environment is as follows:
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 32␈↓In␈α
Algol,␈α∞the␈α
access␈α∞chain␈α
is␈α
called␈α∞the␈α
static␈α∞chain,␈α
and␈α∞the␈α
control
␈↓ α←␈↓chain is called the dynamic chain.
␈↓ α←␈↓␈↓3.10␈↓ ¬NFunctional Arguments and Functional Values 143␈↓
␈↓"∀␈↓ α←␈↓␈↓ ¬←␈↓ εK␈↓↓Form␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬←␈↓ εKE␈↓βcurrent␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬←E␈↓βcontrol␈↓␈↓ εK| E␈↓βaccess␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬←_________________________
␈↓"β␈↓ α←␈↓␈↓ ¬←var␈↓ εK| value
␈↓"β␈↓ α←␈↓␈↓ ¬←␈↓αx␈↓β1␈↓␈↓ εK| . . .
␈↓"β␈↓ α←␈↓␈↓ ¬←␈↓αx␈↓β2␈↓␈↓ εK| . . .
␈↓"β␈↓ α←␈↓␈↓ ¬←. . .␈↓ εK| . . .
␈↓"β␈↓ α←␈↓␈↓ ¬←␈↓αx␈↓βn␈↓␈↓ εK| . . .
␈↓"β␈↓ α←␈↓␈↓ ¬←␈↓ εK|
␈↓"∀␈↓ α←␈↓␈↓ β'Consider␈α_another␈α→example,␈α_involving␈α_a␈α→function␈α_to␈α→produce␈α_the
␈↓ α←␈↓composition␈α
of␈α
two␈α∞unary␈α
functions.␈α
We␈α
will␈α∞call␈α
the␈α
function␈α∞␈↓αcompose␈↓.␈α
The
␈↓ α←␈↓value␈α∀returned␈α∀by␈α∀␈↓αcompose␈↓␈α∀will␈α∪be␈α∀a␈α∀function;␈α∀that␈α∀means␈α∀␈↓αcompose␈↓␈α∪will
␈↓ α←␈↓produce a functional value:
␈↓"∀␈↓ α←␈↓α␈↓ ∧Ecompose[function[car];function[cdr]] = cadr
␈↓" ␈↓ α←␈↓α␈↓with a plausible definition as:␈↓α
␈↓" ␈↓ α←␈↓α␈↓ ¬
compose <= λ[[f;g] λ[[x]f[g[x]]]]
␈↓"∀␈↓ α←␈↓This␈αdefinition␈αof␈α␈↓αcompose␈↓␈αis␈αalmost␈αright.␈α The␈αvalue␈αreturned␈αby␈α␈↓αcompose␈↓␈α
is
␈↓ α←␈↓to␈α
be␈α
a␈α
function.␈α
Indeed␈α
it␈α
is␈αan␈α
instance␈α
of␈α
a␈α
␈↓↓functional␈α
value␈↓,␈α
so,␈α
as␈αwith
␈↓ α←␈↓functional␈α∩arguments,␈α⊃it␈α∩needs␈α∩to␈α⊃be␈α∩decorated␈α∩with␈α⊃␈↓αfunction␈↓␈α∩so␈α∩that␈α⊃the
␈↓ α←␈↓evaluator␈αwill␈αsave␈αthe␈αenvironment␈α
which␈αcontains␈αthe␈αright␈αbindings␈α
for␈α␈↓αf␈↓
␈↓ α←␈↓and␈α→␈↓αg␈↓.␈α→That␈α~environment␈α→is␈α→the␈α→one␈α~which␈α→was␈α→current␈α~when␈α→the
␈↓ α←␈↓␈↓αfunction␈↓-construct was recognized. So we write:
␈↓"∀␈↓ α←␈↓␈↓ ∧G␈↓αcompose <= λ[[f;g] function[λ[[x] f[g[x]]]]]
␈↓"∀␈↓ α←␈↓α␈↓Now try:␈↓ βm␈↓αapp[cons[A;(B . C)];compose[function[car];function[cdr]]]␈↓
␈↓"∞␈↓ α←␈↓α␈↓where:␈↓α␈↓ ¬Zapp <= λ[[y;f] f[y]]␈↓
␈↓"∀␈↓ α←␈↓As␈αusual␈αwe␈αevaluate␈αthe␈αarguments␈αto␈α
␈↓αapp␈↓,␈αbind␈αthe␈αresults␈αto␈α␈↓αy␈↓␈αand␈α
␈↓αf␈↓␈αand
␈↓ α←␈↓evaluate the body of ␈↓αapp␈↓.
␈↓"∀␈↓ α←␈↓␈↓ β∂␈↓αapp[cons[A;(B . C)];compose[function[car];function[cdr]]]␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ ∧π␈↓ ∧gE␈↓β0␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ ∧π␈↓ ∧g/| /
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ ∧π______________
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ ∧π ␈↓αapp␈↓ ∧g | λ[[y;f] f[y]]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧πcompose␈↓ ∧g | λ[[f;g] function[λ[[x] f[g[x]]]]]␈↓
␈↓"∀␈↓ α←␈↓Evaluation␈α↔of␈α⊗the␈α↔first␈α⊗argument␈α↔to␈α⊗␈↓αapp␈↓␈α↔brings␈α⊗no␈α↔surprises;␈α↔we␈α⊗get
␈↓ α←␈↓␈↓α(A . (B . C))␈↓.␈α_We␈α_begin␈α_evaluating␈α_the␈α_second␈α_argument;␈α_we␈α_find␈α_the
␈↓ α←␈↓definition␈αof␈α␈↓αcompose␈↓␈αin␈αthe␈αenvironment␈αand␈αsince␈αit␈αis␈αa␈αλ-definition␈αwe␈αset
␈↓ α←␈↓up a new environment, E␈↓β1␈↓, and evaluate the body ␈↓αfunction[λ[[x] f[g[x]]]]␈↓:
␈↓ α←␈↓␈↓144 Evaluation␈↓
_3.10␈↓
␈↓"∀␈↓ α←␈↓␈↓ ∧␈␈↓αfunction[λ[[x] f[g[x]]]]␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈␈↓ ¬←E␈↓β1␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈ E␈↓β0␈↓␈↓ ¬←| E␈↓β0␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈ __________
␈↓"β␈↓ α←␈↓␈↓ ∧␈ ␈↓αf␈↓ ¬←| car␈↓:E␈↓β0␈↓α
␈↓"β␈↓ α←␈↓α␈↓ ∧␈ g␈↓ ¬←| cdr␈↓:E␈↓β0␈↓
␈↓"∀␈↓ α←␈↓Again,␈αthe␈α
recognition␈αof␈α
the␈α␈↓αfunction␈↓-construct␈αsays␈α
return␈αa␈α
funarg-pair␈αas
␈↓ α←␈↓value.␈αThe␈αenvironment␈αwe␈αassociate␈αis␈αthe␈αcurrent␈αone,␈αE␈↓β1␈↓.␈αWe␈αnow␈αgo␈α
back
␈↓ α←␈↓to␈α∀E␈↓β0␈↓,␈α∀using␈α∃the␈α∀control␈α∀chain.␈α∀Since␈α∃both␈α∀arguments␈α∀to␈α∀␈↓αapp␈↓␈α∃are␈α∀now
␈↓ α←␈↓evaluated,␈α
we␈αfind␈α
the␈αdefinition␈α
of␈α␈↓αapp␈↓␈α
and␈αset␈α
up␈αa␈α
new␈α
environment␈αE␈↓β2␈↓.
␈↓ α←␈↓Thus:
␈↓"∀␈↓ α←␈↓␈↓ ∧π␈↓ ∧g␈↓αf[y]␈↓ ε≠␈↓ εW␈↓ ε{f[g[x]]␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧π␈↓ ∧gE␈↓β2␈↓␈↓ ε≠␈↓ εW␈↓ ε{E␈↓β3␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧π E␈↓β0␈↓␈↓ ∧g| E␈↓β0␈↓␈↓ ε≠␈↓ εWE␈↓β2␈↓␈↓ ε{| E␈↓β1␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧π __________␈↓ ε≠=>␈↓ εW_________
␈↓"β␈↓ α←␈↓␈↓ ∧π ␈↓αy␈↓ ∧g| (A . (B . C))␈↓ ε≠␈↓ εWx␈↓ ε{| (A . (B . C))
␈↓"β␈↓ α←␈↓α␈↓ ∧π f␈↓ ∧g| λ[[x] f[g[x]]]␈↓:E␈↓β1␈↓
␈↓"∀␈↓ α←␈↓The␈α∂form␈α∞to␈α∂be␈α∞evaluated␈α∂in␈α∞E␈↓β2␈↓␈α∂is␈α∂␈↓αf[y]␈↓;␈α∞we␈α∂find␈α∞␈↓αy␈↓␈α∂and␈α∞␈↓αf␈↓␈α∂both␈α∂locally.␈α∞We
␈↓ α←␈↓evaluate␈α⊂the␈α⊃argument␈α⊂␈↓αy␈↓,␈α⊃then␈α⊂since␈α⊃␈↓αf␈↓␈α⊂is␈α⊂a␈α⊃λ-definition,␈α⊂we␈α⊃set␈α⊂up␈α⊃a␈α⊂new
␈↓ α←␈↓environment␈α∩binding␈α⊃the␈α∩λ-variable␈α⊃␈↓αx␈↓␈α∩to␈α⊃the␈α∩value␈α⊃␈↓α(A . (B . C))␈↓.␈α∩But␈α⊃the
␈↓ α←␈↓λ-definition␈αis␈αalso␈αa␈αfunarg;␈αtherefore␈αthe␈αaccess␈αenvironment␈αstored␈αin␈αE␈↓β3␈↓␈α
is
␈↓ α←␈↓E␈↓β1␈↓.␈α
The␈αcontrol␈α
component␈αof␈α
E␈↓β3␈↓␈αis␈α
set␈α
to␈αthe␈α
prior␈αenvironment,␈α
E␈↓β2␈↓;␈αand␈α
we
␈↓ α←␈↓begin evaluation of the body ␈↓αf[g[x]]␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Now␈α
in␈α
E␈↓β3␈↓␈α∞we␈α
find␈α
␈↓αx␈↓␈α
locally␈α∞but␈α
have␈α
to␈α
resort␈α∞to␈α
the␈α
access␈α∞chain␈α
to
␈↓ α←␈↓find␈α∞␈↓αf␈↓␈α
and␈α∞␈↓αg␈↓;␈α
using␈α∞funargs,␈α∞we␈α
have␈α∞set␈α
up␈α∞the␈α∞appropriate␈α
environments.
␈↓ α←␈↓From E␈↓β3␈↓ we have access to E␈↓β1␈↓:␈↓π 33␈↓
␈↓"⊃␈↓ α←␈↓∂ ␈↓E␈↓β0␈↓∂
␈↓"␈↓ α←␈↓∂ /\
␈↓"␈↓ α←␈↓∂ / \
␈↓"␈↓ α←␈↓∂ / \
␈↓"␈↓ α←␈↓∂ ␈↓E␈↓β1␈↓∂ ␈↓E␈↓β2␈↓∂
␈↓"␈↓ α←␈↓∂ ␈↓∞K␈↓∂ \
␈↓"␈↓ α←␈↓∂ ␈↓∞K␈↓∂ \
␈↓"␈↓ α←␈↓∂ ←-␈↓E␈↓β3␈↓∂
␈↓"∀␈↓ α←␈↓The␈αrest␈αof␈αthe␈αevaluation␈αgoes␈αwithout␈αincident:␈αwe␈αfinish␈αthe␈αevaluation␈αin
␈↓ α←␈↓E␈↓β3␈↓ and return to E␈↓β2␈↓ and finally to E␈↓β0␈↓ following the control evironments.
␈↓"β␈↓ α←␈↓␈↓ β'Notice␈α⊃that␈α⊃␈↓αf␈↓␈α⊂and␈α⊃␈↓αg␈↓␈α⊃in␈α⊂the␈α⊃body␈α⊃of␈α⊂␈↓αcompose␈↓␈α⊃are␈α⊃free␈α⊃variables␈α⊂and
␈↓ α←␈↓therefore␈αtheir␈αbindings␈αare␈αnot␈αto␈αbe␈αfound␈αin␈αthe␈αlocal␈αenvironment.␈α Since
␈↓ α←␈↓the␈α
interesting␈α∞applications␈α
of␈α
such␈α∞functions␈α
usually␈α
involve␈α∞free␈α
variables,
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 33␈↓We can go from there to E␈↓β0␈↓ if it were needed.
␈↓ α←␈↓␈↓3.10␈↓ ¬NFunctional Arguments and Functional Values 145␈↓
␈↓"β␈↓ α←␈↓we␈α∪must␈α∪deal␈α∩with␈α∪them.␈α∪ In␈α∪particular,␈α∩the␈α∪␈↓αlabel␈↓␈α∪operator␈α∪will␈α∩typically
␈↓ α←␈↓involve free variables. We remarked that ␈↓αf␈↓ in:
␈↓"∀␈↓ α←␈↓␈↓ ∧V␈↓αf <= λ[[x][zerop[x] → 1; ␈↓
t␈↓α → *[x;f[x-1]]] ]␈↓
␈↓"∀␈↓ α←␈↓is␈αfree.␈αBut␈αwe␈αwant␈αthe␈αoccurrence␈αof␈α␈↓αf␈↓␈αon␈αthe␈αright␈αto␈αbe␈αsynonymous␈αwith
␈↓ α←␈↓the␈α
␈↓αf␈↓␈α
being␈α
defined␈α
on␈αthe␈α
left.␈α
We␈α
can␈α
do␈α
this␈αby␈α
"tying␈α
a␈α
knot"␈α
in␈αthe␈α
access
␈↓ α←␈↓environment chain. Therefore, we should modify the diagram for ␈↓αlabel␈↓ to be:
␈↓"∀␈↓ α←␈↓␈↓ β∂␈↓αlabel[f;λ[[x] ␈↓λx␈↓α[x]]][␈↓εe␈↓α]␈↓␈↓ ¬←␈↓λx␈↓α[␈↓εe␈↓α]␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ βoE␈↓β0␈↓␈↓ ¬#␈↓ ¬←␈↓ εβE␈↓β1␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ βo | /␈↓ ¬#␈↓ ¬←␈↓ εβ| E␈↓β0␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂ ________␈↓ ¬#=>␈↓ ¬←______
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ βo␈↓ ¬#␈↓ ¬←␈↓αf␈↓ εβ| λ[[x] ␈↓λx␈↓α[x]]␈↓:E␈↓β1␈↓α
␈↓"∀␈↓ α←␈↓Notice␈αthat␈αthe␈αeffect␈αof␈α␈↓αlabel␈↓␈αis␈αto␈αbuild␈α␈↓αfunction[λ[[x]␈α␈↓λx␈↓α]]␈↓␈αand␈αassociate␈αthat
␈↓ α←␈↓with␈α
␈↓αf␈↓.␈α
If␈αwe␈α
attempted␈α
to␈α
implement␈α␈↓αfunction[␈↓λf␈↓α]␈↓␈α
by␈α
replacing␈α
all␈αnon-local
␈↓ α←␈↓variables␈α∩in␈α∩␈↓λf␈↓␈α∩by␈α∩their␈α∩current␈α∩values␈α∩we␈α∩wouldn't␈α∩always␈α∩get␈α∩what␈α∩we
␈↓ α←␈↓expect.
␈↓"∞␈↓ α←␈↓Consider ␈↓ ∧L␈↓α fact <= λ[[x][x=0 → 1; ␈↓
t␈↓α → *[x;fact[x-1]]]]␈↓
␈↓"∞␈↓ α←␈↓If the current environment is E␈↓βi␈↓:
␈↓"∀␈↓ α←␈↓␈↓ ¬;␈↓ ¬kE␈↓βi␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬;E␈↓βc␈↓␈↓ ¬k|␈↓ ε≠E␈↓βa␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬;___________
␈↓"β␈↓ α←␈↓␈↓ ¬;␈↓αfact␈↓␈↓ ¬k| ␈↓αfoo␈↓
␈↓"∀␈↓ α←␈↓then executing ␈↓α<= ␈↓ should give something like:
␈↓"∀␈↓ α←␈↓␈↓ ¬;␈↓ ¬kE␈↓βi␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬;E␈↓βc␈↓␈↓ ¬k|␈↓ ε≠E␈↓βa␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬;___________
␈↓"β␈↓ α←␈↓␈↓ ¬;␈↓αfact␈↓␈↓ ¬k| ␈↓αλ[[x] ...fact[x-1]]␈↓ :E␈↓βi␈↓
␈↓"∀␈↓ α←␈↓rather than:
␈↓"β␈↓ α←␈↓␈↓ ¬;␈↓ ¬kE␈↓βi␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬;E␈↓βc␈↓␈↓ ¬k|␈↓ ε≠E␈↓βa␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬;___________
␈↓"β␈↓ α←␈↓␈↓ ¬;␈↓αfact␈↓␈↓ ¬k| ␈↓αλ[[x] ...foo[x-1]]
␈↓"∀␈↓ α←␈↓␈↓ β'Our␈α∩final␈α∪step␈α∩is␈α∪to␈α∩include␈α∪a␈α∩data␈α∪structure␈α∩representation␈α∪of␈α∩the
␈↓ α←␈↓␈↓αfunction␈↓␈αconstruct.␈α The␈αtranslation␈αscheme␈αis␈αsimple:␈αrepresent␈α␈↓αfunction[␈↓λx␈↓α]␈↓␈αas
␈↓ α←␈↓␈↓α(FUNCTION ␈↓
R␈↓∞(␈↓λx␈↓∞)␈↓α)␈↓.
␈↓"∀␈↓ α←␈↓Thus:␈↓ ¬K␈↓αfunction[λ[[x] f[g[x]]]
␈↓" ␈↓ α←␈↓α␈↓ has an ␈↓
R␈↓-image of ␈↓α(FUNCTION (LAMBDA (X) (F (G X)))) ␈↓
␈↓"∀␈↓ α←␈↓We␈α∞must␈α∞also␈α∞develop␈α∞new␈α∞sections␈α
of␈α∞␈↓αeval␈↓␈α∞to␈α∞deal␈α∞with␈α∞␈↓αFUNCTION␈↓.␈α
The
␈↓ α←␈↓device␈α∪LISP␈α∩used␈α∪to␈α∩associate␈α∪environments␈α∩with␈α∪functions␈α∩is␈α∪called␈α∩the
␈↓ α←␈↓␈↓146 Evaluation␈↓
_3.10␈↓
␈↓"β␈↓ α←␈↓␈↓αFUNARG␈↓␈α⊂device.␈↓π 34␈↓␈α⊂When␈α⊂␈↓αeval␈↓␈α∂sees␈α⊂the␈α⊂construction␈α⊂␈↓α(FUNCTION␈α⊂␈↓fn␈↓α)␈↓␈α∂it
␈↓ α←␈↓returns as value the list:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ¬(␈↓α(FUNARG ␈↓fn "saved"␈↓α)␈↓
␈↓"∀␈↓ α←␈↓where␈α⊗"saved"␈α⊗represents␈α∃a␈α⊗pointer␈α⊗to␈α∃the␈α⊗current␈α⊗symbol␈α⊗table.␈α∃ This
␈↓ α←␈↓representation,␈αas␈αa␈αlist␈αof␈αthree␈αobjects,␈αis␈αcalled␈αa␈α␈↓↓funarg␈αtriple␈↓.␈α It␈αis␈α␈↓αapply␈↓
␈↓ α←␈↓that␈αrecognizes␈α␈↓α(FUNARG␈α␈↓fn␈α"saved"␈↓α)␈↓.␈α When␈αwe␈αare␈α␈↓¬calling␈↓α␈αfn␈↓,␈αwe␈αuse␈αthe
␈↓ α←␈↓"saved" symbol table for accessing non-local variables.
␈↓"β␈↓ α←␈↓␈↓ β'Thus␈α∞there␈α∞are␈α∞␈↓¬two␈↓␈α∞environments␈α∞involved␈α∞in␈α∞the␈α∞proper␈α∞handling␈α
of
␈↓ α←␈↓functional␈αarguments.␈αFirst␈αthere␈αis␈αthe␈αenvironment␈αwhich␈αis␈αsaved␈αwith␈αthe
␈↓ α←␈↓␈↓αFUNARG␈↓.␈α→ This␈α→is␈α_called␈α→the␈α→␈↓↓binding␈α_environment␈↓␈α→since␈α→it␈α→is␈α_the
␈↓ α←␈↓environment␈αcurrent␈α
at␈αthe␈α
time␈αthe␈αfunctional␈α
argument␈αwas␈α
constructed␈αor
␈↓ α←␈↓bound.␈α∃The␈α∃second␈α∃environment,␈α∃called␈α∃the␈α∃␈↓↓activation␈α∃environment␈↓␈α∀or
␈↓ α←␈↓application␈α⊂environment,␈α⊂is␈α⊂the␈α⊂environment␈α⊂which␈α⊂is␈α⊂current␈α⊂immediately
␈↓ α←␈↓before the functional argument is ␈↓¬applied␈↓ or activated.
␈↓"β␈↓ α←␈↓␈↓ β'It␈α
is␈α
the␈α
duty␈α
of␈α
␈↓αeval␈↓␈α
and␈α
␈↓αapply␈↓␈α
to␈α
use␈α
the␈α
␈↓αFUNARG␈↓␈α
device␈αto␈α
maintain
␈↓ α←␈↓the proper control of the activation and binding environments.
␈↓"β␈↓ α←␈↓␈↓ β'Finally,␈αwe␈αshould␈αupdate␈αour␈αdescription␈αof␈αthe␈αusage␈αof␈αWeizenbaum
␈↓ α←␈↓environments given on page 135:
␈↓"β␈↓ α←␈↓␈↓ β≠When␈αthe␈α␈↓αfunction␈↓␈αconstruct␈αis␈αrecognized,␈αwe␈αmanufacture␈αa␈α␈↓αFUNARG␈↓
␈↓ α←␈↓␈↓ β≠triple␈α∂consisting␈α∂of␈α∂the␈α∂atom␈α∂␈↓αFUNARG␈↓,␈α∂the␈α∂function␈α∂described␈α⊂in␈α∂the
␈↓ α←␈↓␈↓ β≠instance␈α⊂of␈α⊂␈↓αfunction␈↓,␈α⊂and␈α⊂the␈α⊂current␈α⊂environment.␈α⊂ This␈α⊂triple␈α⊂is␈α∂the
␈↓ α←␈↓␈↓ β≠value␈αof␈αthe␈α
␈↓αfunction␈↓␈αconstruct␈αand␈α
may␈αbe␈αbound␈α
to␈αany␈αLISP␈α
variable;
␈↓ α←␈↓␈↓ β≠typically␈α∞the␈α∞LISP␈α∂variable␈α∞will␈α∞appear␈α∞in␈α∂an␈α∞expression␈α∞in␈α∂a␈α∞position
␈↓ α←␈↓␈↓ β≠reserved for functions.
␈↓"β␈↓ α←␈↓␈↓ β'When␈αdoing␈αa␈αλ-binding,␈αset␈αup␈αa␈αnew␈αE␈↓βnew␈↓␈αwith␈αthe␈αλ-variables␈αas␈α
the
␈↓ α←␈↓␈↓ β≠local␈α≠variable␈α~entries␈α≠and␈α~the␈α≠values␈α~of␈α≠the␈α~arguments␈α≠as␈α~the
␈↓ α←␈↓␈↓ β≠corresponding␈α∞value␈α∞entries.␈α∞ The␈α∂control␈α∞slot␈α∞of␈α∞E␈↓βnew␈↓␈α∞always␈α∂points␈α∞to
␈↓ α←␈↓␈↓ β≠the␈α∂previous␈α∂symbol␈α∂table.␈α∂The␈α⊂access␈α∂slot␈α∂also␈α∂points␈α∂to␈α⊂the␈α∂previous
␈↓ α←␈↓␈↓ β≠environment␈αunless␈αthe␈αfunction␈αbeing␈αapplied␈αis␈αa␈α␈↓αFUNARG␈↓.␈α If␈αit␈αis␈αa
␈↓ α←␈↓␈↓ β≠␈↓αFUNARG␈↓,␈αthen␈αset␈α
the␈αaccess␈αslot␈αto␈α
the␈αenvironment␈αwhich␈α
was␈αsaved
␈↓ α←␈↓␈↓ β≠with the ␈↓αFUNARG␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αevaluation␈αof␈αthe␈αbody␈αof␈αthe␈αλ-expression␈αtakes␈αplace␈αusing␈αE␈↓βnew␈↓;
␈↓ α←␈↓␈↓ β≠when␈α∞a␈α∞local␈α∞variable␈α∞is␈α∞accessed␈α∞we␈α∞find␈α∞it␈α∞in␈α∞E␈↓βnew␈↓;␈α∞when␈α∞a␈α
non-local
␈↓ α←␈↓␈↓ β≠variable␈α
occurs,␈α
we␈α
chase␈α
the␈α
access␈α
chain␈α
to␈α
find␈α
its␈α
value.␈α∞ When␈α
the
␈↓ α←␈↓␈↓ β≠evaluation␈α⊗of␈α⊗the␈α⊗body␈α∃is␈α⊗completed,␈α⊗the␈α⊗previous␈α⊗environment␈α∃is
␈↓ α←␈↓␈↓ β≠restored.␈α E␈↓βnew␈↓␈α
disappears␈αunless␈α
it␈αhas␈α
been␈αsaved␈α
in␈αa␈αfunctional␈α
object
␈↓ α←␈↓␈↓ β≠constructed␈α∃during␈α∃the␈α∃evaluation␈α∃of␈α∃the␈α∃body,␈α∃and␈α∃that␈α∃object␈α∃is
␈↓ α←␈↓␈↓ β≠returned as a functional value.␈↓π 35␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Notice␈αthat␈αthere␈αis␈αa␈αcertain␈αasymmetry␈αabout␈αaccess␈αand␈αcontrol.␈α The
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 34␈↓More is said about implementations of ␈↓αFUNARG␈↓ in Section 6.17.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 35␈↓In␈α∀fact,␈α∪LISP␈α∀will␈α∀allow␈α∪the␈α∀retention␈α∪of␈α∀environments␈α∀in␈α∪more
␈↓ α←␈↓general ways since funarg triples can be manipulated as data structures.
␈↓ α←␈↓␈↓3.10␈↓ ¬NFunctional Arguments and Functional Values 147␈↓
␈↓"β␈↓ α←␈↓control␈α
slot␈α
always␈α
points␈α
at␈α∞the␈α
previous␈α
environment,␈α
while␈α
the␈α∞access␈α
slot
␈↓ α←␈↓may␈α
vary.␈αIt␈α
may␈αfollow␈α
control,␈αas␈α
is␈αthe␈α
case␈αon␈α
simple␈αfunction␈α
calls;␈αit␈α
may
␈↓ α←␈↓point␈α∩to␈α∩an␈α∩environment␈α∩earlier␈α∩in␈α∩the␈α∩control␈α∩chain,␈α∩as␈α∩is␈α∩the␈α∩case␈α∩for
␈↓ α←␈↓functional␈α
arguments;␈α
it␈α
may␈α
point␈α
to␈α
an␈α
environment␈α
which␈α∞control␈α
cannot
␈↓ α←␈↓return␈α
to,␈α
as␈α
is␈α
the␈α
case␈α
for␈α
functional␈α
values;␈α
or␈α
it␈α
may␈α
point␈α
to␈α
itself␈α
as␈α
is␈α
the
␈↓ α←␈↓case for ␈↓αlabel␈↓'s revised implementation.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α⊃is␈α⊂another␈α⊃asymmetry␈α⊂in␈α⊃the␈α⊂properties␈α⊃of␈α⊂access␈α⊃and␈α⊂control.
␈↓ α←␈↓The␈αaccess␈αenvironment␈αis␈αa␈αself-sufficient␈αdata␈αstructure;␈αit␈αcan␈αbe␈αdescribed
␈↓ α←␈↓and␈α_manipulated␈α→as␈α_such␈α→using␈α_the␈α→usual␈α_constructors,␈α→selectors,␈α_and
␈↓ α←␈↓recognizers.␈α Typically␈αsuch␈α
environments␈αcome␈αinto␈αexistence␈α
as␈αa␈αpart␈α
of␈αa
␈↓ α←␈↓computation;␈α⊃they␈α∩are␈α⊃constructed␈α∩during␈α⊃the␈α∩λ-binding␈α⊃process.␈α∩ We␈α⊃can
␈↓ α←␈↓implicitly␈α
save␈α
such␈α
an␈α
environment␈α
through␈α
the␈α
␈↓αFUNARG␈↓␈α
device;␈α∞and␈α
we
␈↓ α←␈↓can␈α∂explicitly␈α⊂build␈α∂such␈α∂environments␈α⊂using␈α∂the␈α∂data␈α⊂structure␈α∂operations
␈↓ α←␈↓and␈αpass␈αthem␈α
to␈α␈↓αeval␈↓␈αas␈α
a␈αsymbol␈αtable.␈α
But␈αsymbol␈αtables␈α
are␈αindependent
␈↓ α←␈↓of␈α⊃the␈α⊃method␈α⊃used␈α⊃to␈α⊃create␈α⊃them.␈α⊃In␈α⊃particular,␈α⊃once␈α⊃a␈α⊃table␈α∩has␈α⊃been
␈↓ α←␈↓captured␈α∩by␈α⊃a␈α∩␈↓αFUNARG␈↓␈α⊃we␈α∩need␈α⊃not␈α∩retain␈α⊃any␈α∩information␈α∩about␈α⊃the
␈↓ α←␈↓computation␈α⊂which␈α∂created␈α⊂that␈α⊂table.␈α∂ However␈α⊂the␈α∂idea␈α⊂of␈α⊂"control"␈α∂and
␈↓ α←␈↓"state␈α
of␈α
computation"␈α
is␈αintegrally␈α
tied␈α
to␈α
access␈αstructure.␈α
The␈α
state␈α
of␈αthe
␈↓ α←␈↓computation␈αinvolves␈αthe␈αexpression␈αcurrently␈αbeing␈αevaluated,␈αthe␈αhistory␈α
of
␈↓ α←␈↓those␈α
computations␈α∞which␈α
are␈α∞suspended␈α
and␈α
waiting␈α∞for␈α
the␈α∞completion␈α
of
␈↓ α←␈↓the␈α∂current␈α∂computation,␈α∂and␈α∂it␈α∂also␈α∂involves␈α∂the␈α∂access␈α∂environment␈α∞since
␈↓ α←␈↓that␈α
is␈α
necessary␈αfor␈α
the␈α
correct␈αevaluation␈α
of␈α
variables.␈α To␈α
"save␈α
the␈αstate␈α
of
␈↓ α←␈↓computation"␈αimplies␈αsaving␈α
the␈αpartial␈αcomputation␈α
to␈αthat␈αpoint,␈αsaving␈α
the
␈↓ α←␈↓expression being evaluated, and saving the current access environment.
␈↓"β␈↓ α←␈↓␈↓ β'To␈α∞a␈α
large␈α∞extent,␈α∞"control␈α
environment"␈α∞is␈α∞a␈α
misnomer.␈α∞What␈α∞we␈α
are
␈↓ α←␈↓intending␈αto␈αcapture␈αis␈αthe␈αidea␈αof␈αa␈αsuspended␈αcomputation:␈αsuspended␈α
until
␈↓ α←␈↓the␈α∀subsidiary␈α∃computation␈α∀has␈α∃been␈α∀completed.␈α∃Part␈α∀of␈α∃the␈α∀suspended
␈↓ α←␈↓computation␈α
␈↓¬is␈↓␈α
the␈α
"control␈α
environment",␈α
but␈α
there's␈α
more.␈αThe␈α
Weizenbaum
␈↓ α←␈↓diagrams␈αshow␈αpart␈αof␈αthe␈αinformation;␈αthey␈αshow␈αthe␈αenvironments␈αand␈αthe
␈↓ α←␈↓expressions␈α
being␈α
evaluated.␈αHowever␈α
they␈α
leave␈αimplicit␈α
the␈α
dynamics␈αof␈α
the
␈↓ α←␈↓computation:␈α∪which␈α∪argument␈α∪is␈α∪being␈α∪evaluated,␈α∪and␈α∪where␈α∪the␈α∪partial
␈↓ α←␈↓results␈α
are␈α
being␈α
stored,␈α
and␈α
where␈α
in␈α
the␈α
expression␈α
we␈α
are␈α
to␈α
continue␈α
when
␈↓ α←␈↓the␈α⊂subsidiary␈α⊂computation␈α⊂is␈α⊃completed.␈α⊂In␈α⊂Section 4.4␈α⊂we␈α⊂will␈α⊃develop␈α⊂a
␈↓ α←␈↓different␈α∩␈↓αeval␈↓␈α⊃family␈α∩which␈α∩will␈α⊃make␈α∩much␈α⊃of␈α∩this␈α∩information␈α⊃explicit.
␈↓ α←␈↓Also␈α↔in␈α⊗Section 4.4␈α↔we␈α↔will␈α⊗examine␈α↔the␈α⊗possibility␈α↔of␈α↔expanding␈α⊗the
␈↓ α←␈↓behavior␈α∩of␈α∩control␈α∩slots.␈α∩That␈α∩is,␈α∩allowing␈α∩environments␈α∩other␈α∩than␈α⊃the
␈↓ α←␈↓predecessor to appear in the control slot of an environment.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α
have␈αalready␈α
remarked␈αthat␈α
functions␈α
are␈αparametric␈α
values;␈αto␈α
that
␈↓ α←␈↓we␈α∞must␈α∞add␈α∞that␈α∞functions␈α∞may␈α∂also␈α∞be␈α∞tied␈α∞to␈α∞the␈α∞environment␈α∂in␈α∞which
␈↓ α←␈↓they␈α∂were␈α∂created;␈α∂they␈α∂cannot␈α∂be␈α∞evaluated␈α∂␈↓αin␈α∂vacuo␈↓.␈α∂ What␈α∂does␈α∂this␈α∞say
␈↓ α←␈↓about␈α
"<="?␈α
It␈α␈↓¬still␈↓␈α
appears␈α
to␈αact␈α
like␈α
an␈αassignment␈α
statement.␈α
It␈α
is␈αtaking
␈↓ α←␈↓on␈α∩more␈α∩distinct␈α∩character␈α∩since␈α∩it␈α∩must␈α∩associate␈α∩environments␈α∩with␈α⊃the
␈↓ α←␈↓function body as it does the assignment.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊂implementation␈α⊂of␈α⊂␈↓αfunction␈↓␈α⊂seems␈α⊂like␈α⊂a␈α⊂lot␈α⊂of␈α⊂work␈α⊂to␈α⊂allow␈α∂a
␈↓ α←␈↓␈↓148 Evaluation␈↓
_3.10␈↓
␈↓"β␈↓ α←␈↓moderately␈α∂obscure␈α∞construct␈α∂to␈α∞appear␈α∂in␈α∞a␈α∂language.␈α∂ However␈α∞constructs
␈↓ α←␈↓like␈α∂functional␈α∂arguments␈α∂appear␈α∞in␈α∂several␈α∂programming␈α∂languages␈α∞under
␈↓ α←␈↓different␈α∂guises.␈α∞Usually␈α∂the␈α∂syntax␈α∞of␈α∂the␈α∞language␈α∂is␈α∂sufficiently␈α∞complex
␈↓ α←␈↓that␈αthe␈αtrue␈αbehavior␈αand␈αimplications␈αof␈αdevices␈αlike␈αfunctional␈αarguments
␈↓ α←␈↓is␈αmisunderstood.␈αFaulty␈α
implementations␈αusually␈αresult.␈α
In␈αLISP␈αthe␈α
problem
␈↓ α←␈↓and the solution appear with exceptional clarity.␈↓π 36␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Here is a sketch of the abstract structure of the current ␈↓αeval␈↓.
␈↓"∀␈↓ α←␈↓αeval <= λ[[exp;environ]
␈↓"β␈↓ α←␈↓α␈↓ βc[isvar[exp] → lookup[exp;environ];
␈↓"β␈↓ α←␈↓α␈↓ βc isconst[exp] → denote[exp];
␈↓"β␈↓ α←␈↓α␈↓ βc iscond[exp] → evcond[args␈↓βc␈↓α[exp];environ];
␈↓"β␈↓ α←␈↓α␈↓ βc isfun[exp] → mkfunarg[exp;environ];
␈↓"β␈↓ α←␈↓α␈↓ βc isfunc+args[exp] → apply[␈↓ ε?func[exp];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ε?evlis[arglist[exp];environ];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ε?environ]] ]
␈↓"∀␈↓ α←␈↓α␈↓where:␈↓α
␈↓"β␈↓ α←␈↓αapply <= λ[[fn;args;environ]
␈↓"β␈↓ α←␈↓α␈↓ β{[isfunname[fn] → ...;
␈↓"β␈↓ α←␈↓α␈↓ β{ islambda[fn] → eval[␈↓ εβbody[fn];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εβmkenv[vars[fn];args;environ]];
␈↓"β␈↓ α←␈↓α␈↓ β{ isfunarg[fn] → apply[␈↓ ε≠func␈↓β1␈↓α[fn];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εβ␈↓ ε≠args;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εβ␈↓ ε≠evn[fn]];
␈↓"β␈↓ α←␈↓α␈↓ β{ ... ... ]]
␈↓"∀␈↓ α←␈↓The␈αreader␈α
is␈αencouraged␈α
to␈αcomplete␈α
the␈αdefinitions,␈α
supplying␈αappropriate
␈↓ α←␈↓constructors, selectors and recognizers.
␈↓"β␈↓ α←␈↓␈↓ β'Now␈α⊗for␈α↔some␈α⊗specific␈α⊗examples.␈α↔ Most␈α⊗implementations␈α↔of␈α⊗LISP
␈↓ α←␈↓include a very useful class of mapping functions.
␈↓"∀␈↓ α←␈↓␈↓αmaplist␈↓␈α∞is␈α∂a␈α∞function␈α∞of␈α∂two␈α∞arguments:␈α∞␈↓αfn␈↓,␈α∂a␈α∞unary␈α∞function;␈α∂and␈α∞␈↓αl␈↓,␈α∂a␈α∞list.
␈↓ α←␈↓␈↓ β'␈↓αmaplist␈↓␈α∀applies␈α∃the␈α∀function␈α∃␈↓αfn␈↓␈α∀to␈α∀the␈α∃list␈α∀␈↓αl␈↓␈α∃and␈α∀its␈α∃tails␈α∀(␈↓αrest[l],
␈↓ α←␈↓α␈↓ β'rest[rest[l]], ..␈↓)␈αuntil␈α␈↓αl␈↓␈αis␈αreduced␈αto␈α␈↓α( )␈↓.␈α The␈αvalue␈αof␈α␈↓αmaplist␈↓␈αis␈αthe␈αlist
␈↓ α←␈↓␈↓ β'of the values returned by ␈↓αfn␈↓. Here's a definition of ␈↓αmaplist␈↓:
␈↓"∀␈↓ α←␈↓␈↓ β≥␈↓αmaplist <= λ[[fn;l][null[l] → ( ); ␈↓
t␈↓α → concat[fn[l];maplist[fn;rest[l]]]]]␈↓
␈↓"∀␈↓ α←␈↓Thus:
␈↓"β␈↓ α←␈↓␈↓ β
␈↓αmaplist[function[reverse];(A B C D)] = ((D C B A) (D C B) (D C) (D))␈↓
␈↓"β␈↓ α←␈↓The␈α
use␈α
of␈α
␈↓αfunction␈↓␈α
is␈α∞not␈α
strictly␈α
necessary␈α
since␈α
␈↓αreverse␈↓␈α
does␈α∞not␈α
reference
␈↓ α←␈↓free variables.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∞mapping␈α∞functionals␈α∞can␈α∞be␈α∞generalized.␈α∞For␈α∂example␈α∞([Moo 74])
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 36␈↓LISP␈α
was␈αthe␈α
first␈αprogramming␈α
language␈αwhich␈α
allowed␈αfunctional
␈↓ α←␈↓values.
␈↓ α←␈↓␈↓3.10␈↓ ¬NFunctional Arguments and Functional Values 149␈↓
␈↓"β␈↓ α←␈↓an␈αapplication␈α␈↓αmapfirst[fn;l␈↓β1␈↓α; ...; l␈↓βn␈↓α]␈↓␈αwould␈αexpect␈α␈↓αfn␈↓␈αto␈αbe␈αan␈αn-ary␈αfunction
␈↓ α←␈↓to␈αbe␈αapplied␈αto␈αconsecutive␈αmembers␈αof␈αeach␈α␈↓αl␈↓βi␈↓,␈αbuilding␈αa␈αlist␈αof␈αthe␈αresults
␈↓ α←␈↓of each application.
␈↓"β␈↓ α←␈↓␈↓ β'An␈αinteresting␈αand␈αnon-trivial␈αuse␈αof␈αfunctional␈αarguments␈αis␈αshown␈αon
␈↓ α←␈↓page 196␈α⊃where␈α⊃we␈α⊂define␈α⊃a␈α⊃new␈α⊂control␈α⊃structure␈α⊃suitable␈α⊃for␈α⊂describing
␈↓ α←␈↓algorithms built to operate on lists.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. What␈α⊂changes␈α⊂should␈α⊃be␈α⊂made␈α⊂to␈α⊂the␈α⊃LISP␈α⊂syntax␈α⊂equations␈α⊃to␈α⊂allow
␈↓ α←␈↓␈↓ β∂functional arguments?
␈↓" ␈↓ α←␈↓2. Use␈α∪␈↓αapp␈↓␈α∪on␈α∀page 143␈α∪to␈α∪define␈α∀a␈α∪function␈α∪which␈α∀computes␈α∪factorial
␈↓ α←␈↓␈↓ β∂without using ␈↓αlabel␈↓ or explicit calls on the evaluator.
␈↓" ␈↓ α←␈↓3. Extend ␈↓αeval␈↓ and friends to handle functional arguments.
␈↓" ␈↓ α←␈↓4. An␈α↔interesting␈α↔use␈α↔of␈α↔functional␈α↔arguments␈α_involves␈α↔self-applicative
␈↓ α←␈↓␈↓ β∂functions.␈α⊃ An␈α⊂application␈α⊃of␈α⊂a␈α⊃function␈α⊃␈↓αf␈↓␈α⊂in␈α⊃a␈α⊂context␈α⊃␈↓αf[...;f;...]␈↓␈α⊃is␈α⊂an
␈↓ α←␈↓␈↓ β∂instance␈α∂of␈α∂self␈α∞application.␈↓π 37␈↓␈α∂Self-applicative␈α∂functions␈α∞can␈α∂be␈α∂used␈α∞to
␈↓ α←␈↓␈↓ β∂define␈α∀recursive␈α∀functions␈α∀in␈α∀such␈α∀a␈α∀way␈α∀that␈α∀the␈α∀definition␈α∀is␈α∪not
␈↓ α←␈↓␈↓ β∂␈↓¬statically␈↓␈αself-referential,␈αbut␈αis␈α␈↓¬dynamically␈↓␈αre-entrant.␈α For␈αexample,␈αhere
␈↓ α←␈↓␈↓ β∂is our canonical example, written using a self-applicative function:
␈↓"∀␈↓ α←␈↓α␈↓ ¬∀fact <= λ[[n] f[function[f]; n]]
␈↓"∞␈↓ α←␈↓α␈↓ ∧Gf <= λ[[g;n][n=0 → 1; ␈↓
t␈↓α → *[n; g[g; n-1]] ]]
␈↓"∀␈↓ α←␈↓␈↓ β∂Use Weizenbaum's environments to show the execution of ␈↓αfact[2]␈↓.
␈↓" ␈↓ α←␈↓5. Write␈α∞a␈α∞LISP␈α∞function␈α∞to␈α∞find␈α
the␈α∞permutations␈α∞on␈α∞a␈α∞set␈α∞of␈α∞␈↓αn␈↓␈α
elements.
␈↓ α←␈↓␈↓ β∂For example ␈↓αperm[(A B C)]␈↓ gives
␈↓"∀␈↓ α←␈↓␈↓ ∧∧␈↓α((A B C) (A C B) (B C A) (B A C) (C A B) (C B A))␈↓
␈↓" ␈↓ α←␈↓6. Write␈α⊂a␈α⊂generalized␈α⊃form␈α⊂of␈α⊂the␈α⊃␈↓αdiff␈↓␈α⊂algorithm␈α⊂(Section 2.3)␈α⊃to␈α⊂handle
␈↓ α←␈↓␈↓ β∂n-ary sums and products.
␈↓"β␈↓ α←␈↓␈↓ ∧$␈↓↓3.11 Binding Strategies and Implementations␈↓
␈↓"β␈↓ α←␈↓␈↓ β'After␈α⊃the␈α⊃discussion␈α⊃of␈α⊃variables␈α⊃in␈α⊃Section 3.7␈α⊃and␈α∩the␈α⊃intervening
␈↓ α←␈↓discussions␈α⊃of␈α⊃environments,␈α∩it␈α⊃should␈α⊃now␈α⊃be␈α∩clear␈α⊃that␈α⊃the␈α⊃root␈α∩of␈α⊃the
␈↓ α←␈↓binding␈α
problem␈α
is␈α
free␈α∞variables.␈α
We␈α
don't␈α
want␈α∞to␈α
restrict␈α
the␈α
use␈α∞of␈α
free
␈↓ α←␈↓variables␈α↔too␈α_precipitously␈α↔since␈α↔they␈α_are␈α↔a␈α↔very␈α_useful␈α↔programming
␈↓ α←␈↓technique.␈α↔For␈α_example,␈α↔the␈α_possible␈α↔alternative␈α_of␈α↔passing␈α_all␈α↔global
␈↓ α←␈↓information␈α∃through␈α∀as␈α∃extra␈α∀parameters␈α∃in␈α∀calling␈α∃sequences␈α∃is␈α∀overly
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 37␈↓Provided the designated argument position is a functional argument.
␈↓ α←␈↓␈↓150 Evaluation␈↓
_3.11␈↓
␈↓"β␈↓ α←␈↓expensive;␈↓π 38␈↓␈α
often␈α
the␈α
most␈α
natural␈α
formulation␈α
of␈α
a␈α
problem␈α
involves␈αfree
␈↓ α←␈↓variables.
␈↓"β␈↓ α←␈↓␈↓ β'Handling␈α∃of␈α∃free␈α∃variables␈α∃varies␈α∃from␈α∃programming␈α∃language␈α∃to
␈↓ α←␈↓programming␈α∞language.␈α∂ The␈α∞solution␈α∞advocated␈α∂by␈α∞Algol-like␈α∂languages␈α∞is
␈↓ α←␈↓called␈α∪␈↓↓static␈α∪binding␈↓␈α∪or␈α∀␈↓↓lexical␈α∪binding␈↓␈α∪and␈α∪dictates␈α∪that␈α∀all␈α∪non-local
␈↓ α←␈↓references␈α⊂be␈α∂fixed␈α⊂in␈α∂the␈α⊂binding␈α∂environment;␈α⊂thus␈α∂free␈α⊂variables␈α∂aren't
␈↓ α←␈↓really␈α
free␈α
in␈α
the␈α
sense␈α
that␈α
we␈α
have␈α
a␈α
choice␈α
to␈α
make.␈α
LISP␈α
at␈α
least␈α
gives
␈↓ α←␈↓you␈α∪a␈α∪choice.␈↓π 39␈↓␈α∪Using␈α∪␈↓αquote␈↓␈α∪you␈α∪will␈α∪get␈α∪the␈α∪dynamic␈α∪binding␈α∀on␈α∪free
␈↓ α←␈↓variables␈α≤in␈α≠a␈α≤functional␈α≠argument;␈α≤using␈α≠␈↓αfunction␈↓␈α≤gives␈α≤the␈α≠static
␈↓ α←␈↓interpretation.␈↓π 40␈↓␈α∀There␈α∀are␈α∀no␈α∀questions␈α∀about␈α∀Algol's␈α∀interpretation␈α∀of
␈↓ α←␈↓functional␈α≡values:␈α≡the␈α≡construct␈α∨is␈α≡not␈α≡allowed.␈α≡When␈α∨we␈α≡discuss
␈↓ α←␈↓implementation of binding strategies in Chapter 5 we will see why.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αbinding␈αstrategy␈αdetermines␈α␈↓¬when␈↓␈αthe␈αvariables␈αwill␈αreceive␈αvalues;
␈↓ α←␈↓the␈α⊂implementation␈α⊂determines␈α∂␈↓¬how␈↓␈α⊂those␈α⊂bindings␈α∂are␈α⊂to␈α⊂be␈α∂accomplished
␈↓ α←␈↓and␈α∂therefore,␈α∂how␈α∂the␈α∂lookup␈α∂of␈α∂values␈α∂is␈α∂to␈α∂be␈α∂accomplished.␈α∂ We␈α∂have
␈↓ α←␈↓seen␈α∩one␈α⊃implementation␈α∩in␈α⊃the␈α∩␈↓αassoc - pairlis␈↓␈α∩pair␈α⊃(Section 3.5, page 124),
␈↓ α←␈↓and␈α∪on␈α∪page 135␈α∪suggested␈α∪a␈α∪related␈α∪implementation␈α∪using␈α∪Weizenbaum
␈↓ α←␈↓environments. Now we examine another implementation.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αmost␈α
general␈αenvironment␈α
structure␈αwhich␈α
LISP␈αcreates␈α
is␈αa␈αtree␈α
of
␈↓ α←␈↓local␈αsymbol␈αtables,␈αrooted␈αin␈αthe␈αglobal␈αtable.␈αThe␈αtypical␈αLISP␈αcomputation
␈↓ α←␈↓generates␈αa␈αsingle␈αbranch,␈αbut␈αfunctional␈αarguments␈αand␈αvalues␈αcan␈αgenerate
␈↓ α←␈↓additional␈α∂branches.␈α⊂Locating␈α∂a␈α⊂variable␈α∂␈↓αn␈↓␈α⊂involved␈α∂searching␈α⊂the␈α∂current
␈↓ α←␈↓branch␈αfrom␈αtip␈α
to␈αroot,␈αlooking␈αfor␈α
the␈αfirst␈αoccurrence␈αof␈α
␈↓αn␈↓.␈αIf␈α␈↓αn␈↓␈αwas␈α
bound
␈↓ α←␈↓very␈α
deep,␈α
the␈α
search␈α
could␈α
be␈αlong.␈α
Indeed␈α
the␈α
time␈α
is␈α
proportional␈α
to␈αthe
␈↓ α←␈↓depth␈α∞of␈α∞the␈α∂branch;␈α∞recall␈α∞our␈α∞sample␈α∂evaluation␈α∞of␈α∞␈↓αequal␈↓␈α∞on␈α∂page 124.␈α∞It
␈↓ α←␈↓has␈α
been␈αnoted␈α
[Wegb 75]␈αthat␈α
variables␈αtend␈α
to␈αbe␈α
rebound␈α
rather␈αseldom;
␈↓ α←␈↓there␈αare␈αfew␈αoccurrences␈αof␈αany␈α
given␈αvariable␈αon␈αany␈αparticular␈αbranch.␈α
If
␈↓ α←␈↓this␈α
is␈α
the␈α
case,␈α
then␈αthe␈α
search␈α
will␈α
examine␈α
many␈α
environment␈αblocks␈α
which
␈↓ α←␈↓do␈α∪not␈α∪contain␈α∪the␈α∩desired␈α∪variable.␈α∪If␈α∪the␈α∩number␈α∪of␈α∪bindings␈α∪of␈α∩any
␈↓ α←␈↓variable␈αis␈αsmall␈α
compared␈αto␈αthe␈αnumber␈α
of␈αenvironment␈αblocks␈αwhich␈α
have
␈↓ α←␈↓to␈α
be␈αsearched␈α
to␈α
find␈αthose␈α
bindings,␈αthen␈α
we␈α
would␈αlike␈α
a␈αviable␈α
alternative
␈↓ α←␈↓to␈α∂the␈α∂␈↓αassoc - pairlis␈↓␈α∂implementation␈α⊂of␈α∂␈↓αlookup - mkenv␈↓.␈α∂ That␈α∂is,␈α⊂a␈α∂scheme
␈↓ α←␈↓whose␈α
search␈α
is␈α
proportional␈α
to␈αthe␈α
number␈α
of␈α
bindings␈α
for␈α
a␈αvariable,␈α
rather
␈↓ α←␈↓than proportional to the depth of the tree. There is such an alternative.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 38␈↓Though much of that expense can be mitigated by a clever compiler.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 39␈↓However␈α[Ste 76b]␈α
shows␈αthat␈αdynamic␈α
binding␈αcan␈αbe␈α
simulated␈αin
␈↓ α←␈↓a statically bound LISP-like language.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 40␈↓A␈αcase␈αcan␈αbe␈αmade␈αfor␈α
even␈αmore␈αflexibility␈αin␈αthe␈αinterpretation␈α
of
␈↓ α←␈↓free␈α⊂variables.␈α∂We␈α⊂could␈α∂ask␈α⊂that␈α∂the␈α⊂binding␈α∂be␈α⊂done␈α∂on␈α⊂a␈α⊂per␈α∂variable
␈↓ α←␈↓basis.␈α∩That␈α⊃is␈α∩we␈α⊃could␈α∩declare␈α∩which␈α⊃free␈α∩variables␈α⊃are␈α∩to␈α∩be␈α⊃captured
␈↓ α←␈↓statically␈αand␈αwhich␈αare␈αto␈αbe␈αcaptured␈αdynamically.␈α We␈αcould␈αalso␈αask␈αthat
␈↓ α←␈↓␈↓¬both␈↓␈αbindings␈αbe␈αavailable␈αand␈α
supply␈αselectors␈αwhich␈αwould␈αaccess␈αeither␈α
the
␈↓ α←␈↓dynamic or the static binding.
␈↓ α←␈↓␈↓3.11␈↓ ε⊃Binding Strategies and Implementations 151␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Namely,␈α∂we␈α∂associate␈α∂␈↓¬all␈↓␈α∂the␈α∂values␈α∞possessed␈α∂by␈α∂␈↓αn␈↓␈α∂with␈α∂␈↓αn␈↓␈α∂itself.␈α∞ To
␈↓ α←␈↓signify␈αwhich␈αenvironment␈αcreated␈α
the␈αbinding,␈αwe␈αassociate␈α
pairs␈αconsisting
␈↓ α←␈↓of␈αthe␈αvalue␈αand␈αthe␈αenvironment␈αname.␈α Thus␈αthe␈αnew␈α␈↓αmkenv[vars;vals;env]␈↓
␈↓ α←␈↓application␈α
must␈α
name␈α
a␈α
new␈α
environment,␈α
call␈α
it␈α
␈↓αnew␈↓,␈α
and␈α
attach␈α
it␈α
to␈α
the␈α
tip
␈↓ α←␈↓of␈α
the␈α
current␈α
branch␈α
in␈α
the␈α
environment␈α
tree.␈α
Also,␈α
for␈α
each␈α
entry␈α∞in␈α
␈↓αvals␈↓,
␈↓ α←␈↓␈↓αmkenv␈↓ must associate a value-␈↓αnew␈↓ pair with each name in ␈↓αvars␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∪␈↓αlookup␈↓␈α∀procedure␈α∪is␈α∪given␈α∀the␈α∪name␈α∪␈↓αn␈↓␈α∀and␈α∪a␈α∪branch␈α∀in␈α∪the
␈↓ α←␈↓environment␈α
tree.␈αAssume␈α
that␈αthe␈α
tip␈α
node␈αof␈α
the␈αtree␈α
is␈αnamed␈α
Env.␈α
If␈α␈↓αn␈↓
␈↓ α←␈↓has␈α
an␈α
attached␈α
value␈α
pair␈α
whose␈α
environment␈α
component␈α
is␈α
Env,␈α∞then␈α
the
␈↓ α←␈↓associated␈αvalue␈αis␈αreturned␈αby␈α␈↓αlookup␈↓.␈αOtherwise␈αthe␈αenvironment␈αbranch␈αis
␈↓ α←␈↓searched␈α
recursively␈α
by␈α
␈↓αlookup␈↓␈α
for␈α
the␈α
first␈α
node␈α
in␈α
that␈α
branch␈α
which␈α
has␈α
an
␈↓ α←␈↓associated value attached to ␈↓αn␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Here's a graphical description of this reorganized symbol table:
␈↓"␈↓ α←␈↓
E1 E2 E4
␈↓"␈↓ α←␈↓
⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπααα⊃
␈↓"␈↓ α←␈↓
~ x ~ 3 ~ ~ y ~ 4 ~ ~ x ~ C ~
␈↓"␈↓ α←␈↓
εαααβαααλ %αααβααα$ %αααβααα$
␈↓"␈↓ α←␈↓
~ y ~ 1 ~ ~ ↓
␈↓"␈↓ α←␈↓
εαααβαααλ ↓ E3
␈↓"␈↓ α←␈↓
~ z ~ A ~ ~ ⊂αααπααα⊃
␈↓"␈↓ α←␈↓
%αααβααα$ ~ ~ z ~ 4 ~
␈↓"␈↓ α←␈↓
~ ~ %αααβααα$
␈↓"␈↓ α←␈↓
↓ ↓ ↓
␈↓"␈↓ α←␈↓
~ E0 ~
␈↓"␈↓ α←␈↓
%ααααα→αααα→⊂αααπααα⊃←αααα←αααα$
␈↓"␈↓ α←␈↓
~ y ~ 2 ~
␈↓"␈↓ α←␈↓
εαααβαααλ
␈↓"␈↓ α←␈↓
~ z ~ 1 ~
␈↓"␈↓ α←␈↓
%ααα∀ααα$
␈↓"␈↓ α←␈↓
␈↓ The ␈↓αassoc-pairlis␈↓ structure ␈↓α
␈↓"≠␈↓ α←␈↓
x y z
␈↓"␈↓ α←␈↓
E1 E4 ⊂ααααπααα⊃ ⊂ααααπααα⊃ ⊂ααααπααα⊃
␈↓"␈↓ α←␈↓
↓ E2 ↓ ~ E1 ~ 3 ~ ~ E0 ~ 2 ~ ~ E0 ~ 1 ~
␈↓"␈↓ α←␈↓
\ ~ E3 εααααβαααλ εααααβαααλ εααααβαααλ
␈↓"␈↓ α←␈↓
\ ↓ / ~ E4 ~ C ~ ~ E1 ~ 1 ~ ~ E1 ~ A ~
␈↓"␈↓ α←␈↓
\~/ %αααα∀ααα$ εααααβαααλ εααααβαααλ
␈↓"␈↓ α←␈↓
↓ ~ E2 ~ 4 ~ ~ E3 ~ 4 ~
␈↓"␈↓ α←␈↓
E0 %αααα∀ααα$ %αααα∀ααα$
␈↓"β␈↓ α←␈↓␈↓ The new structures ␈↓
␈↓ α←␈↓␈↓152 Evaluation␈↓
_3.11␈↓
␈↓"→␈↓ α←␈↓Using the discussion of ␈↓αmkenv␈↓ on page 124, we define:
␈↓"∀␈↓ α←␈↓α␈↓ βKalloc <=λ[[x] gensym[]]
␈↓" ␈↓ α←␈↓α␈↓ βKlink <= λ[[block;env] concat[block;env]]
␈↓" ␈↓ α←␈↓α␈↓ βKsend <= λ[[n;v;blk] addval[n;mkent[first[blk];v]]]
␈↓"∀␈↓ α←␈↓The␈α⊂function␈α∂␈↓αgensym␈↓␈α⊂is␈α⊂to␈α∂generate␈α⊂a␈α⊂new␈α∂environment␈α⊂name␈α⊂which␈α∂␈↓αalloc␈↓
␈↓ α←␈↓makes into a new tip node on the end of the current environment branch.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αfunction␈α␈↓αaddval␈↓␈αadds␈αa␈αnew␈αentry␈αto␈αthe␈αvariable␈α␈↓αn␈↓.␈αThe␈αvalue␈αof
␈↓ α←␈↓␈↓αaddval␈↓ is ␈↓αenv␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∂new␈α∂␈↓αlookup␈↓␈α⊂is␈α∂more␈α∂complicated␈α∂than␈α⊂the␈α∂simple␈α∂␈↓αassoc␈↓.␈α⊂Given␈α∂a
␈↓ α←␈↓node␈α
in␈α
the␈α
environment␈α
branch,␈α
we␈αmust␈α
see␈α
if␈α
there␈α
is␈α
a␈α
related␈αbinding␈α
for
␈↓ α←␈↓␈↓αn␈↓.␈α
If␈α
there␈αis,␈α
then␈α
that's␈α
the␈αbinding␈α
we␈α
want.␈α
If␈αno␈α
binding␈α
is␈α
found␈αwe␈α
look
␈↓ α←␈↓at the next deeper node on the tree, and check its bindings.
␈↓"∀␈↓ α←␈↓αlookup <= λ[[n;env] λ[[z] look␈↓λ'␈↓α[z;z;env]][getval[n]] ]
␈↓" ␈↓ α←␈↓αlook␈↓λ'␈↓α <= λ[[l;l1;env]␈↓ ∧g[null[l] →look␈↓λ'␈↓α[l1;l1;rest[env]]
␈↓"β␈↓ α←␈↓α␈↓ ∧g eq[name[first[l]];first[env]] → value[first[l]]
␈↓"β␈↓ α←␈↓α␈↓ ∧g ␈↓
t␈↓α → look␈↓λ'␈↓α[rest[l];l1;env] ]]
␈↓"∀␈↓ α←␈↓α␈↓ β'This␈αnew␈αscheme␈α
is␈αcalled␈α␈↓↓shallow␈α
binding␈↓,␈αand␈αthe␈α␈↓αassoc-pairlis␈↓␈α
scheme
␈↓ α←␈↓is␈αcalled␈α␈↓↓deep␈αbinding␈↓.␈α The␈αessential␈αdifferences␈αbetween␈αthese␈α
two␈αbinding
␈↓ α←␈↓implementations␈α
is␈α
that␈α
a␈α
deep␈α
binding␈α
search␈α
is␈α
keyed␈α
on␈α
the␈α
name␈α∞of␈α
the
␈↓ α←␈↓variable,␈α⊂whereas␈α∂a␈α⊂shallow␈α∂binding␈α⊂strategy␈α∂is␈α⊂keyed␈α∂on␈α⊂the␈α∂environment
␈↓ α←␈↓name.␈α≡These␈α≡requirements␈α≡have␈α≡corresponding␈α≡implications␈α≡for␈α≡the
␈↓ α←␈↓organization of the symbol tables.␈↓π 41␈↓
␈↓"β␈↓ α←␈↓␈↓ β'A␈α∞further␈α
elaboration␈α∞of␈α
the␈α∞shallow␈α
scheme␈α∞is␈α
possible.␈α∞The␈α
essential
␈↓ α←␈↓aim␈α⊂of␈α⊂shallow␈α⊂binding␈α⊂is␈α⊂the␈α⊂reduction␈α⊂of␈α⊂the␈α⊂search␈α⊂time␈α⊂for␈α⊃values␈α⊂of
␈↓ α←␈↓variables.␈α∞ The␈α∂current␈α∞scheme␈α∂improves␈α∞the␈α∂situation,␈α∞but␈α∂if␈α∞a␈α∂variable␈α∞is
␈↓ α←␈↓bound␈α
several␈α
times␈αwe␈α
still␈α
may␈αhave␈α
to␈α
search␈αthe␈α
table␈α
of␈αvalues␈α
associated
␈↓ α←␈↓with␈α∞the␈α∞atom.␈α∞ The␈α∞next␈α∞modification␈α∞arranges␈α∞that␈α∞the␈α∞correct␈α∞binding␈α
is
␈↓ α←␈↓always␈α⊃found␈α⊂in␈α⊃a␈α⊂fixed␈α⊃location␈α⊂associated␈α⊃with␈α⊂the␈α⊃atom.␈α⊂ As␈α⊃with␈α⊂any
␈↓ α←␈↓scheme,␈α⊂the␈α⊃benefits␈α⊂are␈α⊂not␈α⊃without␈α⊂penalty;␈α⊂we␈α⊃will␈α⊂discuss␈α⊂some␈α⊃of␈α⊂the
␈↓ α←␈↓tradeoffs after we describe the implementation.
␈↓"β␈↓ α←␈↓␈↓ β'With␈α
each␈α
variable␈α∞␈↓αn␈↓␈α
we␈α
associate␈α
a␈α∞␈↓¬single␈↓␈α
entry␈α
called␈α∞the␈α
␈↓↓value cell␈↓.
␈↓ α←␈↓The␈α
binding␈α
and␈α∞unbinding␈α
mechanisms␈α
will␈α∞maintain␈α
the␈α
correct␈α∞value␈α
of
␈↓ α←␈↓the␈α
variable␈αin␈α
the␈αvalue␈α
cell.␈α
The␈αscheme␈α
is␈αa␈α
mixture␈αof␈α
the␈α
two␈αprevious
␈↓ α←␈↓implementations.
␈↓"β␈↓ α←␈↓␈↓ β'The ␈↓αlookup␈↓ routine is similar to the shallow lookup:
␈↓"∀␈↓ α←␈↓α␈↓ ∧{lookup <= λ[[x;env] getval_cell[x]]
␈↓"∀␈↓ α←␈↓Notice that ␈↓αenv␈↓ is ignored in ␈↓αlookup␈↓.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 41␈↓The␈α∞table␈α∞organization␈α∞discussed␈α∞in␈α∞this␈α∞section␈α∞was␈α∞also␈α∞used␈α∞in␈α
a
␈↓ α←␈↓language␈αnamed␈αLC␈↓π2␈↓␈α([Mit 70]).␈αIn␈αtheir␈αterminology,␈α"deep␈αtable"␈αwas␈αcalled
␈↓ α←␈↓"scope␈α
oriented␈α
organization"␈α
and␈α
"shallow␈α
table"␈α
was␈α
called␈α∞"name␈α
oriented
␈↓ α←␈↓organization".
␈↓ α←␈↓␈↓3.11␈↓ ε⊃Binding Strategies and Implementations 153␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈αbinding␈αroutine␈αis␈αa␈αbit␈αmore␈αcomplex.␈α When␈αbinding␈α␈↓αn␈↓␈αwe␈αhave
␈↓ α←␈↓to␈αreplace␈αthe␈α
current␈αvalue␈αcell␈αwith␈α
the␈αnew␈αbinding.␈α
We␈αalso␈αhave␈αto␈α
save
␈↓ α←␈↓the␈α⊃old␈α⊃binding␈α⊃so␈α⊃that␈α⊃it␈α⊃may␈α⊃be␈α⊃restored␈α⊃when␈α⊃the␈α∩sub-computation␈α⊃is
␈↓ α←␈↓completed;␈αthis␈αpart␈α
of␈αthe␈αimplementation␈αis␈α
like␈αdeep␈αbinding.␈α We␈α
perform
␈↓ α←␈↓most␈α∩of␈α∩the␈α∩␈↓αmkenv␈↓␈α∩operations␈α∩as␈α∩given␈α∩on␈α∩page 124,␈α∩but␈α∩require␈α∪a␈α∩new
␈↓ α←␈↓version␈α∞of␈α∞␈↓αsend␈↓.␈α∞Instead␈α∂of␈α∞placing␈α∞the␈α∞new␈α∂bindings␈α∞in␈α∞the␈α∞block␈α∂built␈α∞by
␈↓ α←␈↓␈↓αsend␈↓,␈α
we␈α
place␈α
the␈αcurrent␈α
bindings␈α
there␈α
as␈α
we␈αplace␈α
the␈α
new␈α
bindings␈αin␈α
the
␈↓ α←␈↓value cell. The new definition of ␈↓αsend␈↓ follows:
␈↓"∀␈↓ α←␈↓α␈↓ β
send <= λ[[var;val;block] concat[mkent[var;swp_val_cell[var;val]];block]]
␈↓"∀␈↓ α←␈↓where␈α⊂␈↓αsw_val_cell␈↓␈α⊂places␈α⊂␈↓αval␈↓␈α⊂in␈α⊂the␈α⊂value␈α⊂cell␈α⊂of␈α⊂␈↓αvar␈↓␈α⊂and␈α⊂returns␈α⊂the␈α⊂old
␈↓ α←␈↓value.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αunbinding␈αoperation␈αis␈αeven␈αmore␈αcomplicated.␈αWhen␈αwe␈αleave␈αan
␈↓ α←␈↓environment␈α∞we␈α∞expect␈α∞the␈α∂prior␈α∞environment␈α∞to␈α∞be␈α∞re-established.␈α∂That␈α∞is
␈↓ α←␈↓done␈α∪automatically␈α∪by␈α∩recursion␈α∪in␈α∪the␈α∩deep␈α∪implementation␈α∪and␈α∪in␈α∩the
␈↓ α←␈↓previous␈α∂shallow␈α∂binder.␈α⊂See␈α∂page 117;␈α∂the␈α∂recursive␈α⊂call␈α∂on␈α∂␈↓αeval␈↓␈α⊂with␈α∂the
␈↓ α←␈↓new binding of ␈↓αenviron␈↓ will lose its effect as we leave ␈↓αeval␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'However,␈α⊃the␈α⊂new␈α⊃scheme␈α⊂requires␈α⊃more;␈α⊂we␈α⊃must␈α⊂restore␈α⊃the␈α⊂saved
␈↓ α←␈↓values␈α∞to␈α∞the␈α∞value␈α∞cells,␈α∞and␈α∞recursion␈α∞will␈α∞not␈α∞do␈α∞that␈α∞automatically.␈α∞ We
␈↓ α←␈↓will␈α→discuss␈α_more␈α→of␈α_the␈α→details␈α_of␈α→this␈α_process␈α→in␈α→Section 3.11␈α_and
␈↓ α←␈↓Section 5.19,␈α∂but␈α∞the␈α∂basic␈α∂idea␈α∞is␈α∂to␈α∞swap␈α∂the␈α∂contents␈α∞of␈α∂the␈α∂saved␈α∞block
␈↓ α←␈↓back␈α∞into␈α
the␈α∞value␈α∞cells␈α
as␈α∞we␈α
leave␈α∞the␈α∞inner␈α
call␈α∞on␈α
␈↓αeval␈↓.␈α∞Notice␈α∞that␈α
we
␈↓ α←␈↓cannot␈α
simply␈αthrow␈α
away␈α
the␈αold␈α
bindings␈αsince␈α
a␈α
call␈αon␈α
␈↓αfunction␈↓␈αmay␈α
have
␈↓ α←␈↓occurred.␈α∀The␈α∀␈↓αfunarg␈↓␈α∀triple␈α∀can␈α∪be␈α∀built␈α∀as␈α∀before:␈α∀saving␈α∀the␈α∪current
␈↓ α←␈↓environment␈α⊂name␈α⊂(␈↓¬not␈↓␈α⊂saving␈α⊂the␈α⊂current␈α⊂contents␈α⊂of␈α⊂all␈α⊂the␈α⊂value␈α∂cells).
␈↓ α←␈↓The␈α∂application␈α∂of␈α∂␈↓αfunarg␈↓s␈α∂is␈α∂therefore␈α∂more␈α∂problematic.␈α∂In␈α∂the␈α∞previous
␈↓ α←␈↓binding␈α∃implementations,␈α∃all␈α∃we␈α∃needed␈α∃to␈α∃do␈α∃was␈α∃establish␈α∃the␈α∀saved
␈↓ α←␈↓environment␈α∞name␈α
as␈α∞the␈α
evironment␈α∞to␈α
be␈α∞used␈α
for␈α∞non-local␈α∞searches.␈α
In
␈↓ α←␈↓this␈α⊂latest␈α⊃binder,␈α⊂we␈α⊂must␈α⊃␈↓¬re-establish␈↓␈α⊂the␈α⊂value␈α⊃cells␈α⊂which␈α⊃were␈α⊂current
␈↓ α←␈↓when␈α⊗␈↓αfunction␈↓␈α⊗was␈α∃recognized.␈α⊗ We␈α⊗will␈α∃postpone␈α⊗this␈α⊗discussion␈α∃until
␈↓ α←␈↓Section 5.19 and Section 5.20.
␈↓"β␈↓ α←␈↓␈↓ β'This␈α∞latest␈α∞implementation␈α∞of␈α∞binding␈α∂is␈α∞by␈α∞far␈α∞the␈α∞most␈α∂complex␈α∞we
␈↓ α←␈↓have␈α
seen.␈α
It␈αgives␈α
fast␈α
access␈αto␈α
values␈α
of␈αvariables,␈α
but␈α
requires␈αmore␈α
effort
␈↓ α←␈↓in␈α∂changing␈α∂environments;␈α∂that␈α∂is␈α∂particularly␈α∂evident␈α∂in␈α∂the␈α∂discussion␈α∞of
␈↓ α←␈↓␈↓αfunction␈↓ constructs.␈α" We␈α"will␈α"discuss␈α!the␈α"relative␈α"merits␈α"of␈α!these
␈↓ α←␈↓implementations␈α⊂in␈α⊂more␈α⊃detail␈α⊂in␈α⊂Chapter 5.␈α⊃ Be␈α⊂clear␈α⊂on␈α⊃the␈α⊂distinction
␈↓ α←␈↓between␈α_a␈α↔binding␈α_strategy␈α↔and␈α_a␈α↔binding␈α_implementation.␈α_ The␈α↔two
␈↓ α←␈↓strategies␈α∃we␈α∀have␈α∃discussed␈α∃are␈α∀called␈α∃"dynamic"␈α∀and␈α∃"static";␈α∃the␈α∀two
␈↓ α←␈↓implementations␈α∃are␈α⊗called␈α∃"deep"␈α⊗and␈α∃"shallow".␈α⊗Either␈α∃implementation
␈↓ α←␈↓technique can be used with either binding strategy.
␈↓ α←␈↓␈↓154 Evaluation␈↓
_3.11␈↓
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Suggest␈α∀an␈α∪implementation␈α∀of␈α∪␈↓αaddval␈↓␈α∀which␈α∪will␈α∀improve␈α∀the␈α∪search
␈↓ α←␈↓␈↓ β∂efficiency of ␈↓αlookup␈↓.
␈↓" ␈↓ α←␈↓2. Analyze␈α∩␈↓αlookup␈↓␈α∩in␈α∩a␈α∩manner␈α∩similar␈α∩to␈α∩that␈α∩performed␈α∩on␈α∩␈↓αmkenv␈↓␈α∩on
␈↓ α←␈↓␈↓ β∂page 124.␈α∩Identify␈α∩the␈α∩parts␈α∩of␈α∩␈↓αlookup␈↓␈α∩which␈α∩are␈α∩independent␈α∪of␈α∩the
␈↓ α←␈↓␈↓ β∂binding␈αimplementation.␈α
Rewrite␈αthe␈α
shallow␈αand␈α
deep␈αversions␈αof␈α
␈↓αlookup␈↓
␈↓ α←␈↓␈↓ β∂in this more general setting.
␈↓"β␈↓ α←␈↓␈↓ ¬∧␈↓↓3.12 Special Forms and Macros␈↓
␈↓"β␈↓ α←␈↓We␈α⊗have␈α⊗remarked␈α⊗that␈α⊗the␈α∃evaluation␈α⊗scheme␈α⊗for␈α⊗LISP␈α⊗functions␈α∃is
␈↓ α←␈↓call-by-value␈α≠and,␈α≠for␈α≠functions␈α≠with␈α≠multiple␈α≠arguments,␈α~left-to-right
␈↓ α←␈↓evaluation␈α
of␈α
arguments.␈α
We␈α
have␈α
also␈α
seen,␈α
in␈α
␈↓αquote␈↓␈α
and␈α
␈↓αcond␈↓,␈α
that␈α
not␈αall
␈↓ α←␈↓forms␈α⊂to␈α⊂be␈α∂evaluated␈α⊂in␈α⊂LISP␈α∂fall␈α⊂within␈α⊂this␈α∂category.␈α⊂ The␈α⊂purpose␈α∂of
␈↓ α←␈↓␈↓αquote␈↓␈αwas␈αto␈α␈↓¬stop␈↓␈αevaluation;␈αand␈αthe␈α"argument␈αlist"␈αto␈α␈↓αcond␈↓␈αis␈αalso␈αhandled
␈↓ α←␈↓differently.␈α
Since␈α
␈↓αquote␈↓␈α
and␈α
␈↓αcond␈↓␈αwere␈α
rather␈α
anomalous␈α
we␈α
have␈αcalled␈α
them
␈↓ α←␈↓␈↓↓special␈α⊃forms␈↓.␈α∩Now␈α⊃we␈α∩would␈α⊃like␈α⊃to␈α∩discuss␈α⊃special␈α∩forms␈α⊃as␈α∩a␈α⊃general
␈↓ α←␈↓technique.
␈↓"β␈↓ α←␈↓␈↓ β'Consider␈αthe␈αpredicates␈α␈↓αand␈↓␈αand␈α␈↓αor␈↓.␈α
We␈αmight␈αwish␈αto␈αdefine␈α␈↓αand␈↓␈αto␈α
be
␈↓ α←␈↓a␈αbinary␈αpredicate␈αsuch␈αthat␈α␈↓αand␈↓␈αis␈αtrue␈αjust␈αin␈αcase␈α␈↓¬both␈↓␈αarguments␈αevaluate
␈↓ α←␈↓to␈α∩␈↓
t␈↓,␈α∩and␈α∩define␈α∩␈↓αor␈↓␈α∩to␈α∩be␈α∩binary␈α∩and␈α∩false␈α∩just␈α∩in␈α∩case␈α∩both␈α∩arguments
␈↓ α←␈↓evaluate␈α∂to␈α∂␈↓
f␈↓.␈α∂ Notice␈α∂two␈α∂points.␈α∞First,␈α∂there␈α∂is␈α∂really␈α∂no␈α∂reason␈α∂to␈α∞restrict
␈↓ α←␈↓these␈α∞predicates␈α∂to␈α∞be␈α∂␈↓¬binary␈↓.␈α∞Replacing␈α∂the␈α∞words␈α∂"binary"␈α∞by␈α∂"n-ary"␈α∞and
␈↓ α←␈↓"both"␈αby␈α"all"␈αin␈αthe␈αabove␈αdescription␈αhas␈αthe␈αdesired␈αeffect.␈α Second,␈αif␈αwe
␈↓ α←␈↓evaluate␈α⊂the␈α∂arguments␈α⊂to␈α⊂these␈α∂predicates␈α⊂in␈α∂some␈α⊂order,␈α⊂say␈α∂left-to-right,
␈↓ α←␈↓then␈α∞we␈α∞could␈α∞immediately␈α∞determine␈α∞that␈α∞␈↓αand␈↓␈α∞is␈α∞false␈α∞as␈α∞soon␈α∞as␈α∞we␈α
come
␈↓ α←␈↓across␈α∪an␈α∪argument␈α∪which␈α∪evaluates␈α∪to␈α∪␈↓
f␈↓;␈α∪similarly␈α∪a␈α∪call␈α∪on␈α∪␈↓αor␈↓␈α∪for␈α∪an
␈↓ α←␈↓arbitrary␈αnumber␈αof␈αarguments␈αcan␈αbe␈αterminated␈αas␈αsoon␈αas␈αwe␈αevaluate␈αan
␈↓ α←␈↓argument␈αgiving␈αvalue␈α␈↓
t␈↓.␈α But␈αif␈αwe␈α
insist␈αthat␈α␈↓αand␈↓␈αand␈α␈↓αor␈↓␈αbe␈αLISP␈α
␈↓¬functions␈↓
␈↓ α←␈↓we␈α
can␈α
take␈α
advantage␈α
of␈α
neither␈α
of␈α
these␈α
observations.␈α
Rather␈α
we␈αwill␈α
define
␈↓ α←␈↓␈↓αand␈↓␈α
and␈α
␈↓αor␈↓␈α
as␈α
special␈α∞forms␈α
and␈α
handle␈α
the␈α
evaluation␈α∞ourselves.␈α
Presently,
␈↓ α←␈↓the␈α⊂only␈α⊂way␈α⊂to␈α⊂handle␈α⊂special␈α⊂forms␈α⊂is␈α⊂to␈α⊂make␈α⊂explicit␈α⊂modifications␈α∂to
␈↓ α←␈↓␈↓αeval␈↓.␈↓π 42␈↓
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 42␈↓On␈α∩page 212␈α∩and␈α∩in␈α∩Section 4.10␈α∩we␈α∩will␈α∩discuss␈α∩simple␈α∪ways␈α∩to
␈↓ α←␈↓add such forms without modifying ␈↓αeval␈↓.
␈↓ α←␈↓␈↓3.12␈↓ π>Special Forms and Macros 155␈↓
␈↓"β␈↓ α←␈↓α␈↓Recognizers for the predicates must be added to ␈↓αeval␈↓:
␈↓"∀␈↓ α←␈↓αisand[e] → evand[arglist[e];environ];
␈↓" ␈↓ α←␈↓αisor[e] → evor[arglist[e];environ];
␈↓"∀␈↓ α←␈↓α␈↓where:␈↓α
␈↓"β␈↓ α←␈↓αevand <= λ[[l;a]␈↓ ∧7[null[l]→ ␈↓
t␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ∧7 eval[first[l];a] → evand[rest[l];a];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 ␈↓
t␈↓α → ␈↓
f␈↓α] ]
␈↓"∀␈↓ α←␈↓αevor <= λ[[l;a]␈↓ ∧∨[null[l] → ␈↓
f␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ eval[first[l];a] → ␈↓
t␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ ␈↓
t␈↓α → evor[rest[l];a]] ]
␈↓"∀␈↓ α←␈↓Or, exploiting the duality of ␈↓αand␈↓ and ␈↓αor␈↓ ([Ste pc]):
␈↓"∀␈↓ α←␈↓αisand[e] → evandor[arglist[e];environ;␈↓
t␈↓α];
␈↓" ␈↓ α←␈↓αisor[e] → evandor[arglist[e];environ;␈↓
f␈↓α];
␈↓" ␈↓ α←␈↓αevandor <= λ[[l;a;d]␈↓ ∧g[null[l] → d;
␈↓"β␈↓ α←␈↓α␈↓ ∧g xor[d;eval[first[l];a]] → not[d];
␈↓"β␈↓ α←␈↓α␈↓ ∧g ␈↓
t␈↓α → evandor[rest[l];a;d]]]
␈↓" ␈↓ α←␈↓αxor <= λ[[a;b][a → not[b]; ␈↓
t␈↓α → b]]
␈↓"∀␈↓ α←␈↓In␈α
either␈α
formulation␈αthere␈α
are␈α
explicit␈α
calls␈αon␈α
␈↓αeval␈↓.␈↓π 43␈↓␈α
This␈αseems␈α
expensive,
␈↓ α←␈↓but the arguments must be evaluated.
␈↓"β␈↓ α←␈↓␈↓ β'Special␈α∞forms␈α∂have␈α∞been␈α∞used␈α∂for␈α∞two␈α∞purposes:␈α∂one␈α∞is␈α∞to␈α∂control␈α∞the
␈↓ α←␈↓evaluation␈α⊃of␈α⊃arguments;␈α⊃conditional␈α⊃expressions,␈α⊃quoted␈α⊃expressions,␈α⊃␈↓αand␈↓,
␈↓ α←␈↓and␈α∞␈↓αor␈↓␈α∞are␈α∞examples.␈α∞The␈α∞second␈α∞application␈α∞area␈α∞is␈α∞to␈α∞create␈α∞the␈α∞effect␈α
of
␈↓ α←␈↓call-by-value␈αfunctions␈αwith␈αan␈αindefinite␈αnumber␈αof␈αarguments␈α␈↓¬all␈↓␈αof␈αwhich
␈↓ α←␈↓are␈αto␈αbe␈αevaluated;␈αthe␈αLISP␈αfunctions,␈α␈↓αlist,␈αappend␈↓,␈αand␈α␈↓αplus␈↓␈αare␈αexamples
␈↓ α←␈↓in this category.
␈↓"β␈↓ α←␈↓␈↓ β'Even␈α∩though␈α∩we␈α∩wish␈α∩to␈α∩define␈α∩these␈α∩functions␈α∩as␈α∩if␈α∩they␈α∩had␈α∩an
␈↓ α←␈↓arbitrary␈α∂number␈α∂of␈α∞arguments,␈α∂when␈α∂we␈α∂␈↓¬call␈↓␈α∞the␈α∂function,␈α∂the␈α∂number␈α∞of
␈↓ α←␈↓arguments to be applied ␈↓¬is␈↓ known.␈↓π 44␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Assume,␈α⊂for␈α⊂example,␈α⊂we␈α∂wish␈α⊂to␈α⊂define␈α⊂␈↓αplus␈↓␈α∂as␈α⊂a␈α⊂function␈α⊂with␈α∂an
␈↓ α←␈↓indefinite number of arguments such that:
␈↓"∀␈↓ α←␈↓α␈↓ ε↓plus[4;5] = 9
␈↓" ␈↓ α←␈↓α␈↓ ¬nplus[4;5;6] = 15
␈↓" ␈↓ α←␈↓α␈↓ ¬Jplus[4;add1[2];4] = 11
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 43␈↓Also␈α⊃notice␈α⊃that␈α⊃the␈α⊃abstract␈α⊃versions␈α⊃of␈α⊃␈↓αevand␈↓,␈α⊃␈↓αevor␈↓␈α⊃and␈α⊃␈↓αevandor␈↓
␈↓ α←␈↓␈↓¬know␈↓␈α
that␈αthe␈α
arguments␈αare␈α
represented␈α
as␈αa␈α
sequence␈αand␈α
the␈α
structure␈αof
␈↓ α←␈↓the recursion implies a left-to-right evaluation.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 44␈↓For␈α⊂example,␈α∂at␈α⊂compile␈α⊂time␈α∂the␈α⊂number␈α∂of␈α⊂arguments␈α⊂is␈α∂known,
␈↓ α←␈↓and␈α∂the␈α∞compiler␈α∂can␈α∞often␈α∂generate␈α∂more␈α∞efficient␈α∂code␈α∞than␈α∂just␈α∂calls␈α∞on
␈↓ α←␈↓␈↓αeval␈↓.
␈↓ α←␈↓␈↓156 Evaluation␈↓
_3.12␈↓
␈↓"β␈↓ α←␈↓We can define ␈↓αplus␈↓ in terms of a ␈↓¬binary␈↓ addition function, ␈↓α*plus␈↓.
␈↓"∀␈↓ α←␈↓α␈↓ ¬-plus[4;5] = *plus[4;5] = 9
␈↓" ␈↓ α←␈↓α␈↓ ∧←plus[4;5;6] = *plus[4;*plus[5;6]] = 15
␈↓" ␈↓ α←␈↓α␈↓ ∧∃plus[4;add1[2];4] = *plus[4;*plus[add1[2];4]] = 11
␈↓"∀␈↓ α←␈↓We␈α␈↓↓expand␈↓␈αcalls␈αon␈α␈↓αplus␈↓␈αinto␈αa␈αcomposition␈αof␈αcalls␈αon␈α␈↓α*plus␈↓.␈α ␈↓αplus␈↓␈αis␈αbeing
␈↓ α←␈↓used␈αas␈α
a␈α␈↓↓macro␈↓␈α
and␈αthe␈αexpansion␈α
process␈αin␈α
terms␈αof␈α
␈↓α*plus␈↓␈αis␈αcalled␈α
␈↓↓macro
␈↓ α←␈↓↓expansion␈↓. The macro expansion generates a composition of calls on ␈↓α*plus␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α∂macro␈α∞definition␈α∂is␈α∞a␈α∂λ-expression␈α∞of␈α∂␈↓¬one␈↓␈α∞argument.␈α∂The␈α∞call␈α∂on␈α∞a
␈↓ α←␈↓macro␈α∂looks␈α∂just␈α⊂like␈α∂an␈α∂ordinary␈α⊂function␈α∂call,␈α∂but␈α⊂what␈α∂is␈α∂bound␈α⊂to␈α∂the
␈↓ α←␈↓λ-variable␈α
is␈α
the␈α
whole␈αcall␈α
on␈α
the␈α
macro.␈↓π 45␈↓␈α
The␈αtask␈α
of␈α
the␈α
macro␈α
body␈αis␈α
to
␈↓ α←␈↓expand␈α⊗the␈α⊗macro␈α⊗call␈α⊗and␈α⊗return␈α⊗this␈α⊗expansion␈α⊗as␈α⊗its␈α⊗value.␈α⊗ This
␈↓ α←␈↓expanded␈αform␈αis␈αpassed␈αback␈αthrough␈α␈↓αeval␈↓␈αto␈αcomplete␈αthe␈αcomputation.␈α In
␈↓ α←␈↓Section 4.10␈α∪we␈α∪will␈α∪discuss␈α∪the␈α∪additional␈α∪mechanisms␈α∪which␈α∪evaluators
␈↓ α←␈↓must possess for execution of macros.
␈↓"β␈↓ α←␈↓␈↓ β'Let's␈α⊗define␈α⊗␈↓α<␈↓βm␈↓α=␈↓␈α⊗to␈α⊗mean␈α⊗" is defined to be the macro ...".␈α↔ Then␈α⊗a
␈↓ α←␈↓simple macro definition of ␈↓αplus␈↓ might be:
␈↓"∀␈↓ α←␈↓α␈↓ β∂plus <␈↓βm␈↓α= λ[[l]␈↓ ∧O[eq[length[l];3] → concat[*PLUS;rest[l]];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧O ␈↓
t␈↓α → list[␈↓ ¬;*PLUS;
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧O␈↓ ¬;second[l];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧O␈↓ ¬;concat[PLUS;rest[rest[l]]]]]]
␈↓"∀␈↓ α←␈↓Thus␈α
a␈αcall␈α
␈↓α(PLUS␈α3␈α
4␈α5)␈↓␈α
would␈αbind␈α
␈↓αl␈↓␈αto␈α
␈↓α(PLUS␈α3␈α
4␈α5)␈↓␈α
and␈αthe␈α
evaluation
␈↓ α←␈↓of␈α⊂the␈α∂body␈α⊂would␈α∂result␈α⊂in␈α∂␈↓α(*PLUS␈α⊂3␈α∂(PLUS␈α⊂4␈α∂5))␈↓.␈α⊂ Evaluation␈α⊂of␈α∂this
␈↓ α←␈↓expression␈αwould␈αresult␈αin␈αanother␈αcall␈α
on␈αthe␈αmacro.␈α This␈αtime␈α␈↓αl␈↓␈α
would␈αbe
␈↓ α←␈↓bound␈αto␈α␈↓α(PLUS␈α4␈α5)␈↓.␈αNow␈α␈↓αeq[length[l];3]␈↓␈α␈↓↓is␈↓␈αtrue␈αand␈αthe␈αvalue␈α
returned␈αis
␈↓ α←␈↓␈↓α(*PLUS␈α4␈α5)␈↓.␈α This␈αwill␈αbe␈αevaluated,␈αgiving␈α␈↓α9␈↓;␈αfinally␈αthe␈αoutermost␈αcall␈αon
␈↓ α←␈↓␈↓α*PLUS␈↓ has all its arguments evaluated, and we get the final answer, ␈↓α12␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'A more general macro expander can be described as:
␈↓"∀␈↓ α←␈↓αexpand <= λ[[fn;l]␈↓ ∧O[null[rest[l]] → l;
␈↓"β␈↓ α←␈↓α␈↓ ∧O ␈↓
t␈↓α → list[␈↓ ¬Gfn;
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬Gfirst[l];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬Gexpand[fn;rest[l]]]]]
␈↓"∀␈↓ α←␈↓Then we can define ␈↓αplus␈↓ <␈↓βm␈↓α= λ[[l] expand[*PLUS;rest[l]]]␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'In a similar manner,
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓αappend <␈↓βm␈↓α= λ[[l] expand[*APPEND;rest[l]]]␈↓
␈↓"∀␈↓ α←␈↓where ␈↓α*append␈↓ is the binary version of ␈↓αappend␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Since␈α↔the␈α↔body␈α↔of␈α↔a␈α↔macro␈α↔has␈α↔available␈α↔all␈α↔of␈α↔the␈α↔evaluation
␈↓ α←␈↓mechanism␈α
of␈α
LISP,␈α
and␈αsince␈α
the␈α
program␈α
structure␈αof␈α
LISP␈α
is␈α
also␈αthe␈α
data
␈↓ α←␈↓structure,␈αwe␈αcan␈αperform␈αarbitrary␈αcomputations␈αinside␈αthe␈αexpansion␈αof␈αthe
␈↓ α←␈↓macro.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 45␈↓A similar device, called "meta composition", is used in [Bac 73].
␈↓ α←␈↓␈↓3.12␈↓ π>Special Forms and Macros 157␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∪idea␈α∀of␈α∪macro␈α∀processing␈α∪is␈α∀not␈α∪recent.␈α∀ Some␈α∪of␈α∀the␈α∪earliest
␈↓ α←␈↓assemblers␈α
had␈αextensive␈α
macro␈α
facilities.␈α Lately␈α
macros␈α
have␈αbeen␈α
used␈αas␈α
a
␈↓ α←␈↓means␈α
of␈α
extending␈α
so-called␈α
high␈α
level␈α
languages.␈α
One␈α
of␈α
the␈α
most␈α
simple
␈↓ α←␈↓kinds␈α
of␈αmacros␈α
is␈α␈↓↓textual␈α
substitution␈↓.␈α
That␈αis,␈α
when␈αa␈α
use␈αof␈α
a␈α
macro␈αis
␈↓ α←␈↓detected␈αwe␈αsimply␈αreplace␈αthe␈αcall␈αby␈αits␈αbody.␈α A␈αslightly␈αmore␈α
sophisticated
␈↓ α←␈↓application␈α
is␈α∞the␈α
␈↓↓syntax␈α∞macro␈↓.␈α
Everytime␈α
we␈α∞come␈α
across␈α∞an␈α
application
␈↓ α←␈↓of␈α∂a␈α∂syntax␈α∂macro␈α∂the␈α∂expander␈α⊂processes␈α∂it␈α∂as␈α∂if␈α∂it␈α∂had␈α∂never␈α⊂been␈α∂seen
␈↓ α←␈↓before␈α⊂even␈α⊂though␈α∂much␈α⊂of␈α⊂the␈α⊂expansion␈α∂is␈α⊂repetitious.␈α⊂That␈α⊂is,␈α∂syntax
␈↓ α←␈↓macros have no memory.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓↓Computational␈α→macros␈↓␈α→are␈α→an␈α~attempt␈α→to␈α→reduce␈α→some␈α~of␈α→this
␈↓ α←␈↓repetition.␈α In␈αthis␈αscheme␈αa␈αcertain␈αamount␈αof␈αprocessing␈αis␈αdone␈αat␈αthe␈αtime
␈↓ α←␈↓the␈α⊃macro␈α⊃is␈α⊃␈↓¬defined␈↓.␈α⊃For␈α⊃example␈α⊃a␈α⊃computational␈α⊃subtree␈α⊃reflecting␈α⊃the
␈↓ α←␈↓body␈α
of␈αthe␈α
macro␈α
might␈αbe␈α
formed.␈α Then␈α
whenever␈α
the␈αmacro␈α
is␈α
␈↓¬used␈↓␈αwe
␈↓ α←␈↓can␈α⊃simply␈α⊃make␈α⊃a␈α⊃copy␈α⊃of␈α⊃this␈α⊃subtree␈α⊃and␈α⊃"glue"␈α⊃this␈α⊃subtree␈α∩into␈α⊃the
␈↓ α←␈↓parse-tree␈α∞which␈α∞we␈α
are␈α∞building.␈α∞ This␈α
computational␈α∞subtree␈α∞is␈α
commonly
␈↓ α←␈↓formed␈αby␈αpassing␈αthe␈αbody␈αof␈αthe␈αmacro␈αthrough␈αthe␈αlanguage␈αprocessor␈αin
␈↓ α←␈↓a␈α∩slightly␈α∩non-standard␈α∩way.␈α⊃ The␈α∩main␈α∩problem␈α∩with␈α∩the␈α⊃comput`πional
␈↓ α←␈↓macro␈α
is␈αthat␈α
there␈αare␈α
many␈α
desirable␈αmacros␈α
which␈αhave␈α
no␈α
such␈αsubtree,
␈↓ α←␈↓or␈α⊃there␈α⊃is␈α∩other␈α⊃information␈α⊃necessary␈α⊃to␈α∩process␈α⊃the␈α⊃macro.␈α∩ There␈α⊃are
␈↓ α←␈↓solutions␈α∞to␈α∞this␈α∞problem,␈α∞one␈α∞of␈α∞which␈α∞closely␈α∞parallels␈α∞the␈α∞abstract␈α
syntax
␈↓ α←␈↓ideas␈α∂of␈α∂McCarthy.␈α∂ All␈α∂of␈α∂these␈α∂styles␈α∂of␈α∂macros␈α∂are␈α∂subsets␈α∂of␈α⊂the␈α∂LISP
␈↓ α←␈↓macros.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. What␈α~is␈α~the␈α→difference␈α~between␈α~a␈α→special␈α~form␈α~and␈α→call-by-name
␈↓ α←␈↓␈↓ β∂evaluation?␈α∂Can␈α∂call-by-name␈α∂be␈α∂simulated␈α∂in␈α∂LISP␈α⊂without␈α∂redefining
␈↓ α←␈↓␈↓ β∂␈↓αeval␈↓?
␈↓" ␈↓ α←␈↓2. ␈↓αselect␈↓␈α
is␈α
a␈α
special␈α
form␈α
to␈α
be␈α
called␈α
as:␈α
␈↓αselect[q;q␈↓β1␈↓α;e␈↓β1␈↓α; ... ;q␈↓βn␈↓α;e␈↓βn␈↓α;e]␈↓␈α
and␈α
to␈α
be
␈↓ α←␈↓␈↓ β∂evaluated␈α⊂as␈α∂follows:␈α⊂␈↓αq␈↓␈α∂is␈α⊂evaluated;␈α∂the␈α⊂␈↓αq␈↓βi␈↓'s␈α∂are␈α⊂evaluated␈α∂from␈α⊂left␈α∂to
␈↓ α←␈↓␈↓ β∂right␈α∞until␈α∞one␈α∞is␈α∞found␈α
with␈α∞the␈α∞value␈α∞of␈α∞␈↓αq␈↓.␈α
The␈α∞value␈α∞of␈α∞␈↓αselect␈↓␈α∞is␈α
the
␈↓ α←␈↓␈↓ β∂value␈αof␈αthe␈αcorresponding␈α␈↓αe␈↓βi␈↓.␈αIf␈αno␈αsuch␈α␈↓αq␈↓βi␈↓␈αis␈αfound␈αthe␈αvalue␈αof␈α␈↓αselect␈↓␈αis
␈↓ α←␈↓␈↓ β∂the␈α
value␈α
of␈α␈↓αe␈↓.␈α
The␈α
␈↓αselect␈↓␈αoperator␈α
is␈α
a␈αprecursor␈α
of␈α
the␈α
␈↓↓case␈αstatement␈↓;
␈↓ α←␈↓␈↓ β∂see␈α⊃page 193.␈α∩ Add␈α⊃a␈α⊃recognizer␈α∩to␈α⊃␈↓αeval␈↓␈α⊃to␈α∩handle␈α⊃␈↓αselect␈↓␈α⊃and␈α∩write␈α⊃a
␈↓ α←␈↓␈↓ β∂function to perform the evaluation of ␈↓αselect␈↓.
␈↓" ␈↓ α←␈↓3. Define ␈↓αlist␈↓ as a macro.
␈↓" ␈↓ α←␈↓4. Extend the ␈↓αeval-apply␈↓ pair of Section 3.5 to handle macros.
␈↓"β␈↓ α←␈↓␈↓ ¬≥␈↓↓3.13 Review and Reflection␈↓
␈↓"β␈↓ α←␈↓¬␈↓ β'"I␈α∞think␈α∞that␈α∞it␈α∞is␈α∞important␈α∞to␈α∞maintain␈α∞a␈α∞view␈α∞of␈α∞the␈α∂field␈α∞that
␈↓ α←␈↓¬␈↓ β'helps minimize the distance between theoretical and practical work."
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ πQSaul Amarel, [Ama 72]
␈↓ α←␈↓␈↓158 Evaluation␈↓
_3.13␈↓
␈↓"β␈↓ α←␈↓By␈α
way␈α
of␈α
review␈α
we␈α
sketch␈αthe␈α
basic␈α
LISP␈α
evaluator␈α
of␈α
Section 3.5:␈α␈↓αeval␈↓␈α
plus
␈↓ α←␈↓the additional artifacts for ␈↓αlabel␈↓ and ␈↓αfunction␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α⊃are␈α∩two␈α⊃arguments␈α∩to␈α⊃␈↓αeval␈↓:␈α∩a␈α⊃␈↓↓form␈↓,␈↓π 46␈↓␈α∩that␈α⊃is,␈α∩an␈α⊃expression
␈↓ α←␈↓which␈αcan␈αbe␈αevaluated;␈αand␈αan␈αassociation␈αlist␈αor␈α␈↓↓symbol␈αtable␈↓.␈αIf␈αthe␈αform
␈↓ α←␈↓is␈α
a␈α
constant,␈α
return␈α
that␈α
form.␈α
If␈αthe␈α
form␈α
is␈α
a␈α
variable,␈α
find␈α
the␈α
value␈αof␈α
the
␈↓ α←␈↓variable␈αin␈αthe␈αcurrent␈αenvironment.␈α If␈αthe␈αform␈αis␈αa␈αconditional␈αexpression,
␈↓ α←␈↓then evaluate it according to the semantics of conditional expressions.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αform␈αmight␈αalso␈αbe␈αa␈αfunctional␈αargument.␈αIn␈αthis␈αcase␈αevaluation
␈↓ α←␈↓consists␈α⊗of␈α↔associating␈α⊗the␈α⊗current␈α↔environment␈α⊗with␈α⊗the␈α↔function␈α⊗and
␈↓ α←␈↓returning␈α∩that␈α⊃construct␈α∩as␈α⊃value;␈α∩in␈α∩LISP␈α⊃this␈α∩is␈α⊃done␈α∩with␈α∩the␈α⊃␈↓αfunarg␈↓
␈↓ α←␈↓device.␈α Any␈αother␈αform␈αseen␈αby␈α␈↓αeval␈↓␈α
is␈αassumed␈αto␈αbe␈αan␈αapplication,␈αthat␈α
is,
␈↓ α←␈↓a␈α∃function␈α∀followed␈α∃by␈α∀arguments.␈α∃ The␈α∀arguments␈α∃are␈α∃evaluated␈α∀from
␈↓ α←␈↓left-to-right and the function is then applied to these arguments.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αpart␈αof␈αthe␈α
evaluator␈αwhich␈αhandles␈αfunction␈αapplication␈α
is␈αcalled
␈↓ α←␈↓␈↓αapply␈↓.␈α⊂ ␈↓αapply␈↓␈α⊃takes␈α⊂three␈α⊂arguments:␈α⊃a␈α⊂LISP␈α⊂function,␈α⊃a␈α⊂list␈α⊃of␈α⊂evaluated
␈↓ α←␈↓arguments,␈α∩and␈α∩a␈α⊃symbol␈α∩table.␈α∩If␈α⊃the␈α∩function␈α∩is␈α⊃one␈α∩of␈α∩the␈α∩five␈α⊃LISP
␈↓ α←␈↓primitives␈α⊂then␈α⊂the␈α⊂appropriate␈α⊂action␈α⊂is␈α⊂carried␈α⊂out.␈α⊂If␈α⊂the␈α⊂function␈α⊂is␈α⊂a
␈↓ α←␈↓λ-expression␈α⊗then␈α↔bind␈α⊗the␈α⊗formal␈α↔parameters␈α⊗(the␈α⊗λ-variables)␈α↔to␈α⊗the
␈↓ α←␈↓evaluated␈α∂arguments␈α∂and␈α∂evaluate␈α⊂the␈α∂body␈α∂of␈α∂the␈α∂function.␈α⊂The␈α∂function
␈↓ α←␈↓might␈αalso␈αbe␈αthe␈αresult␈αof␈αa␈αfunctional␈αargument␈αbinding;␈αin␈αthis␈αcase␈αapply
␈↓ α←␈↓the␈α∞function␈α∞to␈α∞the␈α∞arguments␈α∞and␈α∞use␈α∞the␈α∞saved␈α∞environment,␈α∞rather␈α∞than
␈↓ α←␈↓the␈αgiven␈αenvironment,␈αto␈αsearch␈αfor␈αnon-local␈αbindings.␈α If␈αwe␈α
are␈αapplying
␈↓ α←␈↓the␈α
␈↓αlabel␈↓␈α
operator,␈α
we␈α
build␈α
a␈α
␈↓αfunarg␈↓-triple␈α
and␈α
new␈α
environment␈α
such␈αthat
␈↓ α←␈↓the␈αenvironment␈αbound␈αin␈αthe␈αtriple␈αis␈αthe␈αnew␈αenvironment.␈αWe␈αthen␈αapply
␈↓ α←␈↓the function to the arguments in that environment.
␈↓"β␈↓ α←␈↓␈↓ β'If␈α∩the␈α∪function␈α∩has␈α∩a␈α∪name␈α∩we␈α∪look␈α∩up␈α∩that␈α∪name␈α∩in␈α∪the␈α∩current
␈↓ α←␈↓environment.␈α∩ We␈α⊃expect␈α∩that␈α⊃value␈α∩to␈α⊃be␈α∩a␈α⊃λ-expression,␈α∩which␈α∩is␈α⊃then
␈↓ α←␈↓applied.␈αHowever,␈αsince␈αfunction␈αnames␈αare␈αjust␈αvariables,␈αthere␈αis␈αno␈αreason
␈↓ α←␈↓that␈αthe␈αvalue␈αof␈αa␈αfunction␈αname␈αcould␈αnot␈αbe␈αa␈αsimple␈αvalue,␈αsay␈αan␈αatom,
␈↓ α←␈↓and␈α
␈↓¬that␈↓␈α
value␈α
can␈α
be␈α∞applied␈α
to␈α
the␈α
arguments.␈α
Examination␈α
of␈α∞␈↓αapply␈↓␈α
on
␈↓ α←␈↓page 117␈α∞will␈α∞show␈α∞that␈α∞␈↓αapply[X; ((A B)) ; ((X . CAR) ... )]␈↓␈α∞␈↓¬will␈↓␈α∞be␈α∞handled
␈↓ α←␈↓correctly.␈α A␈αgeneralization␈αof␈αthis␈αidea␈αis␈αpossible.␈α If␈αthe␈αfunction␈αpassed␈αto
␈↓ α←␈↓␈↓αapply␈↓␈α∩is␈α∩not␈α∩recognized␈α∩as␈α∪one␈α∩of␈α∩the␈α∩preceding␈α∩cases,␈α∩then␈α∪continue␈α∩to
␈↓ α←␈↓evaluate␈α⊂the␈α⊂function-part␈α∂until␈α⊂it␈α⊂is␈α∂recognized.␈α⊂Such␈α⊂functions␈α⊂are␈α∂called
␈↓ α←␈↓␈↓↓computed function␈↓s.
␈↓"β␈↓ α←␈↓␈↓ β'For example, we will allow such forms as:
␈↓"∀␈↓ α←␈↓α␈↓ ∧
((CAR (QUOTE (CAR (A . B)))) (QUOTE (A . B)))
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 46␈↓Throughout␈α&this␈α§ion␈α&we␈α&will␈α&say␈α&"form",␈α&"variable",
␈↓ α←␈↓"λ-expression",␈α
etc.␈α
rather␈α
than␈α
"a␈α
representation␈α
of␈α
a"␈α
...␈α
"form",␈α
"variable",
␈↓ α←␈↓"λ-expression",␈α⊂etc.␈α⊂No␈α⊂confusion␈α⊂should␈α⊂result,␈α⊂but␈α⊂remember␈α⊂that␈α⊂we␈α⊂␈↓¬are␈↓
␈↓ α←␈↓speaking imprecisely.
␈↓ α←␈↓␈↓3.13␈↓ πfReview and Reflection 159␈↓
␈↓"∀␈↓ α←␈↓The introduction of computed functions modifies ␈↓αapply␈↓ (page 117) slightly:
␈↓"∀␈↓ α←␈↓αapply <= λ[[fn;args;environ]␈↓ ¬←[iscar[fn] → car[arg␈↓β1␈↓α[args]];
␈↓"β␈↓ α←␈↓α␈↓ ¬← iscons[fn] → cons[arg␈↓β1␈↓α[args];arg␈↓β2␈↓α[args]];
␈↓"β␈↓ α←␈↓α␈↓ ¬← ... ...
␈↓"β␈↓ α←␈↓α␈↓ ¬← islambda[fn] → eval[␈↓ πsbody[fn];
␈↓"β␈↓ α←␈↓α␈↓ ¬←␈↓ εo␈↓ πspairlis[␈↓ λSvars[fn];
␈↓"β␈↓ α←␈↓α␈↓ ¬←␈↓ εo␈↓ πs␈↓ λSargs;
␈↓"β␈↓ α←␈↓α␈↓ ¬←␈↓ εo␈↓ πs␈↓ λSenviron]];
␈↓"β␈↓ α←␈↓α␈↓ ¬← ␈↓
t␈↓α → apply[␈↓ εoeval[fn;environ];
␈↓"β␈↓ α←␈↓α␈↓ ¬←␈↓ εoargs;
␈↓"β␈↓ α←␈↓α␈↓ ¬←␈↓ εoenviron] ]]
␈↓"∀␈↓ α←␈↓The␈α⊂subset␈α⊂of␈α⊂LISP␈α⊂which␈α⊂is␈α⊃captured␈α⊂by␈α⊂this␈α⊂evaluator␈α⊂is␈α⊂a␈α⊃strong␈α⊂and
␈↓ α←␈↓useful␈α~language␈α~even␈α~though␈α~it␈α→lacks␈α~several␈α~of␈α~the␈α~more␈α→common
␈↓ α←␈↓programming␈α∀language␈α∪features.␈↓π 47␈↓␈α∀This␈α∀subset␈α∪is␈α∀called␈α∀the␈α∪␈↓↓applicative
␈↓ α←␈↓↓subset␈αof␈αLISP␈↓,␈αsince␈αits␈αcomputational␈αability␈αis␈αbased␈αon␈αthe␈αmathematical
␈↓ α←␈↓idea␈α∩of␈α∩function␈α∪application.␈α∩We␈α∩have␈α∪persistently␈α∩referred␈α∩to␈α∪our␈α∩LISP
␈↓ α←␈↓procedures␈α∂as␈α∞LISP␈α∂functions,␈α∂even␈α∞though␈α∂we␈α∞have␈α∂seen␈α∂some␈α∞differences
␈↓ α←␈↓between␈α↔the␈α↔concept␈α↔of␈α↔function␈α↔in␈α↔mathematics␈α↔and␈α↔the␈α_concepts␈α↔of
␈↓ α←␈↓procedure␈α∩in␈α∪LISP.␈α∩It␈α∩is␈α∪the␈α∩mathematical␈α∩idea␈α∪of␈α∩"function"␈α∪which␈α∩the
␈↓ α←␈↓"procedures"␈α≡of␈α≥our␈α≡applicative␈α≥programming␈α≡language␈α≥approximate.
␈↓ α←␈↓Regardless␈α⊂of␈α⊂differences␈α⊂in␈α∂syntax␈α⊂and␈α⊂evaluation␈α⊂schemes,␈α⊂the␈α∂dominant
␈↓ α←␈↓characteristic␈αof␈αan␈αapplicative␈αlanguage␈αis␈αthat␈αa␈αgiven␈α"function"␈αapplied␈α
to
␈↓ α←␈↓a␈α⊗given␈α⊗set␈α∃of␈α⊗arguments␈α⊗␈↓¬always␈↓␈α⊗produces␈α∃the␈α⊗same␈α⊗result:␈α⊗either␈α∃the
␈↓ α←␈↓computation␈α∩produces␈α∩an␈α∩error,␈α∩or␈α∩it␈α∩doesn't␈α∩terminate,␈α∩or␈α∩it␈α∪produces␈α∩a
␈↓ α←␈↓specific␈α∂value.␈α∂ The␈α∂applicative␈α∂subset␈α∂of␈α∂LISP␈α∂uses␈α∂dynamic␈α⊂binding␈α∂and
␈↓ α←␈↓therefore␈α⊂the␈α⊂occurrence␈α⊂of␈α⊂free␈α∂variables␈α⊂calls␈α⊂the␈α⊂environment␈α⊂into␈α∂play.
␈↓ α←␈↓But␈α
still,␈α
we␈α
have␈α
no␈α
way␈α
to␈α
destructively␈α
change␈α
the␈αenvironment.␈α
Constructs
␈↓ α←␈↓which␈α∀do␈α∃have␈α∀such␈α∃an␈α∀ability␈α∀are␈α∃said␈α∀to␈α∃have␈α∀␈↓↓side-effects␈↓␈α∃and␈α∀are
␈↓ α←␈↓discussed in the next chapter.
␈↓"β␈↓ α←␈↓␈↓ β'LISP␈α⊃was␈α⊂the␈α⊃first␈α⊃language␈α⊂to␈α⊃exploit␈α⊃procedures␈α⊂as␈α⊃objects␈α⊃of␈α⊂the
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 47␈↓It␈α∪is␈α∀"strong",␈α∪both␈α∪practically␈α∀and␈α∪theoretically.␈α∪It␈α∀is␈α∪sufficiently
␈↓ α←␈↓powerful␈α⊂to␈α⊃be␈α⊂"universal"␈α⊃in␈α⊂the␈α⊃sense␈α⊂that␈α⊃all␈α⊂computable␈α⊃functions␈α⊂are
␈↓ α←␈↓representable␈α⊃in␈α⊂LISP.␈α⊃In␈α⊂fact␈α⊃the␈α⊂␈↓αeval-apply␈↓␈α⊃pair␈α⊂represents␈α⊃a␈α⊂"universal
␈↓ α←␈↓function",␈α∃capable␈α∀of␈α∃simulating␈α∀the␈α∃behavior␈α∀of␈α∃any␈α∃other␈α∀computable
␈↓ α←␈↓function.␈α∪The␈α∪usual␈α∪arguments␈α∩about␈α∪the␈α∪halting␈α∪problem ([Rog 67]␈α∩and
␈↓ α←␈↓page 181)␈αand␈αrelated␈αmatters␈αare␈α
easily␈αexpressed␈αand␈αproved␈αin␈α
this␈αLISP.
␈↓ α←␈↓Indeed,␈α↔the␈α↔original␈α↔motivation␈α↔for␈α↔introducing␈α↔the␈α_M-to-S␈α↔expression
␈↓ α←␈↓mapping␈α
was␈α
to␈α
express␈αthe␈α
language␈α
constructs␈α
in␈αthe␈α
data␈α
domain␈α
so␈αthat
␈↓ α←␈↓universality␈α∀could␈α∃be␈α∀demonstrated.␈α∀ "S-expression LISP"␈α∃was␈α∀used␈α∃as␈α∀a
␈↓ α←␈↓programming␈α∃language␈α∀only␈α∃as␈α∀an␈α∃afterthought.␈α∀ It␈α∃was␈α∃S. Russell␈α∀who
␈↓ α←␈↓convinced␈α⊂J. McCarthy␈α∂that␈α⊂the␈α∂theoretical␈α⊂device␈α∂represented␈α⊂in␈α⊂␈↓αeval␈↓␈α∂and
␈↓ α←␈↓␈↓αapply␈↓ could in fact be programmed on the IBM704.
␈↓ α←␈↓␈↓160 Evaluation␈↓
_3.13␈↓
␈↓"β␈↓ α←␈↓language.␈α⊂ The␈α⊂idea␈α⊂has␈α⊃been␈α⊂generalized␈α⊂substantially␈α⊂in␈α⊃the␈α⊂intervening
␈↓ α←␈↓years.␈α_A␈α_concise␈α_statement␈α_of␈α_the␈α_more␈α_general␈α_principle␈α→appears␈α_in
␈↓ α←␈↓[Pop 68a].
␈↓"β␈↓ α←␈↓␈↓ βW"...This␈αbrings␈αus␈αto␈αthe␈αsubject␈αof␈αitems.␈α Anything␈αwhich
␈↓ α←␈↓␈↓ βWcan␈α∞be␈α∞the␈α∞value␈α
of␈α∞a␈α∞variable␈α∞is␈α
an␈α∞item.␈α∞␈↓¬All␈α∞items␈α
have
␈↓ α←␈↓¬␈↓ βWcertain fundamental rights.␈↓
␈↓"β␈↓ α←␈↓␈↓ β{1. All items can be the actual parameters of functions
␈↓"β␈↓ α←␈↓␈↓ β{2. All items can be returned as results of functions
␈↓"β␈↓ α←␈↓␈↓ β{3. All items can be the subject of assignment statements␈↓π 48␈↓
␈↓"β␈↓ α←␈↓␈↓ β{4. All items can be tested for equality.
␈↓"β␈↓ α←␈↓␈↓ βW..."
␈↓"β␈↓ α←␈↓LISP␈αperforms␈α
well␈αon␈α
the␈αfirst␈αtwo␈α
principles,␈αallowing␈α
LISP␈αfunctions␈αto␈α
be
␈↓ α←␈↓the␈α⊂arguments␈α⊂as␈α⊂well␈α⊂as␈α⊂the␈α⊂results␈α⊂of␈α⊂functions.␈α⊂The␈α⊂fourth␈α⊂principle␈α⊂is
␈↓ α←␈↓more␈αdifficult;␈αthat␈αis,␈αtest␈αfor␈αthe␈αequality␈αof␈αtwo␈αLISP␈αfunctions.␈α In␈αcan␈αbe
␈↓ α←␈↓shown␈α∞([Rog 67])␈α∞that␈α∞no␈α∂algorithm␈α∞can␈α∞be␈α∞constructed␈α∞which␈α∂will␈α∞perform
␈↓ α←␈↓such␈α∀a␈α∪test␈α∀for␈α∪arbitrary␈α∀functions.␈α∪ However␈α∀in␈α∪more␈α∀selective␈α∪settings,
␈↓ α←␈↓program␈α∃equality␈α∃can␈α∃be␈α∃established,␈α∃both␈α∃theoretically␈α∃([Man 74]),␈α∀and
␈↓ α←␈↓practically ([Boy 75], [Dar 73], [Car 76]).
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∪language␈α∪POP-2 ([Pop 68])␈α∪has␈α∪also␈α∪generalized␈α∪the␈α∪notion␈α∩of
␈↓ α←␈↓function␈α∞application␈α∞in␈α∞a␈α∞slight,␈α
but␈α∞significant,␈α∞way.␈α∞ The␈α∞generalization␈α
is
␈↓ α←␈↓called ␈↓↓partial application␈↓. Given a function
␈↓"∀␈↓ α←␈↓α␈↓ ¬Wf <= λ[[x␈↓β1␈↓α; ... ;x␈↓βn␈↓α] ␈↓λx␈↓α]␈↓
␈↓"∀␈↓ α←␈↓we␈αcompute␈αa␈αnew␈αfunction␈αof␈α␈↓αn-m␈↓␈αarguments␈αby␈αapplying␈α␈↓αf␈↓␈αto␈α␈↓αm␈↓␈αarguments
␈↓ α←␈↓and obtain a function of the remaining arguments ␈↓αx␈↓βm+1␈↓ through ␈↓αx␈↓βn␈↓:
␈↓"∀␈↓ α←␈↓α␈↓ ¬∪ λ[[x␈↓βm+1␈↓α; ...x␈↓βn␈↓α] ␈↓λx'␈↓α] = f[t␈↓β1␈↓α; ...; t␈↓βm␈↓α]
␈↓"∀␈↓ α←␈↓where␈α∃␈↓λx'␈↓␈α⊗results␈α∃from␈α⊗␈↓λx␈↓␈α∃by␈α⊗binding␈α∃␈↓αx␈↓β1␈↓␈α⊗through␈α∃␈↓αx␈↓βm␈↓␈α⊗to␈α∃␈↓αt␈↓β1␈↓␈α⊗through␈α∃␈↓αt␈↓βm␈↓
␈↓ α←␈↓respectively.␈↓π 49␈↓␈α
Further␈α∞generalizations␈α
of␈α∞partial␈α
application␈α∞are␈α
imaginable
␈↓ α←␈↓([Sta 74]).
␈↓"β␈↓ α←␈↓␈↓ β'We␈αhave␈α
pointed␈αout␈α
several␈α"procedural"␈α
differences.␈αOur␈αtreatment␈α
of
␈↓ α←␈↓conditional␈α~expressions␈α≠differs␈α~from␈α~the␈α≠usual␈α~treatment␈α≠of␈α~function
␈↓ α←␈↓application:␈α∀our␈α∀standard␈α∀rule␈α∪for␈α∀function␈α∀application␈α∀is␈α∪"call by value"
␈↓ α←␈↓which␈α∪requires␈α∪the␈α∪evaluation␈α∪of␈α∩all␈α∪arguments␈α∪before␈α∪calling␈α∪the␈α∩LISP
␈↓ α←␈↓function;␈α⊗only␈α⊗some␈α⊗of␈α↔the␈α⊗arguments␈α⊗to␈α⊗a␈α⊗conditional␈α↔expression␈α⊗are
␈↓ α←␈↓evaluated.␈α∂Note␈α∂that␈α∂the␈α∂whole␈α∂question␈α∂of␈α∂"evaluation␈α∂of␈α∂arguments"␈α⊂is␈α∂a
␈↓ α←␈↓procedural␈αone;␈αarguments␈αto␈αfunctions␈αaren't␈α"evaluated"␈αor␈α"not␈αevaluated",
␈↓ α←␈↓they just "are".
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∂have␈α⊂seen␈α∂different␈α∂algorithms␈α⊂computing␈α∂the␈α∂same␈α⊂function;␈α∂for
␈↓ α←␈↓example␈α⊃␈↓αfact␈↓␈α⊃(page 47␈α⊃and␈α∩page 149)␈α⊃and␈α⊃␈↓αfact␈↓β1␈↓ (page 47)␈α⊃all␈α∩compute␈α⊃the
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 48␈↓This issue will be postponed until Chapter 4.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 49␈↓POP-2 actually binds the ␈↓¬last␈↓ ␈↓αm␈↓ arguments.
␈↓ α←␈↓␈↓3.13␈↓ πfReview and Reflection 161␈↓
␈↓"β␈↓ α←␈↓factorial␈α∞function.␈α∂If␈α∞there␈α∞are␈α∂different␈α∞algorithms␈α∞for␈α∂computing␈α∞factorial,
␈↓ α←␈↓then␈αthere␈αare␈αdifferent␈αalorithms␈αfor␈αcomputing␈αthe␈αvalue␈αof␈αan␈αexpression,
␈↓ α←␈↓and␈α␈↓αeval␈↓␈α
is␈αjust␈αone␈α
such␈αalgorithm.␈α Just␈α
as␈αthe␈αessence␈α
of␈α␈↓αfact␈↓␈αand␈α
␈↓αfact␈↓β1␈↓␈αis
␈↓ α←␈↓the␈α
factorial␈α
function,␈α
we␈α
should␈α
capture␈α
the␈α
essence␈α
expressed␈α
in␈α
␈↓αeval␈↓.␈α Put
␈↓ α←␈↓another␈α∩way,␈α⊃when␈α∩applications␈α⊃programmers␈α∩use␈α⊃␈↓αsqrt␈↓␈α∩or␈α⊃␈↓εp␈↓␈α∩they␈α∩have␈α⊃a
␈↓ α←␈↓specific␈αmathematical␈αconcept␈αin␈αmind.␈α The␈αimplementation␈αof␈αthe␈αlanguage
␈↓ α←␈↓supplies␈α∩approximations␈α⊃to␈α∩these␈α⊃mathematical␈α∩entities,␈α⊃and␈α∩assuming␈α⊃the
␈↓ α←␈↓computations␈α~stay␈α~within␈α~the␈α≠numerical␈α~ranges␈α~of␈α~the␈α≠machine,␈α~the
␈↓ α←␈↓programmers␈αare␈αfree␈α
to␈αinterpret␈αany␈αoutput␈α
as␈αthat␈αwhich␈αthe␈α
mathematical
␈↓ α←␈↓entities␈α∪would␈α∪produce.␈α∪More␈α∪importantly,␈α∪the␈α∪programmers␈α∪have␈α∪placed
␈↓ α←␈↓specific␈α∞interpretations␈α
or␈α∞meanings␈α
on␈α∞symbols.␈α
We␈α∞are␈α
interested␈α∞in␈α
doing
␈↓ α←␈↓the␈α∂same␈α∂thing;␈α∂however␈α∂we␈α⊂wish␈α∂to␈α∂produce␈α∂a␈α∂␈↓¬freer␈↓␈α⊂interpretation,␈α∂which
␈↓ α←␈↓only␈αmirrors␈αthe␈αessential␈αingredients␈αof␈αthe␈αlanguage␈αconstructs.␈αThat␈αis,␈α␈↓αsqrt␈↓
␈↓ α←␈↓represents␈αa␈α␈↓¬function␈↓␈αand␈α␈↓εp␈↓␈αrepresents␈αa␈α␈↓¬constant␈↓.␈α The␈α␈↓αeval-apply␈↓␈αpair␈αgives
␈↓ α←␈↓one␈α∞interpretation␈α
to␈α∞the␈α∞meaning␈α
of␈α∞functions␈α∞and␈α
constants,␈α∞but␈α∞there␈α
are
␈↓ α←␈↓different␈α⊗interpretations␈α⊗and␈α⊗there␈α⊗are␈α⊗different␈α⊗␈↓αeval-apply␈↓␈α↔pairs.␈α⊗ The
␈↓ α←␈↓remainder␈α∞of␈α∞this␈α∞section␈α∞will␈α
resolve␈α∞some␈α∞of␈α∞the␈α∞tension␈α∞between␈α
function
␈↓ α←␈↓and procedure.
␈↓"β␈↓ α←␈↓␈↓ β'What␈α⊂does␈α⊂this␈α⊂discussion␈α⊂have␈α⊂to␈α⊂do␈α⊂with␈α⊂programming␈α∂languages?
␈↓ α←␈↓Clearly␈αthe␈αorder␈αof␈αevaluation␈αor␈αreduction␈αis␈αdirectly␈αapplicable.␈αOur␈αstudy
␈↓ α←␈↓will␈αalso␈αgive␈αinsights␈αinto␈αthe␈αproblem␈αof␈αlanguage␈αspecification.␈α Do␈αwe␈αsay
␈↓ α←␈↓that␈α∂the␈α∂language␈α∂specification␈α∂consists␈α∞of␈α∂a␈α∂syntactic␈α∂component␈α∂and␈α∞some
␈↓ α←␈↓description␈α
of␈α
the␈α
evaluation␈α
of␈α∞constructs␈α
in␈α
that␈α
language?␈α
Or␈α
do␈α∞we␈α
say
␈↓ α←␈↓that␈α⊂these␈α⊂two␈α⊂components,␈α⊂syntax␈α⊂and␈α⊂a␈α⊂machine,␈α⊂are␈α⊂merely␈α⊃devices␈α⊂for
␈↓ α←␈↓describing␈αand␈αformalizing␈αnotions␈αabout␈αsome␈αabstract␈αdomain␈αof␈αdiscourse?
␈↓ α←␈↓A␈α⊃related␈α⊃question␈α⊃for␈α⊃programmers␈α⊃and␈α⊃language␈α⊃designers␈α⊃involves␈α⊂the
␈↓ α←␈↓ideas␈αof␈αcorrectness␈α
and␈αequivalence␈αof␈αprograms.␈α
How␈αdo␈αwe␈αknow␈α
when␈αa
␈↓ α←␈↓program␈α
is␈αcorrect?␈α
This␈α
requires␈αsome␈α
notion␈α
of␈αa␈α
standard␈α
against␈αwhich
␈↓ α←␈↓to␈α∩test␈α∩our␈α∩implementations.␈↓π 50␈↓␈α∩If␈α∩our␈α∩algorithms␈α∩really␈α∩are␈α∩reflections␈α∩of
␈↓ α←␈↓functional␈α⊃properties,␈α⊃then␈α⊃we␈α⊃should␈α⊃develop␈α⊃methods␈α⊃for␈α∩verifying␈α⊃that
␈↓ α←␈↓those␈α∀properties␈α∪are␈α∀expressed␈α∀in␈α∪our␈α∀algorithms.␈α∪Against␈α∀this␈α∀we␈α∪must
␈↓ α←␈↓balance␈α↔the␈α↔realization␈α↔that␈α↔many␈α⊗programs␈α↔don't␈α↔fit␈α↔neatly␈α↔into␈α⊗this
␈↓ α←␈↓mathematical␈α≡framework.␈α≡Many␈α≡programs␈α≡are␈α≡best␈α≡characterized␈α≡as
␈↓ α←␈↓themselves.␈αIn␈αthis␈αcase␈αwe␈αshould␈αthen␈αbe␈αinterested␈αin␈αverfying␈αequivalence
␈↓ α←␈↓of␈α⊃programs.␈α⊃If␈α⊃we␈α⊂develop␈α⊃a␈α⊃new␈α⊃algorithm␈α⊂we␈α⊃have␈α⊃a␈α⊃responsibility␈α⊂to
␈↓ α←␈↓demonstrate that the algorithms are equivalent in very well defined ways.␈↓π 51␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∞study␈α∞of␈α∂formal␈α∞systems␈α∞in␈α∂mathematical␈α∞logic␈α∞offers␈α∂insight␈α∞into
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 50␈↓"Unless␈α⊃there␈α⊂is␈α⊃a␈α⊂prior␈α⊃mathematical␈α⊂definition␈α⊃of␈α⊂a␈α⊃language␈α⊂at
␈↓ α←␈↓hand, who is to say whether a proposed implementation is ␈↓¬correct␈↓?" [Sco 72].
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 51␈↓Current␈α∨theory␈α∨is␈α inadequate␈α∨for␈α∨dealing␈α∨with␈α many␈α∨real
␈↓ α←␈↓programming␈αtasks.␈α
However␈αthe␈α
realization␈αthat␈αone␈α
has␈αa␈α
responsibility␈αto
␈↓ α←␈↓consider␈α⊗the␈α∃questions,␈α⊗␈↓¬even␈α∃informally␈↓,␈α⊗is␈α∃a␈α⊗sobering␈α∃one␈α⊗which␈α∃more
␈↓ α←␈↓programmers should experience.
␈↓ α←␈↓␈↓162 Evaluation␈↓
_3.13␈↓
␈↓"β␈↓ α←␈↓many␈α∂of␈α∂these␈α⊂questions.␈α∂ In␈α∂logic,␈α⊂we␈α∂are␈α∂presented␈α⊂with␈α∂a␈α∂syntax,␈α⊂and␈α∂a
␈↓ α←␈↓system␈αof␈αaxioms␈αand␈αrules␈αof␈αinference.␈α Most␈αusually␈αwe␈αare␈αalso␈αoffered␈αa
␈↓ α←␈↓"model␈α∞theory"␈α∞which␈α∞gives␈α∞us␈α∞interpretations␈α∞for␈α∞the␈α∞objects␈α∞of␈α∞the␈α
formal
␈↓ α←␈↓system;␈α
the␈α∞model␈α
theory␈α∞supplies␈α
additional␈α∞methods␈α
for␈α∞giving␈α
convincing
␈↓ α←␈↓arguments␈α_for␈α_the␈α_validity␈α_of␈α_statements␈α_in␈α_the␈α_formal␈α_system.␈α↔ The
␈↓ α←␈↓arguments␈α→made␈α→within␈α→the␈α→formal␈α→system␈α→are␈α→couched␈α→in␈α~terms␈α→of
␈↓ α←␈↓"provability"␈α∞whereas␈α∞arguments␈α∞of␈α∞the␈α
model␈α∞theory␈α∞are␈α∞given␈α∞in␈α∞terms␈α
of
␈↓ α←␈↓"truth". For a discussion of formal systems and model theory see [Men 64].
␈↓"β␈↓ α←␈↓␈↓ β'C.␈α
W.␈α
Morris␈α
([Mor 55])␈α
isolated␈α
three␈α
perspectives␈α
on␈αlanguage,␈α
syntax,
␈↓ α←␈↓pragmatics, and semantics:
␈↓"∀␈↓ α←␈↓␈↓↓Syntax␈↓:␈αThe␈α
synthesis␈αand␈α
analysis␈αof␈αsentences␈α
in␈αa␈α
language.␈αThis␈α
area␈αis
␈↓ α←␈↓␈↓ β'well cultivated in programming language specification.
␈↓"∀␈↓ α←␈↓␈↓↓Pragmatics␈↓:␈αThe␈αrelation␈αbetween␈αthe␈αlanguage␈αand␈αits␈αuser.␈αEvaluators,␈αlike
␈↓ α←␈↓␈↓ β'␈↓αtgmoaf,␈α∀tgmoafr␈↓␈α∃and␈α∀␈↓αeval␈↓,␈α∃come␈α∀under␈α∃the␈α∀heading␈α∃of␈α∀pragmatics.
␈↓ α←␈↓␈↓ β'Pragmatics␈α
are␈α
more␈α
commonly␈α
referred␈α
to␈α
as␈α
operational␈α
semantics␈αin
␈↓ α←␈↓␈↓ β'discussions of programming languages.
␈↓"∀␈↓ α←␈↓␈↓↓Semantics␈↓:␈αThe␈αrelation␈αbetween␈αconstructs␈αof␈αthe␈αlanguage␈αand␈αthe␈αabstract
␈↓ α←␈↓␈↓ β'objects␈αwhich␈αthey␈αdenote.␈αThis␈αsubdivision␈αis␈αcommonly␈αreferred␈αto␈αas
␈↓ α←␈↓␈↓ β'denotational semantics.
␈↓"∀␈↓ α←␈↓Put␈α$differently,␈α%syntax␈α$describes␈α$appearance;␈α%pragmatics␈α$describes
␈↓ α←␈↓implementation;␈α∀and␈α∀semantics␈α∃describes␈α∀meaning.␈↓π 52␈↓␈α∀Though␈α∀there␈α∃is␈α∀a
␈↓ α←␈↓strong␈αconcensus␈α
on␈αthe␈αsyntactic␈α
tools␈αfor␈αspecifying␈α
languages,␈↓π 53␈↓␈αthere␈αis␈α
no
␈↓ α←␈↓consensus␈α~on␈α~adequate␈α~pragmatics,␈α≠and␈α~even␈α~less␈α~agreement␈α≠on␈α~the
␈↓ α←␈↓adequancy␈α∩of␈α⊃semantic␈α∩descriptions.␈α⊃ We␈α∩will␈α⊃first␈α∩outline␈α∩the␈α⊃pragmatics
␈↓ α←␈↓questions␈α∪and␈α∪then␈α∀discuss␈α∪a␈α∪bit␈α∪more␈α∀of␈α∪the␈α∪semantics␈α∪issues.␈α∀ In␈α∪this
␈↓ α←␈↓discussion␈α∞we␈α∞will␈α∞use␈α∞the␈α
language␈α∞distinctions␈α∞of␈α∞Morris␈α∞even␈α∞though␈α
the
␈↓ α←␈↓practice␈α∩is␈α∩not␈α∪commonly␈α∩followed␈α∩in␈α∪the␈α∩literature.␈α∩ Typically,␈α∪syntax␈α∩is
␈↓ α←␈↓studied␈α∞precisely␈α∞and␈α
semantics␈α∞is␈α∞"everything␈α
else";␈α∞however␈α∞the␈α
distinction
␈↓ α←␈↓between␈α∩computation␈α⊃(pragmatics)␈α∩and␈α⊃truth␈α∩(semantics)␈α⊃is␈α∩important␈α⊃and
␈↓ α←␈↓should not be muddled.
␈↓"β␈↓ α←␈↓␈↓ β'One␈α
thought␈αis␈α
to␈α
describe␈αthe␈α
pragmatics␈αof␈α
a␈α
language␈αin␈α
terms␈αof␈α
the
␈↓ α←␈↓process␈α⊃of␈α⊂compilation.␈α⊃ That␈α⊃is,␈α⊂the␈α⊃pragmatics␈α⊃is␈α⊂specified␈α⊃by␈α⊃a␈α⊂specific
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 52␈↓This␈α∩division␈α∩of␈α∩language␈α⊃reflects␈α∩an␈α∩interesting␈α∩parallel␈α⊃between
␈↓ α←␈↓mathematical␈α⊃logic␈α⊂and␈α⊃programming␈α⊂languages:␈α⊃in␈α⊂mathematical␈α⊃logic␈α⊂we
␈↓ α←␈↓have␈α≡deduction,␈α≡computation,␈α∨and␈α≡truth;␈α≡in␈α∨programming␈α≡language
␈↓ α←␈↓specification␈α~we␈α→have␈α~three␈α~analogous␈α→schools␈α~of␈α~thought:␈α→axiomatic,
␈↓ α←␈↓operational,␈α∞and␈α∞denotational.␈α∞See␈α
[Men 64]␈α∞for␈α∞the␈α∞mathematical␈α∞logic␈α
and
␈↓ α←␈↓[Dav 76]␈α
for␈α
a␈α
study␈α
of␈α
the␈α
interrelationships;␈α
see␈α
[Hoa 69]␈α
for␈α
a␈αdiscussion
␈↓ α←␈↓of␈α⊗the␈α⊗axiomatic␈α⊗school;␈α⊗we␈α⊗will␈α⊗say␈α⊗more␈α⊗about␈α⊗the␈α⊗operational␈α∃and
␈↓ α←␈↓denotational schools in this section.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 53␈↓But see [Pra 73] for a contrary position.
␈↓ α←␈↓␈↓3.13␈↓ πfReview and Reflection 163␈↓
␈↓"β␈↓ α←␈↓standard␈α∩compiler␈α∩producing␈α∩code␈α∩for␈α∩some␈α∩well-defined␈α∩simple␈α∩machine.
␈↓ α←␈↓The␈αmeaning␈αof␈αa␈αprogram␈αis␈αthe␈αoutcome␈αof␈αan␈αinterpreter␈αinterpreting␈αthis
␈↓ α←␈↓code.␈α⊂ But␈α∂then,␈α⊂to␈α∂understand␈α⊂the␈α∂meaning␈α⊂of␈α∂a␈α⊂particular␈α⊂construct,␈α∂this
␈↓ α←␈↓proposal␈α⊃requires␈α⊃that␈α⊃you␈α⊃understand␈α⊃the␈α⊃description␈α⊃of␈α⊃a␈α∩compiler␈α⊃and
␈↓ α←␈↓understand␈αthe␈αsimple␈αmachine.␈α Two␈αproblems␈αarise␈αimmediately.␈αCompilers
␈↓ α←␈↓are␈α⊂not␈α⊃particularly␈α⊂transparent␈α⊂programs.␈α⊃Second,␈α⊂a␈α⊂very␈α⊃simple␈α⊂machine
␈↓ α←␈↓may␈αnot␈αadequately␈α
reflect␈αthe␈αactual␈α
mechanisms␈αused␈αin␈αan␈α
implementation.
␈↓ α←␈↓This␈α∃aspect␈α∀is␈α∃important␈α∀if␈α∃the␈α∀description␈α∃is␈α∀to␈α∃be␈α∀meaningful␈α∃to␈α∀an
␈↓ α←␈↓implementor.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α∀more␈α∃fundamental␈α∀difficulty␈α∃is␈α∀apparent␈α∃when␈α∀we␈α∃consider␈α∀the
␈↓ α←␈↓practical␈α⊃aspects␈α⊃of␈α⊂this␈α⊃proposal.␈α⊃ When␈α⊂asked␈α⊃to␈α⊃understand␈α⊃a␈α⊂program
␈↓ α←␈↓written␈α∃in␈α∃a␈α∀high-level␈α∃language␈α∃you␈α∃think␈α∀about␈α∃the␈α∃␈↓¬behavior␈↓␈α∃of␈α∀that
␈↓ α←␈↓program␈αin␈αa␈αvery␈αdirect␈αway.␈αThe␈αpragmatics␈αis␈αclose␈αto␈αthe␈αsemantics.␈α You
␈↓ α←␈↓think␈α⊃about␈α⊃the␈α⊃computational␈α⊃behavior␈α⊂as␈α⊃it␈α⊃executes;␈α⊃you␈α⊃do␈α⊃not␈α⊂think
␈↓ α←␈↓about␈α∂the␈α∂code␈α∂that␈α∂gets␈α∂generated␈α∂and␈α∂then␈α∂think␈α∂about␈α∂the␈α∂execution␈α∞of
␈↓ α←␈↓that code.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α
more␈α∞natural␈α
pragmatics␈α∞for␈α
the␈α
constructs␈α∞is␈α
given␈α∞in␈α
terms␈α∞of␈α
the
␈↓ α←␈↓execution␈αof␈αthese␈αconstructs;␈αthus␈α␈↓αeval␈↓␈αis␈αthe␈αpragmatic␈αdescription␈αof␈αLISP.
␈↓ α←␈↓The␈α␈↓αeval␈↓␈αfunction␈αdescribes␈αthe␈αexecution␈αsequence␈αof␈αa␈αrepresentation␈αof␈αan
␈↓ α←␈↓arbitrary␈αLISP␈αexpression.␈αThat␈αdescription␈α
has␈αa␈αflavor␈αof␈αcircularity␈α
which
␈↓ α←␈↓some␈α
find␈α
displeasing.␈α
However␈αsome␈α
circularity␈α
in␈α
description␈α
is␈αinevitable;
␈↓ α←␈↓we␈α∩must␈α∪assume␈α∩that␈α∪something␈α∩is␈α∪known␈α∩and␈α∪does␈α∩not␈α∪require␈α∩further
␈↓ α←␈↓explication.␈α∞ If␈α
language␈α∞L␈↓β1␈↓␈α
is␈α∞described␈α
in␈α∞terms␈α
of␈α∞a␈α
simpler␈α∞language␈α
L␈↓β2␈↓
␈↓ α←␈↓then either L␈↓β2␈↓ is "self evident" or a description L␈↓β2␈↓ must be given.
␈↓"β␈↓ α←␈↓␈↓ β'So,␈α
realistically,␈α∞the␈α
choice␈α
is␈α∞where␈α
to␈α
stop,␈α∞not␈α
whether␈α
to␈α∞stop.␈α
The
␈↓ α←␈↓LISP␈αposition␈αis␈αthat␈αthe␈αlanguage␈αand␈αdata␈αstructures␈αare␈αsufficiently␈αsimple
␈↓ α←␈↓that␈α⊗self-description␈α⊗is␈α⊗satisfactory.␈α⊗ Attempts␈α⊗have␈α⊗been␈α⊗made␈α↔to␈α⊗give
␈↓ α←␈↓non-circular␈α∂interpreter-based␈α∂descriptions␈α∂of␈α∂semantics␈α∂for␈α∂languages␈α∂other
␈↓ α←␈↓than␈α
LISP.␈α
There␈α
is␈α
a␈α
Vienna␈α
Definition␈α
Language␈α∞([Weg 72])␈α
description
␈↓ α←␈↓of␈αPL/1,␈αand␈αa␈αdescription␈αof␈α
ALGOL␈α68␈α([Alg 75])␈αby␈αa␈αMarkov␈α
algorithm.
␈↓ α←␈↓Both␈αthese␈αattempts␈αresult␈αin␈αa␈αdescription␈αwhich␈αis␈αlong␈α
and␈αunmanageable
␈↓ α←␈↓for all but the most persistent reader.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α∂are␈α∂some␈α∂compelling␈α∂reasons␈α∂for␈α∂deciding␈α∂on␈α⊂direct␈α∂circularity.
␈↓ α←␈↓First,␈α⊂you␈α⊂need␈α⊂only␈α⊂learn␈α⊂one␈α∂language.␈α⊂ If␈α⊂the␈α⊂specification␈α⊂is␈α⊂given␈α∂the
␈↓ α←␈↓source␈α_language,␈α→then␈α_you␈α→learn␈α_the␈α→programming␈α_language␈α→and␈α_the
␈↓ α←␈↓specification␈α∀language␈α∪at␈α∀the␈α∀same␈α∪time.␈α∀ Second,␈α∪since␈α∀the␈α∀evaluator␈α∪is
␈↓ α←␈↓written␈α∞in␈α
the␈α∞language,␈α∞we␈α
can␈α∞understand␈α
the␈α∞language␈α∞by␈α
understanding
␈↓ α←␈↓the␈α⊃workings␈α⊃of␈α∩the␈α⊃single␈α⊃program,␈α∩␈↓αeval␈↓;␈α⊃and␈α⊃if␈α∩we␈α⊃wish␈α⊃to␈α∩modify␈α⊃the
␈↓ α←␈↓pragmatics,␈α
we␈αneed␈α
change␈αonly␈α
one␈αcollection␈α
of␈αhigh-level␈α
programs.␈α If␈α
we
␈↓ α←␈↓wished␈αto␈αadd␈αnew␈αlanguage␈αconstructs␈αto␈αLISP␈αwe␈αneed␈αonly␈αmodify␈α␈↓αeval␈↓␈αso
␈↓ α←␈↓that␈α∞it␈α∞recognizes␈α∂an␈α∞occurrence␈α∞of␈α∂that␈α∞new␈α∞construct;␈α∂and␈α∞we␈α∞must␈α∂add␈α∞a
␈↓ α←␈↓function␈αto␈αdescribe␈αthe␈αinterpretation␈αof␈αthe␈αconstruct.␈α Those␈αmodifications
␈↓ α←␈↓may␈α
be␈α
extensive,␈αbut␈α
they␈α
will␈α
be␈αcontrolled␈α
revisions␈α
rather␈α
than␈αa␈α
complete
␈↓ α←␈↓reprogramming effort.
␈↓ α←␈↓␈↓164 Evaluation␈↓
_3.13␈↓
␈↓"β␈↓ α←␈↓␈↓ β'There␈α∂is␈α∂another␈α∂common␈α∂method␈α∂for␈α∂specifying␈α∂the␈α∂pragmatics␈α∂of␈α∞a
␈↓ α←␈↓programming␈α∂language.␈α∂The␈α∂Algol␈α∂report␈α∂([Alg 63])␈α∂introduced␈α∂a␈α∞standard
␈↓ α←␈↓for␈αsyntax␈αspecification:␈αthe␈αBNF␈αequation.␈αIt␈αalso␈αgave␈αa␈αreasonably␈αprecise
␈↓ α←␈↓description␈α
of␈α
the␈α
pragmatics␈α
of␈α
Algol␈α
statements␈α
in␈α
natural␈α∞language.␈α
The
␈↓ α←␈↓style␈α
of␈α
presentation␈αwas␈α
concise␈α
and␈α
clear,␈αbut␈α
suffers␈α
from␈α
the␈αimprecision
␈↓ α←␈↓of␈α∂natural␈α⊂language.␈α∂ Regardless,␈α⊂this␈α∂style␈α∂of␈α⊂description␈α∂is␈α⊂quite␈α∂common
␈↓ α←␈↓and␈α↔is␈α⊗very␈α↔useful.␈α↔A␈α⊗recent␈α↔report ([Moor 76])␈α⊗on␈α↔the␈α↔pragmatics␈α⊗of
␈↓ α←␈↓InterLISP␈αused␈αthis␈αdescriptive␈αstyle.␈α If␈αthe␈αlanguage␈αis␈αquite␈αcomplex,␈αthen
␈↓ α←␈↓a␈α∪formal␈α∪description␈α∩can␈α∪be␈α∪equally␈α∪complex.␈α∩In␈α∪Section 4.8␈α∪we␈α∪will␈α∩see
␈↓ α←␈↓that our interpreter definition will extend nicely to richer subsets of LISP.
␈↓"β␈↓ α←␈↓␈↓ β'Regardless␈α∀of␈α∃the␈α∀descriptive␈α∀method␈α∃used,␈α∀a␈α∃language␈α∀description
␈↓ α←␈↓should␈α⊂not␈α⊂attempt␈α⊂to␈α⊂explain␈α⊂everything␈α⊂about␈α⊂a␈α⊂language.␈α⊂ It␈α⊂need␈α∂only
␈↓ α←␈↓explain␈α→what␈α~needs␈α→explaining.␈α→ You␈α~must␈α→assume␈α→that␈α~your␈α→reader
␈↓ α←␈↓understands␈α⊗something␈α⊗...␈α⊗.␈α⊗McCarthy:␈α⊗␈↓¬`Nothing␈α⊗can␈α⊗be␈α⊗explained␈α⊗to␈α⊗a
␈↓ α←␈↓¬stone'␈↓ [McC 66].␈α⊃ A␈α∩description␈α⊃of␈α∩a␈α⊃language␈α⊃must␈α∩be␈α⊃natural␈α∩and␈α⊃must
␈↓ α←␈↓match␈α∞the␈α∞expressive␈α∞power␈α∞of␈α∞the␈α
language.␈α∞That␈α∞is,␈α∞it␈α∞should␈α∞model␈α
how
␈↓ α←␈↓the␈αconstructs␈αare␈αto␈αbe␈αimplemented.␈α What␈αis␈αneeded␈αis␈αa␈αdescription␈αwhich
␈↓ α←␈↓exploits,␈α⊂rather␈α⊂than␈α⊂ignores,␈α∂the␈α⊂structure␈α⊂of␈α⊂the␈α⊂language.␈α∂ Mathematical
␈↓ α←␈↓notation␈α∀is␈α∀no␈α∃substitute␈α∀for␈α∀clear␈α∃thought,␈α∀but␈α∀careful␈α∃formulations␈α∀of
␈↓ α←␈↓semantics␈α∪will␈α∪lead␈α∪to␈α∪additional␈α∩insights␈α∪in␈α∪the␈α∪pragmatics␈α∪of␈α∩language
␈↓ α←␈↓design.␈↓π 54␈↓␈α∞The␈α∞task␈α∞requires␈α∞new␈α∞mathematical␈α∞tools␈α∞to␈α∞model␈α∂the␈α∞language
␈↓ α←␈↓constructs, and requires increased care on the part of the language designer.
␈↓"β␈↓ α←␈↓␈↓ β'Let's␈α↔look␈α↔at␈α↔the␈α↔issue␈α⊗of␈α↔syntax␈α↔for␈α↔a␈α↔moment.␈α↔ A␈α⊗satisfactory
␈↓ α←␈↓description␈α
of␈αmuch␈α
of␈αprogramming␈α
language␈α
syntax␈αis␈α
standard␈αBNF.␈α
The
␈↓ α←␈↓BNF␈α
is␈α∞a␈α
generative,␈α
or␈α∞synthetic␈α
grammar␈α
since␈α∞the␈α
rewriting␈α∞rules␈α
specify
␈↓ α←␈↓how␈α∩to␈α∪␈↓¬generate␈↓␈α∩well␈α∩formed␈α∪strings.␈α∩ An␈α∩evaluator,␈α∪on␈α∩the␈α∪other␈α∩hand,
␈↓ α←␈↓executes␈α⊂the␈α∂already␈α⊂existing␈α∂program.␈α⊂ This␈α∂implies␈α⊂that␈α∂our␈α⊂evaluator␈α∂is
␈↓ α←␈↓␈↓¬analytic␈↓␈α∪rather␈α∪than␈α∪synthetic;␈α∪it␈α∪must␈α∪have␈α∪some␈α∪way␈α∪of␈α∪analyzing␈α∪the
␈↓ α←␈↓structure of the given program.
␈↓"β␈↓ α←␈↓␈↓ β'In␈α↔[McC 62],␈α↔John␈α↔McCarthy␈α↔introduced␈α↔the␈α↔concept␈α↔of␈α↔abstract
␈↓ α←␈↓analytic␈α
syntax.␈α
The␈α
idea␈α
is␈α
directly␈α
derivable␈α
from␈α
the␈α
LISP␈αexperience.␈α
The
␈↓ α←␈↓syntax␈αis␈αanalytic,␈αrather␈αthan␈αsynthetic,␈αin␈αthe␈αsense␈αthat␈αit␈αtells␈αhow␈αto␈αtake
␈↓ α←␈↓programs␈α⊂apart -- how␈α⊂to␈α⊂recognize␈α⊂and␈α⊂select␈α⊂subparts␈α⊂of␈α⊂programs␈α⊂using
␈↓ α←␈↓predicates␈α
and␈α∞selector␈α
functions.␈↓π 55␈↓␈α∞It␈α
is␈α
abstract␈α∞in␈α
the␈α∞sense␈α
that␈α∞it␈α
makes
␈↓ α←␈↓no␈α∩commitment␈α∩to␈α∩the␈α∩external␈α∩representation␈α∩of␈α∩the␈α∩constitutents␈α∩of␈α∩the
␈↓ α←␈↓program.␈α⊂ We␈α⊂need␈α⊂only␈α⊂be␈α⊂able␈α∂to␈α⊂recognize␈α⊂the␈α⊂occurrence␈α⊂of␈α⊂a␈α∂desired
␈↓ α←␈↓construct. For example:
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 54␈↓R.␈α→D.␈α_Tennent␈α→has␈α→invoked␈α_this␈α→approach␈α_in␈α→the␈α→design␈α_of
␈↓ α←␈↓␈↓αQUEST␈↓ ([Ten 76]).
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 55␈↓We␈α_will␈α→deal␈α_with␈α_abstract␈α→␈↓¬synthetic␈↓␈α_syntax␈α_when␈α→we␈α_discuss
␈↓ α←␈↓compilers.
␈↓ α←␈↓␈↓3.13␈↓ πfReview and Reflection 165␈↓
␈↓"∀␈↓ α←␈↓αisterm <= λ[[t] or[␈↓ ∧Oisvar[t];
␈↓"β␈↓ α←␈↓α␈↓ ∧Oisconst[t];
␈↓"β␈↓ α←␈↓α␈↓ ∧Oand[issum[t];isterm[addend[t]];isterm[augend[t]]]]]
␈↓"∀␈↓ α←␈↓and the BNF equation:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ∧7<term> ::= <var> | <const> | <term> + <term>
␈↓"∀␈↓ α←␈↓␈↓αissum,␈αaddend,␈↓␈αand␈α
␈↓αaugend␈↓,␈αdon't␈αreally␈αcare␈α
whether␈αthe␈αsum␈α
is␈αrepresented
␈↓ α←␈↓as␈α≤x+y,␈α≤or␈α≤␈↓α+[x;y]␈↓␈α≤or␈α≤␈↓α(PLUS X Y)␈↓␈α≤or␈α≤␈↓↓xy+␈↓.␈α≤ The␈α≤different␈α≤external
␈↓ α←␈↓representations␈α↔are␈α_reflections␈α↔of␈α↔different␈α_concrete␈α↔syntaxes;␈α_the␈α↔BNF
␈↓ α←␈↓equation␈αabove␈αgives␈α
one.␈α Parsing␈αlinks␈αa␈α
concrete␈αsyntax␈αwith␈α
the␈αabstract
␈↓ α←␈↓syntax.
␈↓"β␈↓ α←␈↓␈↓ β'Since␈αthe␈αevaluator␈αmust␈αoperate␈αon␈α␈↓¬some␈↓␈αinternal␈αrepresentation␈αof␈αthe
␈↓ α←␈↓source␈α∂program,␈α∂a␈α∂representation␈α∂reflecting␈α∞the␈α∂structure␈α∂of␈α∂the␈α∂program␈α∞is
␈↓ α←␈↓most␈α
natural.␈α
This␈α∞representation␈α
is␈α
more␈α
commonly␈α∞known␈α
as␈α
a␈α∞parse␈α
tree.
␈↓ α←␈↓We␈α
can␈α
describe␈α
the␈α
evaluation␈α
of␈α
a␈α
program␈α
in␈α
terms␈α
of␈α
a␈α
function␈α
which
␈↓ α←␈↓operates␈α⊂on␈α∂a␈α⊂parse␈α∂tree␈α⊂using␈α∂the␈α⊂predicates␈α∂and␈α⊂selectors␈α∂of␈α⊂the␈α∂analytic
␈↓ α←␈↓syntax.␈α∃Abstract␈α∃syntax␈α∃concerns␈α∃itself␈α∃only␈α∃with␈α∃those␈α∃properties␈α⊗of␈α∃a
␈↓ α←␈↓program which are of interest to an evaluator.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α↔Meta␈α↔expression␈α_LISP␈α↔constitutes␈α↔a␈α↔concrete␈α_syntax.␈↓π 56␈↓␈α↔The
␈↓ α←␈↓M-to-S-expression␈α≤translator␈α≤is␈α≤the␈α≤parser␈α≤which␈α≤maps␈α≤the␈α≤external
␈↓ α←␈↓representation␈α⊗onto␈α⊗a␈α⊗parse␈α↔(or␈α⊗computational)␈α⊗tree.␈α⊗The␈α↔selectors␈α⊗and
␈↓ α←␈↓predicates␈α⊃of␈α⊂the␈α⊃analytic␈α⊂syntax␈α⊃are␈α⊂straightforward.␈α⊃Recall␈α⊂the␈α⊃BNF␈α⊂for
␈↓ α←␈↓LISP:
␈↓"∀␈↓ α←␈↓<form>␈↓ βW::= <constant>
␈↓"β␈↓ α←␈↓␈↓ βW::= <variable>
␈↓"β␈↓ α←␈↓␈↓ βW::= <function>[<arg>; ... ;<arg>]
␈↓"β␈↓ α←␈↓␈↓ βW::= [<form> → <form>; ... ;<form> → <form>]
␈↓"β␈↓ α←␈↓␈↓ βW ....
␈↓"∀␈↓ α←␈↓We␈α
need␈αto␈α
write␈α
a␈αparser␈α
which␈αtakes␈α
instances␈α
of␈αthese␈α
equations␈α
onto␈αan
␈↓ α←␈↓internal␈α
representation.␈αMuch␈α
is␈α
known␈αabout␈α
parsing␈α
techniques␈α([Aho 72],
␈↓ α←␈↓also␈α≠see␈α~Section 9.4␈α≠and␈α≠Section 9.3)␈α~so␈α≠we␈α~will␈α≠concentrate␈α≠on␈α~the
␈↓ α←␈↓post-parse processing.
␈↓"β␈↓ α←␈↓␈↓ β'Our␈αevaluator␈αwill␈αoperate␈αon␈αthe␈αparse␈αtree␈αand␈αwill␈αtherefore␈αneed␈αto
␈↓ α←␈↓recognize␈αrepresentations␈αof␈αconstants,␈αvariables,␈αconditional␈αexpressions,␈αand
␈↓ α←␈↓applications.␈α
We␈α
need␈α
to␈α
write␈α∞a␈α
function␈α
in␈α
some␈α
language␈α∞expressing␈α
the
␈↓ α←␈↓values␈α⊃of␈α⊃these␈α⊃constructs.␈α∩Since␈α⊃the␈α⊃proposed␈α⊃evaluator␈α⊃is␈α∩to␈α⊃manipulate
␈↓ α←␈↓parse␈αtrees,␈αand␈αsince␈αthe␈αdomain␈αof␈αLISP␈αfunctions␈α␈↓¬is␈↓␈αbinary␈αtrees,␈αit␈αshould
␈↓ α←␈↓seem␈α∞natural␈α
to␈α∞use␈α
LISP␈α∞itself.␈α
If␈α∞this␈α
is␈α∞the␈α
case,␈α∞then␈α
we␈α∞must␈α
represent
␈↓ α←␈↓the␈α
parse␈α
tree␈α
as␈α
a␈α
LISP␈αS-expr␈α
and␈α
represent␈α
the␈α
selectors␈α
and␈αrecognizers␈α
as
␈↓ α←␈↓LISP functions and predicates.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 56␈↓The␈αS-expr␈αnotation␈αis␈αalso␈αa␈αconcrete␈αsyntax,␈αbut␈αone␈αwhich␈αis␈α
more
␈↓ α←␈↓closely related to the abstract syntax.
␈↓ α←␈↓␈↓166 Evaluation␈↓
_3.13␈↓
␈↓"β␈↓ α←␈↓α␈↓Perhaps:␈↓α
␈↓"∀␈↓ α←␈↓αisconst <= λ[[x] or[␈↓ ∧Onumberp[x];
␈↓"β␈↓ α←␈↓α␈↓ ∧Oand[atom[x];eq[x;NIL]];
␈↓"β␈↓ α←␈↓α␈↓ ∧Oand[atom[x];eq[x;T]];
␈↓"β␈↓ α←␈↓α␈↓ ∧Oand[not[atom[x]];eq[first[x];QUOTE]
␈↓"∀␈↓ α←␈↓αisvar <= λ[[x] and[atom[x]; not[isconst[x]]]]
␈↓"∀␈↓ α←␈↓αiscond <= λ[[x] eq[first[x];COND]]
␈↓"∀␈↓ α←␈↓There␈αare␈αreally␈αtwo␈αissues␈αhere:␈αa␈αrepresentation␈αof␈αthe␈αanalytic␈αsyntax␈αof␈αa
␈↓ α←␈↓language,␈α~and␈α~a␈α→representation␈α~in␈α~terms␈α→of␈α~the␈α~language␈α~␈↓¬itself␈↓.␈α→ In
␈↓ α←␈↓conjunction,␈α⊃these␈α⊃two␈α∩ideas␈α⊃give␈α⊃a␈α⊃natural␈α∩and␈α⊃very␈α⊃powerful␈α∩means␈α⊃of
␈↓ α←␈↓specifying␈αlanguages.␈α If␈αthis␈αstyle␈α
of␈αspecification␈αis␈αreally␈αeffective,␈α
then␈αwe
␈↓ α←␈↓should␈α
be␈α
able␈α
to␈α∞specify␈α
other␈α
languages␈α
in␈α∞a␈α
similar␈α
fashion.␈α
One␈α∞of␈α
the
␈↓ α←␈↓weak␈αpoints␈αof␈αLISP␈αas␈αa␈αprogramming␈αlanguage␈αis␈αthe␈αinsistence␈αon␈αbinary
␈↓ α←␈↓tree␈α⊂representations␈α⊂of␈α⊂data.␈↓π 57␈↓␈α⊃Many␈α⊂applications␈α⊂could␈α⊂profit␈α⊃from␈α⊂other
␈↓ α←␈↓data␈α⊃representations.␈α⊂ What␈α⊃we␈α⊃would␈α⊂then␈α⊃like␈α⊃is␈α⊂a␈α⊃language␈α⊃which␈α⊂has
␈↓ α←␈↓richer␈α∞data␈α
structures␈α∞than␈α∞LISP␈α
but␈α∞which␈α∞is␈α
designed␈α∞to␈α∞allow␈α
LISP-style
␈↓ α←␈↓semantic␈αspecification.␈α
We␈αwould␈αbe␈α
able␈αto␈αwrite␈α
an␈αevaluator,␈α
albeit␈αmore
␈↓ α←␈↓complex␈αthan␈α␈↓αeval␈↓,␈αin␈αthe␈αlanguage␈αitself.␈α The␈αevaluator␈αwould␈αoperate␈αon␈αa
␈↓ α←␈↓representation␈α
of␈α
the␈α
program␈α
as␈α
a␈α
data␈α
structure␈α
of␈α
the␈α
language,␈α
just␈α
as␈α
␈↓αeval␈↓
␈↓ α←␈↓operates␈α∪on␈α∪the␈α∪S-expr␈α∀translation␈α∪of␈α∪the␈α∪LISP␈α∪program.␈α∀ The␈α∪concrete
␈↓ α←␈↓syntax␈αwould␈α
be␈αspecified␈αas␈α
a␈αset␈α
of␈αBNF␈αequations,␈α
and␈αour␈α
parser␈αwould
␈↓ α←␈↓translate strings into parse trees.
␈↓"β␈↓ α←␈↓In outline, to specify a construct we must have at least the following:
␈↓"→␈↓ α←␈↓␈↓↓1.␈↓ A concrete production.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ An abstract data type.
␈↓" ␈↓ α←␈↓␈↓↓3␈↓. A mapping from ␈↓↓1␈↓ to ␈↓↓2␈↓.
␈↓" ␈↓ α←␈↓␈↓↓4.␈↓ An evaluator for the abstract type.
␈↓"∀␈↓ α←␈↓In␈α∂Chapter 8␈α∂we␈α∂will␈α∂sketch␈α∂a␈α∂recent␈α∂LISP-like␈α∂language,␈α∂EL1,␈α∂which␈α∞␈↓¬does␈↓
␈↓ α←␈↓apply these principles.␈↓π 58␈↓
␈↓"β␈↓ α←␈↓␈↓ β'From␈αa␈αdiscussion␈αof␈αsyntax␈αwe␈αhave␈αreturned␈αto␈αevaluation.␈α After␈αwe
␈↓ α←␈↓reduce␈α⊂the␈α⊂questions␈α⊂of␈α⊂syntax␈α⊂of␈α⊂programming␈α⊂languages␈α⊂to␈α⊂questions␈α⊂of
␈↓ α←␈↓abstract␈α⊂syntax␈α∂and␈α⊂stripped␈α⊂way␈α∂the␈α⊂syntactic␈α∂differences,␈α⊂how␈α⊂many␈α∂␈↓¬real␈↓
␈↓ α←␈↓differences between languages are there? Semantics addresses this issue.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 57␈↓Many␈α⊃`production'␈α⊃versions␈α⊃of␈α⊃LISP␈α⊃have␈α⊃array,␈α⊃string,␈α⊃and␈α⊂even
␈↓ α←␈↓record␈αstructures␈αavailable.␈αHowever␈αthe␈αprogrammer␈αmust␈α
explicitly␈αrequest
␈↓ α←␈↓and␈αmanipulate␈αsuch␈αstorage␈αstructures.␈αWe␈αwould␈αrather␈αdevelop␈αtechniques
␈↓ α←␈↓in␈αwhich␈α
the␈αstorage␈αstructures␈α
are␈α␈↓¬implied␈↓␈αeither␈α
by␈αthe␈αtypes␈α
of␈αoperations
␈↓ α←␈↓desired,␈αor␈αby␈αthe␈αspecification␈αof␈α
the␈αabstract␈αdata␈αstruture,␈αor␈αby␈α
interaction
␈↓ α←␈↓between the programming system and the user.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 58␈↓Compare steps ␈↓↓1␈↓ through ␈↓↓4␈↓ with those on page 36.
␈↓ α←␈↓␈↓3.13␈↓ πfReview and Reflection 167␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Many␈αof␈αthe␈αsemantic␈αideas␈αin␈αapplicative␈αprogramming␈αlanguages␈αcan
␈↓ α←␈↓be␈αcaptured␈αin␈α␈↓λλ␈↓-calculus␈α([Chu 41]).␈α
The␈α␈↓λλ␈↓-calculus␈αwas␈αdeveloped␈αto␈α
supply
␈↓ α←␈↓a␈α
formalism␈αfor␈α
discussing␈α
the␈αnotions␈α
of␈αfunction␈α
and␈α
function␈αapplication.
␈↓ α←␈↓Rather␈α∞than␈α∞develop␈α∞the␈α∞syntax␈α
of␈α∞␈↓λλ␈↓-calculus,␈α∞we␈α∞will␈α∞use␈α∞LISP-like␈α
syntax
␈↓ α←␈↓and␈α⊃show␈α∩how␈α⊃we␈α⊃can␈α∩abstract␈α⊃from␈α⊃the␈α∩procedural␈α⊃LISP␈α⊃function␈α∩to␈α⊃a
␈↓ α←␈↓mathematical function.
␈↓"β␈↓ α←␈↓␈↓ β'Most␈α∀of␈α∀the␈α∀description␈α∀of␈α∀LISP␈α∀which␈α∀we␈α∀have␈α∀given␈α∀so␈α∃far␈α∀is
␈↓ α←␈↓classified␈αas␈αoperational.␈α That␈αmeans␈αour␈αinformal␈αdescription␈αof␈α
LISP␈αand
␈↓ α←␈↓our␈αlater␈α
description␈αof␈αthe␈α
LISP␈αinterpreter␈αare␈α
presented␈αin␈αterms␈α
of␈α"how
␈↓ α←␈↓does␈α
it␈αwork␈α
or␈α
operate".␈αIndeed␈α
the␈αpurpose␈α
of␈α
␈↓αeval␈↓␈αwas␈α
to␈αdescribe␈α
explicitly
␈↓ α←␈↓what␈α
happens␈αas␈α
a␈α
LISP␈αexpression␈α
is␈α
evaluated.␈α We␈α
have␈α
seen␈α(page 100)
␈↓ α←␈↓that␈α∃discussion␈α∃of␈α∀evaluation␈α∃schemes␈α∃is␈α∀non-trivial;␈α∃and␈α∃that␈α∃order␈α∀of
␈↓ α←␈↓evaluation can effect the outcome (page 21).
␈↓"β␈↓ α←␈↓␈↓ β'However,␈αmany␈αtimes␈αthe␈αorder␈αof␈αevaluation␈αis␈αimmaterial.␈↓π 59␈↓␈αWe␈αsaw
␈↓ α←␈↓on␈α∂page 127␈α∂that␈α∂␈↓αeval␈↓␈α∂will␈α⊂compute␈α∂a␈α∂value␈α∂for␈α∂an␈α⊂application␈α∂␈↓αf[a␈↓β1␈↓α; ...; a␈↓βn␈↓α]␈↓
␈↓ α←␈↓even␈α∩though␈α∩␈↓αf␈↓␈α∩was␈α∩defined␈α∩for␈α∩fewer␈α∩arguments.␈α∩ How␈α∩much␈α∩of␈α∩␈↓αeval␈↓␈α⊃is
␈↓ α←␈↓"really"␈α∞LISP␈α∞and␈α∞how␈α∞much␈α∞is␈α∞"really"␈α∞implementation?␈α∞ On␈α∞one␈α∞hand␈α
we
␈↓ α←␈↓have␈αsaid␈α
that␈αthe␈αmeaning␈α
of␈αa␈αLISP␈α
expression␈αis␈αby definition␈α
what␈α␈↓αeval␈↓
␈↓ α←␈↓will␈αcalculate␈αusing␈αthe␈αrepresentation␈αof␈αthe␈αexpression.␈αOn␈αthe␈αother␈αhand,
␈↓ α←␈↓we␈α∂claim␈α∂that␈α∂␈↓αeval␈↓␈α∂is␈α∂simply␈α∂␈↓¬an␈α∂implementation␈↓.␈α∂ There␈α∂are␈α∂certainly␈α∂other
␈↓ α←␈↓implementations;␈α↔i.e,␈α↔other␈α↔LISP␈α↔functions␈α↔␈↓αeval␈↓βi␈↓␈α↔which␈α↔have␈α↔the␈α⊗same
␈↓ α←␈↓input-output␈α
behavior␈α
as␈α
our␈α
␈↓αeval␈↓.␈α
The␈α
position␈α
here␈α
is␈α
not␈α
that␈α
␈↓αeval␈↓␈α
is␈α
wrong
␈↓ α←␈↓in␈αgiving␈αvalues␈αto␈αthings␈αlike␈α
␈↓αcons[A;B;C]␈↓,␈αbut␈αrather␈αwe␈αwant␈αto␈αknow␈α
what
␈↓ α←␈↓is it that ␈↓αeval␈↓ implements.
␈↓"β␈↓ α←␈↓␈↓ β'This␈α⊂other␈α∂way␈α⊂of␈α⊂looking␈α∂at␈α⊂meaning␈α∂in␈α⊂programming␈α⊂languages␈α∂is
␈↓ α←␈↓exemplified␈α∩by␈α∩denotational␈α∪or␈α∩mathematical␈α∩semantics.␈α∪ This␈α∩perspective
␈↓ α←␈↓springs␈α
from␈αthe␈α
philosophical␈αor␈α
logical␈αdevice␈α
of␈αdistinguishing␈α
between␈αa
␈↓ α←␈↓␈↓¬representation␈↓␈αfor␈αan␈αobject,␈αand␈α
the␈αobject␈αitself.␈αThe␈αmost␈αfamiliar␈α
example
␈↓ α←␈↓is␈α∀the␈α∃numeral-number␈α∀distinction.␈α∀ Numerals␈α∃are␈α∀notations␈α∃(syntax)␈α∀for
␈↓ α←␈↓talking␈α∩about␈α∪␈↓¬numbers␈↓␈α∩(semantics).␈α∪ thus␈α∩the␈α∩Arabic␈α∪␈↓¬numerals␈↓␈α∩␈↓α2,␈α∪02␈↓,␈α∩the
␈↓ α←␈↓Roman␈α
numeral␈α
␈↓II␈↓,␈α
and␈α
the␈α
Binary␈α
notation␈α
␈↓α10␈↓,␈α
all␈α
␈↓¬denote␈↓␈α
the␈α
same␈αnumber
␈↓ α←␈↓denoted␈α≠by␈α~the␈α≠word␈α~"two."␈α≠In␈α~LISP,␈α≠␈↓α(A B),␈α~(A . (B)),␈α≠(A , B)␈↓␈α~and
␈↓ α←␈↓␈↓α(A . (B . NIL))␈↓␈α⊃all␈α⊂are␈α⊃notations␈α⊃for␈α⊂the␈α⊃same␈α⊃S-expr.␈α⊂ That␈α⊃is,␈α⊃an␈α⊂object
␈↓ α←␈↓transends␈αits␈α
representations.␈α We␈αwant␈α
to␈αsay␈αthat␈α
a␈αLISP␈α
form␈α␈↓αcar[(A . B)]␈↓
␈↓ α←␈↓denotes␈α
the␈αsame␈α
object␈αdenoted␈α
by␈α␈↓αA␈↓,␈α
or␈α␈↓αcar[A]␈↓␈α
denotes␈αundefined␈α
just␈αas␈α
we
␈↓ α←␈↓say in mathematics that 2+2 denotes 4 or 1/0 is undefined.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 59␈↓"One␈α
difficulty␈α
with␈α
the␈α
operational␈α
approach␈α
is␈α
that␈α
it␈α
(frequently)
␈↓ α←␈↓specifies too much": C. Wadsworth.
␈↓ α←␈↓␈↓168 Evaluation␈↓
_3.13␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Similarly,␈α
we␈αsay␈α
that␈α
the␈αdenotational␈α
counterpart␈αof␈α
a␈α
LISP␈αfunction
␈↓ α←␈↓is␈α⊃a␈α⊃mathematical␈α⊃function␈α∩defined␈α⊃over␈α⊃an␈α⊃appropriate␈α∩abstract␈α⊃domain.
␈↓ α←␈↓Before␈α⊃proceeding,␈α⊃we␈α⊃introduce␈α⊂some␈α⊃conventions␈α⊃to␈α⊃distinguish␈α⊂notation
␈↓ α←␈↓from ␈↓¬de␈↓notation. We will continue to use italics:
␈↓"∀␈↓ α←␈↓␈↓ ¬$␈↓αA, B, ..., x, ..., car, ..., (A . B) ␈↓
␈↓"∀␈↓ α←␈↓as notation in LISP expressions.
␈↓"∀␈↓ α←␈↓Gothic bold-face: ␈↓A, B, ..., x, ..., car, ..., (A . B)␈↓
␈↓"∀␈↓ α←␈↓will represent denotations.
␈↓"β␈↓ α←␈↓Thus␈α␈↓αA␈↓␈αis␈α
notation␈αfor␈α␈↓A␈↓;␈αthe␈α
expression␈α␈↓αcar[cdr[(A . (B . C))]]␈↓␈αdenotes␈α␈↓B␈↓;␈α
the
␈↓ α←␈↓mapping, ␈↓car␈↓ is the denotation of the LISP function ␈↓αcar␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Several␈α
areas␈αof␈α
LISP␈αmust␈α
be␈αsubjected␈α
to␈αan␈α
abstraction␈α
process.␈α In
␈↓ α←␈↓particular,␈α↔the␈α↔operations␈α↔involved␈α↔in␈α↔the␈α↔evaluation␈α↔process␈α↔must␈α↔be
␈↓ α←␈↓abstracted␈α⊃away.␈α⊃These␈α⊃involve␈α⊃an␈α⊃abstraction␈α⊃from␈α⊃LISP's␈α⊃call␈α⊃by␈α⊂value
␈↓ α←␈↓evaluation␈α∀and␈α∀its␈α∪left␈α∀to␈α∀right␈α∀order␈α∪of␈α∀evaluation␈α∀of␈α∀arguments.␈α∪ For
␈↓ α←␈↓example,␈αthe␈α
operation␈αof␈αcomposition␈α
of␈αLISP␈αfunctions␈α
is␈αmeant␈α
to␈αdenote
␈↓ α←␈↓mathematical␈α⊂composition;␈α⊃in␈α⊂LISP,␈α⊃␈↓αcar[cdr[(A . (B . C))]]␈↓␈α⊂means␈α⊃apply␈α⊂the
␈↓ α←␈↓procedure␈α↔␈↓αcdr␈↓␈α↔to␈α↔the␈α_argument␈α↔␈↓α(A . (B . C))␈↓␈α↔getting␈α↔␈↓α(B . C)␈↓;␈α_apply␈α↔the
␈↓ α←␈↓procedure␈α∪␈↓αcar␈↓␈α∪to␈α∪␈↓α(B . C)␈↓␈α∪getting␈α∩␈↓αB␈↓.␈α∪Mathematically,␈α∪we␈α∪have␈α∪a␈α∩mapping
␈↓ α←␈↓␈↓car␈↓∞o␈↓cdr␈↓,␈αwhich␈α
is␈αa␈αcomposition␈α
of␈αthe␈α␈↓car␈↓␈α
and␈α␈↓cdr␈↓␈αmappings;␈α
the␈αordered
␈↓ α←␈↓pair␈α␈↓<(A . (B . C)) , B>␈↓␈αis␈αin␈αthe␈αgraph␈αof␈αthis␈αcomposed␈αmapping.␈α At␈α
this
␈↓ α←␈↓level␈αof␈αabstraction,␈αany␈αLISP␈αcharacterization␈αof␈αa␈αfunction␈αis␈αequally␈αgood;
␈↓ α←␈↓the␈α~"efficiency"␈α~question␈α≠has␈α~been␈α~abstracted␈α~away.␈α≠Many␈α~important
␈↓ α←␈↓properties␈αof␈αreal␈αprograms␈α␈↓¬can␈↓␈αbe␈αdiscussed␈αin␈αthis␈αmathematical␈αcontext;␈αin
␈↓ α←␈↓particular,␈α⊂questions␈α⊃of␈α⊂equivalence␈α⊂and␈α⊃correctness␈α⊂of␈α⊂programs␈α⊃are␈α⊂even
␈↓ α←␈↓more tractable.
␈↓"β␈↓ α←␈↓␈↓ β'As␈α∃we␈α∃remove␈α∃the␈α∃operational␈α∀aspects,␈α∃we␈α∃discover␈α∃a␈α∃few␈α∀critical
␈↓ α←␈↓properties␈αof␈αfunctions␈αwhich␈αmust␈α
be␈αreconciled␈αwith␈αLISP's␈αprocedures.␈α
We
␈↓ α←␈↓must␈αtreat␈αthe␈αideas␈αof␈αbinding␈αof␈αvariables,␈αand␈αwe␈αmust␈αhandle␈αthe␈αnotion
␈↓ α←␈↓of function application.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αknow␈α
that␈αthere␈αare␈α
at␈αleast␈αtwo␈α
binding␈αstrategies␈α
available:␈αstatic
␈↓ α←␈↓binding␈αand␈αdynamic␈αbinding;␈αwe␈αknow␈αthat␈αthe␈αchoice␈αof␈αstrategy␈αcan␈αeffect
␈↓ α←␈↓the␈α⊂resultant␈α⊃computation.␈α⊂This␈α⊂computational␈α⊃difference␈α⊂must␈α⊃be␈α⊂studied.
␈↓ α←␈↓To illuminate the problem we take an example in LISP.
␈↓"β␈↓ α←␈↓Consider:
␈↓"∀␈↓ α←␈↓α␈↓ βKλ[[z]
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ β{λ[[u]
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ β{␈↓ ∧+λ[[z] u[B]][C]]
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ β{ [λ[[x] cons[x;z]]] ]
␈↓"β␈↓ α←␈↓α␈↓ βK [A]
␈↓"∀␈↓ α←␈↓The␈αdynamic␈αbinding␈α
strategy␈αwill␈αbind␈α␈↓αz␈↓␈α
to␈α␈↓αA␈↓;␈αthen␈α
bind␈α␈↓αu␈↓␈αto␈αthe␈α
functional
␈↓ α←␈↓argument,␈α∀␈↓αλ[[x] cons[x;z]]␈↓;␈α∃next,␈α∀␈↓αz␈↓␈α∀is␈α∃rebound␈α∀to␈α∀␈↓αC␈↓,␈α∃and␈α∀finally␈α∃␈↓αu[B]␈↓␈α∀is
␈↓ α←␈↓evaluated.␈α
That␈α
involves␈α
binding␈α
␈↓αx␈↓␈α∞to␈α
␈↓αB␈↓␈α
and␈α
evaluating␈α
␈↓αcons[x;z]␈↓.␈α∞Since␈α
we
␈↓ α←␈↓␈↓3.13␈↓ πfReview and Reflection 169␈↓
␈↓"β␈↓ α←␈↓are␈α∞using␈α∞dynamic␈α∞binding,␈α∞the␈α∂␈↓¬latest␈α∞active␈α∞␈↓␈α∞bindings␈α∞of␈α∞the␈α∂variables␈α∞are
␈↓ α←␈↓used.␈α
The␈α
latest␈α
active␈α
bindings␈α
for␈α
␈↓αx␈↓␈αand␈α
␈↓αz␈↓␈α
are␈α
␈↓αB␈↓␈α
and␈α
␈↓αC␈↓␈α
respectively,␈αand␈α
the
␈↓ α←␈↓final value is therefore ␈↓α(B . C)␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α≥can␈α≥obtain␈α≥static␈α≤binding␈α≥by␈α≥replacing␈α≥␈↓αλ[[x] cons[x;z]]␈↓␈α≤by
␈↓ α←␈↓␈↓αfunction[λ[[x] cons[x;z]]]␈↓.␈α⊃This␈α⊃has␈α⊂the␈α⊃effect␈α⊃of␈α⊂associating␈α⊃the␈α⊃variable␈α⊂␈↓αz␈↓
␈↓ α←␈↓with␈α∞the␈α
atom␈α∞␈↓αA␈↓.␈α
As␈α∞we␈α∞know,␈α
the␈α∞final␈α
result␈α∞of␈α
this␈α∞computation␈α∞will␈α
be
␈↓ α←␈↓␈↓α(B . A)␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Before␈α⊃discussing␈α⊂binding␈α⊃strategies␈α⊂further,␈α⊃we␈α⊂must␈α⊃strengthen␈α⊂our
␈↓ α←␈↓understanding␈α
of␈α
the␈αideas␈α
underlying␈α
function␈αapplication.␈α
It␈α
is␈α
this␈αnotion
␈↓ α←␈↓which␈α⊃a␈α⊃binding␈α⊃strategy␈α⊃is␈α⊃implementing.␈α⊃ This␈α⊃involves␈α⊃a␈α⊃more␈α⊃careful
␈↓ α←␈↓study␈α∂of␈α∂the␈α∂notion␈α∂of␈α∂λ-notation␈α∂as␈α∂the␈α∂representation␈α∂of␈α∂a␈α∂function.␈α∞ We
␈↓ α←␈↓shall␈αrestrict␈α
out␈αdiscussion␈α
to␈αunary␈αλ-expressions,␈α
since␈αn-ary␈α
functions␈αcan
␈↓ α←␈↓be represented in terms of unary functions:
␈↓"∀␈↓ α←␈↓␈↓ ¬∀␈↓λλ␈↓((x y) ␈↓λx␈↓) = ␈↓λλ␈↓((x) ␈↓λλ␈↓((y) ␈↓λx␈↓))␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'What␈αproperties␈αdo␈αwe␈αexpect␈αa␈αfunction,␈αdenoted␈αby␈αa␈αλ-expression,␈αto
␈↓ α←␈↓possess?␈α∀ For␈α∀example,␈α∀we␈α∀expect␈α∀that␈α∀a␈α∀systematic␈α∀renaming␈α∀of␈α∪formal
␈↓ α←␈↓parameters should not effect the definition of a function.
␈↓"∀␈↓ α←␈↓α␈↓ βiλ[[y] x]␈↓ should denote the same function as ␈↓αλ[[w] x]␈↓
␈↓"β␈↓ α←␈↓But
␈↓"β␈↓ α←␈↓α␈↓ βaλ[[x] λ[[y] x]][w]␈↓ is not the same as ␈↓α λ[[x] λ[[w] x]][w]␈↓
␈↓"∀␈↓ α←␈↓This␈αexample␈α
shows␈αthat␈α
we␈αneed␈αto␈α
be␈αcareful␈α
in␈αdefining␈α
our␈αsubstitution
␈↓ α←␈↓rules.␈α Closer␈αexamination␈αof␈αthe␈αlast␈αexample␈αshows␈αthat␈αthe␈αresult␈α␈↓αλ[[w] w]␈↓
␈↓ α←␈↓would␈α⊂occur␈α⊂if␈α⊂the␈α∂substitution␈α⊂changed␈α⊂a␈α⊂non-local␈α∂name␈α⊂(␈↓αx␈↓)␈α⊂into␈α⊂a␈α∂local
␈↓ α←␈↓name␈α∃(␈↓αw␈↓).␈α∃ The␈α∃expected␈α∃result␈α⊗would␈α∃have␈α∃been␈α∃obtained␈α∃if␈α⊗we␈α∃had
␈↓ α←␈↓recognized␈α∞the␈α∞clash␈α∞of␈α∞names␈α∞and␈α
replaced␈α∞the␈α∞formal␈α∞parameter␈α∞␈↓αy␈↓␈α∞with␈α
a
␈↓ α←␈↓new␈α
name,␈αsay␈α
␈↓αu␈↓,␈αand␈α
then␈αperformed␈α
the␈αsubstitution,␈α
getting␈α␈↓αλ[[u] w]␈↓␈α
which
␈↓ α←␈↓is␈αthe␈αsame␈αas␈α␈↓αλ[[y] w]␈↓.␈α Before␈αgiving␈αa␈αsubstitution␈αrule␈αwhich␈αaccounts␈αfor
␈↓ α←␈↓such changes of name we will introduce some new terminology.
␈↓"β␈↓ α←␈↓␈↓ β'First,␈α
the␈α
"same␈α
as"␈α
relation␈α
will␈α
occur␈α
frequently␈α
in␈α
the␈α
discussion;␈αwe
␈↓ α←␈↓should␈αestablish␈αsome␈αproperties␈αof␈αthis␈αnotion.␈α We␈αintroduce␈α␈↓λ≡␈↓␈αto␈αdenote␈α
"is
␈↓ α←␈↓the same as"; we could therefore say
␈↓"∀␈↓ α←␈↓α␈↓ ¬←λ[[y] x]␈↓ ␈↓λ≡␈↓ ␈↓αλ[[w] x]␈↓
␈↓"∀␈↓ α←␈↓We␈αexpect␈αthat␈α␈↓λ≡␈↓␈αobey␈αthe␈αrules␈αof␈αequality,␈αbeing␈αa␈αreflective,␈αtransitive␈αand
␈↓ α←␈↓symmetric relation. It should also "substitutive" in the following sense:
␈↓"∀␈↓ α←␈↓␈↓ ∧␈if ␈↓αf ␈↓λ≡␈↓α g ␈↓ then ␈↓αf[a] ␈↓λ≡␈↓α g[a]
␈↓"β␈↓ α←␈↓α(␈↓εs␈↓):␈↓ ∧␈if ␈↓αf ␈↓λ≡␈↓α g ␈↓ then ␈↓αλ[[x] f] ␈↓λ≡␈↓α λ[[x] g]␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈if ␈↓αa ␈↓λ≡␈↓α b ␈↓ then ␈↓αf[a] ␈↓λ≡␈↓α f[b]
␈↓ α←␈↓␈↓170 Evaluation␈↓
_3.13␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Next,␈α∞we␈α∞need␈α∞to␈α∞talk␈α∞about␈α∞the␈α∞bindings␈α∞of␈α∞variables␈α∞more␈α
carefully.
␈↓ α←␈↓We␈αwill␈αrefer␈αto␈αwell␈αformed␈α
components␈αof␈αthe␈αstatically␈αbound␈α␈↓λλ␈↓-calculus␈α
as
␈↓ α←␈↓"terms";␈α∂we␈α∞will␈α∂continue␈α∂to␈α∞refer␈α∂to␈α∂components␈α∞of␈α∂the␈α∂dynamically␈α∞bound
␈↓ α←␈↓language␈αLISP␈αas␈α"expressions".␈α Thus,␈αa␈αvariable,␈α␈↓αx␈↓,␈αis␈αa␈α␈↓↓free␈αvariable␈↓␈↓π 60␈↓␈αin
␈↓ α←␈↓an term, ␈↓λx␈↓ if:
␈↓"∀␈↓ α←␈↓␈↓λx␈↓ is the variable, ␈↓αx␈↓.
␈↓" ␈↓ α←␈↓␈↓λx␈↓ is a term ␈↓αf[A]␈↓, and ␈↓αx␈↓ is free in ␈↓αf␈↓ or ␈↓αx␈↓ is free in ␈↓αA␈↓.
␈↓" ␈↓ α←␈↓␈↓λx␈↓ is a term ␈↓αλ[[y] M]␈↓, and ␈↓αx␈↓ is free in ␈↓αM␈↓ and ␈↓αx␈↓ is distinct from ␈↓αy␈↓.
␈↓" ␈↓ α←␈↓Thus ␈↓αw␈↓ is free in ␈↓αλ[[x] w]␈↓.
␈↓"∀␈↓ α←␈↓␈↓ β'We can define a LISP predicate to test for free-ness:
␈↓"∀␈↓ α←␈↓αisfree <= λ[[x;z]␈↓ ∧7[is_var[z] → samevar[x;z];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 is_app[z] →␈↓ ¬←[isfree[x;func[z]] → ␈↓
t␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ¬← ␈↓
t␈↓α → isfree[x;arg␈↓β1␈↓α[arglist[z]]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 samevar[λ_var[z];x] → ␈↓
f␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ∧7 ␈↓
t␈↓α → isfree[x;body[z]]]]
␈↓"∀␈↓ α←␈↓␈↓ β'An␈α⊃occurrence␈α⊃of␈α⊂variable␈α⊃is␈α⊃a␈α⊂␈↓↓bound␈α⊃occurrence␈↓␈α⊃in␈α⊂a␈α⊃term␈α⊃␈↓λx␈↓␈α⊃if␈α⊂it
␈↓ α←␈↓occurs␈αin␈α␈↓λx␈↓␈αand␈αthe␈αoccurrence␈αis␈αnot␈αfree␈αin␈α␈↓λx␈↓.␈α For␈αexample,␈α␈↓αw␈↓␈αis␈αbound␈αin
␈↓ α←␈↓␈↓αλ[[w] w]␈↓.␈α∞ A␈α∞variable␈α∞may␈α∂have␈α∞both␈α∞bound␈α∞and␈α∞free␈α∂occurrences;␈α∞consider
␈↓ α←␈↓␈↓αw[λ[[w] w]]␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Using␈αour␈αnew␈αterminology,␈αwe␈αcan␈αsay␈αthat␈αa␈αsubstitution␈αof␈αthe␈αactual
␈↓ α←␈↓parameter␈α⊗for␈α⊗free␈α⊗occurrences␈α⊗of␈α⊗the␈α⊗formal␈α⊗parameter␈α⊗can␈α↔be␈α⊗made
␈↓ α←␈↓provided␈α∀no␈α∀free␈α∀variables␈α∀in␈α∪the␈α∀actual␈α∀parameter␈α∀will␈α∀become␈α∪bound
␈↓ α←␈↓variables as a result of the substitution.
␈↓"β␈↓ α←␈↓␈↓ β'Here is an appropriate substitution rule:
␈↓"∀␈↓ α←␈↓αsubst␈↓λ'␈↓α <= λ[[x;y;z]␈↓ ∧O[is_var[z] → [eq[y;z] → x; ␈↓
t␈↓α → z];
␈↓"β␈↓ α←␈↓α␈↓ ∧O is_app[z] → mk_app[␈↓ εosubst␈↓λ'␈↓α[x;y;func[z]];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬#␈↓ ε3␈↓ εosubst␈↓λ'␈↓α[x;y;arg␈↓β1␈↓α[arglist[z]]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧O eq[y;λ_var[z]] → z;
␈↓"β␈↓ α←␈↓α␈↓ ∧O not[isfree[y;body[z]]] → z;
␈↓"β␈↓ α←␈↓α␈↓ ∧O not[isfree[λ_var[z];x]] → mk_λ[␈↓ πgλ_var[z];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬#␈↓ ε3␈↓ εo␈↓ π∪␈↓ πgsubst␈↓λ'␈↓α[␈↓ λ;x;
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬#␈↓ ε3␈↓ εo␈↓ π∪␈↓ πg␈↓ πs␈↓ λ;y;
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬#␈↓ ε3␈↓ εo␈↓ π∪␈↓ πg␈↓ πs␈↓ λ;body[z]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧O ␈↓
t␈↓α →␈↓ ¬#λ[[u]mk_λ[␈↓ ε3u;
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬#␈↓ ε3subst␈↓λ'␈↓α[␈↓ π∪x;
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬#␈↓ ε3␈↓ εo␈↓ π∪y;
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬#␈↓ ε3␈↓ εo␈↓ π∪subst␈↓λ'␈↓α[␈↓ πgu;
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬#␈↓ ε3␈↓ εo␈↓ π∪␈↓ πg␈↓ πsλ_var[z];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬#␈↓ ε3␈↓ εo␈↓ π∪␈↓ πg␈↓ πsbody[z]]]]]
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬# [genvar[ ]] ]]
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 60␈↓Compare this definition of free with that in Section 3.7.
␈↓ α←␈↓␈↓3.13␈↓ πfReview and Reflection 171␈↓
␈↓"β␈↓ α←␈↓where ␈↓αgenvar␈↓ is to supply a new identifier for use as a variable name.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∀substitution␈α∀rule,␈α∪␈↓αsubst␈↓λ'␈↓,␈α∀is␈α∀used␈α∀to␈α∪express␈α∀the␈α∀␈↓εb␈↓-rule␈α∀of␈α∪the
␈↓ α←␈↓␈↓λλ␈↓-calculus:
␈↓"∀␈↓ α←␈↓(␈↓εb␈↓):␈↓ β) ␈↓αapp␈↓ ␈↓λ≡␈↓ ␈↓αsubst␈↓λ'␈↓α[arg␈↓β1␈↓α[arglist[app]];λ_var[func[app]];body[func[app]]]␈↓
␈↓"∀␈↓ α←␈↓where ␈↓αapp␈↓ is an anonymous λ-application.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α⊂is␈α∂another␈α⊂basic␈α∂rule␈α⊂of␈α∂the␈α⊂␈↓λλ␈↓-calculus␈α∂called␈α⊂the␈α⊂␈↓εa␈↓-rule.␈α∂ The
␈↓ α←␈↓␈↓εa␈↓-rule␈αcaptures␈αthe␈αnotion␈αthat␈α
␈↓αλ[[y] x]␈↓␈αdenotes␈αthe␈αsame␈αfunction␈αas␈α
␈↓αλ[[w] x]␈↓;
␈↓ α←␈↓that␈α∞is,␈α∞subject␈α∞to␈α∞certain␈α∂restrictions,␈α∞we␈α∞can␈α∞change␈α∞the␈α∂formal␈α∞parameter.
␈↓ α←␈↓The ␈↓εa␈↓-rule says:
␈↓"∀␈↓ α←␈↓(␈↓εa␈↓):␈↓ βo␈↓αfun␈↓ ␈↓λ≡␈↓ ␈↓αλ[[u] mk_λ[u;subst␈↓λ'␈↓α[u;λ_var[fun];body[fun]]][var]
␈↓" ␈↓ α←␈↓α␈↓ ∧Z␈↓provided that ␈↓αnot[isfree[var;body[fun]]]␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'To␈α
summarize␈α∞then,␈α
the␈α
␈↓λλ␈↓-calculus␈α∞is␈α
a␈α
formalism.␈α∞The␈α
␈↓εa␈↓␈α
and␈α∞␈↓εb␈↓␈α
rules
␈↓ α←␈↓are␈αtransformation␈αrules,␈αand␈α␈↓εs␈↓␈αexpresses␈αproperties␈αof␈αthe␈αrelation␈α␈↓λ≡␈↓␈αas␈α
rules
␈↓ α←␈↓of␈α_inference.␈α_ To␈α_complete␈α_the␈α_description,␈α_axioms␈α_which␈α_govern␈α↔the
␈↓ α←␈↓behavior␈αof␈α␈↓λ≡␈↓␈αas␈αan␈αequivalence␈αrelation␈αmust␈αbe␈αgiven.␈α The␈α␈↓εa␈↓␈α
and␈α␈↓εb␈↓-rules
␈↓ α←␈↓are␈α
called␈α
␈↓↓conversion␈α
rules␈↓;␈α
they␈α
express␈α
the␈α
essential␈α
behavior␈α
of␈α
functions
␈↓ α←␈↓and␈α⊃their␈α⊃applications.␈α⊃The␈α⊃␈↓εa␈↓␈α∩rule␈α⊃formalizes␈α⊃the␈α⊃notion␈α⊃that␈α∩a␈α⊃function
␈↓ α←␈↓denoted␈αby␈αa␈α␈↓λλ␈↓-term␈α
is␈αunchanged␈αif␈αwe␈α
change␈αthe␈αformal␈αparameters␈αof␈α
the
␈↓ α←␈↓␈↓λλ␈↓-term. that is:
␈↓"∀␈↓ α←␈↓α␈↓ ¬Hλ[[x] f[x]] ␈↓λ≡␈↓α λ[[y] f[y]]
␈↓"∀␈↓ α←␈↓␈↓ β'We␈α∂know␈α∂that␈α∂such␈α∂a␈α∂rule␈α∂is␈α∂not␈α∂valid␈α∂in␈α∂LISP;␈α∂for␈α∂example␈α∂␈↓αx␈↓␈α∞may
␈↓ α←␈↓occur␈α∀free␈α∀in␈α∀the␈α∪definition␈α∀of␈α∀␈↓αf␈↓.␈α∀ We␈α∪can␈α∀insure␈α∀that␈α∀a␈α∪programming
␈↓ α←␈↓language␈αwill␈α
satisfy␈αthe␈α
␈↓εa␈↓-rule␈αby␈αrequiring␈α
that␈αthe␈α
free␈αvariables␈αbe␈α
bound
␈↓ α←␈↓to␈αthe␈αvalues␈αwhich␈αwere␈αavailable␈α
at␈αthe␈αtime␈αthe␈αdefinition␈αwas␈αmade.␈α
This
␈↓ α←␈↓is␈α∂the␈α∂effect␈α∂of␈α∂the␈α∂␈↓αfunction␈↓␈α∞construct␈α∂of␈α∂LISP.␈α∂A␈α∂language␈α∂which␈α∞satisfies
␈↓ α←␈↓these␈α∪notions␈α∩is␈α∪called␈α∪␈↓↓referential␈α∩transparency␈↓.␈α∪ Thus␈α∪dynamic␈α∩binding
␈↓ α←␈↓violates␈α
referential␈α
transparency.␈α
The␈α
difficulty␈α
again␈α
is␈α
the␈α
treatment␈αof␈α
free
␈↓ α←␈↓variables.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α≥␈↓λλ␈↓-calculus␈α≤␈↓¬does␈↓␈α≥possess␈α≤referential␈α≥transparency.␈α≤Referential
␈↓ α←␈↓transparency␈α⊂is␈α⊃not␈α⊂simply␈α⊂a␈α⊃worthwhile␈α⊂theoretical␈α⊂property;␈α⊃its␈α⊂corollary,
␈↓ α←␈↓static␈α⊃binding,␈α⊂is␈α⊃a␈α⊃very␈α⊂useful␈α⊃practical␈α⊂property.␈α⊃In␈α⊃programming␈α⊂terms,
␈↓ α←␈↓referential␈α∂transparency␈α⊂means␈α∂that␈α⊂to␈α∂understand␈α⊂a␈α∂particular␈α⊂progam␈α∂we
␈↓ α←␈↓need␈α
only␈αunderstand␈α
the␈αeffect␈α
of␈αthe␈α
subprograms␈αrather␈α
than␈αunderstand
␈↓ α←␈↓the␈αimplementation␈αof␈αthose␈αsubprograms.␈αFor␈αexample,␈αwe␈αneed␈αnot␈αconcern
␈↓ α←␈↓ourselves␈αwith␈αthe␈αnaming␈αconventions␈α
used␈αinternal␈αto␈αthe␈αprograms.␈α
These
␈↓ α←␈↓ideas␈α∩are␈α∪expressed␈α∩in␈α∩the␈α∪philosophy␈α∩of␈α∩␈↓↓modular␈α∪programming␈↓.␈α∩ That
␈↓ α←␈↓programming␈α∃style␈α∃encourages␈α∃the␈α∃construction␈α∃of␈α∃program␈α∃segments␈α∃as
␈↓ α←␈↓self-contained␈α→boxes,␈α→or␈α→modules,␈α→with␈α→well-defined␈α→input␈α→and␈α→output
␈↓ α←␈↓specifications.␈α⊂The␈α⊂intent␈α⊂is␈α⊂to␈α⊂manufacture␈α⊂larger␈α⊂programs␈α⊂by␈α⊂systematic
␈↓ α←␈↓combination␈α∞of␈α∞smaller␈α
modules.␈α∞ We␈α∞will␈α
discuss␈α∞some␈α∞further␈α
implications
␈↓ α←␈↓␈↓172 Evaluation␈↓
_3.13␈↓
␈↓"β␈↓ α←␈↓of static binding in Section 5.21.␈↓π 61␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
␈↓εb␈↓-rule␈α
expresses␈α
the␈αintent␈α
of␈α
function␈α
application.␈α
We␈αwould␈α
then
␈↓ α←␈↓expect␈α⊂that␈α⊂a␈α∂model␈α⊂of␈α⊂the␈α∂␈↓λλ␈↓-calculus␈α⊂would␈α⊂have␈α∂a␈α⊂domain␈α⊂consisting␈α∂of
␈↓ α←␈↓functions;␈α
and␈α∞require␈α
that␈α
the␈α∞␈↓εb␈↓-rule␈α
be␈α
reflected␈α∞in␈α
the␈α
model␈α∞as␈α
function
␈↓ α←␈↓application.␈α
The␈α∞equality␈α
preserving␈α∞properties␈α
of␈α∞the␈α
␈↓εa␈↓␈α∞and␈α
␈↓εb␈↓␈α∞rules␈α
would
␈↓ α←␈↓require␈α
that␈αif␈α
␈↓αf␈↓␈αand␈α
␈↓αg␈↓␈αare␈α
terms␈α
such␈αthat␈α
if␈α␈↓f(a) = g(a)␈↓,␈α
then␈αany␈α
␈↓εa␈↓␈α
or␈α␈↓εb␈↓
␈↓ α←␈↓manipulations␈αof␈α␈↓αf␈↓,␈α␈↓αg␈↓␈αor␈α␈↓αa␈↓␈α
yielding␈α␈↓αf␈↓λ'␈↓,␈α␈↓αg␈↓λ'␈↓␈αand␈α␈↓αa␈↓λ'␈↓,␈αthe␈α
relation␈α␈↓f␈↓λ'␈↓(a␈↓λ'␈↓) = g␈↓λ'␈↓(a)␈↓
␈↓ α←␈↓holds.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αare␈αnow␈αin␈αa␈αposition␈αto␈αrelate␈αbinding␈αstrategies␈αwith␈αthe␈αideas␈αof
␈↓ α←␈↓substitution␈α
and␈α
␈↓εb␈↓␈α
reduction.␈α
Static␈α
binding␈α
is␈α
an␈α
implementation␈α
of␈α
the␈α
ideas
␈↓ α←␈↓expressed␈α
in␈α
the␈α␈↓εb␈↓␈α
rule.␈α
We␈αcan␈α
implement␈α
the␈αnotions␈α
using␈α
␈↓αsubst␈↓λ'␈↓␈α
and␈αdo
␈↓ α←␈↓explicit␈α⊂substitutions,␈α⊂or␈α⊂we␈α⊃can␈α⊂simulate␈α⊂the␈α⊂substitutions␈α⊂using␈α⊃a␈α⊂symbol
␈↓ α←␈↓table␈αdevice␈αas␈αLISP␈αdoes␈αfor␈αdynamic␈αbinding.␈αNo␈αproblems␈αshould␈αarise␈αif
␈↓ α←␈↓we␈α
use␈α
␈↓αsubst␈↓λ'␈↓;␈α
however␈α
this␈α
solution␈α
is␈α
not␈α
terribly␈α
efficient.␈α
Particular␈α
care␈α
is
␈↓ α←␈↓needed␈α∀if␈α∪␈↓αsubst␈↓λ'␈↓␈α∀is␈α∪to␈α∀be␈α∪simulated.␈α∀The␈α∪difficulty␈α∀arises␈α∀in␈α∪adequately
␈↓ α←␈↓modelling␈α_the␈α_substitution␈α↔of␈α_values␈α_for␈α↔variables␈α_which␈α_are␈α_free␈α↔in
␈↓ α←␈↓functional␈α⊗arguments␈α⊗or␈α⊗functional␈α↔values.␈α⊗ From␈α⊗LISP,␈α⊗we␈α↔know␈α⊗one
␈↓ α←␈↓solution␈α∞to␈α∂this␈α∞problem:␈α∂use␈α∞the␈α∂␈↓αfunction␈↓␈α∞construct.␈α∂We␈α∞could␈α∂simulate␈α∞the
␈↓ α←␈↓effect␈α∩of␈α∩␈↓αsubst␈↓λ'␈↓␈α∩by␈α∩using␈α∩a␈α∩LISP␈α∩symbol␈α∩table␈α∩and␈α∩requiring␈α∩that␈α⊃every
␈↓ α←␈↓functional argument or value be ␈↓αfunarg␈↓-ed.␈↓π 62␈↓
␈↓"β␈↓ α←␈↓␈↓ β'From␈α≤this␈α≤standpoint,␈α≤dynamic␈α≤binding␈α≤is␈α≤simply␈α≤an␈α≠incorrect
␈↓ α←␈↓implementation␈α⊂of␈α∂substitution.␈α⊂Attempts␈α⊂to␈α∂legitimize␈α⊂dynamic␈α⊂binding␈α∂by
␈↓ α←␈↓supplying␈α
formal␈αrules␈α
lead␈αto␈α
difficulties.␈α
The␈αsimple␈α
properties␈αexpressed␈α
in
␈↓ α←␈↓␈↓αsubst␈↓λ'␈↓␈α
and␈α
the␈α
␈↓εb␈↓ rule␈α
do␈α
not␈α
hold␈α
for␈α
dynamic␈α
binding.␈α
However,␈α
dynamic
␈↓ α←␈↓binding␈αis␈αa␈αvery␈α
useful␈αprogramming␈αtool.␈αIts␈α
ability␈αto␈αpostpone␈αbindings␈α
is
␈↓ α←␈↓particularly␈α∂useful␈α∂in␈α⊂interactive␈α∂programming␈α∂environments␈α∂where␈α⊂we␈α∂are
␈↓ α←␈↓creating␈α→program␈α→modules␈α~incrementally.␈α→The␈α→final␈α→word␈α~on␈α→binding
␈↓ α←␈↓strategies has not been heard.
␈↓"β␈↓ α←␈↓␈↓ β'So␈α
far␈α
the␈α
discussion␈α
has␈α
concentrated␈α
on␈α
binding␈α
strategies.␈α
We␈αnow
␈↓ α←␈↓wish␈α⊃to␈α∩discuss␈α⊃the␈α⊃implications␈α∩of␈α⊃calling␈α⊃style.␈α∩ We␈α⊃have␈α∩discussed␈α⊃two
␈↓ α←␈↓calling␈α∪styles:␈α∀call-by-value␈α∪and␈α∀call-by-name;␈α∪these␈α∀computational␈α∪devices
␈↓ α←␈↓should␈α∂have␈α∞mathematical␈α∂interpretations.␈α∞The␈α∂conversion␈α∞rules␈α∂contain␈α∞no
␈↓ α←␈↓instructions␈α∂for␈α∂their␈α⊂order␈α∂of␈α∂application.␈α∂ If␈α⊂the␈α∂hypotheses␈α∂for␈α∂a␈α⊂rule␈α∂is
␈↓ α←␈↓satisfied,␈α∞then␈α∞it␈α∂may␈α∞be␈α∞applied.␈α∞ In␈α∂the␈α∞reduction␈α∞of␈α∞a␈α∂␈↓λλ␈↓-expression␈α∞there
␈↓ α←␈↓may␈αbe␈αseveral␈αreductions␈αpossible␈αat␈α
any␈αone␈αtime.␈α This␈αis␈αas␈αit␈α
should␈αbe;
␈↓ α←␈↓we␈α⊗are␈α⊗extracting␈α∃the␈α⊗procedural␈α⊗aspects,␈α∃and␈α⊗certainly␈α⊗calling␈α⊗style␈α∃is
␈↓ α←␈↓procedural.␈α∞The␈α∞order␈α∞of␈α∂application␈α∞of␈α∞rules␈α∞expresses␈α∞a␈α∂particular␈α∞calling
␈↓ α←␈↓algorithm.␈α
If␈α
we␈α
design␈α
a␈α
␈↓λλ␈↓-calculus␈α
machine,␈α
we␈α
might␈α
specify␈α
a␈α
preferred
␈↓ α←␈↓order, but the machine reflects "␈↓¬procedure␈↓"; the calculus reflects "␈↓¬function␈↓".
␈↓"β␈↓ α←␈↓␈↓ β'An␈α∀interpretation␈α∀of␈α∀the␈α∃conversion␈α∀rules␈α∀as␈α∀rules␈α∃of␈α∀computation
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 61␈↓There␈α⊂have␈α⊂been␈α⊃several␈α⊂recent␈α⊂investigations␈α⊃([Hew 76],␈α⊂[Sus 75],
␈↓ α←␈↓[Ste 76b], [Ste 76c]) of statically bound LISP-like languages.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 62␈↓Of course, such a claim should be proved.
␈↓ α←␈↓␈↓3.13␈↓ πfReview and Reflection 173␈↓
␈↓"β␈↓ α←␈↓requires␈α∩the␈α⊃establishment␈α∩of␈α⊃a␈α∩notion␈α⊃of␈α∩what␈α⊃is␈α∩to␈α⊃be␈α∩computed.␈α⊃ The
␈↓ α←␈↓conversion␈α
rules␈α
simply␈α
state␈α
equivalences␈α
between␈α
expressions;␈α
however␈αthe
␈↓ α←␈↓␈↓εb␈↓ rule␈αcan␈αbe␈αapplied␈αin␈αa␈αmanner␈αanalogous␈αto␈αLISP's␈αλ-binding.␈αThat␈αis,␈αit
␈↓ α←␈↓can␈α∩be␈α∩used␈α∩to␈α∩replace␈α⊃an␈α∩application␈α∩with␈α∩the␈α∩appropriately␈α⊃substituted
␈↓ α←␈↓body.␈α
In␈α
this␈α
context␈α
the␈α
␈↓εb␈↓ rule␈α
is␈α
called␈α
a␈α
␈↓↓reduction␈α
rule␈↓;␈α
and␈αthe␈α
application
␈↓ α←␈↓of␈α
the␈α
rule␈α
is␈α
called␈α
a␈α∞reduction␈α
step.␈α
There␈α
are␈α
two␈α
common␈α∞strategies␈α
for
␈↓ α←␈↓choosing a reduction step: applicative order and normal order.
␈↓"β␈↓ α←␈↓␈↓ β'Applicative␈α⊂order␈α⊂reduces␈α∂right␈α⊂most␈α⊂term;␈α∂normal␈α⊂order␈α⊂reduces␈α∂the
␈↓ α←␈↓left most␈αterm.␈α
We␈αwill␈αsay␈α
that␈αa␈α
␈↓λλ␈↓-term␈αis␈αin␈α
␈↓↓normal␈αform␈↓␈α
if␈αit␈αcontains␈α
no
␈↓ α←␈↓term␈αreducible␈α
by␈αthe␈α
␈↓λβ␈↓␈αrule.␈α
Not␈αall␈αterms␈α
have␈αnormal␈α
forms:␈αlet␈α
␈↓εD␈↓␈αname
␈↓ α←␈↓␈↓αλ[[x] x[x]]␈↓;␈αthen␈α
␈↓εD␈↓α[␈↓εD␈↓]␈↓␈αdoes␈α
not␈αhave␈α
a␈αnormal␈α
form.␈α Every␈α␈↓εb␈↓␈α
transformation
␈↓ α←␈↓of␈α⊃␈↓εD␈↓␈α⊃produces␈α⊃a␈α⊃new␈α⊃term␈α⊃which␈α⊃is␈α⊃also␈α⊃␈↓εb␈↓␈α⊃reducible.␈α⊃ Not␈α∩all␈α⊃reduction
␈↓ α←␈↓sequences␈α⊂yield␈α⊂a␈α⊂normal␈α⊃form:␈α⊂when␈α⊂␈↓αλ[[x] y][␈↓εD␈↓α[␈↓εD␈↓α]]␈↓␈α⊂is␈α⊂reduced␈α⊃in␈α⊂normal
␈↓ α←␈↓order,␈α∩a␈α∪normal␈α∩form␈α∩results;␈α∪whereas␈α∩applicative␈α∩order␈α∪will␈α∩not␈α∪yield␈α∩a
␈↓ α←␈↓normal form.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αapplication␈αof␈α
the␈αreduction␈αrules␈α
can␈αbe␈αconsidered␈αa␈α
computation
␈↓ α←␈↓scheme.␈α_The␈α_process␈α_of␈α_reducing␈α→a␈α_term␈α_is␈α_the␈α_computation,␈α→and␈α_a
␈↓ α←␈↓computation␈αterminates␈αif␈αthat␈αreduction␈αproduces␈αa␈αnormal␈αform.␈α With␈αthis
␈↓ α←␈↓interpretation,␈α∂some␈α∂computations␈α∂terminate␈α∂and␈α∂some␈α∂don't.␈α∂A␈α∂term␈α∂has␈α∞a
␈↓ α←␈↓normal␈α⊂form␈α⊃just␈α⊂in␈α⊂the␈α⊃case␈α⊂that␈α⊂some␈α⊃reduction␈α⊂sequence␈α⊃terminates.␈α⊂A
␈↓ α←␈↓␈↓λλ␈↓-calculus␈α⊃machine␈α∩([Lan 64])␈α⊃can␈α∩simulate␈α⊃the␈α∩reduction␈α⊃rules␈α∩in␈α⊃several
␈↓ α←␈↓ways␈αsince␈αno␈αorder␈αof␈αapplication␈αis␈αspecified␈αby␈αthe␈αrules.␈α Also,␈αa␈αmachine
␈↓ α←␈↓might␈α∞perform␈α∞the␈α∞substitutions␈α∞directly␈α∞or␈α∞might␈α∞simulate␈α∞the␈α
substitutions
␈↓ α←␈↓in␈αa␈αmanner␈αsimilar␈αto␈αLISP.␈α
Finally␈αwe␈αnote␈αthe␈αclose␈αrelationships␈α
between
␈↓ α←␈↓reduction␈α⊗orders␈α⊗and␈α⊗calling␈α⊗styles:␈α⊗applicative␈α⊗order␈α⊗is␈α⊗most␈α⊗naturally
␈↓ α←␈↓associated␈α∂with␈α∂call-by-value,␈α∞and␈α∂call-by-name␈α∂is␈α∞reflected␈α∂in␈α∂normal␈α∞order
␈↓ α←␈↓reduction.␈↓π 63␈↓
␈↓"β␈↓ α←␈↓␈↓ β'These␈α∞discussions␈α∞suggest␈α∂some␈α∞interesting␈α∞problems.␈α∞ First,␈α∂there␈α∞may
␈↓ α←␈↓well␈α⊂be␈α∂two␈α⊂or␈α∂more␈α⊂sequences␈α∂of␈α⊂reductions␈α∂for␈α⊂a␈α⊂␈↓λλ␈↓-expression;␈α∂assuming
␈↓ α←␈↓they␈αboth␈αterminate,␈αwill␈α
they␈αyield␈αthe␈αsame␈α
normal␈αforms?␈α In␈αfact,␈α
if␈αboth
␈↓ α←␈↓reduction␈α
sequences␈α
terminate␈α
then␈α
they␈αresult␈α
in␈α
the␈α
same␈α
normal␈αform.␈α
This
␈↓ α←␈↓is␈α
called␈αthe␈α
Church-Rosser␈α
theorem␈α([Cur 58],␈α
[Ros 71],␈α[Mil 73],␈α
[Wad 74a],
␈↓ α←␈↓[Leh 73]).
␈↓"β␈↓ α←␈↓␈↓ β'Second,␈αif␈αwe␈αhave␈αtwo␈α␈↓λλ␈↓-terms␈αwhich␈αreduce␈αto␈αdistinct␈α
normal␈αforms,
␈↓ α←␈↓are␈α⊃they␈α⊃"inherently␈α⊃different"␈α⊃terms?␈α⊃ This␈α⊃requires␈α⊃some␈α∩explanation␈α⊃of
␈↓ α←␈↓"inherently␈αdifferent".␈α We␈αmight␈αsay␈αthat␈αby␈αdefinition,␈αterms␈αwith␈αdifferent
␈↓ α←␈↓normal␈αforms␈αare␈α"inherently␈αdifferent".␈αBut␈αthinking␈αof␈α␈↓λλ␈↓-terms␈αas␈αdenoting
␈↓ α←␈↓functions,␈α∃to␈α∃say␈α∃two␈α∃␈↓λλ␈↓-terms␈α∃are␈α∃"different"␈α∃is␈α∃to␈α∃say␈α∃we␈α∃can␈α∃exhibit
␈↓ α←␈↓arguments␈αsuch␈αthat␈αthe␈αvalue␈αof␈αapplication␈αof␈αone␈αfunction␈αis␈αnot␈αequal␈αto
␈↓ α←␈↓the␈α∞value␈α∞of␈α
the␈α∞other␈α∞function␈α
application.␈α∞ C. Boehm␈α∞has␈α∞established␈α
this
␈↓ α←␈↓for ␈↓λλ␈↓-terms which have normal forms ([Wad 71]).
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 63␈↓There␈α∪is␈α∪more␈α∩to␈α∪normal␈α∪order␈α∩reduction␈α∪that␈α∪just␈α∩call-by-name;
␈↓ α←␈↓normal order reduction also performs "partial evaluation" of function bodies.
␈↓ α←␈↓␈↓174 Evaluation␈↓
_3.13␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∂situation␈α∂changes␈α∂when␈α∂we␈α∂examine␈α∂␈↓λλ␈↓-terms␈α∂which␈α∂do␈α∂not␈α∂have
␈↓ α←␈↓normal␈α∞forms.␈α∞ Recalling␈α∞the␈α∞intuitive␈α∞relationship␈α∞between␈α
non-termination
␈↓ α←␈↓and␈α
"no␈αnormal␈α
form",␈α
we␈αmight␈α
expect␈α
that␈αall␈α
expressions␈α
without␈αnormal
␈↓ α←␈↓form␈αare␈α"equivalent".␈α
However,␈αit␈αcan␈αbe␈α
shown␈αthat␈αsuch␈α
an␈αidentification
␈↓ α←␈↓would␈α∪lead␈α∪to␈α∪contradictions.␈α∪ We␈α∪might␈α∪also␈α∪expect␈α∪that␈α∀terms␈α∪without
␈↓ α←␈↓normal␈αforms␈αare␈α"different"␈αfrom␈αterms␈αwhich␈αdo␈αhave␈αnormal␈αforms.␈α This
␈↓ α←␈↓is␈α
also␈α
not␈α
true;␈α
[Wad 71]␈α
exhibits␈αtwo␈α
expressions,␈α
␈↓αI␈↓␈α
and␈α
␈↓αJ␈↓␈α
with␈αand␈α
without
␈↓ α←␈↓normal␈αform,␈αrespectively␈αsuch␈αthat␈α
␈↓αI␈↓λ≡␈↓αJ␈↓.␈α These␈αtwo␈αterms␈αare␈αthe␈α
"same"␈αin
␈↓ α←␈↓the␈αsense␈αthat␈α3␈αand␈α
2.99999 ...␈αare␈αthe␈α"same";␈α␈↓αJ␈↓␈α
is␈αthe␈αlimit␈αof␈αa␈αsequence␈α
of
␈↓ α←␈↓approximations␈α⊂to␈α⊂␈↓αI␈↓.␈α⊂ In␈α⊂fact␈α⊂for␈α⊃any␈α⊂term␈α⊂with␈α⊂normal␈α⊂form␈α⊂there␈α⊃is␈α⊂an
␈↓ α←␈↓equivalent␈αterm␈α
without␈αnormal␈αform.␈α
Also,␈αwe␈αcan␈α
exhibit␈αtwo␈α
␈↓λλ␈↓-terms,␈α␈↓αY␈↓β1␈↓
␈↓ α←␈↓and␈α∞␈↓αY␈↓β2␈↓,␈α∞both␈α
without␈α∞normal␈α∞form,␈α
which␈α∞are␈α∞distinct␈α
in␈α∞that␈α∞no␈α
reduction
␈↓ α←␈↓sequence␈αwill␈α
reduce␈αone␈α
to␈αthe␈αother;␈α
but␈αthey␈α
are␈α"the␈α
same␈αfunction"␈αin␈α
the
␈↓ α←␈↓sense␈αthat,␈αfor␈αany␈αargument,␈α␈↓αa␈↓␈αwe␈αsupply,␈αboth␈αreductions␈αresult␈αin␈αthe␈αsame
␈↓ α←␈↓term.␈α
That␈α
is␈α
␈↓αY␈↓β1␈↓α[a] ␈↓λ≡␈↓α Y␈↓β2␈↓α[a]␈↓.␈↓π 64␈↓␈α
The␈αreduction␈α
rules␈α
of␈α
the␈α
␈↓λλ␈↓-calculus␈αcannot
␈↓ α←␈↓help␈α↔us.␈α↔ The␈α↔resolution␈α↔of␈α⊗the␈α↔idea␈α↔of␈α↔"same-ness"␈α↔requires␈α⊗stronger
␈↓ α←␈↓analysis.␈↓π 65␈↓
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∪can␈α∪give␈α∪an␈α∪interpretation␈α∪to␈α∪the␈α∪␈↓λλ␈↓-calculus␈α∪such␈α∪that␈α∀in␈α∪that
␈↓ α←␈↓interpretation␈αthe␈αpairs␈α␈↓αI␈↓␈α
and␈α␈↓αJ␈↓,␈αor␈α␈↓αY␈↓β1␈↓␈αand␈α
␈↓αY␈↓β2␈↓,␈αhave␈αthe␈αsame␈αmeaning.␈α
This
␈↓ α←␈↓should␈α∂be␈α∂a␈α∞convincing␈α∂argument␈α∂for␈α∂maintaining␈α∞that␈α∂they␈α∂are␈α∂the␈α∞"same
␈↓ α←␈↓function"␈α∞even␈α∞though␈α∞the␈α∞reduction␈α
rules␈α∞are␈α∞␈↓¬not␈↓␈α∞sufficient␈α∞to␈α∞display␈α
that
␈↓ α←␈↓equivalence.␈↓π 66␈↓␈α∀D.␈α∀Scott␈α∀␈↓¬has␈↓␈α∀exhibited␈α∀a␈α∀model␈α∀or␈α∀interpretation␈α∃of␈α∀the
␈↓ α←␈↓␈↓λλ␈↓-calculus,␈αand␈α
D.␈αPark␈αhas␈α
shown␈αthe␈α
equivalence␈αin␈αthis␈α
model␈αof␈α
␈↓αY␈↓β1␈↓␈αand
␈↓ α←␈↓␈↓αY␈↓β2␈↓, and C. Wadsworth as shown the equivalence of ␈↓αI␈↓ and ␈↓αJ␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'As␈α
we␈α
said␈αat␈α
the␈α
beginning␈αof␈α
the␈α
section,␈αthis␈α
calculus␈α
was␈αintended␈α
to
␈↓ α←␈↓explicate␈α∀the␈α∪idea␈α∀of␈α∪"function"␈α∀and␈α∪"function␈α∀application".␈α∪ There␈α∀is␈α∪a
␈↓ α←␈↓reasonably␈αsubtle␈αdistinction␈αbetween␈αChurch's␈αconception␈αof␈αa␈αfunction␈αas␈αa
␈↓ α←␈↓rule␈αof␈αcorrespondence,␈α
and␈αthe␈αusual␈α
set-theoretic␈αview␈αof␈α
a␈αfunction␈αas␈αa␈α
set
␈↓ α←␈↓of␈α∪ordered␈α∩pairs.␈α∪ In␈α∩the␈α∪latter␈α∪setting␈α∩we␈α∪rather␈α∩naturally␈α∪think␈α∪of␈α∩the
␈↓ α←␈↓elements␈α⊃of␈α⊂the␈α⊃domain␈α⊂and␈α⊃range␈α⊂of␈α⊃a␈α⊂function␈α⊃as␈α⊂existing␈α⊃␈↓¬prior␈↓␈α⊃to␈α⊂the
␈↓ α←␈↓specification of the function:
␈↓"∀␈↓ α←␈↓␈↓ ∧?"Let ␈↓f␈↓ be the function ␈↓{<x,1>, <y,2>, ...}␈↓"
␈↓"∀␈↓ α←␈↓When␈αwe␈αthink␈αof␈αa␈αfunction␈α
given␈αas␈αa␈αpredetermined␈αset␈αof␈α
ordered␈αpairs
␈↓ α←␈↓we␈αdo␈αnot␈αexpect␈αfunctions␈αwhich␈αcan␈αtake␈αthemselves␈αas␈αarguments␈αlike␈α
␈↓f(f)␈↓.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 64␈↓Note that ␈↓αf[a]␈↓ may have a normal form even though ␈↓αf␈↓ does not.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 65␈↓The␈α
interpretation␈αof␈α
"same-ness"␈αwhich␈α
we␈αhave␈α
been␈αusing␈α
is␈αthat
␈↓ α←␈↓of␈α∀extensional␈α∀equality.␈α∪That␈α∀is,␈α∀two␈α∪functions␈α∀are␈α∀considered␈α∀the␈α∪same
␈↓ α←␈↓function␈αif␈αno␈αdifferences␈αcan␈αbe␈αdetected␈αunder␈αapplication␈αof␈αthe␈αfunctions
␈↓ α←␈↓to any arguments.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 66␈↓This␈α∀demonstration␈α∀also␈α∀gives␈α∪credence␈α∀to␈α∀the␈α∀position␈α∀that␈α∪the
␈↓ α←␈↓meaning␈αtranscends␈αthe␈α
reduction␈αrules.␈α Compare␈αthe␈α
incompleteness␈αresults
␈↓ α←␈↓of K. Godel ([Men 64]).
␈↓ α←␈↓␈↓3.13␈↓ πfReview and Reflection 175␈↓
␈↓"β␈↓ α←␈↓Such␈α∃functions␈α∀are␈α∃called␈α∀␈↓↓self-applicative␈↓.␈α∃ Several␈α∃languages,␈α∀including
␈↓ α←␈↓LISP,␈αallow␈αthe␈αprocedural␈αanalog␈αof␈αself␈αapplicative␈αfunctions.␈αThey␈αare␈αan
␈↓ α←␈↓instance␈α
of␈α
functional␈αarguments (Section 3.10).␈α
The␈α
LISP␈αfunction␈α
discussed
␈↓ α←␈↓in␈α∃the␈α∃problem␈α∃on␈α∃page 149␈α∃is␈α∃an␈α∃instance␈α∃of␈α∃a␈α⊗self-applicative␈α∃LISP
␈↓ α←␈↓function.␈α Since␈α
we␈αcan␈αdeal␈α
with␈αself-application␈αas␈α
a␈αprocedural␈α
concept␈αat
␈↓ α←␈↓least,␈α∂perhaps␈α∂some␈α∂of␈α∂this␈α∂understanding␈α∂will␈α∂help␈α∂with␈α⊂the␈α∂mathematical
␈↓ α←␈↓questions.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α␈↓λλ␈↓-calculus␈αis␈αan␈αappropriate␈αtool␈αfor␈αstudying␈αself-application␈αsince
␈↓ α←␈↓any␈α
␈↓λλ␈↓-term␈αmay␈α
be␈αapplied␈α
to␈α
any␈α␈↓λλ␈↓-term␈α
including␈αitself.␈α
Compare␈αthis␈α
with
␈↓ α←␈↓the␈α
condition␈α∞in␈α
LISP␈α
when␈α∞we␈α
think␈α
of␈α∞the␈α
S-expression␈α∞representation␈α
of
␈↓ α←␈↓the␈α∃language␈α∃as␈α∃the␈α∃language␈α∃itself.␈α∃For␈α∃example,␈α∃in␈α∃the␈α∃␈↓¬programming
␈↓ α←␈↓¬language␈↓ LISP, we can evaluate expressions like:
␈↓"∀␈↓ α←␈↓␈↓ ∧]␈↓α((LAMBDA (X) ␈↓λx␈↓α) (LAMBDA (X) ␈↓λx␈↓α))␈↓
␈↓"∀␈↓ α←␈↓That␈αis,␈αthe␈αdistinction␈αbetween␈αprogram␈αand␈αdata␈αdisappears,␈αjust␈αas␈αit␈αdoes
␈↓ α←␈↓in the ␈↓λλ␈↓-calculus.
␈↓"β␈↓ α←␈↓␈↓ β'As␈α∃we␈α∃move␈α∃again␈α∀from␈α∃procedural␈α∃notions␈α∃to␈α∃more␈α∀denotational
␈↓ α←␈↓concepts␈α∂we␈α∂remark␈α∂that␈α∞denotational␈α∂interpretations␈α∂have␈α∂been␈α∞introduced
␈↓ α←␈↓before. When we said (page 118) that:
␈↓"∀␈↓ α←␈↓α␈↓ ∧∂f[a␈↓β1␈↓α; ...; a␈↓βn␈↓α] ␈↓ was the same as ␈↓α eval[(F A␈↓β1␈↓α ... A␈↓βn␈↓α);( )]
␈↓"∀␈↓ α←␈↓we␈α
were␈α
relating␈α
a␈α
denotational␈α
notion␈α
with␈α
an␈α
operational␈α
notion.␈α∞The␈α
left
␈↓ α←␈↓hand␈α
side␈α
of␈α
this␈α
equation␈α
can␈α
be␈α
interpreted␈α
denotationally.␈α
The␈α
right␈α
hand
␈↓ α←␈↓side␈α∪is␈α∪operational,␈α∪expressing␈α∩a␈α∪procedure␈α∪call.␈α∪ A␈α∪proper␈α∩mathematical
␈↓ α←␈↓theory␈α
should␈αallow␈α
us␈αto␈α
state␈α
such␈αan␈α
equation␈αprecisely␈α
and␈αshould␈α
contain
␈↓ α←␈↓methods␈α∪which␈α∪allow␈α∪us␈α∪to␈α∩demonstrate␈α∪the␈α∪correctness␈α∪of␈α∪the␈α∩assertion.
␈↓ α←␈↓Recent␈αresearch␈α([Sco 70],␈α
[Sco 73],␈α[Wad 71],␈α[Gor 73],␈α[Gor 75])␈α
has␈αbegun
␈↓ α←␈↓to develop such mathematical methods.
␈↓"β␈↓ α←␈↓␈↓ β'This␈α⊗denotational-operational␈α⊗distinction␈α∃is␈α⊗appropriate␈α⊗in␈α⊗a␈α∃more
␈↓ α←␈↓general␈αcontext.␈α When␈αwe␈αare␈αpresented␈αwith␈αsomeone's␈αprogram␈αand␈αasked
␈↓ α←␈↓"what␈α⊃does␈α⊃it␈α⊃compute?"␈α⊃we␈α⊃usually␈α⊃begin␈α⊃our␈α∩investigation␈α⊃operationally,
␈↓ α←␈↓discovering␈α"what␈αdoes␈αit␈α␈↓¬do␈↓?".␈↓π 67␈↓␈αFrequently,␈αby␈αtracing␈αits␈αexecution,␈αwe␈αcan
␈↓ α←␈↓discover a denotational description: E.g., "ah! it computes the square root".
␈↓"β␈↓ α←␈↓␈↓ β'When␈α∀␈↓αgreat␈α∀mother␈↓␈α∀was␈α∀presented␈α∀it␈α∀was␈α∀given␈α∀as␈α∃an␈α∀operational
␈↓ α←␈↓exercise,␈αwith␈αthe␈αprimary␈αintent␈αof␈αintroducing␈αthe␈αLISP␈αevaluation␈αprocess
␈↓ α←␈↓without␈α⊂involving␈α∂complicated␈α⊂names␈α⊂and␈α∂concepts.␈α⊂Forms␈α⊂involving␈α∂␈↓αgreat
␈↓ α←␈↓αmother␈↓␈αwere␈αevaluated␈αperhaps␈α
without␈αunderstanding␈αthe␈αdenotation,␈α
but␈αif
␈↓ α←␈↓asked␈α∂"what␈α⊂does␈α∂␈↓αgreat␈α⊂mother␈↓␈α∂do?"␈α⊂you␈α∂would␈α⊂be␈α∂hard␈α⊂pressed␈α∂to␈α⊂give␈α∂a
␈↓ α←␈↓comprehensible␈αand␈αpurely␈αoperational␈αdefinition.␈αHowever,␈αyou␈αmight␈αhave
␈↓ α←␈↓discovered␈α⊂the␈α⊂intended␈α∂nature␈α⊂of␈α⊂␈↓αgreat␈α⊂mother␈↓␈α∂yourself;␈α⊂then␈α⊂it␈α⊂would␈α∂be
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 67␈↓Another␈α
common␈α∞manifestation␈α
of␈α∞this␈α
"denotation"␈α∞phenomonon␈α
is
␈↓ α←␈↓the␈α∂common␈α∂programmer␈α∂complaint:␈α∂"It's␈α⊂easier␈α∂to␈α∂write␈α∂your␈α∂own␈α⊂than␈α∂to
␈↓ α←␈↓understand someone else's program."
␈↓ α←␈↓␈↓176 Evaluation␈↓
_3.13␈↓
␈↓"β␈↓ α←␈↓relatively␈αeasy␈αto␈αdescribe␈αher␈αbehavior.␈αIndeed,␈αonce␈αthe␈αdenotation␈αof␈α␈↓αgreat
␈↓ α←␈↓αmother␈↓␈α≤has␈α≤been␈α≤discovered␈α≤questions␈α≤like␈α≤"What␈α≤is␈α≤the␈α≤value␈α≤of
␈↓ α←␈↓␈↓αtgmoaf[(CAR (QUOTE (A . B)))]␈↓?␈α⊂"␈α⊂are␈α⊂more␈α⊂easily␈α⊂answered␈α⊂by␈α⊃using␈α⊂the
␈↓ α←␈↓denotation of ␈↓αtgmoaf␈↓: "what is the value of ␈↓αcar[(A . B)]␈↓?"
␈↓"β␈↓ α←␈↓␈↓ β'In␈α⊃discussing␈α⊃models␈α⊃for␈α⊃LISP,␈α⊃we␈α⊃will␈α⊃parallel␈α⊃our␈α⊃development␈α⊃of
␈↓ α←␈↓interpreters␈α∞for␈α∞LISP␈α∞since␈α∞each␈α∞subset,␈α∞␈↓αtgmoaf,␈α∞tgmoafr␈↓,␈α∞and␈α∞␈↓αeval␈↓,␈α∞will␈α
also
␈↓ α←␈↓introduce␈α⊃new␈α⊃problems␈α⊃for␈α⊃our␈α⊃mathematical␈α⊃semantics.␈α⊃ Our␈α∩first␈α⊃LISP
␈↓ α←␈↓subset␈α∩considers␈α∪functions,␈α∩composition,␈α∩and␈α∪constants.␈α∩ Constants␈α∪will␈α∩be
␈↓ α←␈↓elements␈α⊃of␈α∩our␈α⊃domain␈α∩of␈α⊃interpretation.␈α⊃ That␈α∩domain␈α⊃will␈α∩include␈α⊃the
␈↓ α←␈↓S-expressions␈αsince␈α␈↓¬most␈↓␈αLISP␈αexpressions␈αdenote␈αS-exprs;␈αsince␈αmany␈αof␈αour
␈↓ α←␈↓LISP␈α∪functions␈α∪are␈α∪partial␈α∪functions,␈α∀it␈α∪is␈α∪convenient␈α∪to␈α∪talk␈α∀about␈α∪the
␈↓ α←␈↓undefined␈α⊂value,␈α⊃␈↓λB␈↓.␈α⊂We␈α⊂wish␈α⊃to␈α⊂extend␈α⊂our␈α⊃partial␈α⊂functions␈α⊂to␈α⊃be␈α⊂␈↓↓total␈↓
␈↓ α←␈↓functions␈α⊂on␈α⊂an␈α∂extended␈α⊂domain.␈α⊂ As␈α⊂before␈α∂(page 12),␈α⊂we␈α⊂shall␈α⊂call␈α∂this
␈↓ α←␈↓extended domain ␈↓S␈↓.
␈↓"∀␈↓ α←␈↓␈↓ ¬Q␈↓S␈↓ = ␈↓<sexpr>␈↓ ∪ {␈↓λB␈↓}
␈↓"∀␈↓ α←␈↓␈↓ β'Before␈αwe␈αcan␈αdiscuss␈αthe␈αproperties␈αof␈αmathematical␈αfunctions␈αdenoted
␈↓ α←␈↓by␈α∀LISP␈α∀functions,␈α∃we␈α∀must␈α∀give␈α∀more␈α∃careful␈α∀study␈α∀to␈α∀the␈α∃nature␈α∀of
␈↓ α←␈↓domains.␈α
Our␈α
primitive␈α
domain␈α∞is␈α
␈↓<atom>␈↓.␈α
Its␈α
intuitive␈α
structure␈α∞is␈α
quite
␈↓ α←␈↓simple,␈αbasically␈αjust␈α
a␈αset␈αof␈αatoms␈α
or␈αnames␈αwith␈αno␈α
inherent␈αrelationships
␈↓ α←␈↓among␈α
them.␈α
Another␈α
primitive␈α
domain␈α
is␈α
␈↓Tr␈↓,␈α
the␈α
domain␈α
of␈α
truth␈α
values.
␈↓ α←␈↓The␈α∂domain␈α∂␈↓<sexpr>␈↓␈α⊂is␈α∂more␈α∂complex;␈α⊂it␈α∂is␈α∂a␈α⊂set␈α∂of␈α∂elements,␈α⊂but␈α∂many
␈↓ α←␈↓elements␈α∂are␈α∂related.␈α∂In␈α∂our␈α∂discussion␈α∂of␈α∂␈↓<sexpr>␈↓␈α∂on␈α∂page 6␈α∂we␈α∂made␈α∞it
␈↓ α←␈↓clear␈αthat␈αthere␈αis␈αmore␈αthan␈αsyntax␈αinvolved.␈α We␈αcould␈αsay␈αthat␈αfor␈α␈↓s␈↓β1␈↓␈αand
␈↓ α←␈↓␈↓s␈↓β2␈↓␈αin␈α
␈↓<sexpr>␈↓␈αthe␈α
essence␈αof␈α"dotted␈α
pair"␈αis␈α
contained␈αin␈α
the␈αconcept␈αof␈α
the
␈↓ α←␈↓set-theoretic␈α∞ordered␈α
pair,␈α∞<␈↓s␈↓β1␈↓,␈↓s␈↓β2␈↓>.␈α∞Thus␈α
the␈α∞"meaning"␈α∞of␈α
the␈α∞set␈α∞of␈α
dotted
␈↓ α←␈↓pairs is captured by Cartesian product, ␈↓<sexpr> x <sexpr>␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Let's continue the analysis of:
␈↓"∀␈↓ α←␈↓␈↓ ∧S<sexpr> ::= <atom> | (<sexpr> . <sexpr>)
␈↓"∀␈↓ α←␈↓We␈αare␈αtrying␈α
to␈αinterpret␈αthis␈α
BNF␈αequation␈αas␈α
a␈αdefinition␈αof␈α
the␈αdomain
␈↓ α←␈↓␈↓<sexpr>␈↓.␈α∂Reasonable␈α∂interpretations␈α∂of␈α∂"::="␈α∞and␈α∂"|"␈α∂appear␈α∂to␈α∂be␈α∞equality
␈↓ α←␈↓and set-theoretic union, respectively. This results in the equation:
␈↓"∀␈↓ α←␈↓␈↓ ∧!<sexpr> = <atom> ␈↓∪ (␈↓<sexpr> x <sexpr>␈↓)
␈↓"∀␈↓ α←␈↓This␈α∂looks␈α∂like␈α∂an␈α∂algebraic␈α∂equation,␈α∂and␈α∂as␈α∂such,␈α∂may␈α∂or␈α∂may␈α∂not␈α∞have
␈↓ α←␈↓solutions.␈α∂ This␈α∞particular␈α∂"domain␈α∂equation"␈α∞has␈α∂at␈α∞least␈α∂one␈α∂solution:␈α∞the
␈↓ α←␈↓S-exprs.␈α
There␈α
is␈αa␈α
natural␈α
mapping␈α
of␈αBNF␈α
equations␈α
onto␈α
such␈α"domain
␈↓ α←␈↓equations",␈α
and␈αthe␈α
solutions␈αto␈α
the␈αdomain␈α
equations␈αare␈α
sets␈α
satisfying␈αthe
␈↓ α←␈↓abstract␈α
essence␈α
of␈α∞the␈α
BNF.␈α
The␈α
mapping␈α∞process␈α
is␈α
also␈α
applicable␈α∞to␈α
the
␈↓ α←␈↓language␈α∂constructs.␈α∂ Consider␈α∂the␈α∞BNF␈α∂equations␈α∂for␈α∂a␈α∂simple␈α∞applicative
␈↓ α←␈↓subset of LISP:
␈↓"∀␈↓ α←␈↓␈↓ β→<form> ::= <variable> | ␈↓αλ␈↓[[<variable>] <form>] | <variable>[<form>]
␈↓"∀␈↓ α←␈↓We␈α∩would␈α∩like␈α∩to␈α∩describe␈α∩the␈α∩denotations␈α∩of␈α∩these␈α∩equations␈α∩in␈α∩a␈α∩style
␈↓ α←␈↓␈↓3.13␈↓ πfReview and Reflection 177␈↓
␈↓"β␈↓ α←␈↓similar␈α∀to␈α∪that␈α∀used␈α∪for␈α∀<sexpr>'s.␈α∪ The␈α∀denotations␈α∪of␈α∀the␈α∪expressions,
␈↓ α←␈↓<form>,␈αof␈αapplications,␈α<variable>[form>],␈αand␈αof␈αthe␈αvariables,␈α
<variables>,
␈↓ α←␈↓are␈α⊃just␈α⊃elements␈α⊂of␈α⊃␈↓S␈↓.␈α⊃ Expressions␈α⊂of␈α⊃the␈α⊃form␈α⊂"␈↓αλ␈↓[[<variable>] <form>]"
␈↓ α←␈↓denote␈α⊂functions␈α⊃from␈α⊂␈↓S␈↓␈α⊃to␈α⊂␈↓S␈↓.␈α⊃Write␈α⊂that␈α⊃set␈α⊂as␈α⊃␈↓S→S␈↓.␈α⊂Then␈α⊃our␈α⊂domain
␈↓ α←␈↓equation is expressed:
␈↓"∀␈↓ α←␈↓␈↓ ¬uS = ␈↓(␈↓S→S␈↓) ∪ ␈↓S␈↓
␈↓"∀␈↓ α←␈↓This␈αequation␈αhas␈αno␈α␈↓¬interesting␈↓␈αsolutions.␈αA␈αsimple␈αcounting␈αargument␈αwill
␈↓ α←␈↓establish␈α∪that␈α∪unless␈α∪a␈α∪domain␈α∪␈↓C␈↓␈α∪consists␈α∪of␈α∪a␈α∪single␈α∪element,␈α∪then␈α∩the
␈↓ α←␈↓number␈α∞of␈α∞functions␈α∞in␈α
␈↓C→C␈↓␈α∞is␈α∞greater␈α∞than␈α
the␈α∞number␈α∞of␈α∞elements␈α∞in␈α
␈↓C␈↓.
␈↓ α←␈↓This␈αdoes␈α␈↓¬not␈↓␈αsay␈αthat␈αthere␈αare␈αno␈αmodels␈αfor␈αthis␈αLISP␈αsubset;␈αit␈αsays␈αthat
␈↓ α←␈↓our interpretation of "␈↓→␈↓" is too broad.
␈↓"β␈↓ α←␈↓␈↓ β'What␈αis␈αneeded␈αis␈αan␈αinterpretation␈αof␈αfunctionality␈αwhich␈αwill␈αallow␈αa
␈↓ α←␈↓solution␈α≠to␈α≠the␈α≠above␈α≤domain␈α≠equation;␈α≠it␈α≠should␈α≠allow␈α≤a␈α≠natural
␈↓ α←␈↓interpretation␈α
such␈α
that␈α
the␈α
properties␈α
which␈α
we␈α
expect␈α
functions␈α∞to␈α
possess
␈↓ α←␈↓are␈α∪true␈α∪in␈α∪the␈α∪model.␈α∪ D. Scott␈α∪gave␈α∪one␈α∪interpretation␈α∪of␈α∪"␈↓→␈↓"␈α∪for␈α∪the
␈↓ α←␈↓␈↓λλ␈↓-calculus,␈α⊂defining␈α⊂the␈α⊂class␈α⊂of␈α⊂"continuous␈α⊂functions"␈α⊂([Sco 70],␈α⊂[Sco 73]).
␈↓ α←␈↓This␈α∞class␈α∞of␈α∞functions␈α∞is␈α∞restricted␈α∞enough␈α∞to␈α∞satisfy␈α∞the␈α∂domain␈α∞equation,
␈↓ α←␈↓but␈α⊃broad␈α⊃enough␈α⊃to␈α⊃act␈α∩as␈α⊃the␈α⊃denotations␈α⊃of␈α⊃procedures␈α∩in␈α⊃applicative
␈↓ α←␈↓programming␈αlanguages.␈α We␈αwill␈αuse␈α
the␈αnotation␈α"␈↓[␈↓D␈↓β1␈↓ ␈↓→␈↓ D␈↓β2␈↓]␈↓"␈αto␈αmean␈α
"the
␈↓ α←␈↓set␈α∪of␈α∀continuous␈α∪functions␈α∀from␈α∪domain␈α∪D␈↓β1␈↓␈α∀to␈α∪domain␈α∀D␈↓β2␈↓".␈α∪ It␈α∀is␈α∪the
␈↓ α←␈↓continuous␈αfunctions␈αwhich␈αfirst␈α
supplied␈αa␈αmodel␈αfor␈α
the␈α␈↓λλ␈↓-calculus␈αand␈αit␈α
is
␈↓ α←␈↓these␈αfunctions␈αwhich␈α
supply␈αa␈αbasis␈αfor␈α
a␈αmathematical␈αmodel␈αof␈α
applicative
␈↓ α←␈↓LISP ([Gor 73]).
␈↓"β␈↓ α←␈↓␈↓ β'We␈α⊃can␈α⊃assume␈α⊃that␈α⊂the␈α⊃LISP␈α⊃primitives␈α⊃denote␈α⊃specific␈α⊂continuous
␈↓ α←␈↓functions.␈α∞For␈α∞example,␈α∞the␈α∂mathematical␈α∞counterpart␈α∞to␈α∞the␈α∂LISP␈α∞function
␈↓ α←␈↓␈↓αcar␈↓ is the mapping ␈↓car␈↓ from ␈↓S␈↓ to ␈↓S␈↓ defined as follows:
␈↓"∀␈↓ α←␈↓␈↓ βK␈↓car: [S → S]␈↓
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ ∧Cis ␈↓λB␈↓ if ␈↓t␈↓ is atomic
␈↓"β␈↓ α←␈↓␈↓ βK␈↓car(t)␈↓ ∧C␈↓is ␈↓t␈↓β1␈↓ if ␈↓t␈↓ is ␈↓<t␈↓β1␈↓ , t␈↓β2␈↓>
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ ∧C␈↓is ␈↓λB␈↓ if ␈↓t␈↓ is ␈↓λB␈↓
␈↓"∀␈↓ α←␈↓Similar␈α⊂strategy␈α⊃suffices␈α⊂to␈α⊃give␈α⊂denotations␈α⊂for␈α⊃the␈α⊂other␈α⊃primitive␈α⊂LISP
␈↓ α←␈↓functions and predicates. For example:
␈↓"∀␈↓ α←␈↓␈↓ βK␈↓atom: [S → S]␈↓
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ ∧Cis ␈↓
f␈↓ if ␈↓t␈↓ is not atomic
␈↓"β␈↓ α←␈↓␈↓ βK␈↓atom(t)␈↓ ∧C␈↓is ␈↓
t␈↓ if ␈↓t␈↓ is atomic
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ ∧Cis ␈↓λB␈↓ if ␈↓t␈↓ is ␈↓λB␈↓
␈↓"∀␈↓ α←␈↓Notice that these functions are strict: ␈↓f(␈↓λB␈↓) = ␈↓λB␈↓
␈↓ α←␈↓␈↓178 Evaluation␈↓
_3.13␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Corresponding␈α⊂to␈α⊂␈↓αtgmoaf␈↓,␈α⊃we␈α⊂will␈α⊂have␈α⊃a␈α⊂function,␈α⊂␈↓λD␈↓βtg␈↓,␈α⊃which␈α⊂maps
␈↓ α←␈↓expressions␈αonto␈αtheir␈αdenotations.␈α Since␈α␈↓λD␈↓βtg␈↓␈αis␈αanother␈αmapping␈αlike␈α␈↓λr␈↓,␈αwe
␈↓ α←␈↓will␈α∞use␈α∞the␈α∞"␈↓∞(␈↓"␈α∂and␈α∞"␈↓∞)␈↓"␈α∞brackets␈α∞to␈α∂enclose␈α∞LISP␈α∞constructs.␈α∞ We␈α∂need␈α∞to
␈↓ α←␈↓introduce some notation for elements of the sets <sexpr> and <form>.
␈↓"β␈↓ α←␈↓␈↓ β'Let␈α⊃␈↓∧s␈↓␈α⊃be␈α∩a␈α⊃meta-variable␈α⊃ranging␈α⊃over␈α∩<sexpr>␈α⊃and␈α⊃␈↓∧e␈↓␈α∩range␈α⊃over
␈↓ α←␈↓<form>, then we can write:
␈↓"∀␈↓ α←␈↓␈↓ ε∧␈↓λD␈↓βtg␈↓∞(␈↓∧s␈↓∞)␈↓ = ␈↓s␈↓
␈↓"∞␈↓ α←␈↓␈↓ ¬∪␈↓λD␈↓βtg␈↓∞(␈↓αcar[␈↓∧e␈↓α]␈↓∞)␈↓ = ␈↓car(␈↓λD␈↓βtg␈↓∞(␈↓∧e␈↓∞)␈↓)
␈↓"∀␈↓ α←␈↓with␈α∀similar␈α∃entries␈α∀for␈α∀␈↓αcdr,␈α∃cons,␈α∀eq,␈α∀␈↓and␈α∃␈↓αatom␈↓.␈α∀ The␈α∀structure␈α∃of␈α∀this
␈↓ α←␈↓definition is very similar to that of ␈↓αtgmoaf␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Now␈α∀we␈α∀continue␈α∀to␈α∃the␈α∀next␈α∀subset␈α∀of␈α∀LISP,␈α∃adding␈α∀conditional
␈↓ α←␈↓expressions.␈α
As␈α
we␈α∞noted␈α
on␈α
page 22,␈α∞a␈α
degree␈α
of␈α∞care␈α
need␈α
be␈α∞taken␈α
when
␈↓ α←␈↓we␈α∂attempt␈α∂to␈α∂interpret␈α∂conditional␈α∞expressions␈α∂in␈α∂terms␈α∂of␈α∂mappings.␈α∞ We
␈↓ α←␈↓can␈α⊃simplify␈α⊃the␈α⊃problem␈α⊃slightly:␈α⊃it␈α⊃is␈α⊃easy␈α⊃to␈α⊃show␈α⊃that␈α⊃the␈α⊂conditional
␈↓ α←␈↓expression␈α≠can␈α≠be␈α≠formulated␈α≠in␈α≠terms␈α≠of␈α≠the␈α≤simple␈α≠␈↓αif␈↓-expression:
␈↓ α←␈↓␈↓αif[␈↓↓p␈↓β1␈↓α;␈↓↓e␈↓β1␈↓α;␈↓↓e␈↓β2␈↓α]␈↓.␈α We␈α
will␈αdisplay␈αa␈α
denotation␈αfor␈α
such␈α␈↓αif␈↓␈αexpressions.␈α
It␈αwill␈αbe␈α
a
␈↓ α←␈↓mathematical␈α⊂function,␈α⊂and␈α⊂therefore␈α⊃the␈α⊂evaluation␈α⊂order␈α⊂will␈α⊃have␈α⊂been
␈↓ α←␈↓abstracted out.␈↓π 68␈↓
␈↓"β␈↓ α←␈↓Let ␈↓αif␈↓ denote ␈↓if␈↓ where:
␈↓"∀␈↓ α←␈↓␈↓ βK␈↓if: [TrxSxS → S]
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ ∧[␈↓is␈↓ y ␈↓if␈↓ x ␈↓is␈↓ ␈↓
t␈↓
␈↓"β␈↓ α←␈↓␈↓ βKif(x,y,z)␈↓ ∧[␈↓is ␈↓z␈↓, if ␈↓x␈↓ is ␈↓
f␈↓
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ ∧[is ␈↓λB␈↓, otherwise
␈↓"∀␈↓ α←␈↓This␈αinterpretation␈αof␈αconditional␈αexpressions␈αis␈αappropriate␈αfor␈αLISP;␈αother
␈↓ α←␈↓interpretations of conditionals are possible. For example:
␈↓"∀␈↓ α←␈↓␈↓ βK␈↓if␈↓β1␈↓: [TrxSxS → S]
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ ∧s␈↓is␈↓ y ␈↓if␈↓ x ␈↓is␈↓ ␈↓
t␈↓
␈↓"β␈↓ α←␈↓␈↓ βKif␈↓β1␈↓(x,y,z)␈↓ ∧s␈↓is ␈↓z␈↓, if ␈↓x␈↓ is ␈↓
f␈↓
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ ∧sis ␈↓λB␈↓ if ␈↓x␈↓ is ␈↓λB␈↓ and ␈↓y ≠ z␈↓
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ ∧sis ␈↓y␈↓ if ␈↓x␈↓ is ␈↓λB␈↓ and ␈↓y = z␈↓ ␈↓π 69␈↓
␈↓"∀␈↓ α←␈↓Neither ␈↓if␈↓ nor ␈↓if␈↓β1␈↓ are strict mappings.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 68␈↓Recall␈α⊃the␈α⊃comment␈α⊃of␈α∩Wadsworth␈α⊃(page 167).␈α⊃Indeed,␈α⊃the␈α∩use␈α⊃of
␈↓ α←␈↓conditional␈α→expressions␈α→in␈α~the␈α→more␈α→abstract␈α→representations␈α~of␈α→LISP
␈↓ α←␈↓functions␈αfrequently␈αis␈αsuch␈αthat␈α
exactly␈αone␈αof␈αthe␈α␈↓↓p␈↓βi␈↓'s␈α
is␈α␈↓
t␈↓␈αand␈αall␈αthe␈α
others
␈↓ α←␈↓are␈α␈↓
f␈↓.␈αThus␈αin␈αthis␈αsetting,␈αthe␈αorder␈αof␈αevaluation␈αof␈αthe␈αpredicates␈αis␈αuseful
␈↓ α←␈↓for␈α"efficiency"␈αbut␈αnot␈αnecessary␈αto␈αmaintain␈αthe␈αsense␈αof␈αthe␈αdefinition.␈αSee
␈↓ α←␈↓page 64.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 69␈↓Basing␈α⊂conditional␈α⊂expressions␈α⊃on␈α⊂␈↓if␈↓β1␈↓␈α⊂would␈α⊂give␈α⊃a␈α⊂value␈α⊂of␈α⊃␈↓α1␈↓␈α⊂to
␈↓ α←␈↓␈↓α[car[A] → 1; ␈↓
t␈↓α → 1]␈↓.
␈↓ α←␈↓␈↓3.13␈↓ πfReview and Reflection 179␈↓
␈↓"β␈↓ α←␈↓␈↓ β'To add ␈↓αif␈↓ expressions to ␈↓λD␈↓βtg␈↓, yielding ␈↓λD␈↓βtgr␈↓ we include:
␈↓"∀␈↓ α←␈↓␈↓ βc␈↓λD␈↓βtgr␈↓∞(␈↓αif[␈↓∧e␈↓β1␈↓α; ␈↓∧e␈↓β2␈↓α; ␈↓∧e␈↓β3␈↓α]␈↓∞)␈↓ = ␈↓if(␈↓λD␈↓βtgr␈↓∞(␈↓∧e␈↓β1␈↓∞)␈↓, ␈↓λD␈↓βtgr␈↓∞(␈↓∧e␈↓β2␈↓∞)␈↓, ␈↓λD␈↓βtgr␈↓∞(␈↓∧e␈↓β3␈↓∞)␈↓)␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α→next␈α→consideration␈α→is␈α→the␈α→denotational␈α→description␈α~of␈α→LISP
␈↓ α←␈↓identifiers.␈α∩ Identifiers␈α∩name␈α∩either␈α∩S-exprs␈α∩or␈α∩LISP␈α∩functions.␈α∩ Thus␈α⊃an
␈↓ α←␈↓identifier␈α∂denotes␈α∞either␈α∂an␈α∞object␈α∂on␈α∂our␈α∞domain␈α∂␈↓S␈↓␈α∞or␈α∂denotes␈α∂a␈α∞function
␈↓ α←␈↓object.␈α
Let␈α
␈↓Fn␈↓␈α
name␈α
the␈αset␈α
of␈α
continuous␈α
functions: ␈↓λS␈↓βn=1␈↓[S␈↓πn␈↓ → S]␈↓,␈α
and␈α␈↓Id␈↓␈α
be
␈↓ α←␈↓␈↓<identifier>␈↓∪␈↓λB␈↓.␈α
We␈αknow␈α
that␈α
the␈αvalue␈α
of␈αa␈α
LISP␈α
<identifier>␈α(page 17)
␈↓ α←␈↓depends␈α
on␈αthe␈α
current␈αenvironment.␈α
Then␈αwe␈α
might␈αcharacterize␈α
the␈α
set␈αof
␈↓ α←␈↓environments, ␈↓env␈↓, as:
␈↓"∀␈↓ α←␈↓␈↓ ¬⎇␈↓[Id␈↓ → ␈↓S␈↓∪␈↓Fn␈↓␈↓]␈↓
␈↓"∀␈↓ α←␈↓That␈α
is,␈α
an␈α
element␈α
of␈α
␈↓env␈↓␈α
is␈α
a␈α
continuous␈α
function␈α
which␈α
maps␈α
an␈α
identifier
␈↓ α←␈↓either␈α
onto␈α
a␈αS-expr␈α
or␈α
onto␈αan␈α
n-ary␈α
function␈αfrom␈α
S-exprs␈α
to␈αS-exprs.␈α
This
␈↓ α←␈↓is␈α
the␈α
essence␈α
of␈α
the␈α
argument␈α
used␈α
in␈α
introducing␈α
␈↓αassoc␈↓␈α
(Section 3.3).␈α Note
␈↓ α←␈↓that␈α≠␈↓αassoc[x;l] ␈↓λ≡␈↓ ␈↓αl[x]␈↓␈α≠is␈α≠another␈α≠instance␈α≠of␈α≠a␈α~operational-denotational
␈↓ α←␈↓relationship.
␈↓"β␈↓ α←␈↓␈↓ β'For␈αexample,␈αgiven␈αa␈αLISP␈αidentifier␈α␈↓αx␈↓␈αand␈αa␈αmember␈αof␈α␈↓env␈↓,␈αsay␈αthe
␈↓ α←␈↓function␈α
consisting␈α
of␈α
␈↓{<x,2>,<y,4>}␈↓,␈α
together␈α
with␈α
all␈α
pairs␈α
␈↓<␈↓↓z␈↓,␈↓λB␈↓>␈↓␈α
for␈αany␈α
␈↓↓z␈↓
␈↓ α←␈↓other␈αthan␈α␈↓x␈↓␈αor␈α␈↓y␈↓,␈αthen␈α␈↓λD␈↓␈αshould␈αmap␈α␈↓αx␈↓␈αonto␈α␈↓2␈↓.␈αThis␈αis␈αan␈αintuitive␈αway␈αof
␈↓ α←␈↓saying␈α∞that␈α∂␈↓λD␈↓␈α∞should␈α∂map␈α∞a␈α∞member␈α∂of␈α∞<identifier>␈α∂onto␈α∞a␈α∂␈↓¬function␈↓.␈α∞This
␈↓ α←␈↓function␈αwill␈αmap␈αa␈αmember␈αof␈α␈↓env␈↓␈αonto␈αan␈αelement␈αof␈α␈↓S␈↓.␈α Introducing␈α␈↓∧i␈↓␈αas
␈↓ α←␈↓a meta-variable to range over <identifier>, then for ␈↓l ␈↓λε␈↓ env␈↓ we have:
␈↓"∀␈↓ α←␈↓␈↓ ¬p␈↓λD␈↓∞(␈↓∧i␈↓∞)␈↓(l)␈↓ = ␈↓l(i)␈↓
␈↓"∀␈↓ α←␈↓The␈α∪␈↓¬denotation␈↓␈α∪of␈α∀an␈α∪identifier␈α∪is␈α∪a␈α∀function;␈α∪whereas␈α∪the␈α∪␈↓¬value␈↓␈α∀of␈α∪an
␈↓ α←␈↓identifier is an element of ␈↓S␈↓∪␈↓Fn␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αtreatment␈αof␈αidentifiers␈αleads␈α
directly␈αinto␈αthe␈αdenotional␈αaspects␈α
of
␈↓ α←␈↓function␈α⊃application.␈α⊃ We␈α∩shall␈α⊃maintain␈α⊃the␈α⊃parallels␈α∩between␈α⊃evaluation
␈↓ α←␈↓and␈α
denotation,␈α
by␈αgiving␈α
␈↓λD␈↓βe␈↓␈α
and␈α␈↓λD␈↓βa␈↓␈α
corresponding␈α
to␈α␈↓αeval␈↓␈α
and␈α
␈↓αapply␈↓.␈α Let
␈↓ α←␈↓␈↓∧f␈↓␈α∞be␈α∂a␈α∞member␈α∂of␈α∞<function>␈α∞and␈α∂␈↓∧e␈↓␈α∞be␈α∂a␈α∞member␈α∞of␈α∂<form>,␈α∞then␈α∂for␈α∞a
␈↓ α←␈↓given␈α∞element␈α
of␈α∞␈↓env␈↓,␈α∞␈↓λD␈↓βa␈↓␈α
maps␈α∞␈↓∧f␈↓␈α
onto␈α∞an␈α∞element␈α
of␈α∞␈↓Fn␈↓,␈α
and␈α∞␈↓λD␈↓βe␈↓␈α∞maps␈α
␈↓∧e␈↓
␈↓ α←␈↓onto an element of ␈↓S␈↓.
␈↓"β␈↓ α←␈↓For example: ␈↓ ∧v␈↓λD␈↓βa␈↓∞(␈↓αcar␈↓∞)␈↓(l)␈↓ = ␈↓car␈↓ for all ␈↓l␈↓ in ␈↓env␈↓.
␈↓" ␈↓ α←␈↓Similar␈α
equations␈α
hold␈α
for␈α
the␈α
other␈α
LISP␈α
primitive␈α
functions␈α
and␈α
predicates.
␈↓ α←␈↓In general, then:
␈↓"∀␈↓ α←␈↓␈↓ ¬j␈↓λD␈↓βa␈↓∞(␈↓∧f␈↓∞)␈↓(l)␈↓ = ␈↓l(f)␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'To␈αdescribe␈α
the␈αevaluation␈α
of␈αa␈αfunction-call␈α
in␈αLISP␈α
we␈αmust␈α
add␈αan
␈↓ α←␈↓equation to ␈↓λD␈↓βe␈↓:
␈↓"∀␈↓ α←␈↓␈↓λD␈↓βe␈↓∞(␈↓∧f␈↓[␈↓∧e␈↓β1␈↓, ..., ␈↓∧e␈↓βn␈↓]␈↓∞)␈↓(l)␈↓ = ␈↓λD␈↓βa␈↓∞(␈↓∧f␈↓∞)␈↓(l)(␈↓λD␈↓βe␈↓∞(␈↓∧e␈↓β1␈↓∞)␈↓(l)␈↓, ..., ␈↓λD␈↓βe␈↓∞(␈↓∧e␈↓βn␈↓∞)␈↓(l))␈↓
␈↓"∀␈↓ α←␈↓We␈αmust␈α
also␈αmake␈α
consistent␈αmodifications␈αto␈α
the␈αprevious␈α
clauses␈αof␈α␈↓λD␈↓βtgr␈↓␈α
to
␈↓ α←␈↓account␈αfor␈αenvironments.␈α That␈αis,␈αthe␈αvalue␈αof␈αa␈αconstant␈αis␈αindependent␈α
of
␈↓ α←␈↓the specific environment in which it is evaluated.
␈↓ α←␈↓␈↓180 Evaluation␈↓
_3.13␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬␈↓λD␈↓βe␈↓∞(␈↓∧s␈↓∞)␈↓(l)␈↓ = ␈↓s␈↓ for all ␈↓l␈↓ in ␈↓env␈↓.
␈↓"∀␈↓ α←␈↓We must also extend our equations to account for conditional expressions.
␈↓"β␈↓ α←␈↓␈↓ β'To␈α≤discuss␈α≤function␈α≤application␈α≤we␈α≤must␈α≤give␈α≥a␈α≤mathematical
␈↓ α←␈↓characterization␈α∃of␈α∃function␈α∃definitions.␈α∃ In␈α∃this␈α∃section␈α∃we␈α∃will␈α∀handle
␈↓ α←␈↓λ-notation␈α~without␈α~free␈α~variables,␈α~postponing␈α~more␈α~complex␈α≠cases␈α~to
␈↓ α←␈↓Section 4.11.
␈↓"β␈↓ α←␈↓␈↓ β'Assuming␈α
the␈α
only␈αfree␈α
variables␈α
in␈α
␈↓λx␈↓␈αare␈α
among␈α
the␈α
␈↓αx␈↓βi␈↓'s,␈αthe␈α
denotation
␈↓ α←␈↓of␈α
␈↓αλ[[x␈↓β1␈↓α; ...; x␈↓βn␈↓α]␈α
␈↓λx␈↓]␈α
in␈α
a␈α
specified␈αenvironment␈α
should␈α
be␈α
a␈α
function␈α
from␈α␈↓S␈↓πn␈↓␈α
to
␈↓ α←␈↓␈↓S␈↓ such that:
␈↓"∀␈↓ α←␈↓␈↓ β∂␈↓λD␈↓βa␈↓∞(␈↓αλ[[␈↓∧v␈↓β1␈↓; ... ;␈↓∧v␈↓βn␈↓] ␈↓∧e␈↓]␈↓∞)␈↓(l)␈↓ =
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ ∧␈␈↓␈↓λλ␈↓((x␈↓β1␈↓, ..., x␈↓βn␈↓) ␈↓λD␈↓βe␈↓∞(␈↓∧e␈↓∞)␈↓(l : <x␈↓β1␈↓,v␈↓β1␈↓>, ..., <x␈↓βn␈↓,v␈↓βn␈↓>))␈↓
␈↓"∀␈↓ α←␈↓where␈αλ␈αis␈αthe␈αLISP␈αλ-notation␈αand␈α␈↓λλ␈↓␈αis␈αits␈αmathematical␈αcounterpart␈αand␈α␈↓v␈↓βi␈↓
␈↓ α←␈↓is␈αthe␈αdenotational␈αcounterpart␈αof␈α␈↓∧v␈↓βi␈↓,␈αand␈α␈↓(l : ... )␈↓␈αmeans␈αa␈αnew␈αenvironment
␈↓ α←␈↓which coincides with ␈↓l␈↓ except for the explicitly given pairs.
␈↓"β␈↓ α←␈↓␈↓ β'That␈αis,␈α␈↓(l : <x␈↓β1␈↓,v␈↓β1␈↓>, ..., <x␈↓βn␈↓,v␈↓βn␈↓>)␈↓␈α
is␈αa␈αvariant␈α
of␈α␈↓l␈↓␈αsuch␈α
that␈αeach␈α␈↓v␈↓βi␈↓␈α
is
␈↓ α←␈↓bound to the corresponding ␈↓x␈↓βi␈↓:
␈↓"∀␈↓ α←␈↓␈↓ βK␈↓(l : <x,v>)(v␈↓β1␈↓)␈↓ is:␈↓ ¬;␈↓if(␈↓ ¬←v = ␈↓λB␈↓,
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ ¬;␈↓ ¬←␈↓λB␈↓,
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ ¬;␈↓ ¬←if(␈↓ εβv␈↓β1␈↓ = ␈↓λB␈↓↓,
␈↓"β␈↓ α←␈↓↓␈↓ βK␈↓ ¬;␈↓ ¬←␈↓ εβ␈↓λB␈↓,
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ ¬;␈↓ ¬←␈↓ εβif(␈↓ ε'v␈↓β1␈↓ = x,
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ ¬;␈↓ ¬←␈↓ εβ␈↓ ε'v,
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ ¬;␈↓ ¬←␈↓ εβ␈↓ ε'l(v␈↓β1␈↓))))
␈↓"∀␈↓ α←␈↓In␈α∞more␈α
detail:␈α∞␈↓λλ␈↓((x␈↓β1␈↓,␈α
...␈α∞,x␈↓βn␈↓)␈α
e(x␈↓β1␈↓,␈α∞...␈α
,x␈↓βn␈↓))␈α∞␈↓is␈α
a␈α∞function␈α
␈↓f␈↓:␈α∞␈↓[S␈↓πn␈↓␈α
→␈α∞␈↓S]␈↓␈α
such
␈↓ α←␈↓that:
␈↓"∀␈↓ α←␈↓␈↓ ∧πis ␈↓e(t␈↓β1␈↓, ... ,t␈↓βn␈↓) ␈↓if m␈↓λ≥␈↓n and for every i, ␈↓t␈↓βi␈↓ ␈↓λ≠␈↓ ␈↓λB␈↓ ␈↓π 70␈↓
␈↓"β␈↓ α←␈↓␈↓f(t␈↓β1␈↓, ..., t␈↓βm␈↓) ␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧πis ␈↓λB␈↓ otherwise
␈↓"∀␈↓ α←␈↓Given␈α∞this␈α
basic␈α∞outline,␈α∞we␈α
can␈α∞more␈α∞accurately␈α
describe␈α∞the␈α∞"equation"␈α
of
␈↓ α←␈↓page 175:
␈↓"∀␈↓ α←␈↓α␈↓ ¬f[a␈↓β1␈↓α; ...; a␈↓βn␈↓α] ␈↓λ≡␈↓α eval[(F A␈↓β1␈↓α ... A␈↓βn␈↓α)],
␈↓"∀␈↓ α←␈↓␈↓ β'Namely;
␈↓"∀␈↓ α←␈↓␈↓ β∂␈↓λD␈↓βe␈↓∞(␈↓αeval[␈↓
R␈↓∞(␈↓∧f␈↓α[␈↓∧e␈↓β1␈↓α; ... ␈↓∧e␈↓βn␈↓α]␈↓∞)␈↓α;␈↓
R␈↓∞(␈↓∧a␈↓∞)␈↓α]␈↓∞)␈↓(l␈↓βinit␈↓)␈↓ =
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ ∧␈␈↓λD␈↓βa␈↓∞(␈↓∧f␈↓∞)␈↓(l␈↓βnew␈↓)(␈↓λD␈↓βe␈↓∞(␈↓∧e␈↓β1␈↓∞)␈↓(l␈↓βnew␈↓), ..., ␈↓λD␈↓βe␈↓∞(␈↓∧e␈↓βn␈↓∞)␈↓(l␈↓βnew␈↓))␈↓
␈↓"∀␈↓ α←␈↓where␈α⊃␈↓l␈↓βinit␈↓␈α⊂is␈α⊃the␈α⊃initial␈α⊂symbol␈α⊃table␈α⊂and␈α⊃␈↓l␈↓βnew␈↓␈α⊃is␈α⊂␈↓l␈↓βinit␈↓␈α⊃augmented␈α⊃with␈α⊂the
␈↓ α←␈↓entires from ␈↓∧a␈↓.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 70␈↓Note␈αthat␈α
this␈αequation␈αmodels␈α
the␈αLISP␈αtrick␈α
of␈αsupplying␈αtoo␈α
many
␈↓ α←␈↓arguments.␈α_Other␈α↔anomalies␈α_of␈α↔LISP,␈α_including␈α↔dynamic␈α_binding,␈α↔are
␈↓ α←␈↓describable using these techniques ([Gor 73], [Gor 75]).
␈↓ α←␈↓␈↓3.13␈↓ πfReview and Reflection 181␈↓
␈↓"β␈↓ α←␈↓␈↓ β'One␈α∀of␈α∃the␈α∀major␈α∀difficulties␈α∃in␈α∀supplying␈α∀models␈α∃for␈α∀applicative
␈↓ α←␈↓languages␈α∂is␈α∂caused␈α∂by␈α∞the␈α∂type-free␈α∂requirement.␈↓π 71␈↓␈α∂Self-application␈α∂is␈α∞one
␈↓ α←␈↓indication␈α∪of␈α∩this.␈α∪We␈α∩can␈α∪show␈α∪that␈α∩imposing␈α∪a␈α∩type␈α∪structure␈α∪on␈α∩our
␈↓ α←␈↓language␈αwill␈α
solve␈αmany␈αproblems.␈α
In␈αa␈αtyped␈α
␈↓λλ␈↓-calculus␈αa␈αterm␈α
will␈αalways
␈↓ α←␈↓have␈α∩a␈α∩normal␈α∩form ([Mor 68]).␈α∪Computationally␈α∩this␈α∩means␈α∩that␈α∪all␈α∩the
␈↓ α←␈↓programs␈α∞will␈α∞terminate.␈α∂ Also,␈α∞models␈α∞for␈α∂typed␈α∞␈↓λλ␈↓-calculus␈α∞are␈α∂much␈α∞more
␈↓ α←␈↓readily␈α∪attained ([Mil 73]).␈α∪ However␈α∪the␈α∩type␈α∪free␈α∪calculus␈α∪is␈α∪a␈α∩stronger
␈↓ α←␈↓system;␈α∪requiring␈α∪all␈α∪terms␈α∪to␈α∩have␈α∪a␈α∪consistent␈α∪type␈α∪structure␈α∪rules␈α∩out
␈↓ α←␈↓several␈αuseful␈αconstructs;␈α
in␈αparticular,␈αthe␈α
␈↓λλ␈↓-calculus␈αcounterpart␈αto␈αthe␈α
LISP
␈↓ α←␈↓␈↓αlabel␈↓ operator cannot be consistently typed.
␈↓"β␈↓ α←␈↓␈↓ β'From␈α_the␈α_practical␈α→side,␈α_a␈α_typed␈α→structure␈α_is␈α_a␈α→mixed␈α_blessing.
␈↓ α←␈↓Language␈α∩delarations␈α⊃are␈α∩a␈α⊃form␈α∩of␈α⊃typing␈α∩and␈α⊃can␈α∩be␈α⊃quite␈α∩helpful␈α⊃in
␈↓ α←␈↓pinpointing␈αprogramming␈αerrors.␈αDeclarations␈αcan␈αalso␈αbe␈αused␈α
by␈αcompilers
␈↓ α←␈↓to␈α⊂help␈α⊃produce␈α⊂optimized␈α⊃code.␈α⊂However,␈α⊂a␈α⊃type␈α⊂structure␈α⊃can␈α⊂be␈α⊃a␈α⊂real
␈↓ α←␈↓nuisance␈α∩when␈α∩trying␈α∩to␈α∩debug␈α∩a␈α∩program.␈α∩ It␈α∩is␈α∩frequently␈α∩desirable␈α∩to
␈↓ α←␈↓examine␈α∂and␈α∂modify␈α∂the␈α⊂representations␈α∂of␈α∂abstract␈α∂data␈α⊂structures.␈α∂Those
␈↓ α←␈↓kinds of operations imply the ability to ignore the type information.
␈↓"β␈↓ α←␈↓␈↓ β'As␈α⊃a␈α⊂final␈α⊃bridge␈α⊂between␈α⊃theory␈α⊂and␈α⊃practice␈α⊂we␈α⊃will␈α⊂use␈α⊃LISP␈α⊂to
␈↓ α←␈↓introduce␈α
one␈αof␈α
the␈αfundamental␈α
results␈αin␈α
recursion␈αtheory:␈α
a␈αproof␈α
of␈αthe
␈↓ α←␈↓non-existence␈α
of␈α
an␈α
algorithm␈α
to␈α
determine␈α
whether␈α
or␈α
not␈α
a␈α
LISP␈α
function␈α
is
␈↓ α←␈↓a␈αtotal␈αfunction.␈α This␈αis␈αalso␈αcalled␈αthe␈αunsolvability␈αof␈αthe␈αhalting␈αproblem,
␈↓ α←␈↓since␈α⊃the␈α∩existence␈α⊃of␈α∩such␈α⊃an␈α⊃algorithm␈α∩would␈α⊃tells␈α∩us␈α⊃whether␈α∩a␈α⊃LISP
␈↓ α←␈↓function would terminate for all inputs.␈↓π 72␈↓ That algorithm does not exist.␈↓π 73␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∪proof␈α∪depends␈α∪on␈α∪our␈α∪knowledge␈α∪of␈α∪the␈α∪function␈α∪␈↓αapply␈↓.␈α∪The
␈↓ α←␈↓fundamental relationship is:
␈↓"∀␈↓ α←␈↓␈↓ β'For a function ␈↓αf␈↓ and arguments ␈↓αa␈↓β1␈↓, ... ,␈↓αa␈↓βn␈↓ we know
␈↓" ␈↓ α←␈↓␈↓ β'that if ␈↓αf[a␈↓β1␈↓α; ... ;a␈↓βn␈↓α]␈↓ is defined in ␈↓αenv␈↓
␈↓" ␈↓ α←␈↓␈↓ β'then ␈↓αf[a␈↓β1␈↓α; ...;a␈↓βn␈↓α]␈↓ = ␈↓αapply[␈↓
R␈↓∞(␈↓αf␈↓∞)␈↓α;list[␈↓
R␈↓∞(␈↓αa␈↓β1␈↓∞)␈↓α; ... ;␈↓
R␈↓∞(␈↓αa␈↓βn␈↓∞)␈↓α];env]␈↓
␈↓"∀␈↓ α←␈↓Compare␈α⊂this␈α⊂equation␈α⊃with␈α⊂the␈α⊂equation␈α⊃on␈α⊂page 180.␈α⊂ This␈α⊃property␈α⊂of
␈↓ α←␈↓␈↓αapply␈↓␈α
makes␈α
it␈α
a␈α
␈↓↓universal␈α
function␈↓␈α∞for␈α
LISP␈α
in␈α
the␈α
sense␈α
that␈α
if␈α∞␈↓αapply␈↓␈α
is
␈↓ α←␈↓given␈α∞an␈α
encoding␈α∞of␈α∞a␈α
function,␈α∞of␈α
some␈α∞arguments␈α∞to␈α
be␈α∞applied,␈α∞and␈α
an
␈↓ α←␈↓environment␈α⊗which␈α⊗contains␈α⊗the␈α⊗definition␈α⊗of␈α⊗␈↓αf␈↓␈α⊗and␈α⊗all␈α⊗the␈α⊗necessary
␈↓ α←␈↓subsidiary definitions needed by ␈↓αf␈↓, then ␈↓αapply␈↓ can simulate the behavior of ␈↓αf␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α
will␈α
assume␈α
that␈αthe␈α
representation␈α
of␈α
␈↓αenv␈↓␈αis␈α
the␈α
standard␈α
a-list␈αof
␈↓ α←␈↓dotted␈α≡pairs:␈α≥representation␈α≡of␈α≥name␈α≡dotted␈α≥with␈α≡representation␈α≥of
␈↓ α←␈↓λ-expression.␈α
Given␈α
a␈α
function␈α
named␈α
␈↓αg␈↓,␈α
together␈α
with␈α
its␈α
λ-definition␈αwe␈α
will
␈↓ α←␈↓designate the S-expr representation of the dotted pair as ␈↓αg␈↓πR␈↓.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 71␈↓It␈αwas␈αnot␈αuntil␈α1969␈αthat␈αa␈αmodel␈αfor␈αthe␈α␈↓λλ␈↓-calculus␈αwas␈αdiscovered
␈↓ α←␈↓by D. Scott even though the formalism was invented in the late 1930's.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 73␈↓Again,␈α⊂we␈α⊂use␈α⊂"LISP␈α⊃function"␈α⊂as␈α⊂a␈α⊂synonym␈α⊂for␈α⊃"algorithm".␈α⊂To
␈↓ α←␈↓complete␈αthe␈αhalting␈α
argument␈αwe␈αwould␈αhave␈α
to␈αshow␈αthat␈α
every␈αalgorithm
␈↓ α←␈↓is expressible in LISP.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 73␈↓The argument is adapted from [Lev un].
␈↓ α←␈↓␈↓182 Evaluation␈↓
_3.13␈↓
␈↓"β␈↓ α←␈↓␈↓ β'For example, given
␈↓"∀␈↓ α←␈↓␈↓ ∧G␈↓αfact <= λ[[x][x = 0 → 1; ␈↓
t␈↓α → *[x;fact[x-1]]]]␈↓
␈↓"∀␈↓ α←␈↓Then ␈↓αfact␈↓πR␈↓ is:
␈↓"∀␈↓ α←␈↓α(FACT . (LAMBDA␈↓ ∧s(X)
␈↓"β␈↓ α←␈↓α␈↓ ∧s(COND␈↓ ¬k((ZEROP X) 1)
␈↓"β␈↓ α←␈↓α␈↓ ∧s␈↓ ¬k(T (TIMES␈↓ π∪X
␈↓"β␈↓ α←␈↓α␈↓ ∧s␈↓ ¬k␈↓ π∪(FACT (SUB1 X)))))))␈↓
␈↓"∀␈↓ α←␈↓Next,␈α⊂if␈α⊂␈↓αf␈↓␈α⊂refers␈α⊂to␈α⊂␈↓αf␈↓β1␈↓␈α⊃through␈α⊂␈↓αf␈↓βn␈↓␈α⊂in␈α⊂its␈α⊂evaluation,␈α⊂we␈α⊂will␈α⊃represent␈α⊂the
␈↓ α←␈↓environment as:
␈↓"∀␈↓ α←␈↓α␈↓ ¬llist[f␈↓πR␈↓α;f␈↓β1␈↓πR␈↓α;... ;f␈↓βn␈↓πR␈↓α]
␈↓"∀␈↓ α←␈↓Finally,␈αwe␈αwill␈αidentify␈αsuch␈αan␈αenvironment␈αstructure␈αas␈α
the␈αrepresentation
␈↓ α←␈↓of␈α∞the␈α∞definition␈α∞of␈α∞the␈α∞␈↓¬first␈↓␈α∞function␈α∞in␈α∞that␈α∞environment.␈α∞For␈α∂example,␈α∞a
␈↓ α←␈↓complete␈α∂definition␈α∂of␈α∞␈↓αfact␈↓␈α∂would␈α∂be␈α∞an␈α∂environment␈α∂beginning␈α∂with␈α∞␈↓αfact␈↓πR␈↓
␈↓ α←␈↓and␈α∞followed␈α∞by␈α∞␈↓αzerop␈↓πR␈↓,␈α∞␈↓αtimes␈↓πR␈↓,␈α∞or␈α∞␈↓αsub1␈↓πR␈↓␈α∞if␈α∞any␈α∞of␈α∞these␈α∞functions␈α∂were␈α∞not
␈↓ α←␈↓considered primitive.
␈↓"β␈↓ α←␈↓␈↓ β'Now assume the existence of a unary predicate ␈↓αtotal␈↓ such that:
␈↓"∀␈↓ α←␈↓␈↓ βKgives ␈↓
t␈↓ if ␈↓αx␈↓ is a representation of a total unary function.␈↓π 74␈↓
␈↓"β␈↓ α←␈↓␈↓αtotal[x]␈↓
␈↓"β␈↓ α←␈↓␈↓ βKgives ␈↓
f␈↓ in all other cases
␈↓"∀␈↓ α←␈↓Notice␈α
that␈α
if␈α
␈↓αtotal[list[f␈↓πR␈↓α; ...]]␈↓␈α
is␈α
true,␈αthen␈α
for␈α
arbitrary␈α
␈↓αa␈↓␈α
the␈α
evaluation␈αof
␈↓ α←␈↓␈↓αapply[name[f␈↓πR␈↓α];list[a]; list[f␈↓πR␈↓α; ...]]␈↓ will terminate and give value ␈↓αf[a]␈↓.
␈↓" ␈↓ α←␈↓␈↓ β'Now we define a function:
␈↓"∀␈↓ α←␈↓α␈↓ β/diag <= λ[[x][total[x] → list[apply[name[first[x]];list[x];x]]; ␈↓
t␈↓α → ␈↓
f␈↓α]]
␈↓"∀␈↓ α←␈↓Note that ␈↓αdiag␈↓ is total. Now consider ␈↓αdiag␈↓πR␈↓:
␈↓"∀␈↓ α←␈↓α(DIAG . (␈↓ βcLAMBDA
␈↓"β␈↓ α←␈↓α␈↓ βc(X)
␈↓"β␈↓ α←␈↓α␈↓ βc(COND␈↓ ∧O((TOTAL X) (LIST (APPLY␈↓ π[(NAME (FIRST X))
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O␈↓ π[(LIST X)
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O␈↓ π[X)))
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O(T NIL))) )
␈↓"∀␈↓ α←␈↓␈↓ β'Form␈α␈↓αlist[diag␈↓πR␈↓α;␈αtotal␈↓πR␈↓α;␈αapply␈↓πR␈↓α;␈α...]␈↓,␈αand␈αcall␈αthe␈αresulting␈αlist␈α␈↓αdenv␈↓.␈αThat
␈↓ α←␈↓list will be the representation of ␈↓αdiag␈↓ and all its necessary functions.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 74␈↓This␈α∀discussion␈α∀will␈α∀nominally␈α∀concern␈α∀unary␈α∀functions,␈α∀but␈α∪the
␈↓ α←␈↓generalization to n-ary functions is immediate.
␈↓ α←␈↓␈↓3.13␈↓ πfReview and Reflection 183␈↓
␈↓" ␈↓ α←␈↓␈↓ β'Now␈α⊂consider␈α⊃the␈α⊂evaluation␈α⊃of␈α⊂␈↓αdiag[denv]␈↓.␈α⊂Since␈α⊃␈↓αdiag␈α⊂␈↓¬is␈↓␈α⊃total,␈α⊂then
␈↓ α←␈↓␈↓αtotal[denv]␈↓ is true, and we can reduced the problem to:
␈↓"∀␈↓ α←␈↓α␈↓ ∧:list[apply[name[first[denv]];list[denv];denv]]
␈↓"∀␈↓ α←␈↓but␈α⊂␈↓αname[first[denv]]␈α⊂=␈α∂DIAG␈↓;␈α⊂and␈α⊂therefore␈α⊂the␈α∂call␈α⊂on␈α⊂␈↓αapply␈↓␈α⊂reduces␈α∂to
␈↓ α←␈↓␈↓αapply[DIAG;list[denv];denv]␈↓.␈α∪But␈α∪that's␈α∩just␈α∪␈↓αdiag[denv]␈↓,␈α∪and␈α∪we've␈α∩shown
␈↓ α←␈↓␈↓αdiag[denv] = list[diag[denv]]␈↓.␈α∂That's␈α∞just␈α∂not␈α∞possible.␈α∂Thus␈α∂the␈α∞assumption
␈↓ α←␈↓of the existence of ␈↓αtotal␈↓ must be in error.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αusual␈αproof␈α
of␈αthis␈αresult␈αis␈α
given␈αin␈αnumber␈αtheory␈α
and␈αinvolves
␈↓ α←␈↓encoding␈α
the␈α
functions␈α
into␈α
the␈α
integers␈α
and␈α
then␈α
expressing␈α
the␈α
equivalent
␈↓ α←␈↓of␈α
the␈α
␈↓αapply␈↓␈α
function␈α
as␈α
an␈α
algorithm␈α
in␈α
number␈α
theory.␈α
The␈α
encoding␈αin␈α
the
␈↓ α←␈↓integers␈α
is␈α
analogous␈α
to␈αwhat␈α
␈↓¬we␈↓␈α
did␈α
in␈αencoding␈α
in␈α
the␈α
S-expressions.␈αThis␈α
is
␈↓ α←␈↓the␈α⊃problem␈α⊂of␈α⊃representation␈α⊂again.␈α⊃ LISP␈α⊂␈↓¬is␈↓␈α⊃more␈α⊂than␈α⊃a␈α⊂programming
␈↓ α←␈↓language; it is also a formalism for discussing computation.
␈↓"β␈↓ α←␈↓␈↓ β'To␈αreturn␈αto␈αour␈αdenotational␈αanalysis␈αof␈αLISP,␈αthe␈αnext␈αaddition␈αto␈α␈↓εD␈↓
␈↓ α←␈↓will␈α⊂involve␈α⊂recursion␈α⊂and␈α⊂function␈α⊂definitions:␈α⊂␈↓αlabel␈↓␈α⊂and␈α⊂"<=".␈α⊂ We␈α⊂know
␈↓ α←␈↓that␈α
the␈α
LISP␈α
␈↓αlabel␈↓␈α
operator␈α
is␈αsimilar␈α
to␈α
"<=",␈α
but␈α
␈↓αlabel␈↓␈α
builds␈α
a␈αtemporary
␈↓ α←␈↓definition,␈α~while␈α→"<="␈α~modifies␈α→the␈α~global␈α~environment.␈α→Programming
␈↓ α←␈↓language␈α_constructs␈α_which␈α_modify␈α→the␈α_environment␈α_are␈α_said␈α→to␈α_have
␈↓ α←␈↓␈↓↓side-effects␈↓.␈αSide-effects␈α
are␈αusually␈α
introduced␈αinto␈α
programming␈αlanguages
␈↓ α←␈↓using␈αimperative␈αconstructs.␈α Since␈αour␈α
main␈αinterest␈αlies␈αin␈αthe␈α
programming
␈↓ α←␈↓aspects␈α∞of␈α∞LISP,␈α∞we␈α∞will␈α
postpone␈α∞the␈α∞mathematics␈α∞until␈α∞we␈α∞have␈α
discussed
␈↓ α←␈↓the procedural aspects of imperative constructs and side-effects.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Recall␈α∪the␈α∀problem␈α∪on␈α∪page 149,␈α∀dealing␈α∪with␈α∪the␈α∀following␈α∪factorial
␈↓ α←␈↓␈↓ β∂algorithm:
␈↓" ␈↓ α←␈↓α␈↓ ¬∀fact <= λ[[n] f[function[f]; n]]
␈↓" ␈↓ α←␈↓α␈↓ β∂␈↓where:␈↓α␈↓ ∧Gf <= λ[[g;n][n=0 → 1; ␈↓
t␈↓α → *[n; g[g; n-1]] ]]
␈↓" ␈↓ α←␈↓␈↓ β∂Rewrite ␈↓αfact␈↓ in terms a unary function ␈↓εt␈↓:
␈↓" ␈↓ α←␈↓␈↓ βx␈↓εt␈↓α <= λ[[x] function[λ[[n][n=0 → 1; ␈↓
t␈↓α → *[n; x[n-1]] ]]]]␈↓.
␈↓" ␈↓ α←␈↓␈↓ β'Show that ␈↓αfact␈↓ = ␈↓εt␈↓α[fact]␈↓.
␈↓" ␈↓ α←␈↓2. The␈α∞␈↓λλ␈↓-calculus␈α∂described␈α∞by␈α∞the␈α∂␈↓εa␈↓␈α∞and␈α∞␈↓εb␈↓␈α∂rules␈α∞doesn't␈α∂look␈α∞particularly
␈↓ α←␈↓␈↓ β∂rich,␈αsimilar␈αin␈αpower␈αto␈αLISP␈αwith␈αjust␈αfunction␈αapplication␈αbut␈αwithout
␈↓ α←␈↓␈↓ β∂conditional␈α∩expressions.␈α∪ That␈α∩is␈α∪only␈α∩an␈α∩illusion.␈α∪Show␈α∩that␈α∪we␈α∩can
␈↓ α←␈↓␈↓ β∂represent␈α∂a␈α∂simple␈α∂␈↓αif␈↓␈α∂function␈α∂␈↓αif[p;then;otherwise]␈↓.␈α∂ Hint:␈α∂show␈α∂that␈α∂the
␈↓ α←␈↓␈↓ β∂term␈α␈↓αλ[[x;y] y]␈↓␈αis␈αa␈αgood␈αrepresentation␈α
for␈α␈↓
f␈↓␈αand␈αthe␈αterm␈α␈↓αλ[[x;y] x]␈↓␈α
is␈αa
␈↓ α←␈↓␈↓ β∂good representation for ␈↓
t␈↓.
␈↓ α←␈↓␈↓184 Imperative Constructs in LISP␈↓
84.␈↓
␈↓"β␈↓ α←␈↓␈↓ ≥␈↓↓CHAPTER 4␈↓
␈↓"β␈↓ α←␈↓␈↓ Imperative Constructs in LISP␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬f␈↓↓4.1 Introduction␈↓
␈↓"β␈↓ α←␈↓All␈α⊂of␈α∂the␈α⊂language␈α⊂constructs␈α∂we␈α⊂have␈α∂introduced␈α⊂so␈α⊂far␈α∂are␈α⊂based␈α⊂on␈α∂a
␈↓ α←␈↓computational␈α∞interpretation␈α∂of␈α∞function␈α∞application.␈α∂ We␈α∞therefore␈α∂call␈α∞the
␈↓ α←␈↓language,␈α∩applicative␈α∩LISP.␈↓π 1␈↓␈α⊃Though␈α∩this␈α∩applicative␈α⊃subset␈α∩is␈α∩rich␈α⊃and
␈↓ α←␈↓powerful,␈α∞it␈α∞is␈α∞often␈α∞convenient␈α∞to␈α∞have␈α∞access␈α∞to␈α∞another␈α∞type␈α∞of␈α∞language
␈↓ α←␈↓constuct called the ␈↓↓imperative␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'An␈αimperative␈αconstruct␈αhas␈αthe␈αintent␈αof␈αa␈αcommand.␈α For␈αthat␈αreason
␈↓ α←␈↓imperative␈αcommands␈αare␈α
called␈αstatements␈αrather␈α
than␈αexpressions␈αsince␈αit␈α
is
␈↓ α←␈↓the␈α␈↓¬effect␈↓␈αof␈αthe␈αcommand␈αwhich␈αis␈αimportant;␈αand␈αits␈α␈↓¬value␈↓,␈αif␈αit␈αhas␈αone,␈αis
␈↓ α←␈↓of␈α
secondary␈α
importance.␈↓π 2␈↓␈α
For␈α
example,␈α
most␈α
programming␈α∞languages␈α
have
␈↓ α←␈↓sequencing␈α∪commands:␈α∪"first␈α∪do␈α∪␈↓αS␈↓β1␈↓,␈α∀then␈α∪do␈α∪␈↓αS␈↓β2␈↓";␈α∪that␈α∪might␈α∀be␈α∪written
␈↓ α←␈↓"␈↓αS␈↓β1␈↓α; S␈↓β2␈↓",␈α⊃where␈α∩␈↓αS␈↓β1␈↓␈α⊃and␈α∩␈↓αS␈↓β2␈↓␈α⊃are␈α∩statements.␈α⊃ The␈α∩nature␈α⊃of␈α∩imperatives␈α⊃is
␈↓ α←␈↓somewhat␈α∪illusory,␈α∪since␈α∪sequencing␈α∪can␈α∪be␈α∪expressed␈α∪in␈α∪our␈α∩applicative
␈↓ α←␈↓subset:
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 1␈↓It is also referred to as "pure LISP".
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 2␈↓Some␈α∀programming␈α∃languages␈α∀insist␈α∀that␈α∃statements␈α∀do␈α∃␈↓¬not␈↓␈α∀have
␈↓ α←␈↓value.␈αThe␈α
imperatives␈αof␈α
LISP␈α␈↓¬do␈↓␈α
have␈αvalues;␈α
they␈αmay␈α
be␈αused␈αor␈α
ignored
␈↓ α←␈↓as the programmer desires.
␈↓ α←␈↓␈↓4.1␈↓ λ↑Introduction 185␈↓
␈↓"β␈↓ α←␈↓α␈↓ ¬→S␈↓β1␈↓α;S␈↓β2␈↓ is ␈↓αλ[[] S␈↓β2␈↓α][S␈↓β1␈↓α] ␈↓π 3␈↓α
␈↓"∀␈↓ α←␈↓Here␈α∃we␈α∀depend␈α∃on␈α∀LISP's␈α∃call␈α∀by␈α∃value␈α∀evaluation.␈α∃ Since␈α∀␈↓αS␈↓β1␈↓␈α∃is␈α∀the
␈↓ α←␈↓argument,␈α∪it␈α∀is␈α∪evaluated␈α∪first;␈α∀then␈α∪␈↓αS␈↓β2␈↓␈α∪is␈α∀evaluated.␈α∪The␈α∪value␈α∀of␈α∪the
␈↓ α←␈↓sequence is the value of ␈↓αS␈↓β2␈↓. LISP calls this sequencing operation ␈↓αprog␈↓β2␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'An␈α
area␈αwhich␈α
is␈αthought␈α
to␈αbe␈α
the␈αprovince␈α
of␈αimperatives␈α
is␈α
that␈αof
␈↓ α←␈↓the␈α∂assignment␈α∂statement.␈α∂We␈α∞will␈α∂discuss␈α∂assignment␈α∂statements␈α∂further␈α∞in
␈↓ α←␈↓Section 4.2, but the intent of such a statement, written:
␈↓"∀␈↓ α←␈↓␈↓ ¬%<identifier> ← <expression>
␈↓"∀␈↓ α←␈↓is␈αto␈αthink␈αof␈αthe␈α<identifier>␈αas␈αa␈α"box"␈αand␈αthe␈αintent␈αof␈αthe␈αassignment␈αis
␈↓ α←␈↓to␈αput␈αthe␈αvalue␈αof␈αthe␈α<expression>␈αin␈αthe␈α"box".␈α Yet,␈α
function␈αapplication
␈↓ α←␈↓can encompass many of the traditional uses of assignment.
␈↓"β␈↓ α←␈↓␈↓ β'Recall our definitions of ␈↓αlength␈↓ and ␈↓αlength␈↓β1␈↓:
␈↓"∀␈↓ α←␈↓α␈↓ β∂length[l] <= [null[l] → 0; ␈↓
t␈↓α → add1[length[rest[l]]]]
␈↓" ␈↓ α←␈↓α␈↓ β∂length␈↓β1␈↓α[l] <= length␈↓λ'␈↓α[l;0]
␈↓" ␈↓ α←␈↓α␈↓ β∂length␈↓λ'␈↓α[l1;c] <= [null[l1] → c; ␈↓
t␈↓α → length␈↓λ'␈↓α[rest[l1];add1[c]]]
␈↓"∀␈↓ α←␈↓The␈α∩variable␈α∩␈↓αc␈↓␈α∩is␈α∩being␈α∩used␈α∩as␈α∩a␈α∩"box"␈α∩or␈α∩"accumulator"␈α∩[Moor 74]␈α∩to
␈↓ α←␈↓accumulate␈α
length␈α∞of␈α
the␈α∞list.␈α
We␈α
will␈α∞show␈α
in␈α∞Section 4.2␈α
that␈α∞␈↓αlength␈↓β1␈↓␈α
can
␈↓ α←␈↓be␈α∂translated␈α∂into␈α∂a␈α⊂program␈α∂using␈α∂several␈α∂imperative␈α⊂features:␈α∂statements,
␈↓ α←␈↓sequencing,␈α∂assignments,␈α∞and␈α∂an␈α∞imperative␈α∂control␈α∞regime␈α∂called␈α∞␈↓↓iteration␈↓.
␈↓ α←␈↓We␈α∩will␈α∩study␈α∩iterative␈α∩control␈α∪in␈α∩Section 4.2␈α∩and␈α∩Section 4.3␈α∩but␈α∪it␈α∩can
␈↓ α←␈↓be␈α shown␈α that␈α∨iterative␈α control␈α can␈α∨be␈α translated␈α into␈α∨recursive
␈↓ α←␈↓control ([McC 60], [Sam 75]).
␈↓"β␈↓ α←␈↓␈↓ β'In␈α∩many␈α∩instances␈α⊃the␈α∩most␈α∩important␈α⊃implication␈α∩of␈α∩imperatives␈α⊃is
␈↓ α←␈↓"convenience".␈α∂There␈α∞are␈α∂algorithms␈α∞which␈α∂are␈α∞most␈α∂naturally␈α∂described␈α∞in
␈↓ α←␈↓terms␈αof␈αsequences␈αof␈αstatements␈αand␈αiteration;␈αand␈αthere␈αare␈αmany␈αlessons␈αto
␈↓ α←␈↓be␈α∪learned␈α∪by␈α∪careful␈α∪analysis␈α∪of␈α∪natural␈α∪implementations␈α∪of␈α∪imperative
␈↓ α←␈↓constructs.␈αWe␈αare␈αnot␈αlooking␈αfor␈αa␈αminimal␈αlanguage,␈αwe␈αare␈αlooking␈αfor␈αa
␈↓ α←␈↓useful␈α∩programming␈α∩tool.␈α∪ One␈α∩of␈α∩the␈α∪most␈α∩useful␈α∩places␈α∪for␈α∩imperative
␈↓ α←␈↓constructs␈α∂is␈α∂in␈α∂area␈α⊂of␈α∂non-local␈α∂variables,␈α∂using␈α∂assignment␈α⊂statements␈α∂to
␈↓ α←␈↓pass␈α∀information␈α∪across␈α∀applicative␈α∪boundaries.␈α∀This␈α∪technique␈α∀is␈α∪called
␈↓ α←␈↓using␈α∂side-effects.␈α∂It␈α∞may␈α∂very␈α∂well␈α∞be␈α∂that␈α∂the␈α∞most␈α∂distinctive␈α∂features␈α∞of
␈↓ α←␈↓imperative constructs are involved with their side-effect aspects.
␈↓"β␈↓ α←␈↓␈↓ β'For␈α∞example,␈α∞LISP␈α∞implementations␈α∞include␈α∞a␈α∞unary␈α∞primitive␈α∞named
␈↓ α←␈↓␈↓αprint␈↓␈αwhose␈αeffect␈αis␈αto␈αprint␈αthe␈α
value␈αof␈αits␈αargument␈αon␈αthe␈αcurrent␈α
output
␈↓ α←␈↓device.␈αThis␈αfunction␈αalso␈αreturns␈αits␈αevaluated␈αargument␈αas␈αthe␈αvalue␈αof␈αthe
␈↓ α←␈↓␈↓αprint␈↓␈α
statement.␈αThus␈α
mathematically,␈α␈↓αprint␈↓␈α
acts␈α
like␈αan␈α
identity␈αfunction,␈α
but
␈↓ α←␈↓its execution certainly affects the programmer's environment.␈↓π 4␈↓
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 3␈↓Or␈αbetter␈αperhaps,␈α␈↓αλ[[dummy]␈αS␈↓β2␈↓α][S␈↓β1␈↓α]␈↓␈αwhere␈α␈↓αdummy␈↓␈αis␈αa␈αvariable␈αnot
␈↓ α←␈↓occurring free in any function called within ␈↓αS␈↓β2␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 4␈↓Whether␈α⊃the␈α⊃act␈α⊂of␈α⊃printing␈α⊃is␈α⊂a␈α⊃side-effect␈α⊃or␈α⊂the␈α⊃fact␈α⊃that␈α⊂␈↓αprint␈↓
␈↓ α←␈↓returns a value is a side-effect depends on your point of view.
␈↓ α←␈↓␈↓186 Imperative Constructs in LISP␈↓
(4.1␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Side-effects␈α→can␈α→also␈α_spoil␈α→some␈α→of␈α_the␈α→theoretical␈α→properties␈α_of
␈↓ α←␈↓languages.␈α In␈αour␈αearlier␈αdiscussions,␈αwe␈αhave␈αimplied␈αthat␈αcall-by-value␈αis␈αa
␈↓ α←␈↓subset␈α
of␈α
call-by-name␈α
in␈α
the␈α
sense␈α
that␈α
whenever␈α
a␈αcall-by-value␈α
computation
␈↓ α←␈↓terminates,␈α∂the␈α∂corresponding␈α∞call-by-name␈α∂computation␈α∂will␈α∞as␈α∂well.␈α∂In␈α∞the
␈↓ α←␈↓presence of side-effects, this is ␈↓¬not␈↓ true. We give an example on page 227.
␈↓"β␈↓ α←␈↓␈↓ ¬J␈↓↓4.2 The ␈↓αprog␈↓↓-feature␈↓α
␈↓"β␈↓ α←␈↓Though␈αrecursion␈αis␈αa␈αsignificant␈αtool␈αfor␈αconstructing␈αLISP␈αprograms,␈αthere
␈↓ α←␈↓is␈αanother␈αtechnique␈αfor␈αdefining␈αalgorithms␈αin␈αLISP.␈α It␈αis␈αan␈αiterative␈αstyle
␈↓ α←␈↓of programming which is called the ␈↓αprog␈↓ or ␈↓αprog␈↓ram feature.
␈↓"β␈↓ α←␈↓␈↓ β'Many␈α∩algorithms␈α∩are␈α∩presented␈α∩more␈α∩naturally␈α∩as␈α∩iterative␈α∩schemes.
␈↓ α←␈↓For␈αexample,␈αthe␈αrecursive␈αalgorithms␈α
␈↓αlength␈↓␈αand␈α␈↓αlength␈↓β1␈↓,␈αgiven␈αon␈α
page 185,
␈↓ α←␈↓compute the length of a list. Compare those schemes with the following:
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ Set a variable ␈↓αl1␈↓ to the given list. Set a variable ␈↓αc␈↓ to zero.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ If␈α∀the␈α∀list␈α∃is␈α∀empty,␈α∀return␈α∃the␈α∀current␈α∀value␈α∀in␈α∃␈↓αc␈↓␈α∀as␈α∀value␈α∃of␈α∀the
␈↓ α←␈↓␈↓ β∂computation.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ Otherwise, increment ␈↓αc␈↓ by one.
␈↓" ␈↓ α←␈↓␈↓↓4.␈↓ Set ␈↓αl1␈↓ to the ␈↓αrest␈↓ of ␈↓αl1␈↓.
␈↓" ␈↓ α←␈↓␈↓↓5.␈↓ Go to line 2.
␈↓"β␈↓ α←␈↓Here is a LISP version of the algorithm:
␈↓"∀␈↓ α←␈↓αlength <= λ[[l]prog[[l1;c]
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧Ol1 ← l;
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧Oc ← 0;
␈↓"β␈↓ α←␈↓α␈↓ ∧7a␈↓ ∧O[null[l1] → return[c]];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧Oc ← c+1;
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧Ol1 ← rest[l1];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧Ogo[a]] ]
␈↓"∀␈↓ α←␈↓We␈α⊃have␈α∩introduced␈α⊃several␈α∩new␈α⊃symbols,␈α⊃formats,␈α∩and␈α⊃functions␈α∩in␈α⊃this
␈↓ α←␈↓example.␈α⊗These␈α↔innovations␈α⊗must␈α⊗be␈α↔explained␈α⊗before␈α⊗the␈α↔example␈α⊗is
␈↓ α←␈↓complete. First, the basic syntax of a ␈↓αprog␈↓ is given by:
␈↓"∀␈↓ α←␈↓<prog>␈↓ ∧π::= ␈↓αprog␈↓[[<prog variables>]<prog body>]
␈↓" ␈↓ α←␈↓<prog body>␈↓ ∧π::= <prog element><prog body> | <prog element>
␈↓" ␈↓ α←␈↓<prog element>␈↓ ∧g::= <label> | <prog form>;
␈↓" ␈↓ α←␈↓<label>␈↓ ∧π::= <identifier>
␈↓" ␈↓ α←␈↓<prog form>␈↓ ∧π::= <application>
␈↓"β␈↓ α←␈↓␈↓ ∧π::= <conditional statement>
␈↓"β␈↓ α←␈↓␈↓ ∧π::= <assignment statement>
␈↓"β␈↓ α←␈↓␈↓ ∧π::= <return statement>
␈↓"β␈↓ α←␈↓␈↓ ∧π::= <go statement>
␈↓ α←␈↓␈↓4.2␈↓ λ'The ␈↓αprog␈↓-feature 187␈↓α
␈↓"β␈↓ α←␈↓<conditional statement>␈↓ ¬::= <conditional expression>
␈↓" ␈↓ α←␈↓<assignment statement>␈↓ ¬::= <identifier> ← <form>
␈↓" ␈↓ α←␈↓<return statement>␈↓ ¬::= ␈↓αreturn␈↓[<form>]
␈↓" ␈↓ α←␈↓<go statement>␈↓ ∧+::= ␈↓αgo␈↓[<form>]
␈↓"∀␈↓ α←␈↓␈↓ β'In␈αthe␈αexample,␈αthe␈αvariables,␈α␈↓αl1␈↓␈αand␈α␈↓αc␈↓,␈αare␈αcalled␈α
␈↓αprog␈↓ variables.␈αThey
␈↓ α←␈↓are␈α!local␈α"variables,␈α!similar␈α!in␈α"behavior␈α!to␈α!λ-variables;␈α"in␈α!most
␈↓ α←␈↓implementations␈α
the␈α␈↓αprog␈↓␈α
variables␈α
are␈αinitialized␈α
to␈α
␈↓α( )␈↓␈αand␈α
we␈α
will␈αassume
␈↓ α←␈↓this␈α∞convention␈α∞throughout␈α∞the␈α∞text.␈↓π 5␈↓␈α
This␈α∞behavior␈α∞of␈α∞␈↓αprog␈↓␈α∞variables␈α
can
␈↓ α←␈↓be expressed as:
␈↓"∀␈↓ α←␈↓α␈↓ ∧Aprog[[x␈↓β1␈↓α; ...; x␈↓βn␈↓α] ␈↓λx␈↓α] ␈↓λ ≡ ␈↓αλ[[x␈↓β1␈↓α; ... x␈↓βn␈↓α]␈↓λx␈↓α][( ); ... ( )]
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α⊂body␈α⊃␈↓λx␈↓␈α⊂contains␈α⊂the␈α⊃imperative␈α⊂behavior.␈α⊂ The␈α⊃␈↓αprog␈↓␈α⊂body␈α⊃is␈α⊂a
␈↓ α←␈↓sequence␈αof␈α␈↓αprog␈↓ forms␈α
and␈αlabels.␈α Each␈α␈↓αprog␈↓ form␈α
is␈αevaluated␈αin␈αthe␈α
usual
␈↓ α←␈↓LISP␈α⊗manner,␈α↔and␈α⊗since␈α⊗the␈α↔␈↓αprog␈↓ body␈α⊗can␈α⊗consist␈α↔of␈α⊗a␈α↔sequence␈α⊗of
␈↓ α←␈↓␈↓αprog␈↓ forms, the ␈↓αprog␈↓-body is evaluated from left-to-right.
␈↓"β␈↓ α←␈↓␈↓ β'If␈α∃the␈α∃intent␈α∃of␈α∀the␈α∃␈↓αprog␈↓␈α∃was␈α∃simply␈α∀to␈α∃execute␈α∃the␈α∃sequence␈α∀of
␈↓ α←␈↓␈↓αprog␈↓ forms,␈α⊂in␈α⊂left-to-right␈α⊂order,␈α⊂then␈α⊂␈↓αprog␈↓␈α⊂could␈α⊂be␈α⊂replaced␈α⊂by␈α⊂a␈α⊂much
␈↓ α←␈↓simpler construct like ␈↓αprogn␈↓:
␈↓"∀␈↓ α←␈↓α␈↓ ¬2progn <= λ[[x␈↓β1␈↓α; ...; x␈↓βn␈↓α] x␈↓βn␈↓α]
␈↓"∀␈↓ α←␈↓However␈αwe␈αwill␈α
add␈αconstructs␈αto␈αLISP␈α
which␈αwill␈αallow␈α
us␈αto␈αvary␈αthe␈α
flow
␈↓ α←␈↓of␈αcontrol␈αwithin␈αthe␈α␈↓αprog␈↓ body.␈αIt␈αis␈αto␈αthis␈αend␈αthat␈αwe␈αuse␈αlabels,␈αlike␈α␈↓αa␈↓␈αin
␈↓ α←␈↓the␈α
example.␈↓π 6␈↓␈α
Before␈α
we␈α
discuss␈α
control␈α
structures,␈α
we␈α
give␈α
more␈α
details␈α
on
␈↓ α←␈↓LISP's assignment statement.
␈↓"β␈↓ α←␈↓␈↓ β'As␈α
with␈α
all␈α
LISP␈α
constructs,␈α
the␈α
assignment␈α
statement␈α
returns␈α∞a␈α
value,
␈↓ α←␈↓but␈αwe␈αidentify␈αit␈αas␈αa␈αstatement␈αsince␈αit␈αis␈αexecuted␈αmore␈αfor␈αeffect␈αthan␈αfor
␈↓ α←␈↓value.␈α∃ The␈α∃value␈α∃of␈α∃the␈α∃assignment␈α∃is␈α∃the␈α∃value␈α∃of␈α∃the␈α∃form␈α∃on␈α∀its
␈↓ α←␈↓right-hand-side.␈α In␈αour␈αexample␈αof␈α␈↓αlength␈↓,␈αwe␈αused␈αan␈αassignment␈αto␈αbind␈α␈↓αl1␈↓
␈↓ α←␈↓to␈α∞the␈α∞value␈α∂of␈α∞␈↓αl␈↓␈α∞and␈α∞to␈α∂bind␈α∞␈↓αc␈↓␈α∞to␈α∞␈↓α0␈↓.␈α∂ To␈α∞evaluate␈α∞an␈α∞assignment,␈α∂we␈α∞first
␈↓ α←␈↓evaluate␈α
the␈αform;␈α
then␈α
the␈αidentifier␈α
is␈α
located␈αby␈α
searching␈α
the␈αaccess␈α
chain.
␈↓ α←␈↓Thus␈α∩the␈α∩identifier␈α∩may␈α∩be␈α⊃a␈α∩non-local␈α∩variable.␈α∩When␈α∩the␈α∩identifier␈α⊃is
␈↓ α←␈↓located␈αits␈α
current␈αvalue␈α
is␈α␈↓¬replaced␈↓␈αby␈α
the␈αvalue␈α
of␈αthe␈α
form.␈αNotice␈αthat␈α
this
␈↓ α←␈↓is␈α∂a␈α∂different␈α∂kind␈α∂of␈α∂binding␈α∂than␈α∂that␈α∂previously␈α∂done␈α∂by␈α∂λ-binding.␈α∞In
␈↓ α←␈↓λ-binding␈α∪we␈α∪always␈α∪associated␈α∪a␈α∩new␈α∪value␈α∪with␈α∪a␈α∪newly␈α∪created␈α∩local
␈↓ α←␈↓symbol␈αtable␈αas␈αwe␈αentered␈αthe␈αλ-body.␈α We␈αnever␈αdestroyed␈αthe␈αold␈αbinding
␈↓ α←␈↓of␈α
a␈α
variable.␈α
The␈α
assigment␈α
statement␈α
involves␈α
a␈α
destructive␈α
change␈α
to␈αthe
␈↓ α←␈↓binding.␈αThis␈αis␈αimportant␈αsince␈αassignments␈αto␈αnon-local␈αvariables␈αcan␈αhave
␈↓ α←␈↓effect␈α↔outside␈α_the␈α↔␈↓αprog␈↓␈α↔while␈α_a␈α↔λ-rebinding␈α↔cannot.␈α_This␈α↔is␈α_how␈α↔an
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 5␈↓A␈αuseful␈αalternative␈αis␈αto␈αinitialize␈αthem␈αto␈αsome␈α"unbound"␈αvalue.␈αIn
␈↓ α←␈↓that␈α⊃way␈α⊂the␈α⊃system␈α⊃can␈α⊂recognize␈α⊃attempts␈α⊂to␈α⊃select␈α⊃the␈α⊂value␈α⊃of␈α⊃a␈α⊂␈↓αprog␈↓
␈↓ α←␈↓variable before is has been assigned to.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 6␈↓Labels are also known as "tags".
␈↓ α←␈↓␈↓188 Imperative Constructs in LISP␈↓
(4.2␈↓
␈↓"β␈↓ α←␈↓assignment␈α⊂statement␈α∂can␈α⊂achieve␈α∂a␈α⊂more␈α∂permanent␈α⊂side-effect.␈α∂ However,
␈↓ α←␈↓since␈α⊂␈↓αprog␈↓␈α⊂variables␈α⊂are␈α⊂created␈α⊂like␈α⊂λ-bindings,␈α⊂assignments␈α⊂to␈α⊂local␈α∂␈↓αprog␈↓
␈↓ α←␈↓variables␈αcannot␈αeffect␈αbindings␈αof␈αthese␈αvariables␈αwhich␈αwere␈α
made␈αoutside
␈↓ α←␈↓the␈α∀scope␈α∀of␈α∀the␈α∀current␈α∪␈↓αprog␈↓.␈α∀ This␈α∀behavior␈α∀makes␈α∀␈↓αprog␈↓s␈α∀suitable␈α∪as
␈↓ α←␈↓components␈αin␈α
recursive␈αdefinitions.␈α
For␈αexample,␈α
a␈αfunction␈α
to␈αreverse␈αa␈α
list
␈↓ α←␈↓and all of its sublist can be described as:
␈↓"∀␈↓ α←␈↓α␈↓ βKbigrev <=λ[[l] prog[␈↓ ¬G[x]
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧ga␈↓ ¬[null[l]→ return[x]];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧g␈↓ ¬x ← concat[bigrev[first[l]];x];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧g␈↓ ¬l ← rest[l];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧g␈↓ ¬go[a]]]
␈↓"∀␈↓ α←␈↓␈↓ β'As␈α∩the␈α∩examples␈α∩have␈α⊃intimated,␈α∩␈↓αprog␈↓␈α∩introduces␈α∩some␈α∩new␈α⊃control
␈↓ α←␈↓structures␈α
so␈α
that␈α
the␈α
␈↓αprog␈↓ body␈αneed␈α
not␈α
be␈α
executed␈α
in␈α
simple␈αleft-to-right
␈↓ α←␈↓order.␈α∀ The␈α∃control␈α∀structures␈α∀are:␈α∃the␈α∀conditional␈α∀statement,␈α∃the␈α∀␈↓αreturn␈↓
␈↓ α←␈↓statement, and the ␈↓αgo␈↓ statement.
␈↓"β␈↓ α←␈↓␈↓ β'Though␈α∃conditional␈α∃statements␈α∀in␈α∃␈↓αprog␈↓s␈α∃have␈α∀the␈α∃same␈α∃syntax␈α∀as
␈↓ α←␈↓conditional␈α⊃expressions,␈α⊃their␈α⊃semantics␈α⊂is␈α⊃slightly␈α⊃different.␈α⊃A␈α⊂conditional
␈↓ α←␈↓statement␈α∩is␈α∪executed␈α∩in␈α∪the␈α∩usual␈α∪manner␈α∩unless␈α∪none␈α∩of␈α∪the␈α∩predicate
␈↓ α←␈↓alternatives␈α
is␈α
satisfied.␈α
Recall␈α
that␈α
a␈α
conditional␈α
expression␈α
is␈α
undefined␈α
in
␈↓ α←␈↓this␈α
case;␈α
a␈α
conditional␈α
statement␈α
however␈α
is␈α
defined,␈α
returns␈α
␈↓α( )␈↓,␈αand␈α
executes
␈↓ α←␈↓the␈αnext␈αstatement␈αin␈αthe␈α
␈↓αprog␈↓ body.␈α In␈αour␈α␈↓αlength␈↓␈αexample,␈α
the␈αexpression,
␈↓ α←␈↓ ␈↓α[null[l1] → return[c]]␈↓ ␈α∪indicates␈α∪that␈α∪if␈α∪␈↓αl1␈↓␈α∪is␈α∪not␈α∪empty␈α∀the␈α∪␈↓αprog␈↓ body
␈↓ α←␈↓continues␈α⊃at␈α⊃the␈α⊃next␈α⊃statement␈α∩with␈α⊃the␈α⊃assignment␈α⊃of␈α⊃␈↓αrest[l1]␈↓␈α⊃to␈α∩␈↓αl1␈↓;␈α⊃the
␈↓ α←␈↓assignment␈α∂destroys␈α∂the␈α∂old␈α⊂value␈α∂of␈α∂␈↓αl1␈↓.␈α∂If␈α⊂␈↓αl1␈↓␈α∂␈↓¬is␈↓␈α∂empty,␈α∂then␈α⊂the␈α∂statement
␈↓ α←␈↓␈↓αreturn[c]␈↓ is executed.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α⊗is␈α∃a␈α⊗useful␈α⊗interplay␈α∃between␈α⊗the␈α⊗λ-binding␈α∃of␈α⊗␈↓αl␈↓␈α⊗and␈α∃the
␈↓ α←␈↓assignment␈α
binding␈α
of␈α
␈↓αl1␈↓.␈α
We␈α
could␈α
have␈α
dispensed␈α
with␈α∞the␈α
␈↓αprog␈↓ variable
␈↓ α←␈↓␈↓αl1␈↓,␈α⊃and␈α⊃used␈α⊃␈↓αl␈↓␈α∩throughout␈α⊃the␈α⊃␈↓αprog␈↓ body.␈α⊃Even␈α⊃the␈α∩assignment␈α⊃␈↓αl ← rest[l]␈↓
␈↓ α←␈↓would␈α⊃not␈α⊃have␈α∩effected␈α⊃the␈α⊃binding␈α⊃of␈α∩the␈α⊃original␈α⊃argument␈α∩passed␈α⊃to
␈↓ α←␈↓␈↓αlength␈↓.␈αThis␈αis␈αassured␈αbecause␈αthe␈αλ-binding␈αsaves␈αthat␈αvalue␈αand␈αthe␈αeffect
␈↓ α←␈↓of␈α∞the␈α∂assignment␈α∞is␈α∂only␈α∞to␈α∂change␈α∞the␈α∂contents␈α∞of␈α∂a␈α∞"box"␈α∂whose␈α∞current
␈↓ α←␈↓content␈α∂is␈α∂a␈α∂"pointer"␈α∂to␈α∂the␈α∂value.␈α∂None␈α∂of␈α∂the␈α∂LISP␈α∂operations␈α∂we␈α∞have
␈↓ α←␈↓discussed␈αcan␈αalter␈αa␈αvalue␈α"pointed to"␈αin␈αthis␈αfashion.␈αWe␈αwill␈αdiscuss␈αsuch
␈↓ α←␈↓operations in Section 7.7.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∞␈↓αreturn␈↓␈α∞statement␈α
is␈α∞a␈α∞␈↓αprog␈↓␈α
construct␈α∞similar␈α∞in␈α
effect␈α∞to␈α∞exiting␈α
a
␈↓ α←␈↓λ-expression.␈α
It␈α
is␈α
used␈α
to␈α
leave␈α
a␈α
␈↓αprog␈↓ body␈α
and␈α
return␈α
to␈α
the␈α
caller␈α
of␈αthe
␈↓ α←␈↓␈↓αprog␈↓.␈α
As␈αwe␈α
leave␈αthe␈α
␈↓αprog␈↓,␈α
the␈αbindings␈α
of␈αthe␈α
␈↓αprog␈↓ variables␈α
are␈αremoved
␈↓ α←␈↓as␈αare␈αany␈αλ-bindings␈αmade␈αon␈αentry␈αto␈αthe␈α␈↓αprog␈↓.␈α The␈αvalue␈αreturned␈αis␈αthe
␈↓ α←␈↓value␈αof␈αthe␈αargument␈αto␈αthe␈α␈↓αreturn␈↓␈αstatement.␈αThe␈α␈↓αreturn␈↓␈αstatement␈αmay␈αbe
␈↓ α←␈↓nested within other LISP computation, as for example:
␈↓"∀␈↓ α←␈↓α␈↓ ¬>concat[A;return[list[B]]]
␈↓"∀␈↓ α←␈↓The␈α∂effect␈α∞of␈α∂the␈α∞␈↓αreturn␈↓␈α∂is␈α∞immediate;␈α∂the␈α∞␈↓αconcat␈↓␈α∂would␈α∞never␈α∂complete␈α∞its
␈↓ α←␈↓␈↓4.2␈↓ λ'The ␈↓αprog␈↓-feature 189␈↓α
␈↓"β␈↓ α←␈↓operation.␈αWe␈αwould␈α
return␈α␈↓α(B)␈↓␈αto␈α
the␈αcaller␈αof␈αthe␈α
enclosing␈α␈↓αprog␈↓.␈αA␈α
bit␈αof
␈↓ α←␈↓care␈α∂is␈α∂needed␈α∂in␈α∂describing␈α∂the␈α∂meaning␈α∂of␈α∂␈↓αreturn␈↓:␈α∂we␈α∂look␈α∂for␈α∂the␈α∂latest
␈↓ α←␈↓instance␈α∞of␈α∞an␈α∂entrance␈α∞to␈α∞a␈α∂␈↓αprog␈↓␈α∞and␈α∞return␈α∂from␈α∞that␈α∞␈↓αprog␈↓.␈α∂To␈α∞visualize
␈↓ α←␈↓this,␈α↔we␈α↔use␈α↔the␈α↔Weizenbaum␈α↔environments (page 142).␈α↔We␈α↔search␈α⊗the
␈↓ α←␈↓␈↓↓control␈α∞chain␈↓,␈α∂looking␈α∞for␈α∞the␈α∂first␈α∞␈↓↓Form␈↓␈α∞which␈α∂is␈α∞␈↓αprog[[ ... ] ... ]␈↓.␈α∂We␈α∞then
␈↓ α←␈↓restore␈αusing␈αthe␈αaccess␈αand␈αcontrol␈αinformation␈αfound␈αin␈αthat␈αdiagram.␈α We
␈↓ α←␈↓will give a comprehensive example after discussing the ␈↓αgo␈↓ statement.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∪␈↓αgo␈↓␈α∪statement␈α∪is␈α∪used␈α∪in␈α∪conjunction␈α∪with␈α∪labels␈α∪to␈α∪divert␈α∪the
␈↓ α←␈↓implied␈α~left-to-right␈α→execution␈α~of␈α→the␈α~␈↓αprog␈↓ body.␈α→ Labels␈α~really␈α→aren't
␈↓ α←␈↓executed;␈αthey␈αare␈α
used␈αto␈αname␈α
statements␈αin␈αa␈α␈↓αprog␈↓.␈α
It␈αis␈αthe␈α
␈↓αgo␈↓␈αstatement
␈↓ α←␈↓which␈αuses␈αthe␈αlabel␈αas␈αa␈αdestination␈αfor␈αtransferring␈αcontrol.␈α Labels␈αmay␈αbe
␈↓ α←␈↓in␈α∩conflict␈α∩with␈α⊃the␈α∩λ-variables␈α∩or␈α⊃␈↓αprog␈↓ variables␈α∩since␈α∩the␈α∩evaluator␈α⊃for
␈↓ α←␈↓␈↓αprog␈↓s␈α
can␈α
resolve␈α
the␈α
conflicts␈α
by␈α
context.␈α
Any␈α
identifier␈α
occurring␈α
by␈α
itself␈α
in
␈↓ α←␈↓a␈α␈↓αprog␈↓ body␈αis␈αa␈αlabel.␈αAny␈αidentifier␈αoccurring␈αin␈αan␈αapplication␈αother␈αthan
␈↓ α←␈↓a␈α␈↓αgo␈↓ statement␈αis␈αa␈αvariable␈αand␈αits␈αvalue␈αis␈αsearched␈αfor␈αin␈αthe␈αaccess␈αchain,
␈↓ α←␈↓whereas␈α∞an␈α∂identifier␈α∞appearing␈α∞in␈α∂a␈α∞␈↓αgo␈↓␈α∞statement␈α∂is␈α∞interpreted␈α∞as␈α∂a␈α∞label
␈↓ α←␈↓and searched for in a ␈↓αprog␈↓ body.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α␈↓αgo␈↓␈αstatement␈αis␈αa␈αlittle␈αmore␈αcomplicated␈αthan␈αthe␈α␈↓αreturn␈↓␈αstatement.
␈↓ α←␈↓If␈α∩the␈α∪argument␈α∩to␈α∩␈↓αgo␈↓␈α∪is␈α∩an␈α∩identifier␈α∪then␈α∩it␈α∩is␈α∪interpreted␈α∩as␈α∪a␈α∩label;
␈↓ α←␈↓otherwise,␈α⊗the␈α⊗argument␈α⊗is␈α⊗␈↓¬evaluated␈↓␈α⊗and␈α⊗the␈α⊗result␈α⊗of␈α⊗the␈α∃evaluation
␈↓ α←␈↓examined.␈α∞ This␈α∞process␈α∞continues␈α∞until␈α∂a␈α∞label␈α∞has␈α∞been␈α∞uncovered␈α∂as␈α∞the
␈↓ α←␈↓result␈α∞of␈α∂an␈α∞evaluation.␈α∂ At␈α∞that␈α∂time␈α∞we␈α∞must␈α∂locate␈α∞a␈α∂statment␈α∞in␈α∂a␈α∞␈↓αprog␈↓
␈↓ α←␈↓which␈α
has␈α
a␈α
matching␈αlabel␈α
attached␈α
to␈α
it.␈αOur␈α
intention␈α
is␈α
to␈αtransfer␈α
control
␈↓ α←␈↓to␈α∀that␈α∀statement.␈α∀ We␈α∀locate␈α∀the␈α∀labeled␈α∀statement␈α∀as␈α∀follows:␈α∃we␈α∀look
␈↓ α←␈↓through␈αthe␈α
control␈αchain␈αfor␈α
the␈αfirst␈α␈↓αprog␈↓␈α
which␈αcontains␈αthe␈α
label.␈αWhen
␈↓ α←␈↓the␈αlabel␈αis␈αfound␈αwe␈αtransfer␈αcontrol␈αto␈αthat␈αlabeled␈αstatement,␈αrestoring␈αthe
␈↓ α←␈↓access␈α∂and␈α∂control␈α∂environments␈α∂of␈α∂the␈α∂␈↓αprog␈↓␈α∂which␈α∂contain␈α∂that␈α∂statement.
␈↓ α←␈↓Thus␈αthere␈αis␈αa␈αdouble␈αsearch␈αinvolved:␈αwe␈αsearch␈αthe␈αcontrol␈αchain␈αfor␈α
␈↓αprog␈↓
␈↓ α←␈↓forms,␈αand␈αsearch␈αthe␈α␈↓αprog␈↓␈αforms␈αfor␈αthe␈αlabel.␈α Labels␈αneed␈αnot␈αbe␈αlocal;␈αwe
␈↓ α←␈↓find␈α∂the␈α∂closest␈α∞dynamically␈α∂surrounding␈α∂␈↓αprog␈↓␈α∞which␈α∂contains␈α∂the␈α∞required
␈↓ α←␈↓label.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∂non-local␈α∂␈↓αgo␈↓␈α∂and␈α∂␈↓αreturn␈↓␈α∂differ␈α∂from␈α∂the␈α∂usual␈α∂procedure␈α∂exit␈α∞in
␈↓ α←␈↓that␈α
they␈αdo␈α
not␈αrestore␈α
the␈αenclosing␈α
control␈αenvironment,␈α
but␈αescape␈α
further
␈↓ α←␈↓back the control chain.
␈↓ α←␈↓␈↓190 Imperative Constructs in LISP␈↓
(4.2␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Finally, as an example covering the new features of ␈↓αprog␈↓ consider:
␈↓"∀␈↓ α←␈↓α␈↓ ∧Cf <= λ[[y;z] prog[[l;x]
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬←␈↓ ¬wl ← 2;
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬←l␈↓ ¬wu ← g[y;x;z]
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬←␈↓ ¬w ... ]]
␈↓" ␈↓ α←␈↓α␈↓ ∧Cg <= λ[[x;y;z] prog[[ ]
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬←␈↓ ¬w ...
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬←␈↓ ¬wgo[l]
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬←␈↓ ¬w ...
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬←␈↓ ¬wreturn[first[x]]
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬←␈↓ ¬w ... ]]
␈↓"∀␈↓ α←␈↓In␈α␈↓αf␈↓,␈α␈↓αl␈↓␈α
is␈αboth␈αa␈α
label␈αand␈αa␈α␈↓αprog␈↓ variable.␈α
Notice␈αin␈α␈↓αg␈↓␈α
that␈αwe␈αhave␈αno␈α
␈↓αprog␈↓
␈↓ α←␈↓variables;␈αand␈αsince␈αwe␈αassume␈αthat␈α␈↓αl␈↓␈αis␈αnot␈αa␈αlabel␈αin␈α␈↓αg␈↓␈αwe␈αhave␈αa␈αnon-local
␈↓ α←␈↓␈↓αgo␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Consider the evaluation of ␈↓αf[(A B);3]␈↓.
␈↓"∀␈↓ α←␈↓␈↓ α←␈↓ β≠␈↓αf[(A B);3]␈↓ ∧∪␈↓ ¬G␈↓ εβprog[[l;x]...]␈↓ π∨␈↓ π[[l ← 2; l u ← g[y;x;z];...]
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ β≠␈↓E␈↓β0␈↓␈↓ ∧∪␈↓ ¬G␈↓ εβE␈↓β1␈↓␈↓ εc␈↓ π∨␈↓ π[E␈↓β2␈↓
␈↓"β␈↓ α←␈↓␈↓ α← /␈↓ β≠| /␈↓ ∧∪␈↓ ¬G E␈↓β0␈↓␈↓ εβ| E␈↓β0␈↓␈↓ εc␈↓ π∨ E␈↓β1␈↓␈↓ π[| E␈↓β1␈↓
␈↓"β␈↓ α←␈↓␈↓ α←_______␈↓ ∧∪=>␈↓ ¬G_______␈↓ εc=>␈↓ π∨_______ =>
␈↓"β␈↓ α←␈↓␈↓ α←␈↓α f␈↓ β≠| λ[[y;z] prog[...]]␈↓ ¬G y␈↓ εβ| (A B)␈↓ εc␈↓ π∨ l␈↓ π[| ( )
␈↓"β␈↓ α←␈↓α␈↓ α← g␈↓ β≠| λ[[x;y;z] prog[...]]␈↓ ¬G z␈↓ εβ| 3␈↓ εc␈↓ π∨ x␈↓ π[| ( )
␈↓"∀␈↓ α←␈↓At␈αthis␈αpoint␈αwe␈αhave␈αdone␈αthe␈αλ-binding␈αand␈αinitialized␈αthe␈α␈↓αprog␈↓ variables.
␈↓ α←␈↓As␈α
we␈α
begin␈α
the␈α
execution␈α
of␈α
the␈α␈↓αprog␈↓ body,␈α
we␈α
assign␈α
␈↓α2␈↓␈α
to␈α
␈↓αl␈↓␈α
and,␈αsince␈α
labels
␈↓ α←␈↓have␈α↔no␈α⊗computational␈α↔effect,␈α⊗begin␈α↔the␈α⊗evaluation␈α↔of␈α↔the␈α⊗assignment
␈↓ α←␈↓statement: ␈↓αu ← g[y;x;z]␈↓:
␈↓"∀␈↓ α←␈↓␈↓ βK␈↓ ∧+␈↓α[... l u ← g[y;x;z];...]␈↓ εc␈↓ π∪[...u ← g[y;x;z]; ...]
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧+E␈↓β2␈↓α␈↓ ¬←␈↓ εc␈↓ π∪E␈↓β2␈↓α
␈↓"β␈↓ α←␈↓α␈↓ βK E␈↓β1␈↓α␈↓ ∧+| E␈↓β1␈↓α␈↓ ¬←␈↓ εc E␈↓β1␈↓α␈↓ π∪| E␈↓β1␈↓α
␈↓"β␈↓ α←␈↓α␈↓ βK __________␈↓ ¬←=>␈↓ εc_________
␈↓"β␈↓ α←␈↓α␈↓ βK l␈↓ ∧+| 2␈↓ ¬←␈↓ εcl␈↓ π∪| 2
␈↓"β␈↓ α←␈↓α␈↓ βK x␈↓ ∧+| ( )␈↓ ¬←␈↓ εcx␈↓ π∪| ( )
␈↓"∀␈↓ α←␈↓We evaluate ␈↓αg[y;x;z]␈↓:
␈↓"∞␈↓ α←␈↓␈↓ βK␈↓ ∧+␈↓αprog[[ ] ...]␈↓ ¬←␈↓ εc[...go[l]; ... return[first[x]]; ...]
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧+E␈↓β3␈↓α␈↓ ¬←␈↓ εc␈↓ π∪E␈↓β4␈↓α
␈↓"β␈↓ α←␈↓α␈↓ βK E␈↓β2␈↓α␈↓ ∧+| E␈↓β2␈↓α␈↓ ¬←␈↓ εc E␈↓β3␈↓α␈↓ π∪| E␈↓β3␈↓α
␈↓"β␈↓ α←␈↓α␈↓ βK __________␈↓ ¬←=>␈↓ εc_________
␈↓"β␈↓ α←␈↓α␈↓ βK x␈↓ ∧+| (A B)
␈↓"β␈↓ α←␈↓α␈↓ βK y␈↓ ∧+| ( )
␈↓"β␈↓ α←␈↓α␈↓ βK z␈↓ ∧+| 3
␈↓"∀␈↓ α←␈↓The␈α
␈↓αgo[l]␈↓␈α
will␈α
search␈α
the␈α
control␈α
chain;␈α
it␈α
looks␈α
in␈α
the␈α
␈↓αprog␈↓␈α
form␈α
of␈α
E␈↓β3␈↓␈αbut
␈↓ α←␈↓finds␈αno␈αlabel␈α␈↓αl␈↓.␈αIt␈α
examines␈αthe␈α␈↓αprog␈↓␈αof␈αE␈↓β1␈↓␈α
next,␈αand␈αthere␈αit␈αdoes␈α
find␈αthe
␈↓ α←␈↓␈↓4.2␈↓ λ'The ␈↓αprog␈↓-feature 191␈↓α
␈↓"β␈↓ α←␈↓label␈α
␈↓αl␈↓.␈α
Thus␈αexecution␈α
would␈α
be␈α
continued␈αat␈α
the␈α
assignment␈αstatement␈α
using
␈↓ α←␈↓E␈↓β2␈↓,␈α↔the␈α⊗environment␈α↔which␈α⊗bound␈α↔the␈α⊗␈↓αprog␈↓ variables.␈α↔ In␈α↔general,␈α⊗we
␈↓ α←␈↓continue in the environment which was created on entry to the ␈↓αprog␈↓ body.
␈↓"β␈↓ α←␈↓␈↓ β'Notice␈αthat␈αonce␈αwe␈αhave␈αleft␈αE␈↓β4␈↓␈αthere␈αis␈αno␈αway␈αto␈αjump␈αback␈α
into␈αit.
␈↓ α←␈↓We␈αcan␈αonly␈αsearch␈αdown␈αthe␈αcontrol␈αchain,␈αand␈αthe␈αentry␈αto␈α␈↓αg␈↓␈αis␈α
not␈αbelow
␈↓ α←␈↓that␈αof␈α␈↓αf␈↓␈αon␈αthat␈αchain.␈α
An␈αextension␈αof␈αthe␈αsemantics␈αof␈αLISP␈α
could␈αallow
␈↓ α←␈↓such␈α⊗generalized␈α⊗control␈α⊗and␈α⊗we␈α⊗will␈α⊗develop␈α⊗some␈α⊗of␈α⊗those␈α⊗ideas␈α⊗in
␈↓ α←␈↓Section 4.4.
␈↓"β␈↓ α←␈↓␈↓ β'If␈αwe␈αexecuted␈αthe␈α␈↓αreturn[first[x]]␈↓␈αin␈αE␈↓β4␈↓␈αan␈αaction␈αsimilar␈αto␈αthat␈αof␈α␈↓αgo␈↓
␈↓ α←␈↓would␈αtranspire.␈αWe␈αwould␈αevaluate␈α␈↓αfirst[x]␈↓,␈αgetting␈α␈↓αA␈↓.␈αWe␈αwould␈αsearch␈αthe
␈↓ α←␈↓control␈α⊃chain␈α⊃for␈α∩the␈α⊃␈↓¬latest␈↓␈α⊃␈↓αprog␈↓ expression;␈α⊃here␈α∩found␈α⊃in␈α⊃E␈↓β3␈↓;␈α∩and␈α⊃then
␈↓ α←␈↓return␈α∞control␈α∞to␈α∞the␈α∂environment␈α∞designated␈α∞in␈α∞the␈α∞control␈α∂quadrant;␈α∞here
␈↓ α←␈↓E␈↓β2␈↓.␈α∂Thus␈α∂we␈α∂return␈α∂␈↓αA␈↓␈α∂as␈α∂the␈α⊂value␈α∂of␈α∂␈↓αg[y;x;z]␈↓.␈α∂Since␈α∂the␈α∂call␈α∂on␈α∂␈↓αg␈↓␈α⊂was␈α∂a
␈↓ α←␈↓component␈αof␈αthe␈αassignment␈α␈↓αu ← g[y;x;z]␈↓,␈αwe␈αmust␈αcomplete␈αthat␈αassignment.
␈↓ α←␈↓We␈α∞search␈α∞the␈α∞␈↓↓access␈↓␈α∂chain␈α∞for␈α∞␈↓αu␈↓.␈α∞Since␈α∞␈↓αu␈↓␈α∂is␈α∞not␈α∞found␈α∞we␈α∞make␈α∂a␈α∞global
␈↓ α←␈↓assignment in E␈↓β0␈↓:
␈↓"∀␈↓ α←␈↓␈↓ ∧π␈↓ ∧gE␈↓β0␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧π␈↓ ∧g/| /␈↓β
␈↓"β␈↓ α←␈↓β␈↓ ∧π______________␈↓α
␈↓"β␈↓ α←␈↓α␈↓ ∧π f␈↓ ∧g| λ[[y;z] ...]
␈↓"β␈↓ α←␈↓α␈↓ ∧π g␈↓ ∧g| λ[[x;y;z] ...]
␈↓"β␈↓ α←␈↓α␈↓ ∧π u␈↓ ∧g| A
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α↔ability␈α_to␈α↔evaluate␈α↔the␈α_argument␈α↔to␈α↔␈↓αgo␈↓␈α_results␈α↔in␈α_a␈α↔useful
␈↓ α←␈↓programming␈αtrick.␈α
Let␈α␈↓αl␈↓␈αbe␈α
a␈αlist␈α
of␈αdotted␈αpairs,␈α
each␈αof␈α
the␈αform,␈α␈↓α(␈↓object␈↓βi␈↓␈α
.
␈↓ α←␈↓label␈↓βi␈↓α)␈↓.␈α⊂At␈α⊃each␈α⊂label␈↓βi␈↓␈α⊃we␈α⊂begin␈α⊃a␈α⊂piece␈α⊃of␈α⊂program␈α⊃to␈α⊂be␈α⊃executed␈α⊂when
␈↓ α←␈↓object␈↓βi␈↓ has been recognized. Then the construct:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓↓UGH␈↓␈↓ ¬h␈↓αgo[cdr[assoc[x;l]]]␈↓
␈↓"∀␈↓ α←␈↓can␈α⊃be␈α⊃used␈α⊃to␈α⊂"dispatch"␈α⊃to␈α⊃the␈α⊃appropriate␈α⊃code␈α⊂when␈α⊃␈↓αx␈↓␈α⊃is␈α⊃one␈α⊃of␈α⊂the
␈↓ α←␈↓object␈↓βi␈↓.␈α
This␈α
is␈αan␈α
instance␈α
of␈α
␈↓↓table-driven␈↓␈αprogramming.␈α
The␈α
blocks␈αof␈α
code
␈↓ α←␈↓dispatched␈α
to␈α
can␈α
be␈α
distributed␈α
throughout␈α
the␈α
body␈α
of␈α
the␈α
␈↓αprog␈↓.␈αEach␈α
block
␈↓ α←␈↓of␈αcode␈αwill␈αusually␈αbe␈αfollowed␈αby␈αa␈α␈↓αgo␈↓␈αback␈αto␈αthe␈αcode␈αinvolving␈αequation
␈↓ α←␈↓␈↓↓UGH␈↓␈α∃(above).␈α∃In␈α∃fact␈α∃the␈α∃argument␈α⊗␈↓αl␈↓␈α∃in␈α∃␈↓↓UGH␈↓␈α∃may␈α∃be␈α∃␈↓¬global␈↓␈α⊗to␈α∃the
␈↓ α←␈↓␈↓αprog␈↓-body.␈α↔ The␈α↔effect␈α↔is␈α⊗to␈α↔make␈α↔a␈α↔␈↓αprog␈↓␈α⊗which␈α↔is␈α↔very␈α↔difficult␈α⊗to
␈↓ α←␈↓understand.␈α⊂ The␈α∂LISP␈α⊂␈↓αselect␈↓␈α∂(page 157)␈α⊂will␈α∂handle␈α⊂many␈α∂of␈α⊂the␈α∂possible
␈↓ α←␈↓applications␈αof␈αthis␈αcoding␈αtrick␈αand␈αresult␈αin␈αa␈αmore␈αreadable␈αprogram.␈αThe
␈↓ α←␈↓case-statement␈α⊂(page 193)␈α⊃present␈α⊂in␈α⊃some␈α⊂other␈α⊂languages␈α⊃is␈α⊂also␈α⊃a␈α⊂better
␈↓ α←␈↓means of handling this problem.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α␈↓αgo␈↓␈αstatement␈αis␈α
useful␈αif␈αused␈αwith␈α
discretion.␈αIt␈αis␈αa␈αbuilding␈α
block
␈↓ α←␈↓for␈α⊂constructing␈α⊂more␈α⊂complex␈α⊂control␈α⊂regimes,␈α⊂particularly␈α⊂since␈α⊂the␈α∂label
␈↓ α←␈↓need␈αnot␈αbe␈αlocal␈αto␈αthe␈α␈↓αprog␈↓␈αbut␈αonly␈αneed␈αbe␈αaccessible␈αthrough␈αthe␈αcontrol
␈↓ α←␈↓chain.␈α⊂We␈α⊂will␈α⊂examine␈α⊂some␈α∂more␈α⊂complex␈α⊂kinds␈α⊂of␈α⊂control␈α⊂behavior␈α∂in
␈↓ α←␈↓Section 4.4.
␈↓ α←␈↓␈↓192 Imperative Constructs in LISP␈↓
(4.2␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Now␈α↔to␈α↔the␈α⊗problem␈α↔of␈α↔translating␈α⊗a␈α↔␈↓αprog␈↓␈α↔into␈α↔an␈α⊗S-expression
␈↓ α←␈↓representation: the construct,
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ∧P␈↓αprog[[v␈↓β1␈↓α; ...; v␈↓βn␈↓α] ... ]␈↓ will be translated to:
␈↓" ␈↓ α←␈↓␈↓ β'␈↓ ¬E␈↓α(PROG(V1 ... VN) ... )␈↓
␈↓"∀␈↓ α←␈↓The␈α⊃body␈α⊃of␈α⊃the␈α⊃␈↓αprog␈↓␈α⊃must␈α⊃be␈α⊃handled␈α⊃specially␈α⊃by␈α⊃a␈α⊃new␈α⊃piece␈α⊃of␈α⊂the
␈↓ α←␈↓evaluator since ␈↓αprog␈↓ is a special form.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αmust␈αalso␈αbe␈αcareful␈αabout␈αthe␈αinterpretation␈αof␈α←.␈α We␈αwill␈αwrite␈α␈↓αx
␈↓ α←␈↓α← y␈↓ in prefix form: ␈↓αsetq[x;y]␈↓. We will map this to:
␈↓"∀␈↓ α←␈↓␈↓ ε␈↓α(SETQ X Y) ␈↓
␈↓"β␈↓ α←␈↓The␈α∞assignment,␈α
␈↓αsetq␈↓,␈α∞is␈α∞also␈α
a␈α∞special␈α
form.␈α∞ For␈α∞if␈α
␈↓αx␈↓␈α∞and␈α
␈↓αy␈↓␈α∞have␈α∞values␈α
␈↓α2␈↓
␈↓ α←␈↓and␈α∞␈↓α3␈↓,␈α∞for␈α∞example,␈α∞then␈α∞the␈α∞call-by-value␈α∞interpretation␈α∞of␈α∂␈↓αsetq[x;y]␈↓␈α∞would
␈↓ α←␈↓say␈α∞␈↓αsetq[2;3]␈↓.␈α∞This␈α∞was␈α
not␈α∞our␈α∞intention.␈α∞ We␈α
want␈α∞to␈α∞evaluate␈α∞the␈α
second
␈↓ α←␈↓argument to ␈↓αsetq␈↓ while stopping the evaluation of the first argument.
␈↓"β␈↓ α←␈↓␈↓ β'LISP␈α
has␈αanother␈α
assignment-like␈α
operator␈αcalled␈α
␈↓αset␈↓.␈α
Both␈αarguments
␈↓ α←␈↓of␈α⊃this␈α⊃binary␈α⊃operator␈α⊃are␈α⊃evaluated;␈α⊃the␈α⊃value␈α⊃of␈α⊃the␈α⊃first␈α⊃argument␈α⊂is
␈↓ α←␈↓expected␈α⊃to␈α⊃be␈α⊃a␈α⊃representation␈α⊃of␈α⊂a␈α⊃variable;␈α⊃that␈α⊃is,␈α⊃the␈α⊃first␈α⊂argument
␈↓ α←␈↓evaluates␈αto␈αa␈αliteral␈αatom.␈α The␈αsecond␈αargument␈αis␈αa␈αLISP␈αform␈αand␈αusing
␈↓ α←␈↓the␈αvalue␈αof␈αthat␈αform,␈αan␈αassignment␈αis␈αmade␈αto␈αthe␈αvariable␈αrepresented␈αby
␈↓ α←␈↓the first argument. Thus ␈↓αsetq[x;y]␈↓ is synonymous with ␈↓αset[quote[x];y]␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'As␈α∞a␈α∞more␈α∞complex␈α∞example,␈α∞consider␈α∞␈↓αset[z;␈α∞plus[x;1]]␈↓.␈α∞ If␈α∞the␈α
current
␈↓ α←␈↓value␈α∀of␈α∀variable␈α∀␈↓αz␈↓␈α∀is␈α∀an␈α∀identifier,␈α∀then␈α∀␈↓αset[z;␈α∀plus[x;1]]␈↓␈α∃makes␈α∀sense.
␈↓ α←␈↓Assume␈αthe␈αcurrent␈αvalue␈αof␈α
␈↓αz␈↓␈αis␈α␈↓αA␈↓;␈αand␈αassume␈α
the␈αcurrent␈αvalue␈αof␈α␈↓αx␈↓␈α
is␈α␈↓α2␈↓;
␈↓ α←␈↓since␈α␈↓αA␈↓␈αrepresents␈αthe␈αidentifier␈α␈↓αa␈↓,␈αthe␈αeffect␈αof␈αthe␈α␈↓αset␈↓␈αstatement␈αis␈αto␈αassign
␈↓ α←␈↓the␈α
value␈α␈↓α3␈↓␈α
to␈α
␈↓αa␈↓.␈α Normally␈α
when␈α
making␈αassignments,␈α
we␈α
want␈αto␈α
assign␈αto␈α
a
␈↓ α←␈↓␈↓¬name␈↓ and not a ␈↓¬value␈↓; thus we will tend to use the ␈↓αsetq␈↓ form.
␈↓"β␈↓ α←␈↓␈↓ β'Finally, here is a translation of the body of the ␈↓αprog␈↓ version of ␈↓αlength:␈↓ ␈↓α
␈↓"∀␈↓ α←␈↓α␈↓ β'(LAMBDA␈↓ ∧C(L)
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧C(PROG (L1 C)
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧C␈↓ ∧s(SETQ L1 L)
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧C␈↓ ∧s(SETQ C 0)
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧CA␈↓ ∧s(COND ((NULL L1) (RETURN C)))
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧C␈↓ ∧s(SETQ C (ADD1 C))
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧C␈↓ ∧s(SETQ L1 (REST L1))
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧C␈↓ ∧s(GO A) ))
␈↓"∀␈↓ α←␈↓α␈↓ β'␈↓ Now␈α
that␈α
assignment␈α
statements␈α
have␈α
been␈α
described,␈α
let's␈α
re-examine
␈↓ α←␈↓"<=".␈αWe␈αalready␈αknow␈α(page 147)␈αthat␈α"<="␈αdoes␈αmore␈αthan␈αsimply␈αassociate
␈↓ α←␈↓the␈α
right␈α
hand␈αside␈α
with␈α
a␈α
symbol␈αtable␈α
entry␈α
of␈α
the␈αleft␈α
hand␈α
side;␈α
it␈αmust
␈↓ α←␈↓also␈αassociate␈αan␈αenvironment␈α
with␈αthe␈αfunction␈αbody,␈αand␈α
this␈αenvironment
␈↓ α←␈↓is␈αto␈α
be␈αused␈α
for␈αaccessing␈α
non-local␈αvariables.␈α
This␈αoperation␈α
of␈αassociating
␈↓ α←␈↓environments is called forming the ␈↓↓closure␈↓. We thus might be tempted to say:
␈↓"∀␈↓ α←␈↓α␈↓ ∧Kf <= λ[[ ... ] ...] ␈↓is␈↓α f ← function[λ[[ ...] ...] ]
␈↓"∀␈↓ α←␈↓Alas, this implementation is still not sufficient as we will see in Section 4.11.
␈↓ α←␈↓␈↓4.2␈↓ λ'The ␈↓αprog␈↓-feature 193␈↓α
␈↓"β␈↓ α←␈↓␈↓ ¬2␈↓↓Problems involving ␈↓αprog␈↓↓␈↓α
␈↓"→␈↓ α←␈↓1. Write ␈↓αprog␈↓-versions of the following functions (or predicates).
␈↓" ␈↓ α←␈↓␈↓ β∂a. ␈↓αmember␈α∞<=␈α∂λ[[x;y]␈α∞...]␈↓:␈↓α␈α∞x␈↓␈α∂is␈α∞atomic;␈α∞␈↓αy␈↓␈α∂is␈α∞a␈α∞list␈α∂of␈α∞atoms.␈α∞ ␈↓αmember␈↓␈α∂is␈α∞to
␈↓ α←␈↓␈↓ β?return ␈↓
t␈↓ just in the case that ␈↓αx␈↓ is one of the elements in ␈↓αy␈↓.
␈↓" ␈↓ α←␈↓␈↓ β∂b. The factorial function.
␈↓" ␈↓ α←␈↓␈↓ β∂c. delete␈α<=␈α
λ[[x;y]␈α...␈α
]:␈α␈↓αx␈↓␈α
is␈αatomic;␈α
␈↓αy␈↓␈αis␈α
a␈αlist␈α
of␈αatoms.␈α
␈↓αdelete␈↓␈αis␈αto␈α
return
␈↓ α←␈↓␈↓ β?a list which looks like ␈↓αy␈↓, except all occurrences of ␈↓αx␈↓ have been deleted.
␈↓" ␈↓ α←␈↓␈↓ β∂d. The ␈↓αappend␈↓ function.
␈↓" ␈↓ α←␈↓␈↓ β∂e. ␈↓αlast␈α<=␈α
λ[[x]␈α...]␈↓:␈α␈↓αx␈↓␈α
is␈αa␈α
non-empty␈αlist.␈α ␈↓αlast␈↓␈α
is␈αto␈α
return␈αthe␈αlast␈α
element
␈↓ α←␈↓␈↓ β?in ␈↓αx␈↓.
␈↓" ␈↓ α←␈↓␈↓ β∂f. Now write the S-expr translations of each of your functions.
␈↓" ␈↓ α←␈↓2. What is necessary to extend the evaluator to recognize ␈↓αprog␈↓ and friends?
␈↓" ␈↓ α←␈↓3. The␈α⊗␈↓αgo[cdr[...]]␈↓-construct␈α∃on␈α⊗page 191␈α∃is␈α⊗better␈α∃handled␈α⊗with␈α⊗a␈α∃␈↓↓case
␈↓ α←␈↓↓␈↓ β∂statement␈↓. A typical syntax for such might be:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ ∧tcase<index>of <form␈↓β1␈↓>; ... ;<form␈↓βn␈↓>
␈↓"∀␈↓ α←␈↓␈↓ β∂<index>␈α
is␈α
to␈α
evaluate␈α
to␈α
an␈α∞integer,␈α
i.␈α
Where␈α
0<i≤n.␈α
The␈α
i␈↓πth␈↓␈α∞<form>␈α
of
␈↓ α←␈↓␈↓ β∂the␈αcase-statement␈αis␈αexecuted,␈αand␈αis␈αthe␈αvalue␈αof␈αthe␈αstatement.␈α Give␈αa
␈↓ α←␈↓␈↓ β∂representation␈α↔for␈α↔the␈α↔case␈α_statement␈α↔and␈α↔extend␈α↔the␈α_evaluator␈α↔to
␈↓ α←␈↓␈↓ β∂recognize it.
␈↓" ␈↓ α←␈↓4. Some languages allow constructs like:
␈↓"∀␈↓ α←␈↓␈↓ βW(␈↓↓if␈↓ p(x) ␈↓↓then␈↓ x ␈↓↓else␈↓ y) ← exp, which is to mean the same as:
␈↓"∀␈↓ α←␈↓␈↓ ¬β␈↓↓if␈↓ p(x) ␈↓↓then␈↓ x← exp ␈↓↓else␈↓ y ← exp
␈↓"β␈↓ α←␈↓␈↓ β∂Can such a construct be written in LISP?
␈↓" ␈↓ α←␈↓5. Compare␈αthe␈α␈↓αprog␈↓␈α
version␈αof␈α␈↓αlength␈↓␈αon␈α
page 186␈αwith␈α␈↓αlength␈↓β1␈↓␈αon␈α
page 47.
␈↓ α←␈↓␈↓ β∂Do you see any interesting relationships?
␈↓" ␈↓ α←␈↓6. Give␈α∃a␈α∃macro␈α∃definition␈α∃of␈α∀an␈α∃extended␈α∃␈↓αSETQ␈↓,␈α∃which␈α∃is␈α∃called␈α∀as
␈↓ α←␈↓␈↓ β∂␈↓α(SETQ ␈↓var␈↓β1␈↓ exp␈↓β1␈↓ ... var␈↓βn␈↓ exp␈↓βn␈↓α)␈↓.␈α∩ Each␈α∩var␈↓βi␈↓␈α⊃is␈α∩a␈α∩name;␈α⊃each␈α∩exp␈↓βi␈↓␈α∩is␈α⊃an
␈↓ α←␈↓␈↓ β∂expression␈αto␈αbe␈αevaluated␈αand␈αassigned␈αto␈αvar␈↓βi␈↓.␈αThe␈αassignments␈αshould
␈↓ α←␈↓␈↓ β∂take␈α⊂effect␈α∂from␈α⊂"left-to-right".␈α⊂ Thus␈α∂␈↓α(SETQ X 2 Y (TIMES 2 X) X 3)␈↓
␈↓ α←␈↓␈↓ β∂when executed should assign ␈↓α3␈↓ to ␈↓αX␈↓ and ␈↓α4␈↓ to ␈↓αY␈↓.
␈↓" ␈↓ α←␈↓7. Express ␈↓αsetq␈↓ as a macro over ␈↓αset␈↓.
␈↓" ␈↓ α←␈↓8. Write␈αa␈α␈↓αprog␈↓␈αwhich␈αwill␈αterminate␈αif␈αcall-by-value␈αevaluation␈αis␈αused,␈αbut
␈↓ α←␈↓␈↓ β∂will not terminate under call-by-name.
␈↓" ␈↓ α←␈↓9. Use␈α∪your␈α∪␈↓αprog␈↓␈α∪version␈α∪of␈α∩␈↓αfact␈↓␈α∪(prob 1, ␈↓↓b␈↓)␈α∪and␈α∪evaluate␈α∪␈↓αfact[2]␈↓␈α∩using
␈↓ α←␈↓␈↓ β∂Weizenbaum␈α≤diagrams.␈α≤Note␈α≤the␈α≤difference␈α≤between␈α≤the␈α≠internal
␈↓ α←␈↓␈↓ β∂structures␈α
used␈α
here␈α
and␈α
the␈α
structures␈α
used␈α
in␈α
the␈α
recursive␈α
version.␈α
This
␈↓ α←␈↓␈↓ β∂difference␈α⊃in␈α⊂implementation␈α⊃overhead␈α⊂is␈α⊃a␈α⊂quantitive␈α⊃measure␈α⊃of␈α⊂the
␈↓ α←␈↓␈↓ β∂expense of recursion versus the expense of iteration.
␈↓ α←␈↓␈↓194 Imperative Constructs in LISP␈↓
(4.3␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬5␈↓↓4.3 Alternatives to ␈↓αprog␈↓↓␈↓α
␈↓"β␈↓ α←␈↓The␈α∪␈↓αprog␈↓␈α∀feature␈α∪of␈α∪LISP␈α∀is␈α∪an␈α∪effective␈α∀means␈α∪for␈α∀encoding␈α∪iterative
␈↓ α←␈↓algorithms,␈α⊃however␈α⊃it␈α⊃suffers␈α⊃from␈α⊂a␈α⊃few␈α⊃draw-backs.␈α⊃For␈α⊃example,␈α⊂The
␈↓ α←␈↓label-and-␈↓αgo␈↓␈α∀style␈α∃of␈α∀control␈α∃is␈α∀only␈α∃a␈α∀slight␈α∃elaboration␈α∀of␈α∃the␈α∀control
␈↓ α←␈↓mechanisms␈α⊂which␈α⊂are␈α⊂typically␈α⊂used␈α⊂to␈α⊂control␈α⊂a␈α⊂hardware␈α⊂machine,␈α∂and
␈↓ α←␈↓thus␈α∂the␈α∞level␈α∂of␈α∞description␈α∂which␈α∂is␈α∞required␈α∂tends␈α∞to␈α∂obscure␈α∂the␈α∞actual
␈↓ α←␈↓flow␈αof␈αthe␈αalgorithm␈αunless␈αthe␈αprogrammer␈αis␈αcareful.␈αA␈αslight␈αextension␈αto
␈↓ α←␈↓conditional␈α
expressions␈α
and␈α
conditional␈α
statements␈α
can␈α
alleviate␈α
some␈α
of␈αthe
␈↓ α←␈↓confusion which is likely when constucting complex ␈↓αprog␈↓s.
␈↓"β␈↓ α←␈↓␈↓ β'Conditional␈αexpressions␈αare␈αcurrently␈αdefined␈αsuch␈αthat␈αeach␈α␈↓↓e␈↓βi␈↓␈αmust␈αbe
␈↓ α←␈↓a␈αsingle␈αexpression.␈α With␈αthe␈αintroduction␈αof␈αside␈αeffects,␈αit␈αis␈αconvenient␈αto
␈↓ α←␈↓extend␈αconditionals␈α
to␈αinclude␈α
components␈αof␈α
the␈αform:␈α
␈↓↓p␈↓βi␈↓α → ␈↓↓e␈↓βi1␈↓α; ... ;␈↓↓e␈↓βin␈↓.␈α This
␈↓ α←␈↓extended␈αcomponent␈αis␈αto␈αbe␈αevaluated␈αas␈αfollows:␈αif␈α␈↓↓p␈↓βi␈↓␈αis␈αtrue,␈αthen␈αevaluate
␈↓ α←␈↓the␈α␈↓↓e␈↓βij␈↓'s␈α
from␈αleft␈α
to␈αright,␈α
with␈αthe␈αvalue␈α
of␈αthe␈α
component␈αto␈α
be␈αthe␈αvalue␈α
of
␈↓ α←␈↓␈↓↓e␈↓βin␈↓.␈↓π 7␈↓
␈↓"β␈↓ α←␈↓␈↓ β'For example, this feature, used in ␈↓αprog␈↓s would allow us to replace:
␈↓"∀␈↓ α←␈↓␈↓ βK␈↓ β{ ....
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ β{[␈↓↓p␈↓β1␈↓α → go[l]]
␈↓"β␈↓ α←␈↓α␈↓ βKm
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ β{ ...
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ β{return[␈↓
t␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ βKl␈↓ β{␈↓↓e␈↓β1␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ β{␈↓↓e␈↓β2␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ β{ ...
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ β{go[m];
␈↓" ␈↓ α←␈↓with:
␈↓" ␈↓ α←␈↓␈↓ ∧} ... [␈↓↓p␈↓β1␈↓α → ␈↓↓e␈↓β1␈↓α; ␈↓↓e␈↓β2␈↓α; ... ] ...; return[␈↓
t␈↓α]]
␈↓"∀␈↓ α←␈↓The␈αimproved␈αreadibility␈αis␈αlargely␈αdo␈α
to␈αthe␈αlocalizing␈αor␈α"packaging"␈αof␈α
the
␈↓ α←␈↓actions␈α
with␈α
their␈αinitiators;␈α
we␈α
need␈αnot␈α
scan␈α
an␈αarbitrarily␈α
long␈α
piece␈αof␈α
text
␈↓ α←␈↓to discover what the computation will be when the predicate is true.
␈↓"β␈↓ α←␈↓␈↓ β'Several␈αlanguages␈α
have␈αincluded␈αmore␈α
"packaged"␈αversions␈α
of␈αiterative
␈↓ α←␈↓control.␈α∀The␈α∀motivation␈α∀is␈α∀similar␈α∀to␈α∀that␈α∀which␈α∀we␈α∀used␈α∀in␈α∪justifying
␈↓ α←␈↓recursive␈α∪control:␈α∪we␈α∪didn't␈α∪care␈α∪␈↓¬how␈↓␈α∪recursion␈α∪was␈α∪implemented,␈α∪all␈α∪we
␈↓ α←␈↓wished to discuss was the ␈↓¬effect␈↓ or ␈↓¬behavior␈↓ of recursion.␈↓π 8␈↓
␈↓"β␈↓ α←␈↓␈↓ β'An␈α∂iterative␈α∂unit␈α∂must␈α∂allow␈α∂the␈α∂programmer␈α∂a␈α∂reasonable␈α∂degree␈α∂of
␈↓ α←␈↓freedom␈α∂and␈α∂naturalness␈α∂in␈α∂expression.␈α∞What␈α∂should␈α∂also␈α∂be␈α∂recognized␈α∞is
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 7␈↓This␈α_extended␈α↔conditional␈α_expression␈α↔([Bob 69])␈α_is␈α_available␈α↔in
␈↓ α←␈↓several␈α∀versions␈α∀of␈α∀LISP:␈α∀LISP 1.6␈α∀[Qua 72],␈α∀MACLISP␈α∀[Moo 74],␈α∀and
␈↓ α←␈↓INTERLISP [Int 75].
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 8␈↓We␈α_␈↓¬could␈↓␈α_have␈α_replaced␈α_recursive␈α_control␈α_with␈α_an␈α_appropriate
␈↓ α←␈↓combination of label-and-␈↓αgo␈↓'s. and a simulated stack. We will do so shortly.
␈↓ α←␈↓␈↓4.3␈↓ λαAlternatives to ␈↓αprog␈↓ 195␈↓α
␈↓"β␈↓ α←␈↓that␈αthe␈αstructural␈αunit␈αshould␈αbe␈αamenable␈αto␈αanalysis␈αto␈αthe␈αsame␈αdegree␈αas
␈↓ α←␈↓that␈α⊃allowed␈α⊃in␈α⊂recursion.␈α⊃We␈α⊃must␈α⊂be␈α⊃able␈α⊃to␈α⊂state␈α⊃precise␈α⊃properties␈α⊂of
␈↓ α←␈↓algorithms␈α∪which␈α∪use␈α∪these␈α∪constructs,␈α∪and␈α∪we␈α∪should␈α∪be␈α∪able␈α∪to␈α∩prove
␈↓ α←␈↓properties␈α
of␈αsuch␈α
algorithms.␈α With␈α
the␈αcontrol␈α
of␈αthe␈α
loop␈αstructure␈α
in␈αthe
␈↓ α←␈↓language␈αrather␈α
than␈αin␈αthe␈α
hands␈αof␈α
the␈αprogrammer,␈αthe␈α
static␈αtext␈αand␈α
the
␈↓ α←␈↓dynamic␈α⊃flow␈α∩of␈α⊃the␈α∩execution␈α⊃have␈α∩a␈α⊃close␈α∩relationship.␈α⊃General␈α∩use␈α⊃of
␈↓ α←␈↓label-and-␈↓αgo␈↓'s␈α∃and␈α∀assignments␈α∃does␈α∃not␈α∀maintain␈α∃such␈α∃properties.␈α∀ Our
␈↓ α←␈↓iterative␈α⊗control␈α⊗construct␈α⊗should␈α↔therefore␈α⊗capture␈α⊗all␈α⊗of␈α↔the␈α⊗essential
␈↓ α←␈↓ingredients␈α
of␈αan␈α
iteration,␈α
and␈αits␈α
semantics␈α
should␈αbe␈α
restricted␈α
such␈αthat␈α
its
␈↓ α←␈↓static text does indeed reflect its dynamic flow.
␈↓"β␈↓ α←␈↓␈↓ β'Our␈α∞first␈α
example␈α∞is␈α∞based␈α
on␈α∞the␈α∞MacLISP␈α
␈↓αdo␈↓␈α∞[Moo 74].␈α∞With␈α
some
␈↓ α←␈↓inessential changes, its syntax is:
␈↓"∀␈↓ α←␈↓␈↓ ∧∨␈↓αdo␈↓ ∧C[␈↓<var␈↓β1␈↓> <init␈↓β1␈↓> <step␈↓β1␈↓>;
␈↓"β␈↓ α←␈↓␈↓ ∧∨␈↓ ∧C <var␈↓β2␈↓> <init␈↓β2␈↓> <step␈↓β2␈↓>;
␈↓"β␈↓ α←␈↓␈↓ ∧∨␈↓ ∧C ...
␈↓"β␈↓ α←␈↓␈↓ ∧∨␈↓ ∧C <var␈↓βn␈↓> <init␈↓βn␈↓> <step␈↓βn␈↓>;
␈↓"β␈↓ α←␈↓␈↓ ∧∨␈↓ ∧C[<pred> → <exit>]
␈↓"β␈↓ α←␈↓␈↓ ∧∨␈↓ ∧C <body> ]
␈↓"∀␈↓ α←␈↓The␈α∞construct␈α∞captures␈α∞the␈α∞ideas␈α∞of␈α∞intialization␈α∞and␈α∞updating␈α∂of␈α∞variables
␈↓ α←␈↓nicely.␈α⊂ Each␈α⊂<var␈↓βi␈↓>␈α⊂is␈α⊂initialized␈α⊂to␈α⊂its␈α⊂<init␈↓βi␈↓>-value␈α⊂simultaneously.␈α∂ Each
␈↓ α←␈↓<step␈↓βi␈↓>␈α∃is␈α∀a␈α∃form␈α∃which␈α∀will␈α∃be␈α∀evaluated␈α∃simultaneously␈α∃upon␈α∀proper
␈↓ α←␈↓completion␈α
of␈α
each␈α
cycle␈α
of␈α
the␈α
␈↓αdo␈↓.␈α
The␈α
<pred>␈α
is␈α
evaluated,␈α
and␈α
on␈αgiving
␈↓ α←␈↓value␈α⊂␈↓
t␈↓␈α⊂the␈α⊂loop␈α⊂will␈α⊂terminate,␈α⊂returning␈α⊂the␈α⊂value␈α⊂of␈α⊂<exit>.␈α⊃ If␈α⊂<pred>
␈↓ α←␈↓gives␈α
␈↓
f␈↓␈α∞then␈α
<body>␈α
is␈α∞executed.␈α
This␈α
component␈α∞of␈α
the␈α
␈↓αdo␈↓␈α∞is␈α
a␈α∞␈↓αprog␈↓␈α
body;
␈↓ α←␈↓when␈α⊗the␈α∃last␈α⊗statement␈α⊗in␈α∃<body>␈α⊗is␈α⊗executed,␈α∃the␈α⊗<step␈↓βi␈↓>␈α⊗forms␈α∃are
␈↓ α←␈↓evaluated and assigned to the <var␈↓βi␈↓>'s, and another cycle of the ␈↓αdo␈↓ is begun.
␈↓"β␈↓ α←␈↓␈↓ β'Since␈α∞the␈α∞<body>␈α∞of␈α∞the␈α∞␈↓αdo␈↓␈α∂is␈α∞a␈α∞␈↓αprog␈↓␈α∞body,␈α∞the␈α∞␈↓αreturn␈↓␈α∂statement␈α∞may
␈↓ α←␈↓appear.␈α∩This␈α⊃feature␈α∩allows␈α∩the␈α⊃dynamic␈α∩flow␈α∩to␈α⊃diverge␈α∩from␈α∩the␈α⊃static
␈↓ α←␈↓text.␈↓π 9␈↓ But consider the ␈↓αdo␈↓ version of ␈↓αmember␈↓:
␈↓"∀␈↓ α←␈↓α␈↓ β'member <= λ[[a;l]␈↓ ¬do␈↓ ¬/[x l rest[x];
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ¬␈↓ ¬/ [null[x] → ␈↓
f␈↓α]
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ¬␈↓ ¬/ [eq[first[x];a] → return[␈↓
t␈↓α]] ]
␈↓"∀␈↓ α←␈↓This␈αalgorithm␈αcould␈αbe␈αexpressed␈αwithout␈α␈↓αreturn␈↓␈αbut␈αthe␈αresulting␈αprogram
␈↓ α←␈↓is unnecessarily complex.
␈↓"β␈↓ α←␈↓␈↓ β'An alternative iterative construct was proposed in [Wis 75].
␈↓"∀␈↓ α←␈↓α␈↓ β2repeat[␈↓<st-list␈↓β1␈↓>;␈↓αwhile ␈↓<pred␈↓β1␈↓>; <st-list␈↓β2␈↓>; ␈↓αuntil ␈↓<pred␈↓β2␈↓>; <st-list␈↓β3␈↓>]
␈↓"∀␈↓ α←␈↓where␈α
<pred␈↓βi␈↓>␈αis␈α
a␈αpredicate,␈α
and␈α<st-list␈↓βi␈↓>␈α
is␈α
a␈αlist␈α
of␈αstatements.␈α
The␈αlist␈α
may
␈↓"β␈↓ α←␈↓be empty, but may not contain ␈↓αreturn␈↓s or ␈↓αgo␈↓s.␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 9␈↓Compare␈α⊗this␈α∃to␈α⊗the␈α∃behavior␈α⊗of␈α∃free␈α⊗variables␈α⊗under␈α∃dynamic
␈↓ α←␈↓binding.␈α
From␈α
a␈αprogramming␈α
point␈α
of␈αview,␈α
being␈α
able␈αto␈α
escape␈α
from␈αthe
␈↓ α←␈↓static␈α⊂text,␈α⊂either␈α⊂for␈α⊂variable␈α∂reference␈α⊂or␈α⊂for␈α⊂control␈α⊂may␈α⊂be␈α∂convenient.
␈↓ α←␈↓Whether either feature is "good practice" is a matter of taste.
␈↓ α←␈↓␈↓196 Imperative Constructs in LISP␈↓
(4.3␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∪semantics␈α∪is␈α∪as␈α∪follows:␈α∪<st-list␈↓β1␈↓>␈α∪is␈α∪executed;␈α∪<pred␈↓β1␈↓>␈α∀is␈α∪then
␈↓ α←␈↓evaluated␈α
and␈αif␈α
false␈αwe␈α
exit␈αthe␈α
␈↓αrepeat␈↓␈αwith␈α
␈↓
f␈↓.␈αIf␈α
<pred␈↓β1␈↓>␈αis␈α
true,␈α
then␈αwe
␈↓ α←␈↓execute␈α
<st-list␈↓β2␈↓>␈α
and␈α
test␈α
<pred␈↓β2␈↓>;␈α
if␈α
<pred␈↓β2␈↓>␈α
is␈α
true␈α
we␈α
exit␈α
with␈α
␈↓
t␈↓,␈α
otherwise
␈↓ α←␈↓we execute <st-list␈↓β3␈↓> and iterate the loop beginning again at <st-list␈↓β1␈↓>.
␈↓"β␈↓ α←␈↓␈↓ β'For example we could write ␈↓αmember␈↓ as:
␈↓"∀␈↓ α←␈↓α␈↓ α←member <= λ[[a;l]
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧πrepeat[␈↓ ∧[while
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧π␈↓ ∧[ not[null[l]];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧π␈↓ ∧[until
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧π␈↓ ∧[ equal[a;first[l]];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ∧π␈↓ ∧[l ← rest[l]]]
␈↓"∀␈↓ α←␈↓The␈α⊗difficulty␈α⊗which␈α∃we␈α⊗encountered␈α⊗with␈α∃the␈α⊗MacLISP␈α⊗␈↓αdo␈↓␈α⊗has␈α∃been
␈↓ α←␈↓alleviated,␈αhowever␈αthe␈α␈↓αrepeat␈↓␈αconstruct␈αhas␈αseveral␈αshortcomings␈αof␈α
its␈αown.
␈↓ α←␈↓In␈α∂particular,␈α∂we␈α∂have␈α∂no␈α∂means␈α∂for␈α∂designating␈α∂what␈α∂variables␈α∂are␈α∂to␈α∂be
␈↓ α←␈↓initialized␈α
and␈α
incremented␈α
within␈αthe␈α
loop.␈α
Such␈α
variables␈α
must␈αbe␈α
declared
␈↓ α←␈↓and␈αinitialized␈αexternal␈αto␈αthe␈α␈↓αrepeat␈↓;␈αalso␈αthe␈αstepping␈αof␈αthe␈αloop␈αvariables
␈↓ α←␈↓must␈α∃be␈α∃done␈α∃using␈α∃the␈α∃assignment␈α∃statement.␈α∃ Similarly␈α∃the␈α∃power␈α∀of
␈↓ α←␈↓expression␈αon␈αleaving␈αthe␈α␈↓αrepeat␈↓␈αis␈αlimited;␈αwe␈αcannot␈αexplicitly␈αdeclare␈αwhat
␈↓ α←␈↓values are to be returned. The value is that of the appropriate <pred␈↓βi␈↓>.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Some␈α∂of␈α∂the␈α∂generality␈α∂of␈α∂␈↓αprog␈↓s␈α∂can␈α∞be␈α∂controlled␈α∂by␈α∂the␈α∂use␈α∂of␈α∂a␈α∞new
␈↓ α←␈↓␈↓ β∂control␈αstructure␈αfor␈αlist␈αoperations.␈αThe␈αconstruct␈αis␈αcalled␈α␈↓αlit␈↓.␈↓π 10␈↓␈α␈↓αlit␈↓␈αtakes
␈↓ α←␈↓␈↓ β∂three␈α
arguments:␈αa␈α
binary␈αfunction␈α
␈↓αf␈↓,␈αa␈α
list␈α
␈↓αl␈↓,␈αand␈α
a␈αvalue␈α
␈↓αv␈↓.␈α If␈α
␈↓αl␈↓␈αis␈α
empty,
␈↓ α←␈↓␈↓ β∂give␈α∩␈↓αv␈↓;␈α∩otherwise␈α∩apply␈α∩␈↓αf␈↓␈α∩to␈α∩the␈α⊃first␈α∩element␈α∩of␈α∩␈↓αl␈↓␈α∩and␈α∩the␈α∩effect␈α⊃of
␈↓ α←␈↓␈↓ β∂applying ␈↓αlit␈↓ to the remainder of ␈↓αl␈↓.
␈↓"β␈↓ α←␈↓␈↓ β∂For example ␈↓αappend␈↓ could be expressed as:
␈↓" ␈↓ α←␈↓α␈↓ ∧Eappend <= λ[[x;y] lit[function[concat];x;y]]
␈↓" ␈↓ α←␈↓␈↓ β∂Give a non-␈↓αprog␈↓ definition for ␈↓αlit␈↓.
␈↓" ␈↓ α←␈↓2. Here␈α∞is␈α∂another␈α∞useful␈α∞extension␈α∂to␈α∞LISP:␈α∞Instead␈α∂of␈α∞requiring␈α∂that␈α∞the
␈↓ α←␈↓␈↓ β∂body␈αof␈αa␈αλ-definition␈αbe␈αa␈αsingle␈αexpression:␈α␈↓λx␈↓␈αin␈α␈↓αλ[[ ... ] ␈↓λx␈↓α]␈↓,␈αallow␈αbodies
␈↓ α←␈↓␈↓ β∂of␈α∂the␈α∂form:␈α∂␈↓λx␈↓β1␈↓α; ...; ␈↓λx␈↓βn␈↓α␈↓,␈α∂giving␈α∂rise␈α∂to␈α∂λ-definitions␈α∂like␈α∂␈↓αλ[[ ... ] ␈↓λx␈↓β1␈↓α; ...; ␈↓λx␈↓βn␈↓α]␈↓.
␈↓ α←␈↓␈↓ β∂The␈α
application␈α
of␈α
such␈α
a␈α
definition␈α
means:␈α
bind␈α
the␈α
λ-variables␈α
as␈α
usual,
␈↓ α←␈↓␈↓ β∂then␈α
evaluate␈α
the␈α
␈↓λx␈↓βi␈↓'s␈αfrom␈α
left␈α
to␈α
right␈αreturning␈α
as␈α
value,␈α
␈↓λx␈↓βn␈↓.␈α Extend␈α
the
␈↓ α←␈↓␈↓ β∂evaluator of Section 3.5 to handle such constructs.
␈↓" ␈↓ α←␈↓3. Give␈α
an␈α
S-expr␈α
representation␈α
for␈αthe␈α
␈↓αrepeat␈↓␈α
expression␈α
and␈α
add␈α␈↓αrepeat␈↓␈α
to
␈↓ α←␈↓␈↓ β∂␈↓αeval␈↓ of Section 3.5.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 10␈↓Named for ␈↓αl␈↓ist ␈↓αit␈↓erator [Bar 66]
␈↓ α←␈↓␈↓4.4␈↓ λ→Extensions to ␈↓αeval␈↓ 197␈↓α
␈↓"β␈↓ α←␈↓␈↓ ¬?␈↓↓4.4 Extensions to ␈↓αeval␈↓↓␈↓α
␈↓"β␈↓ α←␈↓The␈α∂introduction␈α∂of␈α∞the␈α∂␈↓αprog␈↓-feature␈α∂completes␈α∞our␈α∂syntactic␈α∂description␈α∞of
␈↓ α←␈↓the␈α
language␈αconstructs␈α
of␈αLISP.␈α
We␈αwould␈α
like␈α
to␈αgive␈α
a␈αnew␈α
version␈αof␈α
␈↓αeval␈↓
␈↓ α←␈↓which␈αdescribes␈αthe␈αsemantics␈αof␈α␈↓αprog␈↓s␈αin␈αa␈αmanner␈αwhich␈αaccurately␈αreflects
␈↓ α←␈↓the␈α∪techniques␈α∪used␈α∪in␈α∪implementations.␈α∪We␈α∪could␈α∪simply␈α∀simulate␈α∪␈↓αprog␈↓
␈↓ α←␈↓behavior␈α∂using␈α⊂recursive␈α∂techniques,␈α∂but␈α⊂the␈α∂iterative␈α∂control␈α⊂expressed␈α∂in
␈↓ α←␈↓␈↓αprog␈↓s␈α⊃is␈α⊃an␈α⊃important␈α⊃idea␈α⊃in␈α∩its␈α⊃own␈α⊃right␈α⊃and␈α⊃is␈α⊃a␈α⊃simple␈α∩instance␈α⊃of
␈↓ α←␈↓non-recursive␈αcontrol.␈α
A␈αmechanism␈α
which␈αfaithfully␈α
implements␈αsuch␈α
control
␈↓ α←␈↓structures leads easily to the idea of ␈↓↓generalized control structures␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α≤second␈α≤interesting␈α≤feature␈α≠introduced␈α≤with␈α≤␈↓αprog␈↓s␈α≤was␈α≠the
␈↓ α←␈↓assignment␈α∀statement.␈α∀Again,␈α∀we␈α∀could␈α∀mirror␈α∀most␈α∀of␈α∀the␈α∃behavior␈α∀of
␈↓ α←␈↓assignments␈αby␈αcareful␈αuse␈αof␈αthe␈αtechniques␈αof␈αrecursion␈αand␈α
symbol␈αtables,
␈↓ α←␈↓but␈α
such␈αmodelling␈α
would␈αnot␈α
adequately␈α
reflect␈αthe␈α
intent␈αof␈α
the␈αconstruct␈α
or
␈↓ α←␈↓give␈α∂insight␈α∂into␈α∂the␈α∂techniques␈α∞used␈α∂in␈α∂implementing␈α∂such␈α∂constructs.␈α∞We
␈↓ α←␈↓could␈α⊂describe␈α⊂such␈α⊂implementations␈α∂in␈α⊂a␈α⊂low-level␈α⊂machine␈α⊂language,␈α∂but
␈↓ α←␈↓such␈α∀practice␈α∃would␈α∀only␈α∀introduce␈α∃unnecessary␈α∀details.␈α∀Rather,␈α∃we␈α∀will
␈↓ α←␈↓describe␈α~an␈α→evaluator␈α~in␈α→LISP␈α~using␈α→the␈α~techniques␈α→we␈α~have␈α→been
␈↓ α←␈↓developing.␈αIn␈α
the␈αprocess␈α
we␈αwill␈αelucidate␈α
much␈αmore␈α
than␈αjust␈α
␈↓αprog␈↓s␈αand
␈↓ α←␈↓assignments;␈αwe␈αwill␈αlay␈αbare␈αmuch␈αmore␈αof␈αthe␈αbehavior␈αwhich␈αwas␈αimplicit
␈↓ α←␈↓in␈α≠the␈α~previous␈α≠evaluators.␈α~Those␈α≠evaluators␈α~used␈α≠recursion␈α≠in␈α~the
␈↓ α←␈↓explication␈α→of␈α→recursion,␈α~frequently␈α→depended␈α→on␈α→call-by-value␈α~in␈α→the
␈↓ α←␈↓explanation␈α∞of␈α∞call-by-value,␈α∞and␈α∞suppressed␈α∞much␈α∞of␈α∞the␈α∞detail␈α∞of␈α
binding
␈↓ α←␈↓and␈α
look␈α
up.␈α
The␈α∞Weizenbaum␈α
environments␈α
added␈α
more␈α
detail,␈α∞but␈α
failed
␈↓ α←␈↓to␈α
describe␈α
an␈α
explicit␈α
mechanism␈α
for␈α
the␈α
handling␈α
of␈α
partial␈α
computations,
␈↓ α←␈↓neither␈αshowing␈α
where␈αpartial␈α
results␈αwere␈α
maintained␈αnor␈α
how␈αthe␈α
evaluator
␈↓ α←␈↓was␈α∞to␈α
remember␈α∞where␈α∞it␈α
was␈α∞in␈α∞an␈α
expression␈α∞when␈α∞it␈α
had␈α∞to␈α∞evaluate␈α
a
␈↓ α←␈↓sub-expression.␈α
All␈α
of␈α
this␈α
detail␈α
will␈α
come␈α
out␈α
in␈α
the␈α
new␈α
evaluator.␈α
Since
␈↓ α←␈↓the␈α⊂structure␈α⊃of␈α⊂the␈α⊂new␈α⊃␈↓αeval␈↓␈α⊂is␈α⊃quite␈α⊂different␈α⊂from␈α⊃those␈α⊂we␈α⊃have␈α⊂seen
␈↓ α←␈↓before,␈α
and␈α
since␈α
the␈α
level␈α
of␈α
detail␈α
is␈α
more␈α
intense,␈α
we␈α
will␈α
proceed␈αin␈α
several
␈↓ α←␈↓steps.
␈↓"β␈↓ α←␈↓␈↓ β'First␈α_we␈α_discuss␈α_some␈α_generalizations␈α_of␈α_the␈α_label-and-␈↓αgo␈↓␈α↔control
␈↓ α←␈↓structures.␈αThese␈α
ideas␈αhave␈αimportance␈α
in␈αtheir␈αown␈α
right␈αwhen␈α
we␈αdiscuss
␈↓ α←␈↓actual␈α∩interactive␈α∩implementations␈α∩of␈α∪LISP.␈α∩Next␈α∩we␈α∩develop␈α∩an␈α∪␈↓αeval␈↓␈α∩in
␈↓ α←␈↓which␈α
the␈α
handling␈α
of␈α
access␈α
structures␈α
is␈α
explicit.␈α
The␈α
innovations␈α
in␈α
this
␈↓ α←␈↓evaluator␈α∞will␈α∞form␈α∞the␈α∞basic␈α∞blocks␈α
which␈α∞we␈α∞will␈α∞use␈α∞to␈α∞model␈α
parameter
␈↓ α←␈↓passing␈α∂and␈α⊂assignments.␈α∂This␈α∂evaluator␈α⊂will␈α∂still␈α∂be␈α⊂recursively␈α∂described,
␈↓ α←␈↓and␈α∪will␈α∪not␈α∪handle␈α∪the␈α∪␈↓αprog␈↓␈α∪feature.␈α∪In␈α∪the␈α∪final␈α∪step␈α∪we␈α∀replace␈α∪the
␈↓ α←␈↓recursion␈α∞with␈α∞explicit␈α∞control␈α∞and␈α∂with␈α∞this␈α∞change␈α∞we␈α∞have␈α∞the␈α∂basis␈α∞for
␈↓ α←␈↓adequate␈αtreatment␈αof␈αnon-recursive␈αcontrol.␈α Finally␈αwe␈αpresent␈αan␈αevaluator
␈↓ α←␈↓which handles all of the ␈↓αprog␈↓-related constructs.
␈↓ α←␈↓␈↓198 Imperative Constructs in LISP␈↓
(4.5␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧]␈↓↓4.5 Non-recursive Control Structures␈↓
␈↓"β␈↓ α←␈↓On␈αpage 189␈αwe␈αdiscussed␈αthe␈α
␈↓αgo␈↓␈αconstruct.␈αIn␈αthat␈αdiscussion␈αwe␈α
noted␈αthat
␈↓ α←␈↓the␈αscope␈αof␈α
the␈α␈↓αgo␈↓␈αwas␈αnot␈α
restricted␈αto␈αthe␈αcurrent␈α
␈↓αprog␈↓;␈αwe␈αneed␈αonly␈α
locate
␈↓ α←␈↓an␈α
appropriate␈α
label␈α
in␈αa␈α
␈↓¬dynamically␈↓␈α
surrounding␈α
expression.␈α
Thus␈αwe␈α
could
␈↓ α←␈↓jump␈α∞out␈α∂of␈α∞an␈α∂expression,␈α∞passing␈α∂through␈α∞many␈α∂intervening␈α∞expressions,
␈↓ α←␈↓whereas␈α_strict␈α_recursive␈α→control␈α_requires␈α_that␈α→we␈α_exit␈α_functions␈α→in␈α_a
␈↓ α←␈↓level-by-level␈αfashion.␈α This␈αability␈αto␈αexit␈αacross␈αmany␈αlevels␈αof␈αcomputation
␈↓ α←␈↓finds␈α⊂applications␈α⊂at␈α⊂the␈α⊂system␈α⊂level␈α⊂in␈α⊂interactive␈α⊃LISP␈α⊂implementations
␈↓ α←␈↓and␈α≥is␈α≤also␈α≥a␈α≥useful␈α≤programming␈α≥feature.␈α≤For␈α≥example,␈α≥if␈α≤some
␈↓ α←␈↓extraordinary␈αcondition␈αoccurs␈αwithin␈αa␈αcomputation␈αwe␈αmight␈αwish␈αto␈αabort
␈↓ α←␈↓that␈αwhole␈αendeavor.␈αAs␈αthings␈αcurrently␈αstand␈αwe␈αwould␈αhave␈αto␈α
supply␈αan
␈↓ α←␈↓additional␈α⊂value␈α∂in␈α⊂the␈α∂range␈α⊂of␈α∂each␈α⊂function␈α∂which␈α⊂could␈α∂occur␈α⊂in␈α∂that
␈↓ α←␈↓computation.␈α
Each␈αfunction␈α
would␈αhave␈α
to␈αtest␈α
for␈αthat␈α
exception-value␈αand
␈↓ α←␈↓when␈αit␈αis␈αfound,␈αreturn␈αthat␈αvalue␈αto␈αthe␈αcaller.␈αThis␈αis␈αan␈αeffective,␈αbut␈αnot
␈↓ α←␈↓elegant,␈αsolution␈αto␈αthe␈αproblem.␈αNotice␈αthat␈αthis␈αis␈αthe␈αsolution␈αposed␈αin␈αour
␈↓ α←␈↓use␈α
of␈α
␈↓λB␈↓␈αin␈α
conjunction␈α
with␈α
strict␈αfunctions.␈α
Indeed,␈α
a␈α
more␈αelegant␈α
solution
␈↓ α←␈↓has␈αits␈αorigins␈αin␈αthe␈αearly␈αLISP␈αdebugging␈αtools.␈αIf␈αa␈αcomputation␈αproduced
␈↓ α←␈↓a␈α∂detectable␈α⊂error,␈α∂then␈α⊂it␈α∂was␈α∂the␈α⊂responsibility␈α∂of␈α⊂the␈α∂LISP␈α⊂controller␈α∂to
␈↓ α←␈↓print␈α∞an␈α∂error␈α∞message␈α∂and␈α∞terminate␈α∞the␈α∂computation.␈α∞Such␈α∂behavior␈α∞was
␈↓ α←␈↓acceptable␈α
for␈αsimple␈α
computations.␈αAs␈α
computations␈αbecame␈α
more␈αcomplex␈α
it
␈↓ α←␈↓became␈αclear␈αthat␈α
the␈αoccurrence␈αof␈α
one␈αerror␈αneed␈α
not␈αsignal␈αthe␈α
termination
␈↓ α←␈↓of␈α␈↓¬all␈↓␈αcomputation.␈α Particularly␈αsince␈αthe␈αexpressions␈αwere␈αavailable␈αas␈αdata
␈↓ α←␈↓structures,␈α
the␈αopportunity␈α
for␈α
self-correcting␈αprograms␈α
existed␈α
in␈αLISP.␈α
Thus
␈↓ α←␈↓LISP needed a mechanism for more selective control of error messages.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αearly␈αLISP␈αsystems␈αsupplied␈αa␈αpair␈αof␈αfunctions␈αnamed␈α
␈↓αerrset␈↓␈αand
␈↓ α←␈↓␈↓αerr␈↓.␈α
The␈α∞function␈α
␈↓αerrset␈↓␈α∞evaluates␈α
its␈α∞first␈α
argument␈α∞in␈α
the␈α∞current␈α
context.
␈↓ α←␈↓If␈α⊃no␈α⊃error␈α⊃occurs␈α∩in␈α⊃that␈α⊃evaluation,␈α⊃the␈α∩result␈α⊃is␈α⊃␈↓αconcat␈↓ed␈α⊃onto␈α∩( )␈α⊃and
␈↓ α←␈↓returned.␈αIf␈αan␈αerror␈αdoes␈αoccur␈αthen␈αthe␈αvalue␈αof␈αthe␈α␈↓αerrset␈↓␈αis␈α␈↓
f␈↓.␈αNotice␈αthat
␈↓ α←␈↓in␈αeither␈αcase␈αthe␈α␈↓αerrset␈↓␈αterminates.␈αWe␈αcan␈αtest␈αthe␈αsuccess␈αof␈αour␈αcalculation
␈↓ α←␈↓by␈αsampling␈αthe␈αvalue␈αof␈α␈↓αerrset␈↓: ␈↓
f␈↓␈αimplies␈αfailure;␈αotherwise␈αthe␈α
␈↓αfirst␈↓␈αelement
␈↓ α←␈↓of the result is the true value.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
user␈α
can␈α
also␈αforce␈α
the␈α
occurrence␈α
of␈αan␈α
"error"␈α
by␈α
calling␈α␈↓αerr␈↓.␈α
The
␈↓ α←␈↓unary␈α∩function␈α∩␈↓αerr␈↓␈α⊃returns␈α∩the␈α∩value␈α∩of␈α⊃its␈α∩argument␈α∩to␈α∩the␈α⊃dynamically
␈↓ α←␈↓enclosing␈α
␈↓αerrset␈↓␈α
or,␈α
if␈α
there␈α
is␈α
no␈α
such␈α
␈↓αerrset␈↓,␈α
the␈α
value␈α
is␈α
returned␈α
as␈αthe␈α
final
␈↓ α←␈↓value␈αof␈αthe␈αcomputation.␈αFor␈αexample␈αif␈α␈↓αerr␈↓␈αis␈αrestricted␈αto␈αreturning␈αvalues
␈↓ α←␈↓in␈αa␈αset␈αdisjoint␈αfrom␈α
those␈αreturned␈αby␈αa␈αnon-"error"␈αcomputation,␈α
then␈αthe
␈↓ α←␈↓user can test the value of ␈↓αerrset␈↓ to discover the type of "error".
␈↓"β␈↓ α←␈↓␈↓ β'The␈α↔freedom␈α↔allowed␈α↔by␈α↔the␈α↔␈↓αerrset-err␈↓␈α↔combination␈α↔soon␈α⊗became
␈↓ α←␈↓exploited␈α∩in␈α∪ways␈α∩not␈α∩originally␈α∪intended.␈α∩The␈α∩use␈α∪of␈α∩␈↓αerrset␈↓␈α∩and␈α∪␈↓αerr␈↓␈α∩in
␈↓ α←␈↓non-error-handling␈α∩contexts␈α∩often␈α∩became␈α∩quite␈α∩confusing.␈α∩The␈α∩MacLISP
␈↓ α←␈↓([Moo 74])␈αdialect␈αincludes␈αa␈αpair␈αof␈αconstructs␈αnamed␈α␈↓αcatch␈↓␈αand␈α␈↓αthrow␈↓␈α
to␈αbe
␈↓ α←␈↓used in these situations.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓αcatch␈↓␈α⊂and␈α⊂␈↓αthrow␈↓␈α⊂are␈α⊂both␈α⊂binary␈α⊂functions.␈α⊂Both␈α⊂first␈α⊂arguments␈α⊂are
␈↓ α←␈↓expressions; both second arguments are interpreted like ␈↓αprog␈↓ labels.
␈↓ α←␈↓␈↓4.5␈↓ ε]Non-recursive Control Structures 199␈↓
␈↓"β␈↓ α←␈↓␈↓ β'␈↓αcatch␈↓[<form>;<label>]␈αevaluates␈α
its␈αfirst␈α
argument␈αin␈α
the␈αcurrent␈α
context,
␈↓ α←␈↓and␈α≡returns␈α≥that␈α≡value,␈α≥except␈α≡that␈α≥if␈α≡during␈α≥that␈α≡evaluation,␈α≥a
␈↓ α←␈↓␈↓αthrow[␈↓<form>;<label>]␈α
with␈α
the␈α
same␈α
<label>␈α
is␈α
evaluated,␈α
then␈α
the␈α
value␈α
of
␈↓ α←␈↓␈↓αthrow␈↓'s␈α⊂<form>␈α⊂is␈α⊂returned␈α⊂immediately␈α⊂as␈α⊂the␈α⊂value␈α⊂of␈α⊂the␈α∂corresponding
␈↓ α←␈↓␈↓αcatch␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'For example␈↓π 11␈↓:
␈↓"∀␈↓ α←␈↓α␈↓ β∂catch[␈↓ βWmapfirst[␈↓ ∧Ofunction[λ[[x][x<0 → throw[x;negative];␈↓
t␈↓α → f[x]]]];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βW␈↓ ∧Oy];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βWnegative]
␈↓"∀␈↓ α←␈↓Assuming␈α␈↓αy␈↓␈α
is␈αa␈α
list␈αof␈α
numbers,␈αthis␈α
expression␈αwill␈α
return␈αa␈α
list␈αof␈α␈↓αf␈↓␈α
applied
␈↓ α←␈↓to␈α∞each␈α∞element␈α∞of␈α∂␈↓αy␈↓␈α∞if␈α∞each␈α∞element␈α∞of␈α∂␈↓αy␈↓␈α∞is␈α∞non-negative,␈α∞otherwise␈α∂it␈α∞will
␈↓ α←␈↓return the first negative element of ␈↓αy␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α.␈↓αcatch-throw␈↓␈α.pair␈α/are␈α.the␈α.control␈α.analog␈α/of␈α.the
␈↓ α←␈↓␈↓αfunction-funarg␈↓ application␈α∩pair␈α∪for␈α∩access.␈α∪ A␈α∩general␈α∪implementation␈α∩of
␈↓ α←␈↓␈↓αcatch-throw␈↓␈α∃introduces␈α∃a␈α∃very␈α∃non-recursive␈α∃control␈α∃regime.␈α∃ The␈α∃ususal
␈↓ α←␈↓implementation␈α∪corresponds␈α∩to␈α∪allowing␈α∩functional␈α∪␈↓¬arguments␈↓␈α∩only;␈α∪if␈α∩we
␈↓ α←␈↓wish␈α⊂to␈α⊂␈↓αthrow␈↓␈α⊂into␈α⊂procedure␈α⊂activations␈α⊂which␈α⊂have␈α⊂already␈α⊃been␈α⊂exited,
␈↓ α←␈↓then␈α∩we␈α∩must␈α∩implement␈α∩a␈α∩control␈α∩tree␈α∩similar␈α∩to␈α∩the␈α∪environment␈α∩trees
␈↓ α←␈↓generated␈α~for␈α~functional␈α→values.␈α~The␈α~next␈α→few␈α~sections␈α~will␈α→develop
␈↓ α←␈↓implementation techniques which will support such tree-like implementations.
␈↓"β␈↓ α←␈↓␈↓ β'As␈α∂motiviation␈α∂for␈α⊂those␈α∂techniques,␈α∂recall␈α∂the␈α⊂the␈α∂"value"␈α∂of␈α⊂a␈α∂␈↓αprog␈↓
␈↓ α←␈↓label␈αis␈α
essentially␈αa␈αpointer␈α
to␈αa␈α
segment␈αof␈αtext␈α
in␈αthe␈α
␈↓αprog␈↓␈αbody.␈α The␈α
label
␈↓ α←␈↓which␈α
appears␈αin␈α
a␈α␈↓αcatch␈↓␈α
is␈α
evaluated␈αsimilarly;␈α
in␈αthis␈α
case␈αthe␈α
"value"␈α
is␈αa
␈↓ α←␈↓pointer␈α
just␈αprior␈α
to␈αthe␈α
return␈α
mechanism␈αimplemented␈α
in␈αthe␈α
call␈α
to␈α␈↓αcatch␈↓.
␈↓ α←␈↓The␈αaction␈αof␈α␈↓αthrow␈↓␈α
searches␈αthe␈αcontrol␈αtree␈α
for␈αa␈αmatching␈αlabel␈αand␈α
jumps
␈↓ α←␈↓to␈α
that␈α
saved␈αvalue,␈α
thus␈α
returning␈αfrom␈α
the␈α
␈↓αcatch␈↓.␈α
If␈αthe␈α
value␈α
which␈αa␈α
␈↓αthrow␈↓
␈↓ α←␈↓returns␈αis␈α
a␈α␈↓αcatch␈↓␈αlabel,␈α
then␈αwe␈α
have␈αa␈αhandle␈α
into␈αthe␈α
control␈αtree␈αsimilar␈α
to
␈↓ α←␈↓that␈α⊗created␈α⊗by␈α⊗a␈α⊗functional␈α⊗value␈α⊗when␈α⊗it␈α⊗creates␈α⊗a␈α⊗handle␈α⊗into␈α∃an
␈↓ α←␈↓environment tree.
␈↓"β␈↓ α←␈↓␈↓ ¬⊃␈↓↓4.6 ␈↓αeval␈↓↓ with Explicit Access␈↓α
␈↓"β␈↓ α←␈↓There␈α∂are␈α∞two␈α∂major␈α∂portions␈α∞of␈α∂the␈α∂evaluation␈α∞schemes␈α∂which␈α∂we␈α∞should
␈↓ α←␈↓scrutinize␈α→before␈α~we␈α→discuss␈α→implementations:␈α~the␈α→access␈α~and␈α→binding
␈↓ α←␈↓structures,␈αand␈αthe␈αdescription␈αof␈αrecursive␈αcontrol.␈α This␈αsection␈αwill␈αlook␈αat
␈↓ α←␈↓access and binding.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αWeizenbaum␈αenvironments␈αgive␈αa␈αnice␈αgraphical␈αrepresentation␈αof
␈↓ α←␈↓the␈α⊂access␈α⊂structures,␈α∂but␈α⊂it␈α⊂would␈α∂be␈α⊂instructive␈α⊂to␈α∂express␈α⊂these␈α⊂ideas␈α∂in
␈↓ α←␈↓terms␈α⊃of␈α⊃LISP␈α∩functions.␈α⊃ This␈α⊃would␈α⊃give␈α∩us␈α⊃an␈α⊃algorithm,␈α∩suitable␈α⊃for
␈↓ α←␈↓implementation,␈α∩and␈α∩would␈α∩describe␈α∩the␈α∩mechanisms␈α∩of␈α∩LISP␈α∩at␈α∪a␈α∩more
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 11␈↓[Moo 74]
␈↓ α←␈↓␈↓200 Imperative Constructs in LISP␈↓
(4.6␈↓
␈↓"β␈↓ α←␈↓detailed␈αlevel␈αthan␈αthat␈αin␈αthe␈αevaluators␈αof␈αSection 3.5.␈αThe␈αdescription␈αwill
␈↓ α←␈↓involve␈α∂primitive␈α∂notions␈α∂just␈α∂as␈α⊂the␈α∂prior␈α∂␈↓αeval␈↓'s␈α∂do,␈α∂however␈α∂the␈α⊂level␈α∂of
␈↓ α←␈↓detail␈α#which␈α#they␈α#capture␈α#will␈α#be␈α#more␈α#readily␈α#transcribed␈α"to
␈↓ α←␈↓implementations.␈α_ As␈α_we␈α_have␈α_previously␈α_mentioned,␈α_the␈α_Weizenbaum
␈↓ α←␈↓environments␈αleave␈αmuch␈αof␈αthe␈αdetail␈αof␈αaccess␈αand␈αbinding␈αimplicit;␈αit␈αwill
␈↓ α←␈↓be a goal of this section to fill in these details.
␈↓"β␈↓ α←␈↓␈↓ β'Recall␈α∂that␈α∞a␈α∂Weizenbaum␈α∂environment␈α∞was␈α∂created␈α∂at␈α∞function-entry
␈↓ α←␈↓time.␈α
As␈α
we␈αevaluated␈α
the␈α
arguments␈αto␈α
a␈α
function,␈αwe␈α
saved␈α
the␈α
results␈αin
␈↓ α←␈↓some␈αinternal␈αdata␈αstructure.␈α When␈αall␈αarguments␈αwere␈αevaluated␈αwe␈α
formed
␈↓ α←␈↓a␈αnew␈αlocal␈αblock,␈αlinking␈αit␈αonto␈αthe␈αfront␈αof␈αthe␈αexisting␈αenvironment.␈α
The
␈↓ α←␈↓resulting␈α∞structure␈α∞became␈α∞the␈α∞new␈α∞environment.␈α∞An␈α∞analysis␈α∞of␈α∞these␈α
steps
␈↓ α←␈↓highlights␈α≠several␈α≠points.␈α≠ We␈α~need␈α≠space␈α≠to␈α≠contain␈α≠the␈α~evaluated
␈↓ α←␈↓parameters,␈α∩and␈α∩those␈α∩results␈α∩are␈α∩then␈α∩moved␈α∩into␈α∩a␈α∩environment␈α∩block;
␈↓ α←␈↓therefore,␈α∪if␈α∩we␈α∪construct␈α∩the␈α∪space␈α∩which␈α∪is␈α∩to␈α∪contain␈α∪those␈α∩evaluated
␈↓ α←␈↓parameters␈α
like␈α∞an␈α
environment␈α∞block␈α
then␈α
the␈α∞linking␈α
operation␈α∞need␈α
only
␈↓ α←␈↓attach␈αthat␈αnew␈αobject.␈αThis␈αstrategy␈αis␈αpossible␈αsince␈αthe␈αspace␈αrequirements
␈↓ α←␈↓for␈α⊂the␈α⊂evaluated␈α⊂parameters␈α⊂is␈α⊂known:␈α⊂the␈α⊂block␈α⊂must␈α⊂be␈α⊂as␈α⊂long␈α⊂as␈α∂the
␈↓ α←␈↓number␈α≠of␈α≠formal␈α≠parameters␈α~expected.␈↓π 12␈↓␈α≠This␈α≠requirement␈α≠can␈α~be
␈↓ α←␈↓ascertained␈α∞by␈α∞examining␈α∞the␈α∞definition␈α∞of␈α∞the␈α∞function␈α∞being␈α∂called.␈α∞Once
␈↓ α←␈↓the␈αblock␈αis␈αallocated,␈αthe␈αactual␈αparameters␈αare␈αevaluated␈αand␈αthe␈αresults␈α
are
␈↓ α←␈↓sent␈α
to␈α∞the␈α
proper␈α∞slot␈α
in␈α∞the␈α
allocated␈α∞block.␈α
Such␈α∞a␈α
block␈α∞will␈α
be␈α∞called␈α
a
␈↓ α←␈↓␈↓↓destination␈αblock␈↓;␈αand␈αthe␈αoperation␈αof␈αplacing␈αa␈αresult␈αin␈αa␈αdestination␈αwill
␈↓ α←␈↓be␈α∂called␈α∂␈↓↓sending␈↓.␈α⊂ Once␈α∂all␈α∂the␈α∂evaluated␈α⊂parameters␈α∂have␈α∂been␈α⊂sent,␈α∂we
␈↓ α←␈↓␈↓↓link␈↓␈α⊂the␈α⊂completed␈α∂block␈α⊂into␈α⊂the␈α⊂front␈α∂of␈α⊂the␈α⊂current␈α⊂environment.␈α∂ The
␈↓ α←␈↓ideas␈α∞expressed␈α∂in␈α∞this␈α∂section␈α∞are␈α∂an␈α∞embellishment␈α∂of␈α∞those␈α∂on␈α∞page 124.
␈↓ α←␈↓The␈α∪innovation␈α∪is␈α∪to␈α∪allocate␈α∪space␈α∪for␈α∪the␈α∪evaluated␈α∀arguments␈α∪before
␈↓ α←␈↓beginning␈α⊃their␈α⊃actual␈α⊃evaluation.␈α∩The␈α⊃evaluator␈α⊃sends␈α⊃the␈α⊃values␈α∩to␈α⊃the
␈↓ α←␈↓allocated block.
␈↓"β␈↓ α←␈↓␈↓ β'Here are the primitive routines:
␈↓"∀␈↓ α←␈↓␈↓↓1.␈↓␈α
␈↓αalloc_dest␈↓:␈αThis␈α
unary␈α
function␈αis␈α
supplied␈α
with␈αthe␈α
formal␈α
parameter␈αlist
␈↓ α←␈↓␈↓ β'of␈α⊂a␈α⊂function,␈α∂and␈α⊂supplies␈α⊂a␈α⊂new␈α∂destination␈α⊂block␈α⊂with␈α⊂the␈α∂formal
␈↓ α←␈↓␈↓ β'parameters␈αplaced␈αin␈αthe␈αname-section␈αof␈αthe␈αblock.␈αAn␈αinternal␈αpointer
␈↓ α←␈↓␈↓ β'is initialized to the first slot in the block. Thus:
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 12␈↓Some␈α
LISP␈α
systems␈α
allow␈αdiscrepancies␈α
between␈α
the␈α
number␈αof␈α
actual
␈↓ α←␈↓parameters␈α⊃and␈α⊂formal␈α⊃parameters.␈α⊂The␈α⊃current␈α⊂scheme␈α⊃will␈α⊂accommodate
␈↓ α←␈↓that generality.
␈↓ α←␈↓␈↓4.6␈↓ πF␈↓αeval␈↓ with Explicit Access 201␈↓α
␈↓"¬␈↓ α←␈↓
␈↓ a destination block␈↓
␈↓"␈↓ α←␈↓
⊂ααααααα⊃
␈↓"␈↓ α←␈↓
~ #αβα⊃
␈↓"␈↓ α←␈↓
εαααπαααλ ↓ ␈↓internal pointer␈↓
␈↓"␈↓ α←␈↓
~ ~ ~←$
␈↓"␈↓ α←␈↓
εαααβαααλ
␈↓"␈↓ α←␈↓
~ ~ ~
␈↓"␈↓ α←␈↓
# # #
␈↓"␈↓ α←␈↓
εαααβαααλ
␈↓"␈↓ α←␈↓
~ ~ ~
␈↓"␈↓ α←␈↓
%ααα∀ααα$
␈↓"␈↓ α←␈↓
↑ ↑
␈↓"␈↓ α←␈↓
␈↓formal parameters values␈↓
␈↓"β␈↓ α←␈↓␈↓↓2.␈↓␈α␈↓αsend␈↓:␈αThis␈αis␈αa␈αbinary␈α
function␈αwhose␈αfirst␈αargument␈αis␈αa␈αdestination␈α
block
␈↓ α←␈↓␈↓ β'and␈αwhose␈αsecond␈αargument␈αis␈αa␈αvalue␈αto␈αbe␈αsent.␈αThe␈αvalue␈αof␈α␈↓αsend␈↓␈αis
␈↓ α←␈↓␈↓ β'the␈αdestination␈αblock.␈αThe␈αeffect␈αof␈α␈↓αsend␈↓␈αis␈αto␈αsend␈αits␈αsecond␈αargument
␈↓ α←␈↓␈↓ β'to␈αthe␈αcurrent␈αdestination␈αslot.␈αThe␈αinternal␈αpointer␈αis␈α␈↓¬not␈↓␈αupdated;␈αthat
␈↓ α←␈↓␈↓ β'is the business of ␈↓αnext␈↓.
␈↓"∀␈↓ α←␈↓␈↓↓3.␈↓␈α
␈↓αnext␈↓:␈α
This␈α
function␈α
takes␈α
a␈αdestination␈α
block␈α
as␈α
argument␈α
and␈α
moves␈αthe
␈↓ α←␈↓␈↓ β'internal␈αpointer␈αof␈αthe␈αblock␈α
to␈αpoint␈αto␈αthe␈αsucceeding␈αslot.␈α
The␈αvalue
␈↓ α←␈↓␈↓ β'of␈α
␈↓αnext␈↓␈α
is␈α
the␈α
destination␈α
block.␈α
Thus␈α
␈↓αnext␈↓␈α
is␈α
an␈α
identity␈α
function␈α
with␈α
a
␈↓ α←␈↓␈↓ β'side-effect.
␈↓"∀␈↓ α←␈↓␈↓↓4.␈↓␈α␈↓αlink␈↓:␈α␈↓αlink␈↓␈αtakes␈αa␈αdestination␈αblock␈αand␈αan␈αenvironment␈αas␈αarguments␈αand
␈↓ α←␈↓␈↓ β'links␈α∩the␈α∩destination␈α⊃block␈α∩onto␈α∩the␈α⊃front␈α∩of␈α∩the␈α∩environment.␈α⊃The
␈↓ α←␈↓␈↓ β'resulting␈αenvironment␈αis␈αthe␈αvalue␈αof␈α␈↓αlink␈↓.␈αSince␈αthe␈αinternal␈αpointer␈αis
␈↓ α←␈↓␈↓ β'only␈α
used␈αduring␈α
the␈αfilling␈α
of␈αthe␈α
dest-block,␈αwe␈α
can␈αassume␈α
that␈α␈↓αlink␈↓
␈↓ α←␈↓␈↓ β'replaces that pointer with a pointer to the previous environment.
␈↓"∀␈↓ α←␈↓␈↓↓5.␈↓␈α∂␈↓αreceive␈↓:␈α∂Sometimes␈α∂we␈α∂will␈α⊂wish␈α∂to␈α∂examine␈α∂the␈α∂result␈α∂of␈α⊂a␈α∂computation
␈↓ α←␈↓␈↓ β'before␈α_making␈α_a␈α_decision␈α_on␈α_how␈α_to␈α_proceed.␈α_In␈α_particular,␈α_in
␈↓ α←␈↓␈↓ β'conditional␈α
expressions␈α
we␈α
must␈α
evaluate␈α
the␈α
predicate␈α∞position␈α
before
␈↓ α←␈↓␈↓ β'knowing␈α
how␈α
to␈α
handle␈α
the␈αrest␈α
of␈α
the␈α
conditional.␈α
The␈αunary␈α
primitive
␈↓ α←␈↓␈↓ β'␈↓αreceive␈↓␈α⊂lets␈α⊂us␈α⊂look␈α⊂at␈α⊂the␈α⊂result␈α⊂of␈α⊂a␈α⊂computation.␈α⊂The␈α⊂argument␈α∂to
␈↓ α←␈↓␈↓ β'␈↓αreceive␈↓␈αis␈αa␈α
destination␈αblock,␈αand␈α
the␈αvalue␈αreturned␈α
is␈αthe␈αvalue␈αin␈α
the
␈↓ α←␈↓␈↓ β'current slot.
␈↓"∪␈↓ α←␈↓␈↓ ε≡␈↓↓Problem␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Give␈α∂a␈α∞full␈α∂LISP␈α∞representation␈α∂of␈α∞destination␈α∂blocks␈α∞and␈α∂supply␈α∞the
␈↓ α←␈↓corresponding implementations for the primitive routines, ␈↓↓1␈↓ through ␈↓↓5␈↓.
␈↓ α←␈↓␈↓202 Imperative Constructs in LISP␈↓
(4.6␈↓
␈↓"β␈↓ α←␈↓␈↓ β'In␈α
the␈αfollowing␈α
evaluators␈α
we␈αwill␈α
freely␈αuse␈α
the␈α
extended␈αconditional
␈↓ α←␈↓expressions␈α∂and␈α⊂λ-expressions␈α∂introduced␈α∂on␈α⊂page 194␈α∂and␈α⊂page 196.␈α∂ The
␈↓ α←␈↓first evaluator is named ␈↓αdeval␈↓, with the "␈↓αd␈↓ coming from "destination".
␈↓"∀␈↓ α←␈↓αdeval <= λ[[exp;env;dest]
␈↓"β␈↓ α←␈↓α␈↓ ∧π[isconst[exp] → send[dest;denote[exp]];
␈↓"β␈↓ α←␈↓α␈↓ ∧π isvar[exp] → send[dest;lookup[exp;env]];
␈↓"β␈↓ α←␈↓α␈↓ ∧π ␈↓
t␈↓α → deval␈↓β1␈↓α[func[exp]; arglist[exp]; env; dest] ]]
␈↓"∀␈↓ α←␈↓αdeval␈↓β1␈↓α <=
␈↓"β␈↓ α←␈↓α λ[[fun;args;env;dest]
␈↓"β␈↓ α←␈↓α␈↓ ββ[atom[fun] →␈↓ ∧+[iscond[fun] → devcond[args;env;dest]
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧+ isprim[fun] → execute[␈↓ εWfun;
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧+␈↓ ∧g␈↓ ¬←␈↓ ε≠␈↓ ε?␈↓ εWlink[␈↓ π∪evalargs[␈↓ λargs;
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧+␈↓ ∧g␈↓ ¬←␈↓ ε≠␈↓ ε?␈↓ εW␈↓ π∪␈↓ λenv;
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧+␈↓ ∧g␈↓ ¬←␈↓ ε≠␈↓ ε?␈↓ εW␈↓ π∪␈↓ λalloc_dest
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧+␈↓ ∧g␈↓ ¬←␈↓ ε≠␈↓ ε?␈↓ εW␈↓ π∪␈↓ λ␈↓ λ;[createvars[args]];
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧+␈↓ ∧g␈↓ ¬←␈↓ ε≠␈↓ ε?␈↓ εW␈↓ π∪env];
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧+␈↓ ∧g␈↓ ¬←␈↓ ε≠␈↓ ε?␈↓ εWdest];
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧+ ␈↓
t␈↓α →␈↓ ∧gdeval[fun;env;dest];
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧+␈↓ ∧gdeval␈↓β1␈↓α[receive[dest];args;env;dest] ]
␈↓"β␈↓ α←␈↓α␈↓ ββ islambda[fun] → evalargs[␈↓ ¬←bodylist[fun];
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧+␈↓ ∧g␈↓ ¬←link[␈↓ ε≠evalargs[␈↓ π∪args;
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧+␈↓ ∧g␈↓ ¬←␈↓ ε≠␈↓ ε?␈↓ εW␈↓ π∪env;
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧+␈↓ ∧g␈↓ ¬←␈↓ ε≠␈↓ ε?␈↓ εW␈↓ π∪alloc_dest[vars[fun]]]];
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧+␈↓ ∧g␈↓ ¬←␈↓ ε≠env];
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧+␈↓ ∧g␈↓ ¬←dest]
␈↓"β␈↓ α←␈↓α␈↓ ββ ␈↓
t␈↓α → deval[fun;env;dest]; deval␈↓β1␈↓α[receive[dest];args;env;dest] ]]
␈↓"∀␈↓ α←␈↓αevalargs <= λ[[args;env;dest]
␈↓"β␈↓ α←␈↓α␈↓ ∧∪[null[args] →dest;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪ null[rest[args]] → deval[first[args];env;dest];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪ ␈↓
t␈↓α →␈↓ ∧Odeval[first[args];env;dest];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧Onext[dest];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧Oevalargs[rest[args];env;dest] ]]
␈↓" ␈↓ α←␈↓αexecute <= λ[[fun;env;dest]send[dest;apply[fun;vals[env];( )]]]
␈↓"∀␈↓ α←␈↓Note that ␈↓αexecute␈↓ resorts to ␈↓αapply␈↓ to handle primitive application.
␈↓"∀␈↓ α←␈↓αdevcond <= λ[[args;env;dest]
␈↓"β␈↓ α←␈↓α␈↓ ∧∪deval[pred[first[args]];env;dest];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪[receive[dest] → evalargs[condbody[first[args]];env;dest];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪ ␈↓
t␈↓α → devcond[rest[args]; env;dest] ]]
␈↓"∀␈↓ α←␈↓␈↓ β'This␈αnew␈αevaluator␈αmust␈αbe␈αsupplied␈αwith␈αan␈αinitial␈αdestination␈αas␈αwell
␈↓ α←␈↓␈↓4.6␈↓ πF␈↓αeval␈↓ with Explicit Access 203␈↓α
␈↓"β␈↓ α←␈↓as␈α
being␈α
supplied␈α
with␈α∞an␈α
initial␈α
symbol␈α
table.␈α∞Also,␈α
since␈α
the␈α
result␈α∞of␈α
any
␈↓ α←␈↓calculation␈αis␈α
a␈αdestination␈αblock␈α
rather␈αthan␈αjust␈α
a␈αsimple␈αvalue,␈α
we␈αshould
␈↓ α←␈↓supply␈α∞a␈α∞selector␈α∞to␈α∞extract␈α∞the␈α
desired␈α∞value.␈α∞For␈α∞example,␈α∞if␈α∞we␈α
designate
␈↓ α←␈↓␈↓αval␈↓␈αas␈αsuch␈αa␈αselector,␈αand␈αdesignate␈αthe␈αatom␈α␈↓αTLB␈↓␈αas␈αthe␈αrepository␈α
for␈αthe
␈↓ α←␈↓top level binding then:
␈↓"∀␈↓ α←␈↓α␈↓ βdeval <= λ[[exp;env] val[deval[exp;env;alloc_dest[(TLB)]]]]
␈↓"∀␈↓ α←␈↓␈↓ β'More␈α of␈α the␈α details␈α∨of␈α argument␈α handling␈α should␈α now␈α∨be
␈↓ α←␈↓understandable:␈α↔when␈α↔a␈α↔function␈α↔application␈α↔has␈α↔been␈α↔recognized,␈α↔the
␈↓ α←␈↓evaluator␈α∃sets␈α∀up␈α∃a␈α∀block␈α∃to␈α∀hold␈α∃the␈α∀results␈α∃of␈α∀evaluating␈α∃the␈α∀actual
␈↓ α←␈↓parameters.␈α∞If␈α∞the␈α∂function␈α∞is␈α∞a␈α∞primitive␈α∂function␈α∞then␈α∞the␈α∞name␈α∂slots␈α∞are
␈↓ α←␈↓filled with some system-created names, otherwise the λ-variables are used.
␈↓"α␈↓ α←␈↓␈↓ ε≡␈↓↓Problem␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Using␈α
the␈α
new␈α
evaluator,␈α
sketch␈α
the␈α
evaluation␈α
of␈α
␈↓αf[A]␈↓␈αwhere:␈α
␈↓αf <= λ[[x]
␈↓ α←␈↓αeq[x;A]]␈↓.
␈↓"α␈↓ α←␈↓␈↓ β'Notice␈αthat␈α
for␈αmost␈α
of␈αthe␈αevaluation␈α
process,␈α␈↓αdest␈↓␈α
is␈αa␈αpassive␈α
element.
␈↓ α←␈↓A␈α
new␈αdestination␈α
block␈αis␈α
created␈αon␈α
function␈αapplications,␈α
but␈αthat␈α
␈↓αdest␈↓␈αis
␈↓ α←␈↓passed␈α
around␈α∞as␈α
an␈α
argument␈α∞through␈α
most␈α
of␈α∞the␈α
pieces␈α
of␈α∞the␈α
evaluator
␈↓ α←␈↓without␈α∂explicit␈α⊂modification.␈α∂That␈α⊂is,␈α∂in␈α∂most␈α⊂λ-bindings␈α∂␈↓αdest␈↓␈α⊂simply␈α∂gets
␈↓ α←␈↓rebound␈α
to␈α
the␈αsame␈α
object.␈α
Since␈α
the␈αλ-binding␈α
process␈α
is␈α
not␈αinexpensive␈α
it
␈↓ α←␈↓is␈α∩tempting␈α∩to␈α∪make␈α∩variables␈α∩like␈α∪␈↓αdest␈↓,␈α∩which␈α∩change␈α∪infrequently,␈α∩into
␈↓ α←␈↓non-local␈α∃variables;␈α∃they␈α⊗would␈α∃be␈α∃initialized␈α⊗at␈α∃the␈α∃outside␈α⊗layer␈α∃and
␈↓ α←␈↓modified␈α
by␈α
side-effects␈α
during␈α
the␈α
evaluation.␈α
However␈α
the␈α
current␈αvalue␈α
of
␈↓ α←␈↓␈↓αdest␈↓␈α∂␈↓¬does␈↓␈α∞need␈α∂to␈α∞be␈α∂saved␈α∞occasionally.␈α∂Those␈α∞occasions␈α∂correspond␈α∂to␈α∞the
␈↓ α←␈↓places␈αwhere␈α␈↓αdest␈↓␈α
gets␈αrebound␈αto␈α
something␈αother␈αthan␈α
␈↓αdest␈↓.␈α We␈αwill␈α
supply
␈↓ α←␈↓two new primitives to handle explicit saving and restoring of values:
␈↓"∀␈↓ α←␈↓␈↓↓1.␈↓␈α∞␈↓αsave␈↓:␈α∞This␈α∂binary␈α∞function␈α∞would␈α∞be␈α∂implemented␈α∞as␈α∞a␈α∞special␈α∂form.␈α∞ Its
␈↓ α←␈↓␈↓ β'first␈α
argument␈α
is␈α
a␈α
name␈α
␈↓αold␈↓,␈α
and␈α
its␈α
second␈α
argument␈α
is␈α
a␈α∞value␈α
␈↓αnew␈↓.
␈↓ α←␈↓␈↓ β'The␈α⊃current␈α∩value␈α⊃associated␈α∩with␈α⊃␈↓αold␈↓␈α∩is␈α⊃saved,␈α∩and␈α⊃the␈α∩value␈α⊃␈↓αnew␈↓
␈↓ α←␈↓␈↓ β'becomes the value of ␈↓αold␈↓.
␈↓"∀␈↓ α←␈↓␈↓↓2.␈↓␈α␈↓αrestore␈↓:␈αThis␈αis␈αa␈αunary␈αfunction;␈αits␈αargument␈αis␈αa␈αname␈α␈↓αname␈↓.␈α
The␈αlatest
␈↓ α←␈↓␈↓ β'value␈α∂which␈α∂was␈α⊂saved␈α∂for␈α∂␈↓αname␈↓␈α∂is␈α⊂restored.␈α∂The␈α∂value␈α⊂which␈α∂␈↓αname␈↓
␈↓ α←␈↓␈↓ β'had on entry to ␈↓αrestore␈↓ is lost.
␈↓"∀␈↓ α←␈↓␈↓ β'Using␈α~␈↓αsave␈↓␈α~and␈α≠␈↓αrestore␈↓␈α~we␈α~could␈α≠express␈α~the␈α~evaluation␈α≠of␈α~a
␈↓ α←␈↓λ-application something like:
␈↓ α←␈↓␈↓204 Imperative Constructs in LISP␈↓
(4.6␈↓
␈↓"∀␈↓ α←␈↓α␈↓ α←eval[␈↓
R␈↓∞(␈↓α λ[[x;y]␈↓λx␈↓α][a;b] ␈↓∞)␈↓α; env] =␈↓ ε∂save[x;a];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε∂save[y;b];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε∂eval[␈↓
R␈↓∞( ␈↓λx ␈↓∞)␈↓α ;env␈↓λ'␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε∂restore[y];
␈↓"β␈↓ α←␈↓α␈↓ α←␈↓ ε∂restore[x];
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α
implementation␈α
details␈α∞of␈α
␈↓αsave␈↓␈α
and␈α
␈↓αrestore␈↓␈α∞will␈α
not␈α
be␈α∞needed␈α
for
␈↓ α←␈↓most␈α_of␈α_our␈α↔discussion,␈α_however␈α_we␈α_include␈α↔some␈α_of␈α_them␈α_here␈α↔for
␈↓ α←␈↓completeness.␈α∪The␈α∀information␈α∪which␈α∀is␈α∪␈↓αsave␈↓d␈α∀and␈α∪␈↓αrestore␈↓d␈α∀is␈α∪accessible
␈↓ α←␈↓through␈α⊂a␈α∂global␈α⊂variable␈α∂named␈α⊂␈↓αcontrol␈↓.␈α∂A␈α⊂␈↓αsave[␈↓<name>;<value>]␈α⊂has␈α∂the
␈↓ α←␈↓effect␈α
of␈α
␈↓αconcat␈↓-ing␈αthe␈α
current␈α
value␈αof␈α
<name>␈α
onto␈αthe␈α
front␈α
of␈α
␈↓αcontrol␈↓;␈αit
␈↓ α←␈↓then sets the new value of <name> to <value>.
␈↓"∀␈↓ α←␈↓That is:
␈↓"β␈↓ α←␈↓␈↓ βK␈↓αcontrol ← concat[eval[␈↓<name>␈↓α;env];control];
␈↓"β␈↓ α←␈↓α␈↓ βKset[␈↓<name>; ␈↓αeval[␈↓<value>␈↓α;env]];␈↓
␈↓"∀␈↓ α←␈↓Then ␈↓αrestore␈↓[<name>] performs:
␈↓"∀␈↓ α←␈↓α␈↓ ∧%set␈↓[<name>␈↓α;first[control]]; control ← rest[control]
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α
manipulation␈α∞of␈α
␈↓αcontrol␈↓␈α∞by␈α
␈↓αsave␈↓␈α∞and␈α
␈↓αrestore␈↓␈α∞is␈α
stack-like␈α∞in␈α
LISP.
␈↓ α←␈↓That␈α∩means␈α∩that␈α⊃only␈α∩the␈α∩␈↓αfirst␈↓␈α⊃element␈α∩of␈α∩␈↓αcontrol␈↓␈α⊃is␈α∩accessible;␈α∩to␈α⊃access
␈↓ α←␈↓elements␈α∪in␈α∩the␈α∪interior␈α∩of␈α∪␈↓αcontrol␈↓␈α∩requires␈α∪␈↓αrestore␈↓-ing␈α∩down␈α∪to␈α∪them␈α∩by
␈↓ α←␈↓sequence␈α∞of␈α∂"␈↓αcontrol␈↓ ← ␈↓αrest[control]␈↓".␈α∞ Once␈α∞we␈α∂have␈α∞removed␈α∂elements␈α∞from
␈↓ α←␈↓␈↓αcontrol␈↓␈α
there␈α
is␈α
no␈α
way␈α
to␈α
access␈α
that␈α
information␈α
again.␈α The␈α
␈↓αcontrol␈↓-structure
␈↓ α←␈↓is␈αnot␈αaccessible␈αas␈αa␈αdata␈αstructure␈αto␈αthe␈αsame␈αdegree␈αof␈αgenerality␈αas␈αis␈αthe
␈↓ α←␈↓access␈α⊃structure.␈α∩The␈α⊃closest␈α∩analogy␈α⊃to␈α∩␈↓αfunction-funarg␈↓␈α⊃is␈α∩the␈α⊃␈↓αcatch-throw␈↓
␈↓ α←␈↓pair.␈α
However␈α
now␈αthat␈α
␈↓αcontrol␈↓␈α
is␈α
explicit␈αwe␈α
can␈α
begin␈α
to␈αdescribe␈α
extensions
␈↓ α←␈↓to LISP which ␈↓¬will␈↓ allow us to capture ␈↓αcontrol␈↓ like ␈↓αfunction␈↓ captures ␈↓αenv␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Given␈α⊂␈↓αsave␈↓␈α⊂and␈α⊂␈↓αrestore␈↓␈α⊂we␈α∂can␈α⊂rewrite␈α⊂␈↓αdeval␈↓␈α⊂and␈α⊂its␈α⊂subfunctions␈α∂to
␈↓ α←␈↓access␈αnon-local␈αrepresentations␈αof␈αvariables␈αused␈αin␈αthe␈αcurrent␈α␈↓αdeval␈↓.␈α Thus
␈↓ α←␈↓the␈α
evaluator␈α
becomes␈α
a␈αfunction␈α
of␈α
no␈α
arguments;␈αit␈α
␈↓¬knows␈↓␈α
where␈α
to␈αfind␈α
the
␈↓ α←␈↓values␈α
and␈α
it␈α
knows␈α
how␈α
to␈α
save␈α
and␈α
restore␈α
those␈α
variables.␈α
The␈α
result␈α
is
␈↓ α←␈↓an evaluator which has even ␈↓¬fewer␈↓ implict operations than ␈↓αdeval␈↓.
␈↓"∀␈↓ α←␈↓αdeval␈↓λ'␈↓α <= λ[[]␈↓ ∧∨[isconst[] → send[denote[]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ isvar[] → send[lookup[]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ ␈↓
t␈↓α →␈↓ ∧[save[fun;func[]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧[save[args;arglist[]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧[deval␈↓β1␈↓α[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧[restore[args];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧[restore[fun] ]]
␈↓"∀␈↓ α←␈↓␈↓ β'A␈αfew␈α
points␈αshould␈αbe␈α
noted␈αnow.␈α
We␈αwill␈αbe␈α
using␈αthe␈α
same␈αnames
␈↓ α←␈↓as␈αwe␈αdid␈αin␈α␈↓αdeval␈↓␈αfor␈αall␈αsubfunctions␈αof␈α␈↓αdeval␈↓λ'␈↓.␈αThe␈αdifference␈αwill␈αbe␈αthat
␈↓ α←␈↓here␈α
those␈α
functions␈α
will␈α
␈↓¬know␈↓␈α
where␈α
their␈α
arguments␈α
are␈α
to␈α
be␈α
found;␈αthey
␈↓ α←␈↓need␈α∪not␈α∪be␈α∪explicitly␈α∪passed␈α∪in.␈α∪Thus␈α∪␈↓αsend[denote[]]␈↓␈α∪means␈α∪that␈α∪␈↓αdenote␈↓
␈↓ α←␈↓␈↓4.6␈↓ πF␈↓αeval␈↓ with Explicit Access 205␈↓α
␈↓"β␈↓ α←␈↓extracts␈αa␈αvalue␈αfrom␈α
the␈αrepresentation␈αin␈α␈↓αexp␈↓␈αand␈α
␈↓αsend␈↓␈αknows␈αthat␈αit␈α
is␈αto
␈↓ α←␈↓send its value to ␈↓αdest␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'With this new evaluator we can define ␈↓αeval␈↓ as:
␈↓"∀␈↓ α←␈↓α␈↓ β?eval <= λ[[x;y]␈↓ ∧␈fun ← ();
␈↓"β␈↓ α←␈↓α␈↓ β?␈↓ ∧␈args ← ();
␈↓"β␈↓ α←␈↓α␈↓ β?␈↓ ∧␈exp ← x;
␈↓"β␈↓ α←␈↓α␈↓ β?␈↓ ∧␈env ← y;
␈↓"β␈↓ α←␈↓α␈↓ β?␈↓ ∧␈dest ← alloc_dest[(TLB)];
␈↓"β␈↓ α←␈↓α␈↓ β?␈↓ ∧␈deval␈↓λ'␈↓α[];
␈↓"β␈↓ α←␈↓α␈↓ β?␈↓ ∧␈val[dest]]
␈↓"β␈↓ α←␈↓α␈↓Here is the remainder of ␈↓αdeval␈↓λ'␈↓:
␈↓"β␈↓ α←␈↓αdeval␈↓β1␈↓α <= λ[[]␈↓ ∧∪[isatom[] →␈↓ ¬;[iscond[] → devcond[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬; isprim[] →␈↓ εWsave[env;env];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬;␈↓ εWsave[dest;alloc_dest[createvars[]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬;␈↓ εWevalargs[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬;␈↓ εWlink[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬;␈↓ εWrestore[dest];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬;␈↓ εWexecute[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬;␈↓ εWrestore[env];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬; ␈↓
t␈↓α → deval␈↓β2␈↓α[] ]
␈↓"β␈↓ α←␈↓α␈↓ ∧∪ islambda[] →␈↓ ¬;save[env;env];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬;save[dest;alloc_dest[vars[]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬;evalargs[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬;link[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬;restore[dest];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬;save[args;bodylist[]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬;evalargs[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬;restore[args];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬;restore[env];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪ ␈↓
t␈↓α → deval␈↓β2␈↓α[] ]]
␈↓ α←␈↓␈↓206 Imperative Constructs in LISP␈↓
(4.6␈↓
␈↓" ␈↓ α←␈↓αdeval␈↓β2␈↓α <= λ[[ ]␈↓ ∧+save[exp;fun];
␈↓"β␈↓ α←␈↓α␈↓ ∧+deval␈↓λ'␈↓α[];
␈↓"β␈↓ α←␈↓α␈↓ ∧+restore[exp];
␈↓"β␈↓ α←␈↓α␈↓ ∧+save[fun;receive[]];
␈↓"β␈↓ α←␈↓α␈↓ ∧+deval␈↓β1␈↓α[];
␈↓"β␈↓ α←␈↓α␈↓ ∧+restore[fun] ]
␈↓"∀␈↓ α←␈↓We␈αintroduced␈α␈↓αdeval␈↓β2␈↓␈αto␈αcapture␈αthe␈αcomputation␈αto␈αbe␈αperformed␈αwhen␈αthe
␈↓ α←␈↓function-position␈αis␈αnot␈αrecognized␈αas␈αeither␈αa␈αλ-expression,␈αa␈α
conditional,␈αor
␈↓ α←␈↓a␈α⊂primitive.␈α⊂ Note␈α⊂that␈α∂we␈α⊂perform␈α⊂␈↓αsave[env;env]␈↓␈α⊂in␈α∂a␈α⊂couple␈α⊂of␈α⊂places␈α∂in
␈↓ α←␈↓␈↓αdeval␈↓β1␈↓.␈α This␈αis␈αnecessary␈αto␈αsave␈α
the␈αcurrent␈αvalue␈αof␈α␈↓αenv␈↓␈αsince␈α␈↓αlink␈↓␈α
modifies
␈↓ α←␈↓␈↓αenv␈↓.␈α
Indeed,␈αthe␈α
sequence:␈αsave␈α
␈↓αenv␈↓␈αand␈α
␈↓αdest␈↓,␈α␈↓αevalargs␈↓,␈α
␈↓αlink␈↓,␈αand␈α
restore␈α␈↓αdest␈↓
␈↓ α←␈↓can be simplified to: save ␈↓αdest␈↓, ␈↓αevalargs␈↓, followed by ␈↓αlink␈↓λ'␈↓. where:
␈↓"∀␈↓ α←␈↓α␈↓ β`link␈↓λ'␈↓α <=λ[[] set_int[dest;env];rotate[env;first[control];dest]]
␈↓"∀␈↓ α←␈↓and␈α⊗␈↓αset_int␈↓␈α↔sets␈α⊗the␈α↔internal␈α⊗pointer␈α↔of␈α⊗the␈α↔dest-block␈α⊗to␈α↔the␈α⊗current
␈↓ α←␈↓environment,␈αand␈α
␈↓αrotate[x;y;z]␈↓␈αmoves␈α
the␈αcontents␈αof␈α
␈↓αx␈↓␈αto␈α
␈↓αy␈↓,␈αcontents␈α
of␈α␈↓αy␈↓␈αto␈α
␈↓αz␈↓,
␈↓ α←␈↓and contents of ␈↓αz␈↓ to ␈↓αx␈↓.
␈↓"∀␈↓ α←␈↓αevalargs <= λ[[]␈↓ ∧7[emptyargs[] → ( );
␈↓"β␈↓ α←␈↓α␈↓ ∧7 singlearg[] →␈↓ ¬wsave[exp;first[args]];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧s␈↓ ¬wdeval␈↓λ'␈↓α[];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧s␈↓ ¬wrestore[exp];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 ␈↓
t␈↓α →␈↓ ∧ssave[exp;first[args]];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧sdeval␈↓λ'␈↓α[];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧srestore[exp];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧snext[];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧ssave[args;rest[args]];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧sevalargs[];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧srestore[args] ]]
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α∞discussions␈α
surrounding␈α∞this␈α
evaluator␈α∞tacitly␈α
assume␈α∞that␈α∞a␈α
deep
␈↓ α←␈↓binding␈α⊃strategy␈α⊃is␈α⊃being␈α⊃implemented.␈α⊃That␈α⊃assumption␈α⊃is␈α⊃not␈α⊃necessary.
␈↓ α←␈↓The␈α↔final␈α↔shallow␈α↔binder␈α↔of␈α↔Section 3.11␈α↔can␈α↔be␈α↔incorporated␈α_in␈α↔the
␈↓ α←␈↓framework␈α↔of␈α_these␈α↔latest␈α↔evaluators.␈α_The␈α↔key␈α↔alterations␈α_involve␈α↔the
␈↓ α←␈↓rebinding␈α∞of␈α
the␈α∞value␈α
cells␈α∞inside␈α∞␈↓αdeval␈↓β1␈↓␈α
when␈α∞␈↓αislambda␈↓␈α
is␈α∞true.␈α∞ We␈α
leave
␈↓ α←␈↓the␈α
modifications␈αas␈α
a␈α
problem␈αfor␈α
the␈α
reader;␈αand␈α
we␈α
postpone␈αthe␈α
treatment
␈↓ α←␈↓of ␈↓αfunction␈↓ until Section 5.19 and Section 5.20.
␈↓"β␈↓ α←␈↓␈↓ β'Note␈αalso␈αthat␈αwe␈α
are␈αnever␈αinterested␈αin␈α
the␈αvalue␈αreturned␈αfrom␈αa␈α
call
␈↓ α←␈↓on␈αa␈αsub-function␈αin␈αthe␈αevaluator;␈αall␈αvalues␈αare␈αpassed␈αexplicity␈αfrom␈αtheir
␈↓ α←␈↓creator␈α
to␈α
a␈αdestination.␈α
We␈α
might␈αsay␈α
that␈α
␈↓αdeval␈↓λ'␈↓␈αnever␈α
returns␈α
a␈α
value.␈αIn
␈↓ α←␈↓the next section we will build an evaluator which never returns at all.
␈↓ α←␈↓␈↓4.6␈↓ πF␈↓αeval␈↓ with Explicit Access 207␈↓α
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Write the new version of ␈↓αdevcond␈↓.
␈↓" ␈↓ α←␈↓2. Examine␈α⊃the␈α⊃␈↓αsave-restore␈↓␈α⊃sequences␈α⊃in␈α⊃␈↓αdeval␈↓λ'␈↓␈α⊃and␈α⊃its␈α⊃sub-functions␈α⊃for
␈↓ α←␈↓␈↓ β∂possible␈αinefficiencies.␈αThat␈αis,␈αare␈αall␈αthe␈αsaves␈αand␈αrestores␈αnecessary␈αor
␈↓ α←␈↓␈↓ β∂could␈αexplicit␈αassignments␈αto␈αsome␈αof␈αthe␈αnon-local␈αvariables␈αspeed␈α
things
␈↓ α←␈↓␈↓ β∂up?
␈↓" ␈↓ α←␈↓3. Using␈α≥the␈α≥new␈α≤evaluator,␈α≥sketch␈α≥the␈α≤evaluation␈α≥of␈α≥␈↓αf[A]␈↓␈α≤where:
␈↓ α←␈↓␈↓ β∂␈↓αf <= λ[[x]eq[x;A]]␈↓.
␈↓" ␈↓ α←␈↓4. Revise␈α∞the␈α
new␈α∞evaluator␈α∞to␈α
use␈α∞shallow␈α∞binding.␈α
You␈α∞may␈α∞restrict␈α
your
␈↓ α←␈↓␈↓ β∂solution to the case of simple function application without ␈↓αfunction␈↓.
␈↓"β␈↓ α←␈↓␈↓ ¬
␈↓↓4.7 ␈↓αeval␈↓↓ with Explicit Control␈↓α
␈↓"β␈↓ α←␈↓Recursion␈αand␈αcall-by-value␈αare␈αused␈αto␈α
guide␈αthe␈αflow␈αof␈αcontrol␈αin␈α
a␈αLISP
␈↓ α←␈↓evaluator.␈α∂We␈α∂have␈α∂started␈α∂to␈α∂explore␈α∂the␈α∂implementation␈α⊂of␈α∂call-by-value,
␈↓ α←␈↓and␈α∪now␈α∪we␈α∀wish␈α∪to␈α∪discuss␈α∪the␈α∀implementation␈α∪of␈α∪recursion.␈α∪It␈α∀is␈α∪not
␈↓ α←␈↓necessary␈α∂to␈α∂understand␈α∂␈↓¬how␈↓␈α⊂recursion␈α∂works␈α∂to␈α∂understand␈α⊂recursion;␈α∂that
␈↓ α←␈↓understanding␈α∪␈↓¬is␈↓␈α∀necessary␈α∪when␈α∀we␈α∪wish␈α∀to␈α∪implement␈α∀recursion.␈α∪ The
␈↓ α←␈↓mechanisms␈αused␈αin␈α
the␈αimplementation␈αof␈αany␈α
concept␈αmust␈αbe␈αof␈α
a␈αhigher
␈↓ α←␈↓level␈α∪of␈α∪detail␈α∪than␈α∪the␈α∪mechanism␈α∪being␈α∪implemented.␈α∪ We␈α∪cannot␈α∩use
␈↓ α←␈↓recursion␈α
to␈α
implement␈α
recursion.␈α∞The␈α
basic␈α
purpose␈α
of␈α
recursive␈α∞control␈α
in
␈↓ α←␈↓the␈α∀evaluator␈α∀is␈α∀to␈α∀describe␈α∀what␈α∀computation␈α∀to␈α∀perform␈α∀next␈α∀and␈α∀to
␈↓ α←␈↓describe␈α
where␈α
to␈αgo␈α
when␈α
finished.␈αThe␈α
evaluator␈α
of␈αthis␈α
section␈α
will␈αrely␈α
on
␈↓ α←␈↓explicit␈αdirections␈α
to␈αtell␈αit␈α
what␈αto␈α
do␈αnext.␈α The␈α
idea␈αis␈α
closely␈αrelated␈αto␈α
the
␈↓ α←␈↓logical␈α∞notion␈α∞of␈α∞␈↓↓continuations␈↓␈α
([Str 74a],␈α∞[Rey 72],␈α∞[Fis 72],␈α∞[Hew 76])␈α
and
␈↓ α←␈↓thus␈α
we␈α
will␈αuse␈α
that␈α
terminology␈αhere.␈α
In␈α
the␈αevaluators␈α
of␈α
this␈α
section␈αwe
␈↓ α←␈↓will␈αuse␈αthe␈αdestination␈αto␈αtell␈αwhere␈αthe␈αresult␈αof␈αthe␈αcurrent␈αcomputation␈αis
␈↓ α←␈↓to be put, and use the continuation to tell what the next computation will be.
␈↓"β␈↓ α←␈↓␈↓ β'Note that the computations in ␈↓αdeval␈↓ are basically of two categories:
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ Simple␈α∪transformations␈α∪like␈α∪sending,␈α∩building␈α∪␈↓αdest␈↓␈α∪blocks,␈α∪or␈α∩selecting
␈↓ α←␈↓␈↓ β∂components␈α≠of␈α≤expressions.␈α≠These␈α≠computations␈α≤are␈α≠non-recursive,
␈↓ α←␈↓␈↓ β∂requiring a bounded amount of computation.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ Recursive␈α
calls␈α∞on␈α
the␈α∞evaluator␈α
or␈α∞its␈α
subfunctions.␈α∞These␈α
computations
␈↓ α←␈↓␈↓ β∂can be arbitrarily complex.
␈↓"β␈↓ α←␈↓␈↓ β'It␈αis␈αthe␈αrecursive␈αcomputations␈αwhich␈αwe␈αwish␈αto␈αexamine.␈αOne␈αof␈αthe
␈↓ α←␈↓implications␈α⊂of␈α⊂a␈α⊂function␈α∂call␈α⊂is␈α⊂that␈α⊂we␈α∂have␈α⊂further␈α⊂computation␈α⊂to␈α∂be
␈↓ α←␈↓performed␈α␈↓¬after␈↓␈αthe␈αcall␈αis␈αcompleted.␈αIt␈αis␈αthe␈αresponsibility␈αof␈αthe␈αevaluator
␈↓ α←␈↓to␈α
remember␈α
where␈α
a␈α
computation␈α
has␈αbeen␈α
interrupted␈α
so␈α
that␈α
it␈α
may␈αpick
␈↓ α←␈↓up␈αwhere␈αit␈αleft␈αoff,␈αafter␈αcompleting␈αthe␈αcall.␈α One␈αof␈αthe␈αmajor␈αproblems␈αin
␈↓ α←␈↓implementing␈αevaluators␈α
is␈α"how␈α
to␈αremember".␈α If␈α
the␈αfunction␈α
being␈αcalled
␈↓ α←␈↓is␈αa␈αsimple␈αcalculation␈αof␈αtype␈α␈↓↓1.␈↓␈αabove,␈αthen␈αwe␈αcould␈αreplace␈αthe␈αcall␈αwith␈αa
␈↓ α←␈↓␈↓208 Imperative Constructs in LISP␈↓
(4.7␈↓
␈↓"β␈↓ α←␈↓copy␈α
of␈α
the␈α
body␈α
of␈α
the␈α
definition␈α
where␈α
we␈α
have␈α
replaced␈α
each␈αoccurrence
␈↓ α←␈↓of␈α⊃a␈α⊃formal␈α⊃parameter␈α⊃with␈α⊃the␈α⊃appropriate␈α⊃actual␈α⊃parameter;␈α⊃this␈α⊃works
␈↓ α←␈↓nicely.␈α
Indeed␈α
making␈α
such␈α
formal␈α
substitutions␈α
at␈α
runtime␈α
is␈α
sufficient␈αfor
␈↓ α←␈↓computations␈α⊂of␈α⊂type␈α∂␈↓↓2.␈↓␈α⊂as␈α⊂well.␈α∂ However␈α⊂the␈α⊂solution␈α∂in␈α⊂this␈α⊂case␈α⊂is␈α∂not
␈↓ α←␈↓sufficiently efficient.
␈↓"β␈↓ α←␈↓␈↓ β'Previous␈αevaluators␈α"remembered"␈αwhat␈αwas␈αto␈αbe␈αdone␈αeither␈αby␈αusing
␈↓ α←␈↓recursion,␈αas␈α
in␈α␈↓αeval␈↓␈α
(Section 3.5),␈αor␈αby␈α
explicit␈αsequencing␈α
as␈αin␈α
␈↓αdeval␈↓.␈α We
␈↓ α←␈↓now␈αpropose␈αto␈αexplicitly␈α␈↓↓pass␈αalong␈↓␈αinformation␈αabout␈αwhat␈αto␈αdo␈αafter␈αthe
␈↓ α←␈↓current␈α computation␈α is␈α completed.␈α This␈α information␈α is␈α called␈α the
␈↓ α←␈↓␈↓↓continuation␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊂first␈α⊂evaluator␈α∂of␈α⊂this␈α⊂section␈α∂is␈α⊂␈↓αceval␈↓;␈↓π 13␈↓␈α⊂it␈α∂is␈α⊂a␈α⊂modification␈α∂of
␈↓ α←␈↓␈↓αdeval␈↓λ'␈↓␈α⊂of␈α⊂page 204.␈α⊂It␈α⊂takes␈α⊂a␈α∂single␈α⊂argument␈α⊂␈↓αc␈↓␈α⊂which␈α⊂is␈α⊂a␈α∂continutation.
␈↓ α←␈↓The␈α∪continutation␈α∩is␈α∪passed␈α∪along␈α∩as␈α∪a␈α∪funarg␈α∩structure␈α∪until␈α∪␈↓αceval␈↓␈α∩has
␈↓ α←␈↓completed␈α
its␈α
current␈α
computation.␈α
At␈α∞that␈α
time␈α
␈↓αc␈↓␈α
is␈α
executed.␈α∞ For␈α
example
␈↓ α←␈↓we␈α
transform␈α
␈↓αdeval␈↓λ'␈↓␈α
into␈α
␈↓αceval␈↓␈α
by␈α
forming␈α
a␈α
continuation␈α
from␈α
that␈α
portion
␈↓ α←␈↓of ␈↓αdeval␈↓λ'␈↓ which follows the call on ␈↓αdeval␈↓β1␈↓. Thus:
␈↓"∀␈↓ α←␈↓αceval <= λ[[c]␈↓ ∧∪[isconst[] → send[denote[]];c[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪ isvar[] → send[lookup[]];c[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪ ␈↓
t␈↓α →␈↓ ∧Csave[fun;func[]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧Csave[args;arglist[]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧Cceval␈↓β1␈↓α[function[ev1]] ]]
␈↓" ␈↓ α←␈↓αev1 <= λ[[ ] restore[args]; restore[func]; c[] ]]
␈↓"∀␈↓ α←␈↓For␈α
the␈αsimple␈α
cases␈αwe␈α
just␈αexecute␈α
the␈αcontinuation␈α
after␈αthe␈α
␈↓αsend␈↓;␈αwhen␈α
we
␈↓ α←␈↓have␈αa␈αfunction␈α
application␈αwe␈αmake␈α
up␈αa␈αnew␈α
continuation.␈α When␈α␈↓αceval␈↓β1␈↓␈α
is
␈↓ α←␈↓finished␈α∂with␈α∞the␈α∂function␈α∞application␈α∂it␈α∞executes␈α∂␈↓αev1␈↓;␈α∞that␈α∂does␈α∂the␈α∞␈↓αrestore␈↓
␈↓ α←␈↓operations and then performs the saved continuation.
␈↓"β␈↓ α←␈↓␈↓ β'Note␈αthe␈αuse␈αof␈α␈↓αfunction␈↓.␈αThe␈αnon-local␈αvariable␈α␈↓αc␈↓␈αin␈α␈↓αev1␈↓␈αrepresents␈αthe
␈↓ α←␈↓continuation␈α≠passed␈α≠into␈α≠␈↓αceval␈↓.␈α≠Therefore␈α≠␈↓αc␈↓␈α≠must␈α≠be␈α≠found␈α≤in␈α≠the
␈↓ α←␈↓environment␈α∞of␈α∞the␈α∞body␈α∞of␈α∞␈↓αceval␈↓␈α∞not␈α∞in␈α∞the␈α∞environment␈α∞which␈α∂is␈α∞current
␈↓ α←␈↓when ␈↓αev1␈↓ is applied.
␈↓"β␈↓ α←␈↓␈↓ β'As before, ␈↓αeval␈↓ is expressible with the new evaluator:
␈↓"∀␈↓ α←␈↓α␈↓ αkeval <= λ[[x;y]␈↓ ∧+fun ← ();
␈↓"β␈↓ α←␈↓α␈↓ αk␈↓ ∧+args ← ();
␈↓"β␈↓ α←␈↓α␈↓ αk␈↓ ∧+exp ← x;
␈↓"β␈↓ α←␈↓α␈↓ αk␈↓ ∧+env ← y;
␈↓"β␈↓ α←␈↓α␈↓ αk␈↓ ∧+dest ← alloc_dest[(TLB)];
␈↓"β␈↓ α←␈↓α␈↓ αk␈↓ ∧+ceval[function[λ[[ ] val[dest]]]] ]
␈↓"∀␈↓ α←␈↓Transforming␈α∞the␈α
sub-functions␈α∞of␈α
␈↓αdeval␈↓λ'␈↓␈α∞is␈α
reasonably␈α∞straightforward:␈α
the
␈↓ α←␈↓segment␈α∪of␈α∪program␈α∪below␈α∪a␈α∪call␈α∀on␈α∪one␈α∪of␈α∪the␈α∪recursive␈α∪parts␈α∀of␈α∪the
␈↓ α←␈↓evaluator␈αis␈αgiven␈αa␈αname;␈αa␈α
new␈αcontinuation␈αis␈αmade,␈αsimilar␈αto␈αthe␈α
process
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 13␈↓␈↓αc␈↓ for ␈↓αc␈↓ontrol or ␈↓αc␈↓ontinuation
␈↓ α←␈↓␈↓4.7␈↓ π9␈↓αeval␈↓ with Explicit Control 209␈↓α
␈↓"β␈↓ α←␈↓of␈α∪creating␈α∪␈↓αev1␈↓;␈α∪then␈α∪the␈α∪transformation␈α∪process␈α∪is␈α∪applied␈α∪to␈α∪each␈α∩new
␈↓ α←␈↓continuation. For example, here's ␈↓αceval␈↓β1␈↓:
␈↓"∀␈↓ α←␈↓αceval␈↓β1␈↓α <= λ[[c]␈↓ ∧∨[isatom[] →␈↓ ¬;[iscond[] → devcond[c];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬; isprim[] →␈↓ εKsave[env;env];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬;␈↓ ¬G␈↓ εKsave[dest;alloc_dest[createvars[]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬;␈↓ ¬G␈↓ εKevalargs[function[ev2]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬; ␈↓
t␈↓α → ceval␈↓β2␈↓α[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ islambda[] →␈↓ ¬Gsave[env;env];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬;␈↓ ¬Gsave[dest;alloc_dest[vars[]]
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬;␈↓ ¬Gevalargs[function[ev5]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ ␈↓
t␈↓α → ceval␈↓β2␈↓α[]] ]]
␈↓" ␈↓ α←␈↓␈↓αceval␈↓β2␈↓α <= λ[[ ] save[exp;fun]; ceval[function[ev3]]]␈↓
␈↓"∀␈↓ α←␈↓αev2 <= λ[[ ]␈↓ β{link[];␈↓ εKev3 <= λ[[ ]␈↓ πsrestore[exp];
␈↓"β␈↓ α←␈↓α␈↓ β{restore[dest];␈↓ εK␈↓ πssave[fun;receive[]];
␈↓"β␈↓ α←␈↓α␈↓ β{execute[];␈↓ εK␈↓ πsceval␈↓β1␈↓α[function[ev4]]]
␈↓"β␈↓ α←␈↓α␈↓ β{restore[env];
␈↓"β␈↓ α←␈↓α␈↓ β{c[]]
␈↓"∀␈↓ α←␈↓αev4 <= λ[[ ] restore[fun]; c[]]␈↓ εKev5 <= λ[[ ]␈↓ πslink[];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εK␈↓ πsrestore[dest];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εK␈↓ πssave[args;bodylist[]];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εK␈↓ πsevalargs[function[ev6]]]
␈↓"∞␈↓ α←␈↓αev6 <= λ[[ ]␈↓ β{restore[args];
␈↓"β␈↓ α←␈↓α␈↓ β{restore[env];
␈↓"β␈↓ α←␈↓α␈↓ β{c[]]
␈↓"∪␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Continuations␈α⊗can␈α↔also␈α⊗be␈α⊗used␈α↔as␈α⊗general␈α⊗programming␈α↔tools.␈α⊗For
␈↓ α←␈↓␈↓ β∂example, evaluate ␈↓αfact␈↓β2␈↓α[2]␈↓ where:
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓αfact␈↓β2␈↓α <= λ[[x] fact␈↓β2␈↓λ'␈↓α[x;function[λ[[x] x]]]]
␈↓" ␈↓ α←␈↓α␈↓ ββfact␈↓β2␈↓λ'␈↓α <= λ[[n;f]␈↓ ∧g[zerop[n] → f[1];
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧g ␈↓
t␈↓α → fact␈↓β2␈↓λ'␈↓α[␈↓ ¬wsub1[n];
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧g␈↓ ¬wfunction[λ[[x] f[times[n;x]]]]]]]
␈↓" ␈↓ α←␈↓2. Write the new version of ␈↓αdevcond␈↓ and ␈↓αevalargs␈↓.
␈↓" ␈↓ α←␈↓3. Using␈α≥the␈α≥new␈α≤evaluator,␈α≥sketch␈α≥the␈α≤evaluation␈α≥of␈α≥␈↓αf[A]␈↓␈α≤where:
␈↓ α←␈↓␈↓ β∂␈↓αf <= λ[[x]eq[x;A]]␈↓.
␈↓"α␈↓ α←␈↓␈↓ β'The␈αfinal␈αtransformation␈αstep␈αis␈αanalogous␈αto␈αthat␈αwhich␈αwe␈αperformed
␈↓ α←␈↓in␈α
moving␈α
from␈α
␈↓αdeval␈↓␈α∞to␈α
␈↓αdeval␈↓λ'␈↓:␈α
we␈α
remove␈α∞the␈α
argument␈α
to␈α
␈↓αceval␈↓␈α∞and␈α
pass
␈↓ α←␈↓the␈α⊂continuation␈α⊃explicitly␈α⊂in␈α⊃a␈α⊂non-local␈α⊂variable␈α⊃named␈α⊂␈↓αcont␈↓.␈α⊃ This␈α⊂new
␈↓ α←␈↓evaluator is named ␈↓αceval␈↓λ'␈↓:
␈↓ α←␈↓␈↓210 Imperative Constructs in LISP␈↓
(4.7␈↓
␈↓"β␈↓ α←␈↓αceval␈↓λ'␈↓α <= λ[[ ]␈↓ ∧∨[isconst[] → send[denote[]]; cont[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ isvar[] → send[lookup[]]; cont[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ ␈↓
t␈↓α →␈↓ ∧[save[fun;func[]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧[save[args;arglist[]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧[save[cont;function[ev1]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧[ceval␈↓β1␈↓α[] ]]
␈↓" ␈↓ α←␈↓αev1 <= λ[[ ] restore[args]; restore[func]; restore[cont]; cont[] ]]
␈↓"∀␈↓ α←␈↓We␈α⊂can␈α⊂remove␈α⊂more␈α⊂control␈α⊂structure␈α⊂from␈α⊂the␈α⊂evaluator␈α⊂by␈α⊂noting␈α∂that
␈↓ α←␈↓executing␈α∂the␈α∂continuation,␈α∂"␈↓αcont[]␈↓",␈α∂and␈α∂executing␈α∂the␈α∂explicit␈α∂calls␈α∂on␈α∂the
␈↓ α←␈↓evaluator's␈αsubfunctions␈αare␈αtwo␈αmanifestations␈αof␈αthe␈αsame␈αphenomenon.␈α
In
␈↓ α←␈↓the␈α∂first␈α∂case␈α⊂we␈α∂restore␈α∂to␈α⊂a␈α∂variable␈α∂and␈α∂then␈α⊂execute␈α∂the␈α∂variable␈α⊂as␈α∂a
␈↓ α←␈↓function␈αapplication;␈αin␈αthe␈αsecond,␈αwe␈αexecute␈αa␈αknown␈αcall.␈αWe␈αcan␈αreplace
␈↓ α←␈↓these␈αtwo␈αactions␈αby␈αa␈αcommon␈α
action␈αif␈αwe␈αalways␈αexecute␈αfrom␈αthe␈α
variable
␈↓ α←␈↓␈↓αcont␈↓ and replace calls like "␈↓αceval␈↓β1␈↓α[]␈↓" with the sequence:
␈↓"∀␈↓ α←␈↓α␈↓ ¬¬save[cont;function[ceval␈↓β1␈↓α]]; cont[]
␈↓"∀␈↓ α←␈↓Notice␈α
that␈α
when␈α
we␈αmake␈α
this␈α
last␈α
␈↓αsave␈↓␈α
we␈αknow␈α
that␈α
the␈α
current␈α
value␈αof
␈↓ α←␈↓␈↓αcont␈↓␈α⊃is␈α⊃␈↓αev1␈↓.␈α⊃Notice␈α∩also␈α⊃that␈α⊃when␈α⊃we␈α∩execute␈α⊃␈↓αcont[]␈↓␈α⊃we␈α⊃enter␈α∩␈↓αceval␈↓β1␈↓␈α⊃and
␈↓ α←␈↓therefore␈αwithin␈αthis␈αcall␈αon␈α␈↓αceval␈↓β1␈↓,␈α␈↓αcont␈↓␈α␈↓¬is␈↓␈α␈↓αceval␈↓β1␈↓.␈α All␈αthis␈αdiscussion␈αcan␈αbe
␈↓ α←␈↓simplified␈αif␈αwe␈αthink␈αa␈αbit␈αabout␈αthe␈αpurpose␈αof␈αcontinuations:␈αwe␈αwill␈αneed
␈↓ α←␈↓to␈α_make␈α_note␈α_of␈α_what␈α_the␈α_continuation␈α_should␈α_be␈α_␈↓¬after␈↓␈α_the␈α↔current
␈↓ α←␈↓computation␈α⊃is␈α⊃finished;␈α⊃and␈α⊂we␈α⊃will␈α⊃need␈α⊃to␈α⊂set␈α⊃␈↓αcont␈↓␈α⊃to␈α⊃designate␈α⊂which
␈↓ α←␈↓computation␈α⊃to␈α⊃perform␈α⊃now.␈α⊃ We␈α⊃therefore␈α⊃introduce␈α⊃a␈α⊃binary␈α⊃primitive
␈↓ α←␈↓␈↓αsave_cont␈↓␈αwhich␈αwill␈αsave␈αits␈αfirst␈αargument␈αsuch␈αthat␈α␈↓αrestore␈↓␈αcan␈αrestore␈αit␈αto
␈↓ α←␈↓␈↓αcont␈↓ at the appropriate time; ␈↓αsave_cont␈↓ will set ␈↓αcont␈↓ from its second argument.
␈↓"∀␈↓ α←␈↓α␈↓ ∧Gsave_cont <= λ[[x;y] cont ← x; save[cont;y]]
␈↓"∀␈↓ α←␈↓We␈α⊂can␈α⊂remove␈α⊂the␈α⊂calls␈α⊃␈↓αcont[]␈↓,␈α⊂and␈α⊂perform␈α⊂the␈α⊂execution␈α⊃outside␈α⊂␈↓αceval␈↓λ'␈↓
␈↓ α←␈↓using a simple loop:
␈↓"∀␈↓ α←␈↓α␈↓ β{loop <= λ[[] prog[[]
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ¬;l␈↓ ¬Scont[]
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ¬;␈↓ ¬Sgo[l] ]]
␈↓"∀␈↓ α←␈↓Each␈α∂function␈α∂executed␈α∂by␈α∂␈↓αcont[]␈↓␈α∂will␈α∂perform␈α∂some␈α∂simple␈α∂operations␈α∂like
␈↓ α←␈↓␈↓αsend␈↓␈α∞or␈α∞␈↓αalloc_dest␈↓,␈α∞and␈α∞then␈α∞will␈α∞exit,␈α∞setting␈α∞␈↓αcont␈↓␈α∞to␈α∞a␈α∞function␈α∞name.␈α
The
␈↓ α←␈↓next␈αpass␈α
around,␈α␈↓αloop␈↓␈αwill␈α
execute␈αthe␈αnew␈α
␈↓αcont␈↓.␈αAfter␈α
slight␈αreorganization
␈↓ α←␈↓to eliminate some ␈↓αsave-restore␈↓ operations on ␈↓αcont␈↓, we have:
␈↓"∀␈↓ α←␈↓αceval␈↓λ''␈↓α <= λ[[ ]␈↓ ∧+[isconst[] → send[denote[]]; restore[cont];
␈↓"β␈↓ α←␈↓α␈↓ ∧+ isvar[] → send[lookup[]]; restore[cont];
␈↓"β␈↓ α←␈↓α␈↓ ∧+ ␈↓
t␈↓α →␈↓ ∧gsave[fun;func[]];
␈↓"β␈↓ α←␈↓α␈↓ ∧+␈↓ ∧gsave[args;arglist[]];
␈↓"β␈↓ α←␈↓α␈↓ ∧+␈↓ ∧gsave_cont[quote[ev1];quote[ceval␈↓β1␈↓α]] ]]
␈↓" ␈↓ α←␈↓αev1 <= λ[[ ] restore[args]; restore[func]; restore[cont] ]]
␈↓ α←␈↓␈↓4.7␈↓ π9␈↓αeval␈↓ with Explicit Control 211␈↓α
␈↓"β␈↓ α←␈↓Notice␈α
the␈α∞use␈α
of␈α∞␈↓αquote␈↓␈α
rather␈α
than␈α∞␈↓αfunction␈↓.␈α
In␈α∞the␈α
previous␈α∞evaluators␈α
we
␈↓ α←␈↓used␈α∃␈↓αfunction␈↓␈α⊗since␈α∃we␈α∃had␈α⊗to␈α∃save␈α∃the␈α⊗current␈α∃environment;␈α⊗but␈α∃the
␈↓ α←␈↓continuation␈α
␈↓αc␈↓␈α
was␈α
the␈α
only␈α∞free␈α
variable␈α
which␈α
was␈α
in␈α
jeopardy.␈α∞In␈α
␈↓αceval␈↓λ''␈↓
␈↓ α←␈↓we␈α⊂have␈α⊂explicitly␈α⊂saved␈α⊂the␈α∂continuation␈α⊂using␈α⊂␈↓αsave_cont␈↓,␈α⊂and␈α⊂thus␈α∂␈↓αquote␈↓
␈↓ α←␈↓plus␈αthe␈αproper␈αuse␈αof␈α␈↓αrestore␈↓␈αcan␈αreplace␈α␈↓αfunction␈↓.␈αWe␈αwill␈αalso␈αintroduce␈αan
␈↓ α←␈↓abbreviation, writing ␈↓λ`␈↓αxx␈↓ for ␈↓αquote[xx]␈↓.␈↓π 14␈↓
␈↓"∀␈↓ α←␈↓α␈↓Finally, here's ␈↓αeval␈↓:
␈↓" ␈↓ α←␈↓α␈↓ β∂eval <= λ[[x;y] catch[␈↓ ¬#prog[[ ]␈↓ ε∂fun ← ();
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ¬#␈↓ ε∂args ← ();
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ¬#␈↓ ε∂exp ← x;
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ¬#␈↓ ε∂env ← y;
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ¬#␈↓ ε∂dest ← alloc_dest[(TLB)];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ¬#␈↓ ε∂save_cont[␈↓ π∪␈↓λ`␈↓αλ[[]throw[val[dest]; out]]];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ¬#␈↓ ε∂␈↓ π∪␈↓λ`␈↓αceval␈↓λ''␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ¬#␈↓ ε∂loop[]];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ¬#out]]
␈↓"β␈↓ α←␈↓␈↓ β'What␈α∂has␈α∂been␈α∂gained␈α∂by␈α∂these␈α∂transformations␈α∂of␈α∂the␈α∂original␈α∞␈↓αeval␈↓?
␈↓ α←␈↓We␈α∞have␈α∞made␈α∞the␈α∞mechanisms␈α∞which␈α∞were␈α∞implicit␈α∞in␈α∞LISP␈α∞very␈α∞explicit.
␈↓ α←␈↓We␈α↔have␈α↔described␈α↔the␈α↔implementations␈α↔of␈α↔LISP's␈α↔access␈α_and␈α↔control
␈↓ α←␈↓requirements␈αin␈αterms␈αof␈αvery␈αsimple␈αcomputations.␈αWe␈αnow␈αhave␈αdeveloped
␈↓ α←␈↓enough␈α
detail␈α
that␈αwe␈α
can␈α
give␈α
a␈αfaithful␈α
implementation␈α
description␈α
of␈αall␈α
of
␈↓ α←␈↓LISP including the ␈↓αfunction␈↓ and ␈↓αprog␈↓ constructs.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Could␈α#we␈α$use␈α#statements␈α$like␈α#␈↓αsave_cont[ev1;eval␈↓β1␈↓α]␈↓␈α$rather␈α#than
␈↓ α←␈↓␈↓ β∂␈↓αsave_cont[␈↓λ`␈↓αev1; ␈↓λ`␈↓αeval␈↓β1␈↓α]␈↓ ?
␈↓" ␈↓ α←␈↓2. Using␈α≥the␈α≥new␈α≤evaluator,␈α≥sketch␈α≥the␈α≤evaluation␈α≥of␈α≥␈↓αf[A]␈↓␈α≤where:
␈↓ α←␈↓␈↓ β∂␈↓αf <= λ[[x] eq[x;A]]␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬%␈↓↓4.8 An Evaluator for ␈↓αprog␈↓↓␈↓α
␈↓"β␈↓ α←␈↓The␈α⊃evaluator␈α∩in␈α⊃this␈α∩section␈α⊃will␈α∩be␈α⊃the␈α∩definitive␈α⊃interpreter␈α∩for␈α⊃LISP
␈↓ α←␈↓throughout␈α⊂the␈α⊃rest␈α⊂of␈α⊃this␈α⊂book.␈α⊂It␈α⊃will␈α⊂handle␈α⊃the␈α⊂applicative␈α⊃subset␈α⊂of
␈↓ α←␈↓LISP as well as handling ␈↓αprog␈↓ related constructs.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α⊃need␈α⊃to␈α⊂add␈α⊃more␈α⊃mechanism␈α⊃to␈α⊂handle␈α⊃␈↓αprog␈↓.␈α⊃For␈α⊃example␈α⊂the
␈↓ α←␈↓execution␈α↔of␈α↔the␈α↔␈↓αreturn␈↓␈α↔statement␈α↔requires␈α↔that␈α↔we␈α↔locate␈α⊗dynamically
␈↓ α←␈↓surrounding␈α
␈↓αprog␈↓s.␈α
The␈α␈↓αgo␈↓␈α
must␈α
also␈α
locate␈αthe␈α
latest␈α
␈↓αprog␈↓␈α
which␈αsurrounds
␈↓ α←␈↓the␈α
␈↓αgo␈↓␈αand␈α
contains␈α
the␈αdesired␈α
label.␈α The␈α
evaluator␈α
needs␈αto␈α
know␈αwhen␈α
we
␈↓ α←␈↓are␈α∀evaluating␈α∀a␈α∀conditional␈α∀expression␈α∪and␈α∀when␈α∀we␈α∀are␈α∀evaluating␈α∪a
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 14␈↓This␈α∞abbreviation␈α∂is␈α∞used␈α∂in␈α∞several␈α∂implementations␈α∞of␈α∂LISP.␈α∞See
␈↓ α←␈↓page 280.
␈↓ α←␈↓␈↓212 Imperative Constructs in LISP␈↓
(4.8␈↓
␈↓"β␈↓ α←␈↓conditional␈α
statement;␈α
if␈αwe␈α
are␈α
(immediately)␈α
in␈αa␈α
␈↓αprog␈↓␈α
then␈α
it's␈αa␈α
conditional
␈↓ α←␈↓statement,␈α⊃otherwise␈α⊃it's␈α⊃a␈α⊂conditional␈α⊃expression.␈α⊃ All␈α⊃of␈α⊃this␈α⊂information
␈↓ α←␈↓could␈α≤be␈α≤discovered␈α≠by␈α≤the␈α≤evaluator␈α≠using␈α≤the␈α≤currently␈α≠supplied
␈↓ α←␈↓information,␈αhowever␈αthe␈αevaluator␈αcan␈αbe␈αmade␈αmore␈αefficient␈αby␈αadding␈αa
␈↓ α←␈↓bit␈α∞more␈α∞explicit␈α∞information.␈α∞Most␈α∞of␈α∞the␈α∞additions␈α∞involve␈α∞␈↓αprog␈↓-entry,␈α
␈↓αgo␈↓,
␈↓ α←␈↓and␈α␈↓αreturn␈↓␈αand␈αwill␈αtherefore␈αbe␈αpresented␈αwhen␈αwe␈αdiscuss␈αthat␈αpart␈αof␈αthe
␈↓ α←␈↓evaluator.␈α⊂The␈α⊂only␈α⊂addition␈α∂we␈α⊂will␈α⊂make␈α⊂now␈α∂will␈α⊂be␈α⊂introduction␈α⊂of␈α∂a
␈↓ α←␈↓variable␈α⊂␈↓αtype␈↓␈α⊂which␈α⊂is␈α⊃set␈α⊂to␈α⊂␈↓αPROC␈↓␈α⊂when␈α⊃we␈α⊂begin␈α⊂an␈α⊂evaluation␈α⊃of␈α⊂an
␈↓ α←␈↓application, and is set to ␈↓αPROG␈↓ when we enter a ␈↓αprog␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∩will␈α∩also␈α∩rework␈α∩some␈α∩of␈α∩our␈α∩current␈α∩sub-functions␈α∩to␈α∩improve
␈↓ α←␈↓readability.␈α Since␈αsequences␈αof␈α␈↓αsave␈↓s␈αhappen␈αfrequently␈αin␈αthe␈αevaluators␈αwe
␈↓ α←␈↓introduce␈α
a␈α
new␈α
procedure␈α
named␈α
␈↓αsave␈↓λ'␈↓␈αwhich␈α
acts␈α
like␈α
a␈α
sequence␈α
of␈αcalls␈α
on
␈↓ α←␈↓␈↓αsave␈↓␈αfor␈αarguments␈αother␈αthan␈α␈↓αcont␈↓.␈α In␈αthis␈αlatter␈αcase,␈αa␈αcall␈αon␈α␈↓αsave_cont␈↓␈αis
␈↓ α←␈↓simulated.␈α∀ Similarly␈α∀we␈α∀introduce␈α∀an␈α∀iterated␈α∀version␈α∀of␈α∃␈↓αrestore␈↓␈α∀named
␈↓ α←␈↓␈↓αrestore␈↓λ'␈↓.
␈↓"β␈↓ α←␈↓␈↓ ε≡␈↓↓Problem␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Write␈α
␈↓αrestore␈↓λ'␈↓␈α
and␈α
␈↓αsave␈↓λ'␈↓␈α
as␈α
macros␈α
which␈α
expand␈α
to␈α
calls␈α
on␈α
␈↓αsave␈↓␈αand
␈↓ α←␈↓␈↓αrestore␈↓.
␈↓"α␈↓ α←␈↓␈↓ β'Here's the new ␈↓αpeval␈↓:
␈↓" ␈↓ α←␈↓αpeval <= λ[[ ]␈↓ ∧∨[isconst[] → send[denote[]];restore[cont];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ isvar[] → send[lookup[]];restore[cont];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ ␈↓
t␈↓α →␈↓ ∧[save␈↓λ'␈↓α[␈↓ ¬#fun;func[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧[␈↓ ¬#args;arglist[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧[␈↓ ¬#cont; ␈↓λ`␈↓αev1; ␈↓λ`␈↓αpeval␈↓β1␈↓α] ]]
␈↓"β␈↓ α←␈↓αev1 <= λ[[ ] restore␈↓λ'␈↓α[args;func;cont] ]]
␈↓"∀␈↓ α←␈↓It␈α
is␈α
the␈αresponsibility␈α
of␈α
␈↓αpeval␈↓␈αto␈α
recognize␈α
the␈α
occurrence␈αof␈α
one␈α
of␈αthe␈α
basic
␈↓ α←␈↓forms:␈α∩a␈α⊃variable,␈α∩a␈α⊃constant,␈α∩or␈α⊃a␈α∩function␈α⊃application.␈α∩ Discovering␈α⊃the
␈↓ α←␈↓structure␈α⊃of␈α∩an␈α⊃application␈α∩is␈α⊃the␈α⊃business␈α∩of␈α⊃␈↓αpeval␈↓β1␈↓.␈α∩ We␈α⊃need␈α∩to␈α⊃know
␈↓ α←␈↓whether␈αthe␈αfunction␈αposition␈αrepresents␈αa␈αcall-by-value␈αfunction␈αor␈αa␈αspecial
␈↓ α←␈↓form.␈αSo␈αfar␈αthe␈αonly␈αspecial␈αform␈αwe␈αrecognize␈αis␈α␈↓αcond␈↓;␈↓π 15␈↓␈αhowever␈αmany␈αof
␈↓ α←␈↓the␈α⊃constructs␈α⊃which␈α⊃␈↓αprog␈↓␈α⊃introduced␈α⊃are␈α⊃special␈α⊃forms.␈α⊃We␈α⊃could␈α∩add␈α⊃a
␈↓ α←␈↓collection␈α∪of␈α∪recognizers␈α∪␈↓αissetq␈↓,␈α∪␈↓αisgo␈↓,␈α∩␈↓αisprog␈↓,␈α∪etc.,␈α∪to␈α∪augment␈α∪the␈α∩existing
␈↓ α←␈↓␈↓αiscond␈↓.␈αInstead␈αwe␈αwould␈αrather␈αadd␈αa␈αdevice␈αsimilar␈αto␈α␈↓αisprim␈↓␈αbut␈αinstead␈α
of
␈↓ α←␈↓handling␈αthe␈αcall-by-value␈αprimitives␈αwith␈αthe␈αunderlying␈αevaluator,␈αwe␈αwish
␈↓ α←␈↓to␈α
handle␈α
special␈α∞forms␈α
with␈α
our␈α∞own␈α
pieces␈α
of␈α∞␈↓αpeval␈↓.␈α
We␈α
need␈α∞a␈α
predicate
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 15␈↓Actually␈α␈↓αquote␈↓␈αis␈αalso␈αa␈αspecial␈αform␈αwhich␈αwe␈αrecognize,␈αhowever␈αits
␈↓ α←␈↓recognition is handled within ␈↓αisconst␈↓.
␈↓ α←␈↓␈↓4.8␈↓ πkAn Evaluator for ␈↓αprog␈↓ 213␈↓α
␈↓"β␈↓ α←␈↓named␈α␈↓αisspec␈↓␈αto␈α
recognize␈αoccurrences␈αof␈αspecial␈α
forms␈αand␈αwill␈αneed␈α
to␈αadd
␈↓ α←␈↓functions␈α
to␈α∞␈↓αpeval␈↓␈α
to␈α∞execute␈α
the␈α∞appropriate␈α
programs␈α∞when␈α
␈↓αisspec␈↓␈α∞is␈α
true.
␈↓ α←␈↓We␈α∩will␈α∩do␈α∩this␈α∩by␈α∪introducing␈α∩a␈α∩global␈α∩table␈α∩called␈α∩␈↓αspectbl␈↓.␈α∪The␈α∩name
␈↓ α←␈↓components␈α∪of␈α∩the␈α∪table␈α∩will␈α∪be␈α∩the␈α∪names␈α∩for␈α∪special␈α∩forms;␈α∪the␈α∩value
␈↓ α←␈↓components␈αof␈α␈↓αspectbl␈↓␈αwill␈αbe␈αthe␈αnames␈αof␈αfunctions␈αwhich␈αwill␈αevaluate␈αthe
␈↓ α←␈↓corresponding special form.␈↓π 16␈↓ Then we can write:
␈↓"∀␈↓ α←␈↓α␈↓ β∂isspec <= λ[[ ][null[nassoc[fun;spectbl]] → ␈↓
f␈↓α; ␈↓
t␈↓α → ␈↓
t␈↓α]
␈↓" ␈↓ α←␈↓α␈↓ β∂nassoc <= λ[[x;l]␈↓ ∧s[null[l] → ( );
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧s eq[x;name[first[l]]] → first[l];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧s ␈↓
t␈↓α → nassoc[x;rest[l]]]
␈↓"∀␈↓ α←␈↓␈↓ β'To␈α
execute␈α
the␈α
appropriate␈α
routine␈α
we␈α
need␈α
only␈α
put␈α
the␈α
name␈α
in␈αthe
␈↓ α←␈↓variable ␈↓αcont␈↓ and ␈↓αloop␈↓ will do the rest. We can load ␈↓αcont␈↓ by:
␈↓"∀␈↓ α←␈↓α␈↓ β≠cont ← valspec[ ]; ␈↓where: ␈↓αvalspec <= λ[[ ]value[nassoc[fun;spectbl]]]
␈↓"∀␈↓ α←␈↓For␈α⊃example,␈α∩with␈α⊃␈↓αspectbl␈↓␈α∩bound␈α⊃to␈α∩␈↓α((COND␈α⊃DEVCOND))␈↓,␈α∩our␈α⊃previous
␈↓ α←␈↓␈↓αceval␈↓β1␈↓ would work quite nicely as:
␈↓"∀␈↓ α←␈↓αceval␈↓β1␈↓α <= λ[[ ]␈↓ ∧+[isatom[] →␈↓ ¬G[isspec[] → cont ← valspec[ ];
␈↓"β␈↓ α←␈↓α␈↓ ∧+␈↓ ¬G isprim[] →␈↓ εcsave[env;env];
␈↓"β␈↓ α←␈↓α␈↓ ∧+␈↓ ¬G ... ] ... ]]
␈↓"∀␈↓ α←␈↓␈↓ β'Before␈α∂introducing␈α∂␈↓αpeval␈↓β1␈↓␈α∂we␈α∂should␈α∂say␈α∂a␈α∂bit␈α∂about␈α∂the␈α∂inefficiency
␈↓ α←␈↓involved␈α
in␈α
the␈α
␈↓αisspec␈↓-␈↓αvalspec␈↓␈α
pair.␈αWe␈α
already␈α
noted␈α
that␈α
the␈α
linear␈αsearch
␈↓ α←␈↓encoded␈α≥in␈α≥␈↓αassoc␈↓␈α≥is␈α≥unnecessarily␈α≥inefficient.␈α≥ However␈α≡the␈α≥present
␈↓ α←␈↓predicate-function␈α∂pair␈α∂is␈α∂even␈α∂more␈α∞wasteful;␈α∂if␈α∂␈↓αisspec␈↓␈α∂␈↓¬is␈↓␈α∂true␈α∂we␈α∞perform
␈↓ α←␈↓␈↓αnassoc[fun;spectbl]␈↓␈αtwice.␈α A␈αmore␈αefficient␈αcomputation␈αmight␈αsave␈αthe␈α
result
␈↓ α←␈↓of␈α∞the␈α∂first␈α∞call␈α∂on␈α∞␈↓αnassoc␈↓␈α∞in␈α∂a␈α∞temporary␈α∂variable␈α∞␈↓αt1␈↓␈α∞and␈α∂if␈α∞␈↓αisspec␈↓␈α∂is␈α∞true,
␈↓ α←␈↓move the ␈↓αvalue␈↓-part of ␈↓αt1␈↓ to ␈↓αcont␈↓. Thus:
␈↓"∀␈↓ α←␈↓α␈↓ β∂isspec <= λ[[ ]␈↓ ∧[t1 ← nassoc[fun;spectbl]]
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧[[null[t1] → ␈↓
f␈↓α; ␈↓
t␈↓α → ␈↓
t␈↓α] ]
␈↓" ␈↓ α←␈↓α␈↓with: ␈↓αvalspec <= λ[[ ] value[t1]]
␈↓"∀␈↓ α←␈↓This␈α∂is␈α∞a␈α∂useful␈α∞programming␈α∂trick␈α∂but␈α∞does␈α∂not␈α∞add␈α∂to␈α∞the␈α∂clarity␈α∂of␈α∞the
␈↓ α←␈↓program. In Section 5.5 we shall see a more subtle, but related trick.
␈↓"β␈↓ α←␈↓␈↓ β'What␈α∀follows␈α∃is␈α∀the␈α∃remainder␈α∀of␈α∃the␈α∀evaluator␈α∃interspersed␈α∀with
␈↓ α←␈↓commentary.␈α
The␈α∞main␈α
function␈α
is␈α∞␈↓αpeval␈↓β1␈↓;␈α
it␈α
handles␈α∞function␈α
applications.
␈↓ α←␈↓The␈α∞application␈α∞is␈α
either␈α∞a␈α∞call-by-value␈α
application␈α∞or␈α∞it␈α
is␈α∞a␈α∞special␈α
form.
␈↓ α←␈↓An␈α∂instance␈α∂of␈α∞the␈α∂first␈α∂requires␈α∞evaluation␈α∂of␈α∂the␈α∞argument␈α∂list␈α∂and␈α∞then
␈↓ α←␈↓evaluation␈α∞of␈α∞the␈α∞procedure␈α∞body.␈α∞If␈α
the␈α∞application␈α∞is␈α∞a␈α∞special␈α∞form␈α
then
␈↓ α←␈↓the␈α∩evaluation␈α⊃is␈α∩handled␈α⊃by␈α∩a␈α∩special␈α⊃piece␈α∩of␈α⊃the␈α∩evaluator,␈α∩using␈α⊃the
␈↓ α←␈↓mechanism␈α→described␈α→above.␈α~The␈α→call-by-value␈α→applications␈α~are␈α→either
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 16␈↓In␈αthe␈αnext␈αchapter␈α
we␈αwill␈αsee␈αa␈α
more␈αefficient␈αway␈αto␈αrecognize␈α
and
␈↓ α←␈↓execute special forms.
␈↓ α←␈↓␈↓214 Imperative Constructs in LISP␈↓
(4.8␈↓
␈↓"β␈↓ α←␈↓primitive␈αapplications␈αor␈αare␈αanonymous␈αfunction␈αapplications.␈αIf␈αthe␈αform␈αis
␈↓ α←␈↓not␈α∪recognizable␈α∩then␈α∪the␈α∩function-position␈α∪is␈α∩evaluated␈α∪until␈α∪a␈α∩function
␈↓ α←␈↓object ␈↓¬is␈↓ recognized. At that time, the function is applied to its argument list.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
anomalous␈α
situation␈α
involves␈α
the␈α
application␈α
of␈α
a␈α
␈↓αfunarg␈↓;␈αthough
␈↓ α←␈↓it␈α⊃is␈α⊂possible␈α⊃to␈α⊂handle␈α⊃this␈α⊃case␈α⊂as␈α⊃a␈α⊂primitive,␈α⊃it␈α⊂is␈α⊃more␈α⊃instructive␈α⊂to
␈↓ α←␈↓present it in detail. Here is ␈↓αpeval␈↓β1␈↓:
␈↓"∀␈↓ α←␈↓␈↓αpeval␈↓β1␈↓α <= λ[[ ]␈↓ ∧∨[isatom[] →␈↓ ¬;[isspec[] → cont ← valspec[ ];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬; isprim[] → save␈↓λ'␈↓α[␈↓ π∨env;env;
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬;␈↓ ¬S␈↓ ε'␈↓ εo␈↓ π∨dest;alloc_dest[createvars[]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬;␈↓ ¬S␈↓ ε'␈↓ εo␈↓ π∨cont; ␈↓λ`␈↓αev2; ␈↓λ`␈↓αevalargs];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬; ␈↓
t␈↓α → save␈↓λ'␈↓α[exp;fun;cont; ␈↓λ`␈↓αev3; ␈↓λ`␈↓αpeval];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ islambda[] →␈↓ ¬Ssave␈↓λ'␈↓α[␈↓ ε'env;env;
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬;␈↓ ¬S␈↓ ε'dest;alloc_dest[vars[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬;␈↓ ¬S␈↓ ε'cont; ␈↓λ`␈↓αev5; ␈↓λ`␈↓αevalargs];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ isfunarg[] →␈↓ ¬Sprog[[x;y]
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬;␈↓ ¬S␈↓ ε'x ← args;
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬;␈↓ ¬S␈↓ ε'args ← bodylist[second[fun]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬;␈↓ ¬S␈↓ ε'y ← env;
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬;␈↓ ¬S␈↓ ε'env ← third[fun];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬;␈↓ ¬S␈↓ ε'save␈↓λ'␈↓α[␈↓ εoenv;env;
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬;␈↓ ¬S␈↓ ε'␈↓ εoargs;x;
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬;␈↓ ¬S␈↓ ε'␈↓ εodest;alloc_dest[vars[second[fun]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬;␈↓ ¬S␈↓ ε'␈↓ εoenv;y;
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ¬;␈↓ ¬S␈↓ ε'␈↓ εocont; ␈↓λ`␈↓αev7; ␈↓λ`␈↓αevalargs]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ ␈↓
t␈↓α → save␈↓λ'␈↓α[exp;fun; cont; ␈↓λ`␈↓αev3; ␈↓λ`␈↓αpeval] ]]
␈↓"β␈↓ α←␈↓The functions ␈↓αev2␈↓ through ␈↓αev8␈↓ handle the control in ␈↓αpeval␈↓β1␈↓:
␈↓" ␈↓ α←␈↓α␈↓ βSev2 <= λ[[ ] link[]; restore[dest]; execute[]; restore␈↓λ'␈↓α[env;cont]]
␈↓" ␈↓ α←␈↓α␈↓This function passes the evaluation to the body of the primitive.
␈↓" ␈↓ α←␈↓␈↓ β>␈↓αev3 <= λ[[ ] restore[exp]; save␈↓λ'␈↓α[fun;receive[];cont; ␈↓λ`␈↓αev4; ␈↓λ`␈↓αpeval␈↓β1␈↓α]]
␈↓"β␈↓ α←␈↓␈↓αev3␈↓␈α
is␈α
the␈α
return␈α
point␈α
if␈α
we␈α
have␈α
to␈α
evaluate␈α
the␈α
function␈α
position␈α
of␈αa␈α
form.
␈↓ α←␈↓When␈α␈↓αev3␈↓␈αis␈α
called␈αthe␈αresult␈αof␈α
that␈αevaluation␈αis␈α
in␈αthe␈αcurrent␈αdest-slot.␈α
A
␈↓ α←␈↓␈↓αreceive␈↓ gets the value; we then pass the new form back to ␈↓αpeval␈↓β1␈↓.
␈↓" ␈↓ α←␈↓α␈↓ ¬∃ev4 <= λ[[ ] restore␈↓λ'␈↓α[fun;cont]]
␈↓" ␈↓ α←␈↓α␈↓ αsev5 <= λ[[ ] link[]; restore[dest]; save␈↓λ'␈↓α[args;bodylist[]; cont; ␈↓λ`␈↓αev6; ␈↓λ`␈↓αevalargs]]
␈↓"∀␈↓ α←␈↓␈↓αev5␈↓␈α∩handles␈α∩the␈α∩evaluation␈α⊃of␈α∩the␈α∩body␈α∩of␈α⊃a␈α∩λ-expression.␈α∩Since␈α∩we␈α⊃are
␈↓ α←␈↓allowing␈α∩multiple-bodied␈α∩λ-expressions␈α∩(page 196),␈α∩we␈α∩pass␈α∩the␈α∩␈↓αbodylist␈↓␈α⊃to
␈↓ α←␈↓␈↓αevalargs␈↓.␈α∂If␈α∂we␈α∂were␈α∂restricting␈α∂ourselves␈α∂to␈α∂single-bodied␈α∂expressions,␈α∞then
␈↓ α←␈↓passing ␈↓αbody␈↓ to ␈↓αpeval␈↓ would suffice.
␈↓" ␈↓ α←␈↓α␈↓ ∧uev6 <= λ[[ ] restore␈↓λ'␈↓α[args;env;cont]]
␈↓ α←␈↓␈↓4.8␈↓ πkAn Evaluator for ␈↓αprog␈↓ 215␈↓α
␈↓" ␈↓ α←␈↓The␈αnext␈αfour␈αfunctions␈αhandle␈αthe␈αevaluation␈αof␈αa␈αsequence␈αof␈αexpressions.
␈↓ α←␈↓If␈α∂the␈α⊂sequence␈α∂is␈α⊂empty,␈α∂then␈α∂there␈α⊂is␈α∂nothing␈α⊂to␈α∂do.␈α∂If␈α⊂there␈α∂is␈α⊂a␈α∂single
␈↓ α←␈↓argument␈α∞then␈α∞evaluate␈α∞it␈α
and␈α∞restore␈α∞the␈α∞continuation.␈α∞Otherwise␈α
evaluate
␈↓ α←␈↓the␈α
first␈αone␈α
using␈α
␈↓αpeval␈↓␈α(sending␈α
its␈α
result␈αto␈α
␈↓αdest␈↓)␈α
and␈αthen␈α
execute␈α␈↓αev11␈↓.␈α
At
␈↓ α←␈↓␈↓αev11␈↓␈α
we␈α∞update␈α
the␈α
destination␈α∞block␈α
using␈α∞␈↓αnext␈↓␈α
and␈α
get␈α∞set␈α
to␈α∞evaluate␈α
the
␈↓ α←␈↓next argument.
␈↓" ␈↓ α←␈↓αevalargs <= λ[[]␈↓ ∧7[emptyargs[] → restore[cont];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 ␈↓
t␈↓α →␈↓ ∧ssave[exp;first[args]];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧scont ← ␈↓λ`␈↓αev9 ]]
␈↓" ␈↓ α←␈↓αev9 <= λ[[ ]␈↓ β{[singlearg[] →␈↓ ¬;save_cont[ ␈↓λ`␈↓αev10; ␈↓λ`␈↓αpeval];
␈↓"β␈↓ α←␈↓α␈↓ β{ ␈↓
t␈↓α →␈↓ ∧Csave_cont[ ␈↓λ`␈↓αev11; ␈↓λ`␈↓αpeval] ]]
␈↓" ␈↓ α←␈↓αev10 <= λ[[ ] restore␈↓λ'␈↓α[exp;cont]]
␈↓" ␈↓ α←␈↓αev11 <= λ[[ ]␈↓ β{ next[];
␈↓"β␈↓ α←␈↓α␈↓ β{ args ← rest[args];
␈↓"β␈↓ α←␈↓α␈↓ β{ exp ← first[args];
␈↓"β␈↓ α←␈↓α␈↓ β{ cont ← ␈↓λ`␈↓αev9 ] ]
␈↓"α␈↓ α←␈↓␈↓ ε≡␈↓↓Problem␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Using␈α→the␈α→new␈α→evaluator,␈α~sketch␈α→the␈α→evaluation␈α→of␈α~␈↓αf[A]␈↓␈α→where:
␈↓ α←␈↓␈↓αf <= λ[[x]eq[x;A]]␈↓.
␈↓"α␈↓ α←␈↓The␈α∪combination␈α∪of␈α∪␈↓αevcond␈↓␈α∪and␈α∪␈↓αcond1␈↓␈α∪handle␈α∀conditional␈α∪expressions.␈↓π 17␈↓
␈↓ α←␈↓␈↓αevcond␈↓␈α⊗sets␈α⊗up␈α∃the␈α⊗evaluation␈α⊗of␈α⊗the␈α∃predicate␈α⊗position␈α⊗such␈α⊗that␈α∃the
␈↓ α←␈↓computation␈αwill␈αcontinue␈αat␈α␈↓αcond1␈↓.␈αWhen␈αthat␈αevaluation␈αis␈αcompleted␈α␈↓αcond1␈↓
␈↓ α←␈↓␈↓αreceive␈↓s␈α↔the␈α↔result.␈α⊗If␈α↔␈↓
t␈↓␈α↔is␈α⊗received␈α↔then␈α↔the␈α⊗consequent␈α↔part␈α↔of␈α⊗that
␈↓ α←␈↓conditional␈αclause␈αis␈α
evaluated.␈αNote␈αthat␈α
we␈αuse␈α␈↓αevalargs␈↓␈α
here␈αsince␈αwe␈α
allow
␈↓ α←␈↓extended␈αconditionals␈α(page 194).␈α If␈α␈↓
f␈↓␈αis␈αreceived␈αwe␈αgo␈αback␈αto␈α␈↓αevcond␈↓␈αwith
␈↓ α←␈↓the remaining part of the conditional.
␈↓"∀␈↓ α←␈↓αevcond <= λ[[ ]␈↓ ∧∨[emptyargs[] →␈↓ ¬kerr[NO_TRUE_COND_CLAUSE];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧[␈↓ ¬kcont ← ␈↓λ`␈↓αev1;
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ ␈↓
t␈↓α →␈↓ ∧[save_cont[ ␈↓λ`␈↓αcond1; ␈↓λ`␈↓αpeval];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧[exp ← pred[first[args]] ]]
␈↓" ␈↓ α←␈↓αcond1 <= λ[[ ]␈↓ ∧∪[receive[] →␈↓ ¬#args ← conseq[first[args]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧O␈↓ ¬#save_cont[ ␈↓λ`␈↓αev1; ␈↓λ`␈↓αevalargs];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪ ␈↓
t␈↓α →␈↓ ∧Oargs ← rest[args];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧Ocont ← ␈↓λ`␈↓αevcond ]]
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 17␈↓See the problem on page 219.
␈↓ α←␈↓␈↓216 Imperative Constructs in LISP␈↓
(4.8␈↓
␈↓"∀␈↓ α←␈↓The␈αnext␈αfour␈αfunctions␈αdeal␈αwith␈αfunctional␈αarguments.␈α If␈αthe␈αargument␈αis
␈↓ α←␈↓a␈α⊃primitive,␈α⊃then␈α⊃we␈α⊂just␈α⊃␈↓αquote␈↓␈α⊃it;␈α⊃the␈α⊂assumption␈α⊃is␈α⊃that␈α⊃primitives␈α⊂only
␈↓ α←␈↓access␈α
local␈α
variables␈αand␈α
therefore␈α
don't␈α
need␈αto␈α
save␈α
the␈α
environment.␈αAn
␈↓ α←␈↓expression␈αwhich␈αis␈αalready␈α␈↓αfunarg␈↓-ed␈αis␈α
passed␈αas␈αis␈αsince␈αit␈αis␈αaready␈α
closed
␈↓ α←␈↓and␈α⊃therefore␈α∩has␈α⊃no␈α∩free␈α⊃variables.␈α∩ If␈α⊃it␈α∩is␈α⊃a␈α∩λ-expression,␈α⊃we␈α∩make␈α⊃a
␈↓ α←␈↓␈↓αfunarg␈↓; otherwise we evaluate the function until we discover its character.
␈↓"∀␈↓ α←␈↓αevfunction <= λ[[ ]␈↓ ∧[fun ← first[args];
␈↓"β␈↓ α←␈↓α␈↓ ∧[[isprim[] → send[mkquote[fun]];restore[cont];
␈↓"β␈↓ α←␈↓α␈↓ ∧[ islambda[] → send[mkfunarg[fun;env]];restore[cont];
␈↓"β␈↓ α←␈↓α␈↓ ∧[ isfunarg[] → send[fun];restore[cont];
␈↓"β␈↓ α←␈↓α␈↓ ∧[ ␈↓
t␈↓α →␈↓ ¬↔save_cont[ ␈↓λ`␈↓αfun1; ␈↓λ`␈↓αpeval];
␈↓"β␈↓ α←␈↓α␈↓ ∧[␈↓ ¬↔exp ← fun ]]
␈↓" ␈↓ α←␈↓αfun1 <= λ[[ ] send[mkfun[receive[ ]]; cont ← ␈↓λ`␈↓αev1]
␈↓"∀␈↓ α←␈↓The functions ␈↓αev7␈↓ and ␈↓αev8␈↓ control the application of a ␈↓αfunarg␈↓.
␈↓"∀␈↓ α←␈↓αev7 <= λ[[ ]␈↓ ∧πrestore[env];
␈↓"β␈↓ α←␈↓α␈↓ ∧πlink[];
␈↓"β␈↓ α←␈↓α␈↓ ∧πrestore␈↓λ'␈↓α[dest;args];
␈↓"β␈↓ α←␈↓α␈↓ ∧πsave_cont[ ␈↓λ`␈↓αev8; ␈↓λ`␈↓αevalargs] ]]
␈↓" ␈↓ α←␈↓αev8 <= λ[[ ] restore␈↓λ'␈↓α[env;cont]]
␈↓"∀␈↓ α←␈↓Special␈α∃functions␈α∃are␈α∃needed␈α∃to␈α∃handle␈α∃explicit␈α∃calls␈α∃on␈α∃the␈α∃evaluator:
␈↓ α←␈↓␈↓αeval␈↓[<form>;<env>].␈αWe␈αset␈αup␈αa␈αdestination␈αto␈αreceive␈αthe␈αvalues␈αof␈α<form>
␈↓ α←␈↓and␈α<env>,␈αand␈αask␈α␈↓αevalargs␈↓␈αto␈αevaluate␈αthese␈αarguments.␈α The␈αresults␈αof␈αthe
␈↓ α←␈↓computation are seen by ␈↓αev12␈↓; this function sets up the call on ␈↓αpeval␈↓.
␈↓"∀␈↓ α←␈↓αeveval <= λ[[ ]␈↓ ∧∨save␈↓λ'␈↓α[␈↓ ∧genv;env;
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧gdest;alloc_dest[createvars[(G1 G2)]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧gcont; ␈↓λ`␈↓αev12; ␈↓λ`␈↓αevalargs]]
␈↓" ␈↓ α←␈↓αev12 <= λ[[ ]␈↓ ∧∪exp ← first_dest[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪env ← second_dest[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪restore[dest];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪save_cont[ ␈↓λ`␈↓αev13; ␈↓λ`␈↓αpeval]]
␈↓" ␈↓ α←␈↓αev13 <= λ[[ ] restore␈↓λ'␈↓α[env;cont]] (␈↓λ≡␈↓α ev8)
␈↓"∀␈↓ α←␈↓There␈α∃is␈α∃a␈α∃second␈α∃form␈α∃of␈α∃call␈α∃on␈α∃␈↓αeval␈↓␈α∃which␈α∃is␈α∃useful.␈α∃If␈α⊗we␈α∃write
␈↓ α←␈↓␈↓αeval␈↓[<form>],␈α
then␈αthe␈α
<form>␈α
is␈αevaluated␈α
in␈αthe␈α
environment␈α
which␈αexists
␈↓ α←␈↓at the point of call. See problem on page 220.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊂remainder␈α∂of␈α⊂the␈α∂evaluator␈α⊂involves␈α∂the␈α⊂␈↓αprog␈↓␈α⊂related␈α∂constructs.
␈↓ α←␈↓Several␈α∞new␈α∂ideas␈α∞are␈α∂involved.␈α∞As␈α∞we␈α∂discussed␈α∞on␈α∂page 211,␈α∞we␈α∂must␈α∞be
␈↓ α←␈↓able␈α∀to␈α∀determine␈α∀whether␈α∀or␈α∀not␈α∀we␈α∀are␈α∀executing␈α∀within␈α∀a␈α∀␈↓αprog␈↓:␈α∀we
␈↓ α←␈↓introduced␈α
␈↓αtype␈↓␈αto␈α
handle␈α
this.␈α Also␈α
every␈α
expression␈αor␈α
statement␈α
in␈αLISP
␈↓ α←␈↓has␈αa␈αvalue.␈α
Since␈αwe␈αare␈α
always␈α␈↓αsend␈↓-ing␈αvalues,␈α
we␈αmust␈αhave␈αa␈α
destination
␈↓ α←␈↓to␈αreceive␈αthe␈α
values␈αcreated␈αby␈α␈↓αprog␈↓ statements:␈α
we␈αwill␈αintroduce␈α
a␈αdummy
␈↓ α←␈↓destination␈α∀which␈α∀will␈α∀always␈α∀receive␈α∀the␈α∀value␈α∀of␈α∀any␈α∃statement.␈α∀This
␈↓ α←␈↓␈↓4.8␈↓ πkAn Evaluator for ␈↓αprog␈↓ 217␈↓α
␈↓"β␈↓ α←␈↓destination␈αis␈αnamed␈α
␈↓αbb␈↓,␈αfor␈α"bit bucket".␈α
Finally,␈αwe␈αmust␈αhandle␈α
assignment
␈↓ α←␈↓statements.␈α∂The␈α∞innovation␈α∂here␈α∂is␈α∞that␈α∂the␈α∂␈↓αsend␈↓␈α∞goes␈α∂to␈α∂some␈α∞pre-existing
␈↓ α←␈↓destination␈αand␈αdestroys␈αthe␈αcurrent␈αvalue:␈αwe␈αuse␈αa␈αprimitive␈α
␈↓αmkdest␈↓␈αwhose
␈↓ α←␈↓effect␈α
is␈α
to␈α
generate␈α
a␈α
destination␈α
pointer␈α
to␈α
the␈α
slot␈α
which␈α
is␈α
to␈α
receive␈α
the
␈↓ α←␈↓value␈α
of␈α
the␈α
right-hand-side␈αof␈α
the␈α
assignment.␈α
In␈α␈↓αevsetq␈↓␈α
we␈α
use␈α
a␈αfunction
␈↓ α←␈↓␈↓αlookup␈↓λ'␈↓␈αwhich␈αis␈αsimilar␈αto␈α␈↓αlookup␈↓␈αexcept␈αthat␈αit␈αreturns␈αa␈αpointer␈αto␈αthe␈αslot
␈↓ α←␈↓containing a value, rather than returning the value in the slot.
␈↓"β␈↓ α←␈↓␈↓ β'Here are the evaluators for ␈↓αsetq␈↓ and ␈↓αset␈↓:
␈↓"∀␈↓ α←␈↓αevsetq <= λ[[ ]␈↓ ∧+save␈↓λ'␈↓α[␈↓ ∧sdest;mkdest[lookup␈↓λ'␈↓α[first[args]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧+␈↓ ∧scont; ␈↓λ`␈↓αsetq1; ␈↓λ`␈↓αpeval];
␈↓"β␈↓ α←␈↓α␈↓ ∧+exp ← second[args]]
␈↓"∀␈↓ α←␈↓αsetq1 <= λ[[ ]␈↓ ∧πλ[[x]␈↓ ∧[restore[dest];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ∧[send[x];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ∧[cont ← ␈↓λ`␈↓αev1 ][receive[ ]]
␈↓" ␈↓ α←␈↓αevset <= λ[[ ]␈↓ ∧∪save␈↓λ'␈↓α[args;args; cont; ␈↓λ`␈↓αset1; ␈↓λ`␈↓αpeval];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪exp ← first[args] ]
␈↓" ␈↓ α←␈↓αset1 <= λ[[ ]␈↓ ∧πrestore[args];
␈↓"β␈↓ α←␈↓α␈↓ ∧πargs ← mkass[receive[];rest[args]];
␈↓"β␈↓ α←␈↓α␈↓ ∧πcont ← ␈↓λ`␈↓αevsetq ]
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α∂␈↓αprog␈↓␈α∂evaluator,␈α∂␈↓αevprog␈↓,␈α∂must␈α∂handle␈α∂all␈α∂of␈α∂the␈α∂control␈α∂structures
␈↓ α←␈↓which␈αcan␈αoccur␈αwithin␈αa␈α␈↓αprog␈↓.␈αBesides␈αordinary␈αrecursion,␈αwe␈αcan␈α
have␈α␈↓αgo␈↓s
␈↓ α←␈↓and␈α∀␈↓αreturn␈↓s.␈α∪The␈α∀␈↓αgo␈↓␈α∪must␈α∀be␈α∪able␈α∀to␈α∪search␈α∀the␈α∪control␈α∀chain␈α∀for␈α∪the
␈↓ α←␈↓appropriate␈α
label,␈α
and␈α
the␈α∞␈↓αreturn␈↓␈α
must␈α
find␈α
the␈α
dynamically␈α∞enclosing␈α
␈↓αprog␈↓.
␈↓ α←␈↓To␈αhandle␈αeither␈αof␈αthese␈αeventualities,␈αwe␈α␈↓αsave␈↓␈αsome␈αadditional␈αinformation
␈↓ α←␈↓when␈α
we␈αenter␈α
a␈α␈↓αprog␈↓.␈α
First␈αwe␈α
save␈α
the␈αcurrent␈α
state␈αof␈α
the␈αcomputation;␈α
this
␈↓ α←␈↓will␈α∂allow␈α⊂the␈α∂␈↓αreturn␈↓␈α∂to␈α⊂␈↓αrestore␈↓␈α∂everything␈α⊂as␈α∂it␈α∂leaves␈α⊂the␈α∂␈↓αprog␈↓.␈α⊂ Next␈α∂we
␈↓ α←␈↓make␈αa␈α
new␈α␈↓αenv␈↓␈α
which␈αhas␈αbound␈α
all␈αthe␈α
␈↓αprog␈↓ variables␈αto␈α
␈↓α( )␈↓.␈α We␈αsave␈α
␈↓¬that␈↓
␈↓ α←␈↓␈↓αenv␈↓,␈α∩since␈α∩a␈α∩non-local␈α∩␈↓αgo␈↓␈α∩will␈α∪want␈α∩to␈α∩restore␈α∩that␈α∩␈↓αenv␈↓␈α∩as␈α∩it␈α∪returns␈α∩for
␈↓ α←␈↓execution.␈α
Finally␈α
we␈αcreate␈α
a␈α
␈↓αgolist␈↓␈α
which␈αis␈α
a␈α
list␈α
of␈αall␈α
points␈α
in␈α
the␈α␈↓αprog␈↓
␈↓ α←␈↓which␈αhave␈αlabels.␈α
This␈αconstruct␈αallows␈αus␈α
to␈αdiscover␈αquickly␈α
which␈αlabels
␈↓ α←␈↓are␈α
present␈αin␈α
the␈α
␈↓αprog␈↓␈αand␈α
where␈αthey␈α
are.␈↓π 18␈↓␈α
After␈αall␈α
this␈αis␈α
done␈α
we␈αare
␈↓ α←␈↓ready to execute the first line of the ␈↓αprog␈↓ body.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 18␈↓If␈α it␈α weren't␈α for␈α!the␈α existence␈α of␈α anonymous␈α!␈↓αprog␈↓s␈α and
␈↓ α←␈↓function-modifying␈α
functions,␈α
we␈α
could␈α
put␈α
the␈α
responsibility␈α
of␈α∞making␈α
the
␈↓ α←␈↓go-list on "<=".
␈↓ α←␈↓␈↓218 Imperative Constructs in LISP␈↓
(4.8␈↓
␈↓"∀␈↓ α←␈↓αevprog <= λ[[ ]␈↓ ∧+save␈↓λ'␈↓α[␈↓ ∧sexp;exp;
␈↓"β␈↓ α←␈↓α␈↓ ∧+␈↓ ∧senv;env;
␈↓"β␈↓ α←␈↓α␈↓ ∧+␈↓ ∧sdest;alloc_dest[prog_vars[args]];
␈↓"β␈↓ α←␈↓α␈↓ ∧+␈↓ ∧sfun;fun;
␈↓"β␈↓ α←␈↓α␈↓ ∧+␈↓ ∧sargs;prog_body[args];
␈↓"β␈↓ α←␈↓α␈↓ ∧+␈↓ ∧stype;PROG];
␈↓"β␈↓ α←␈↓α␈↓ ∧+link[];
␈↓"β␈↓ α←␈↓α␈↓ ∧+save␈↓λ'␈↓α[␈↓ ∧senv;env;
␈↓"β␈↓ α←␈↓α␈↓ ∧+␈↓ ∧sgolist;mkgolist[args]];
␈↓"β␈↓ α←␈↓α␈↓ ∧+cont ← ␈↓λ`␈↓αline ]
␈↓"∀␈↓ α←␈↓αmkgolist <= λ[[body] prog[[z]
␈↓"β␈↓ α←␈↓α␈↓ ¬a␈↓ ¬;[null[body] → return[z];
␈↓"β␈↓ α←␈↓α␈↓ ¬␈↓ ¬; islabel[first[body]] → z ← concat[body;z] ]; ␈↓π 19␈↓α
␈↓"β␈↓ α←␈↓α␈↓ ¬␈↓ ¬;body ← rest[body];
␈↓"β␈↓ α←␈↓α␈↓ ¬␈↓ ¬;go[a] ]]
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α∞actual␈α∞execution␈α
of␈α∞each␈α∞line␈α
of␈α∞a␈α∞␈↓αprog␈↓ body␈α
is␈α∞controlled␈α∞by␈α
the
␈↓ α←␈↓pair␈α⊃␈↓αline␈↓␈α⊃and␈α⊃␈↓αline1␈↓.␈α⊃Their␈α∩behavior␈α⊃is␈α⊃similar␈α⊃to␈α⊃that␈α⊃of␈α∩␈↓αevalargs␈↓. ␈α⊃␈↓αline␈↓
␈↓ α←␈↓examines␈αthe␈α
next␈αexpression;␈α
if␈αthere␈α
is␈αno␈α
next␈αstatement,␈α
we␈αexit␈α
with␈α␈↓α( )␈↓
␈↓ α←␈↓using␈α∞␈↓αprog_exit␈↓;␈α
if␈α∞the␈α∞next␈α
statement␈α∞is␈α
a␈α∞label,␈α∞it␈α
is␈α∞ignored;␈α∞otherwise␈α
we
␈↓ α←␈↓prepare to evaluate the expression, setting the destination to ␈↓αbb␈↓.
␈↓"∀␈↓ α←␈↓αline <= λ[[ ]␈↓ ∧π[null[args] → prog_exit[( )];
␈↓"β␈↓ α←␈↓α␈↓ ∧π islabel[first[args]] → args ← rest[args];
␈↓"β␈↓ α←␈↓α␈↓ ∧π ␈↓
t␈↓α →␈↓ ∧Cexp ← first[args];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ∧Cdest ← bb;
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ∧Csave_cont[ ␈↓λ`␈↓αline1; ␈↓λ`␈↓αpeval] ]]
␈↓"∞␈↓ α←␈↓αline1 <= λ[[ ]␈↓ ∧∪args ← rest[args];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪cont ← ␈↓λ`␈↓αline ]
␈↓"∀␈↓ α←␈↓Note␈α
that␈α
we␈α
don't␈α
change␈α␈↓αcont␈↓␈α
in␈α
␈↓αline␈↓␈α
when␈α
we␈α
see␈αa␈α
label;␈α
we␈α
just␈α
leave␈αit␈α
at
␈↓ α←␈↓␈↓αline␈↓ and ␈↓αloop␈↓ does the rest.
␈↓"∀␈↓ α←␈↓␈↓ β'We␈α∂call␈α∂␈↓αprog_exit␈↓␈α∂to␈α∂return␈α∂␈↓α( )␈↓␈α∞when␈α∂the␈α∂body␈α∂of␈α∂the␈α∂␈↓αprog␈↓␈α∂is␈α∞empty.
␈↓ α←␈↓Thus␈α∞the␈α∞discussion␈α∞of␈α∞␈↓αprog_exit␈↓␈α∂involves␈α∞the␈α∞semantics␈α∞of␈α∞␈↓αreturn␈↓.␈α∂ Of␈α∞the
␈↓ α←␈↓two␈α
control␈α
mechanisms,␈α
␈↓αreturn␈↓␈α
is␈α
simpler␈α
than␈α
␈↓αgo␈↓.␈α
Recalling␈α
the␈α
discussion
␈↓ α←␈↓of␈α
␈↓αsave␈↓␈α
on␈α
page 204,␈α
we␈α
need␈αto␈α
look␈α
through␈α
the␈α
␈↓αcontrol␈↓-list␈α
for␈α
the␈αlast␈α
block
␈↓ α←␈↓designating␈α
a␈α␈↓αprog␈↓␈α
entry.␈αWe␈α
␈↓αrestore␈↓␈αto␈α
that␈α
saved␈αstate␈α
and␈αset␈α
␈↓αcontrol␈↓␈αto␈α
that
␈↓ α←␈↓prior point.
␈↓"∀␈↓ α←␈↓αevreturn <= λ[[ ]␈↓ ∧7exp ← first[args];
␈↓"β␈↓ α←␈↓α␈↓ ∧7save_cont[ ␈↓λ`␈↓αret1; ␈↓λ`␈↓αpeval] ]
␈↓"∞␈↓ α←␈↓αret1 <= λ[[ ] prog_exit[receive[]]]
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 19␈↓Note that this program handles multiply-labeled statements.
␈↓ α←␈↓␈↓4.8␈↓ πkAn Evaluator for ␈↓αprog␈↓ 219␈↓α
␈↓"∞␈↓ α←␈↓αprog_exit <= λ[[val]␈↓ ∧gcontrol ← find_prog[control];
␈↓"β␈↓ α←␈↓α␈↓ ∧grestore␈↓λ'␈↓α[type;args;fun;dest;env;exp;cont];
␈↓"β␈↓ α←␈↓α␈↓ ∧gsend[val] ]
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α∞␈↓αgo␈↓␈α∞statement␈α∂is␈α∞a␈α∞bit␈α∞more␈α∂complicated.␈α∞ When␈α∞a␈α∞␈↓αgo␈↓␈α∂statement␈α∞is
␈↓ α←␈↓recognized,␈α∀we␈α∃look␈α∀back␈α∃through␈α∀the␈α∃dynamic␈α∀chain␈α∃to␈α∀find␈α∃the␈α∀first
␈↓ α←␈↓occurrence␈α∂of␈α⊂the␈α∂desired␈α⊂label.␈α∂If␈α⊂we␈α∂are␈α∂in␈α⊂a␈α∂␈↓αprog␈↓␈α⊂we␈α∂check␈α⊂the␈α∂current
␈↓ α←␈↓␈↓αgolist␈↓;␈α
if␈α
the␈α∞label␈α
is␈α
not␈α∞found,␈α
or␈α
if␈α∞we␈α
are␈α
not␈α∞immediately␈α
in␈α
a␈α∞␈↓αprog␈↓,␈α
we
␈↓ α←␈↓look␈α⊂for␈α⊂the␈α∂latest␈α⊂␈↓αgolist␈↓␈α⊂and␈α∂search␈α⊂it.␈α⊂We␈α∂continue␈α⊂this␈α⊂process␈α⊂until␈α∂we
␈↓ α←␈↓discover␈α∂the␈α∂label.␈α∂At␈α∂that␈α∂time␈α∂we␈α∂restore␈α∂the␈α∂environment␈α∂to␈α⊂that␈α∂which
␈↓ α←␈↓encloses the label, reset ␈↓αcontrol␈↓, and continue the computation at that point.
␈↓"∀␈↓ α←␈↓αevgo <= λ[[ ]␈↓ ∧πexp ← first[args];
␈↓"β␈↓ α←␈↓α␈↓ ∧π[isconst[] →err[BAD_PROG_LABEL];
␈↓"β␈↓ α←␈↓α␈↓ ∧π not[isvar[]] →␈↓ ¬Ssave_cont[ ␈↓λ`␈↓αgo1; ␈↓λ`␈↓αpeval]
␈↓"β␈↓ α←␈↓α␈↓ ∧π ␈↓
t␈↓α → control ← prog_go[control;exp] ]]
␈↓"∞␈↓ α←␈↓αprog_go <=
␈↓"β␈↓ α←␈↓αλ[[cntrl;exp]
␈↓"β␈↓ α←␈↓α␈↓ β'prog[[ ]␈↓ ∧∪a␈↓ ∧7[eq[type;PROG] →␈↓ ε'[check_go[exp;golist[cntrl]] →␈↓ 'restore[env];
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧∪␈↓ ∧7␈↓ ε'␈↓ 'cont ← ␈↓λ`␈↓αline;
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧∪␈↓ ∧7␈↓ ε'␈↓ 'return[cntrl];
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧∪␈↓ ∧7␈↓ ε' ␈↓
t␈↓α → cntrl ← find_go[rest[cntrl]]; go[a] ];
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧∪␈↓ ∧7 ␈↓
t␈↓α → cntrl ← find_go[cntrl]; go[a] ]]]
␈↓" ␈↓ α←␈↓αcheck_go <= λ[[lab;glist]␈↓ ¬↔[null[glist] → ␈↓
f␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ¬↔ eq[lab;first[first[glist]]] → args ← rest[first[glist]];␈↓
t␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ¬↔ ␈↓
t␈↓α → check_go[lab;rest[glist]] ]]
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α
origins␈α
of␈α
the␈α
interpreter␈α
presented␈α
here␈α
can␈α
be␈α
traced␈αfrom␈α
several
␈↓ α←␈↓sources: [Bla 71], [Con 73], [Sus 75], [Ste 76b].
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. This␈α∂problem␈α∂involves␈α∂the␈α∂␈↓αescape␈↓␈α∂expression␈α∂discussed␈α∂in␈α⊂[Rey 72]␈α∂and
␈↓ α←␈↓␈↓ β∂implemented␈α∞in␈α∞the␈α∞University␈α∞of␈α∞Paris's␈α∞LISP␈α∞[Gre 75].␈α∂ We␈α∞introduce
␈↓ α←␈↓␈↓ β∂the form:
␈↓"β␈↓ α←␈↓α␈↓ ∧T escape␈↓[<function>; <form␈↓β1␈↓>; ...;<form␈↓βn␈↓>]
␈↓"∞␈↓ α←␈↓␈↓ β∂with␈αthe␈αfollowing␈αsemantics:␈αwe␈αevaluate␈αthe␈α<form␈↓βi␈↓>'s␈αfrom␈αleft␈αto␈αright,
␈↓ α←␈↓␈↓ β∂returning␈α∃the␈α∃value␈α∃of␈α∀<form␈↓βn␈↓>␈α∃unless␈α∃we␈α∃encounter␈α∃an␈α∀application
␈↓ α←␈↓␈↓ β∂involving␈α<function>.␈α
If␈αsuch␈αan␈α
application␈α␈↓¬does␈↓␈α
appear␈αwe␈αperform␈α
that
␈↓ α←␈↓␈↓ β∂application␈α
and␈α
immediately␈α
return␈α
the␈α
resulting␈α
value␈α
as␈α
the␈α
value␈α
of␈α
the
␈↓ α←␈↓␈↓ β∂␈↓αescape␈↓ expression.
␈↓" ␈↓ α←␈↓␈↓ β∂Extend␈α~our␈α~latest␈α→evaluator␈α~to␈α~recognize␈α→and␈α~execute␈α~the␈α→␈↓αescape␈↓
␈↓ α←␈↓␈↓ β∂expression.
␈↓" ␈↓ α←␈↓2. The␈αsemantics␈αof␈α␈↓αgo␈↓␈αspecified␈αthat␈αthe␈αargument␈αwould␈αbe␈αevaluated␈αif␈αit
␈↓ α←␈↓␈↓ β∂were␈α∞a␈α
function␈α∞application,␈α∞however␈α
the␈α∞current␈α
␈↓αpeval␈↓␈α∞does␈α∞not␈α
handle
␈↓ α←␈↓␈↓ β∂this case. Correct that oversight.
␈↓ α←␈↓␈↓220 Imperative Constructs in LISP␈↓
(4.8␈↓
␈↓"β␈↓ α←␈↓3. Extend ␈↓αevcond␈↓ to handle conditional statements.
␈↓" ␈↓ α←␈↓4. On␈α⊗page 204␈α↔we␈α⊗discussed␈α↔the␈α⊗implementation␈α↔of␈α⊗␈↓αsave␈↓␈α↔and␈α⊗␈↓αrestore␈↓.
␈↓ α←␈↓␈↓ β∂Implement ␈↓αsave␈↓ and ␈↓αrestore␈↓ for ␈↓αpeval␈↓.
␈↓" ␈↓ α←␈↓5. Write ␈↓αfind_go␈↓ and ␈↓αfind_prog␈↓.
␈↓" ␈↓ α←␈↓6. Revise␈α∞␈↓αeveval␈↓␈α∞to␈α∂handle␈α∞calls␈α∞on␈α∞␈↓αeval␈↓␈α∂with␈α∞either␈α∞one␈α∞or␈α∂two␈α∞arguments.
␈↓ α←␈↓␈↓ β∂See page 216.
␈↓" ␈↓ α←␈↓7. Refer␈α∂to␈α∞the␈α∂problem␈α∞involving␈α∂multiple␈α∞␈↓αsetq␈↓'s␈α∂on␈α∞page 193.␈α∂ There␈α∞you
␈↓ α←␈↓␈↓ β∂were␈α
asked␈αto␈α
implement␈α
that␈αfeature␈α
using␈αmacros.␈α
Either␈α
implement␈αa
␈↓ α←␈↓␈↓ β∂macro␈αfacility␈αin␈α␈↓αpeval␈↓␈αor␈αexplicitly␈αintroduce␈αsuch␈αa␈αmultiple␈αassignment
␈↓ α←␈↓␈↓ β∂feature.␈α~You␈α≠may␈α~implement␈α≠that␈α~feature␈α≠as␈α~either␈α≠a␈α~sequential
␈↓ α←␈↓␈↓ β∂assignment or a parallel assignment.
␈↓" ␈↓ α←␈↓8. Recall␈α↔our␈α_discussion␈α↔of␈α_the␈α↔general␈α_␈↓αcatch-throw␈↓␈α↔pair␈α_on␈α↔page 199.
␈↓ α←␈↓␈↓ β∂Implement these functions in ␈↓αpeval␈↓.
␈↓"β␈↓ α←␈↓␈↓ ¬9␈↓↓4.9 Alternatives to ␈↓αeval␈↓↓␈↓α
␈↓"β␈↓ α←␈↓We␈αhave␈αseen␈αa␈αlot␈αof␈αevaluators␈αfor␈αLISP.␈αWe␈αshould␈αat␈αleast␈αlook␈αa␈αbit␈αat
␈↓ α←␈↓other␈α⊃possibilities␈α⊂for␈α⊃describing␈α⊂computational␈α⊃behavior.␈α⊂ Indeed,␈α⊃what␈α⊂is
␈↓ α←␈↓"computation"?␈α
When␈α
we␈αare␈α
given␈α
an␈α
expression␈αto␈α
evaluate␈α
we␈α
are␈αreally
␈↓ α←␈↓simulating␈α∀the␈α∀application␈α∀of␈α∀simplification␈α∀and␈α∀substitution␈α∀rules.␈α∀ The
␈↓ α←␈↓simplification␈α∞rules␈α∂tell␈α∞us␈α∂when␈α∞an␈α∂expression␈α∞can␈α∂be␈α∞replaced␈α∂by␈α∞another
␈↓ α←␈↓expression;␈α
typically␈αwe␈α
think␈α
of␈αthe␈α
replacing␈αexpression␈α
as␈α
being␈α"simpler"
␈↓ α←␈↓than␈α⊂the␈α⊃replaced␈α⊂expression.␈α⊃Thus␈α⊂␈↓αcar[(A . B)]␈↓␈α⊃can␈α⊂be␈α⊃replaced␈α⊂by␈α⊃␈↓αA␈↓,␈α⊂or
␈↓ α←␈↓␈↓α[␈↓
t␈↓ → 2; ...] can be replaced by ␈↓α2␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
substitution␈α
rules␈αtypically␈α
allow␈α
us␈α
to␈αreplace␈α
a␈α
procedure␈αcall␈α
with
␈↓ α←␈↓an␈α~appropriately␈α≠instantiated␈α~copy␈α≠of␈α~the␈α≠procedure␈α~body.␈α≠ Thus␈α~a
␈↓ α←␈↓computation␈α∞involving␈α∞␈↓αappend[(A B);(2 3)]␈↓␈α∂is␈α∞identical␈α∞to␈α∞that␈α∂obtained␈α∞by
␈↓ α←␈↓replacing the occurrence of ␈↓αappend[(A B);(2 3)]␈↓
␈↓" ␈↓ α←␈↓α␈↓with␈↓α [null[(A B)] → (2 3); ␈↓
t␈↓α → concat[␈↓ ππfirst[(A B)];
␈↓"β␈↓ α←␈↓α␈↓ ππappend[␈↓ πsrest[(A B)];
␈↓"β␈↓ α←␈↓α␈↓ ππ␈↓ πs(2 3)]]]
␈↓"∀␈↓ α←␈↓The␈α↔result␈α↔of␈α_such␈α↔a␈α↔substitution␈α↔is␈α_usually␈α↔a␈α↔candidate␈α_for␈α↔further
␈↓ α←␈↓substitutions␈α→and␈α→simplifications.␈α→ The␈α→collection␈α→of␈α→simplification␈α_and
␈↓ α←␈↓substitution␈α⊂rules␈α∂is␈α⊂called␈α∂the␈α⊂reduction␈α∂rules␈α⊂for␈α∂the␈α⊂language.␈α⊂Given␈α∂an
␈↓ α←␈↓expression,␈α⊂a␈α⊂computation␈α⊂is␈α⊂said␈α⊃to␈α⊂terminate␈α⊂when␈α⊂there␈α⊂are␈α⊃no␈α⊂further
␈↓ α←␈↓reduction␈α∂rules␈α∂applicable␈α∂and␈α∂the␈α∂reduced␈α∂expression␈α∂is␈α∂a␈α∂constant␈α∂of␈α∞the
␈↓ α←␈↓language. That reduced expression is the value of the original expression.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∀difficulties␈α∀with␈α∀these␈α∀schemes␈α∀come␈α∀from␈α∀both␈α∀practical␈α∀and
␈↓ α←␈↓theoretical␈α∞considerations.␈α∞The␈α∞direct␈α∞application␈α∞of␈α∞reduction␈α∞rules␈α∞is␈α
quite
␈↓ α←␈↓inefficient:␈α∂making␈α∞textual␈α∂substitutions␈α∂is␈α∞expensive.␈α∂Instead␈α∂we␈α∞developed
␈↓ α←␈↓the␈α∂ideas␈α∂of␈α∂symbol␈α∞tables␈α∂to␈α∂contain␈α∂the␈α∞bindings␈α∂of␈α∂the␈α∂variables,␈α∞rather
␈↓ α←␈↓than perform the actual substitutions.
␈↓ α←␈↓␈↓4.9␈↓ λ
Alternatives to ␈↓αeval␈↓ 221␈↓α
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
theoretical␈α
difficulty␈α∞appears␈α
since,␈α
at␈α∞any␈α
time␈α
in␈α∞a␈α
computation,
␈↓ α←␈↓there␈α∞may␈α∞be␈α∞more␈α∞than␈α∂one␈α∞reduction␈α∞rule␈α∞which␈α∞is␈α∞applicable.␈α∂A␈α∞further
␈↓ α←␈↓difficulty␈α⊂is␈α∂that␈α⊂one␈α⊂sequence␈α∂of␈α⊂reductions␈α∂may␈α⊂terminate,␈α⊂while␈α∂another
␈↓ α←␈↓sequence␈α
of␈αreductions␈α
is␈αnon-terminating.␈α
We␈α
have␈αseen␈α
this␈αphenomenon␈α
in
␈↓ α←␈↓previous␈α∂discussions␈α∂of␈α∂call-by-value␈α∂versus␈α∂call-by-name,␈α∂inner-most␈α∞versus
␈↓ α←␈↓outer-most, and normal order reductions versus applicative order reductions.
␈↓"β␈↓ α←␈↓␈↓ β'Though␈αLISP␈αopted␈αfor␈αthe␈αcall-by-value␈αinterpretation␈α
of␈αexpressions,
␈↓ α←␈↓it␈α
is␈α
possible␈α
to␈α
develop␈α
a␈α
call-by-name␈α
evaluator.␈α
Call-by-name␈α
implies␈αthat
␈↓ α←␈↓we␈α∞substitute␈α∞the␈α∞unevaluated␈α∞actual␈α∞parameters␈α∞for␈α∞the␈α∂formal␈α∞parameters.
␈↓ α←␈↓As␈α
in␈α
␈↓αeval␈↓,␈α
we␈α
need␈α
not␈α
make␈α
explicit␈α
substitutions;␈α
appropriate␈α
use␈α
of␈α
symbol
␈↓ α←␈↓tables␈α∂will␈α∞simulate␈α∂the␈α∞action,␈α∂but␈α∞now,␈α∂when␈α∞we␈α∂build␈α∞a␈α∂symbol␈α∂table␈α∞on
␈↓ α←␈↓entry␈α⊂to␈α⊂a␈α⊂λ-expression␈α⊂we␈α⊂bind␈α⊂the␈α⊂actual␈α⊂expressions␈α⊂to␈α⊂the␈α⊂λ-variables.
␈↓ α←␈↓When␈αwe␈α
encounter␈αa␈αvariable␈α
in␈αthe␈α
body␈αof␈αthe␈α
expression␈αwe␈αevaluate␈α
the
␈↓ α←␈↓actual␈α∀parameter.␈α∀The␈α∀difficulty␈α∃is␈α∀that␈α∀an␈α∀actual␈α∀parameter␈α∃itself␈α∀may
␈↓ α←␈↓contain␈αvariables,␈α
and␈αthose␈α
variables␈αneed␈α
to␈αbe␈α
interpreted␈αin␈α
the␈αbinding
␈↓ α←␈↓environment.␈α∞This␈α∞means␈α∞that␈α∞we␈α
must␈α∞bind␈α∞␈↓αfunarg␈↓-like␈α∞expressions␈α∞to␈α
the
␈↓ α←␈↓formal parameters.
␈↓"β␈↓ α←␈↓␈↓ β'Most␈α∪of␈α∪␈↓αeval␈↓βname␈↓␈α∪is␈α∪like␈α∀␈↓αeval␈↓␈α∪of␈α∪Section 3.5,␈α∪so␈α∪we␈α∪only␈α∀sketch␈α∪the
␈↓ α←␈↓interesting␈α∩parts.␈α∩Assume␈α∪the␈α∩␈↓αfunarg␈↓-expression␈α∩we␈α∩manufacture␈α∪has␈α∩two
␈↓ α←␈↓components, the ␈↓αexpr␈↓-component, and the ␈↓αenv␈↓-component.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α⊗can␈α∃implement␈α⊗␈↓αeval␈↓βname␈↓␈α∃by␈α⊗simply␈α∃changing␈α⊗the␈α⊗symbol␈α∃table
␈↓ α←␈↓orgainzation,␈αsupplying␈αnew␈α
versions␈αof␈α␈↓αlookup␈↓␈α
and␈α␈↓αmkenv␈↓.␈αSee␈αpage 124␈α
and
␈↓ α←␈↓page 152.
␈↓"∀␈↓ α←␈↓α␈↓ β∂alloc <= λ[[vars] ()]
␈↓" ␈↓ α←␈↓α␈↓ β∂send <= λ[[var;val;dest] concat[mkent[var;val];dest]
␈↓" ␈↓ α←␈↓α␈↓ β∂link <= λ[[dest;env] concat[dest;env]]
␈↓" ␈↓ α←␈↓αlookup <= λ[[var;env] l␈↓λ'␈↓α[var;first[env];rest[env]]
␈↓" ␈↓ α←␈↓αl␈↓λ'␈↓α <= λ[[n;bl;env]␈↓ ∧C[null[bl] → l␈↓λ'␈↓α[n;first[env];rest[env]];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧C eq[n;name[first[bl]] → eval[␈↓ π∨expr[value[first[bl]]];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧C␈↓ π∨envir[value[first[bl]]]];
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ∧C ␈↓
t␈↓α → l␈↓λ'␈↓α[n;rest[bl];env] ]]
␈↓"∀␈↓ α←␈↓One␈αadvantage␈αof␈αsuch␈αan␈αevaluator␈αis␈αthat␈αit␈αwill␈αnot␈αevaluate␈αa␈αparameter
␈↓ α←␈↓until␈α∂it␈α∂actually␈α∂needs␈α∂it,␈α∞whereas␈α∂␈↓αeval␈↓␈α∂evaluates␈α∂all␈α∂parameters␈α∂at␈α∞function
␈↓ α←␈↓entry␈α∂time.␈α∞If␈α∂an␈α∞actual␈α∂parameter␈α∞is␈α∂not␈α∞used␈α∂in␈α∞the␈α∂computation␈α∂and␈α∞the
␈↓ α←␈↓computation␈αof␈αthat␈αparameter␈αfails␈αto␈αterminate,␈αthen␈α␈↓αeval␈↓βname␈↓␈αwill␈αterminate
␈↓ α←␈↓while␈α␈↓αeval␈↓␈αwill␈αnot.␈α There␈αare␈αdisadvantages␈αto␈α␈↓αeval␈↓βname␈↓.␈αEvery␈αoccurrence␈αof
␈↓ α←␈↓a␈α
variable␈αwithin␈α
the␈αbody␈α
of␈αthe␈α
function␈αwill␈α
involve␈αa␈α
re-evaluation␈αof␈α
the
␈↓ α←␈↓corresponding␈α≤actual␈α≤parameter.␈α≤If␈α≠there␈α≤are␈α≤no␈α≤side-effects␈α≤in␈α≠the
␈↓ α←␈↓computation␈α∂then␈α∂these␈α∂repeated␈α∞computations␈α∂are␈α∂an␈α∂unnecessary␈α∞expense.
␈↓ α←␈↓Several␈α⊗people␈α⊗([Wad 71],␈α⊗[Vui 74],␈α⊗[Pac 73],␈α⊗[Hen 76],␈α↔[Fri 76a])␈α⊗have
␈↓ α←␈↓suggested␈α
modifications␈αto␈α
␈↓αeval␈↓βname␈↓␈αto␈α
reduce␈α
the␈αinefficiency.␈α
The␈αbasic␈α
idea,
␈↓ α←␈↓described␈αas␈α␈↓↓call-by-need␈↓,␈αis␈αto␈αproceed␈αin␈αthe␈α␈↓αeval␈↓βname␈↓␈αstyle␈αuntil␈αthe␈αfirst␈α
use
␈↓ α←␈↓␈↓222 Imperative Constructs in LISP␈↓
(4.9␈↓
␈↓"β␈↓ α←␈↓of␈αa␈αvariable.␈αAt␈αthat␈αtime␈αwe␈αevaluate␈αthe␈αactual␈αparameter,␈αand␈αmodify␈αthe
␈↓ α←␈↓symbol␈α→table,␈α→␈↓¬replacing␈↓␈α→the␈α→actual␈α→parameter␈α→with␈α→its␈α→value.␈α→Further
␈↓ α←␈↓references␈α∞to␈α∞that␈α∞variable␈α∞simply␈α∞get␈α∞the␈α∞value.␈α∞Obviously␈α∞the␈α∞scheme␈α
will
␈↓ α←␈↓not␈α∂work␈α∂correctly␈α∞if␈α∂side-effects␈α∂are␈α∞present.␈α∂ We␈α∂leave␈α∞it␈α∂to␈α∂the␈α∂reader␈α∞to
␈↓ α←␈↓supply the details of ␈↓αeval␈↓βneed␈↓; see page 224.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αnow␈αexplore␈αa␈αdifferent␈α
kind␈αof␈αmodification␈αto␈αLISP.␈αThis␈α
one␈αis
␈↓ α←␈↓grounded␈α∀more␈α∪in␈α∀practical␈α∀experience␈α∪with␈α∀the␈α∀programming␈α∪language,
␈↓ α←␈↓though␈α∀the␈α∀results␈α∀do␈α∀have␈α∃theoretical␈α∀interest.␈α∀It␈α∀has␈α∀been␈α∃noted␈α∀that
␈↓ α←␈↓programmers␈αfrequently␈αwish␈αto␈αreturn␈αmore␈αthan␈αone␈αvalue␈αas␈αthe␈αresult␈αof
␈↓ α←␈↓a␈αfunction␈αapplication.␈αThe␈αstandard␈αalternatives␈αin␈αLISP␈αare␈αeither␈αto␈αmake
␈↓ α←␈↓global␈α
assignments␈α
from␈α
within␈α
the␈α
body␈αof␈α
the␈α
function,␈α
or␈α
to␈α
return␈α
a␈αlist␈α
of
␈↓ α←␈↓the␈α
desired␈α
values␈α
making␈α
it␈α
the␈α
responsibility␈α
of␈α
the␈α
calling␈α
program␈α
to␈α
select
␈↓ α←␈↓the␈α_proper␈α_components.␈α_ Neither␈α_alternative␈α_is␈α→particularly␈α_compelling.
␈↓ α←␈↓Programming␈α∞with␈α∂extensive␈α∞side-effects␈α∞tends␈α∂to␈α∞lead␈α∞to␈α∂obscure␈α∞programs
␈↓ α←␈↓and␈α∪may␈α∪incur␈α∪unnecessary␈α∪complications␈α∪in␈α∪debugging;␈α∪see␈α∩Section 6.23.
␈↓ α←␈↓Passing␈α
lists␈α
back␈α
as␈α
values␈α
requires␈α
much␈α
additional␈α
computation:␈αsomeone
␈↓ α←␈↓must␈αbuild␈α
the␈αlist;␈αsomeone␈α
must␈αtear␈αit␈α
apart.␈αIt␈αis␈α
also␈αdisturbing␈α
that␈αthe
␈↓ α←␈↓operation␈α→being␈α_modelled,␈α→--multiple-values--,␈α_is␈α→not␈α_recognizable␈α→as␈α_a
␈↓ α←␈↓construct.␈α∃ This␈α∀is␈α∃a␈α∃similar␈α∀complaint␈α∃to␈α∃that␈α∀we␈α∃raised␈α∃in␈α∀discussing
␈↓ α←␈↓labels-and-␈↓αgo␈↓s versus an iterative construct.
␈↓"β␈↓ α←␈↓␈↓ β'Our␈α
goal␈α
is␈α∞realizable␈α
by␈α
a␈α
slight␈α∞extension␈α
of␈α
the␈α∞re-interpretation␈α
of
␈↓ α←␈↓conditional␈α?␈αexpressions␈α?␈αand␈α?␈αmultiple-bodied␈α?␈αλ-expressions
␈↓ α←␈↓(page 194, page 196).
␈↓"β␈↓ α←␈↓We will interpret the form:
␈↓" ␈↓ α←␈↓␈↓ εα␈↓↓p␈↓βi␈↓α → ␈↓↓e␈↓βi1␈↓α; ... ␈↓↓e␈↓βin␈↓α
␈↓" ␈↓ α←␈↓to␈α
return␈α
the␈α
␈↓↓e␈↓βij␈↓-values␈α
to␈α
the␈α∞calling␈α
function␈α
in␈α
a␈α
left-to-right␈α
order.␈α∞If␈α
the
␈↓ α←␈↓calling␈α
program␈α
is␈α
single-valued␈α
then␈α
the␈α
value␈α
it␈α
sees␈α
is␈α
the␈α
value␈α
of␈α
␈↓↓e␈↓βin␈↓.␈α
This
␈↓ α←␈↓is compatible with our current interpretation.
␈↓"β␈↓ α←␈↓The evaluation of:
␈↓" ␈↓ α←␈↓␈↓ ¬5␈↓αλ[[ ... ] f␈↓β1␈↓α[ ... ]; ...; f␈↓βn␈↓α[ ... ]]␈↓
␈↓" ␈↓ α←␈↓will be interpreted similarly.
␈↓"β␈↓ α←␈↓␈↓ β'For␈α∃example␈α∃[Fri 76b]␈α∃discusses␈α∃a␈α∃multiple-valued␈α∃function␈α∃named
␈↓ α←␈↓␈↓αsigmasum␈↓.␈αThis␈αfunction␈αis␈αto␈αtake␈αa␈αlist␈αof␈αnumbers␈αand␈αreturn␈α
three␈αitems:
␈↓ α←␈↓the␈αlength␈αof␈αthe␈αlist,␈αthe␈αsum␈αof␈αthe␈αnumbers␈αin␈αthe␈αlist,␈αand␈αthe␈αsum␈αof␈αthe
␈↓ α←␈↓squares␈α∀of␈α∀the␈α∀numbers␈α∀in␈α∀the␈α∀list.␈α∀ In␈α∀our␈α∀notation␈α∀␈↓αsigmasum␈↓␈α∀can␈α∪be
␈↓ α←␈↓expressed as:
␈↓"∀␈↓ α←␈↓αsigmasum <= λ[[x]␈↓ ∧O[null[x] → 0;0;0;
␈↓"β␈↓ α←␈↓α␈↓ ∧O ␈↓
t␈↓α → λ[␈↓ ¬/[z␈↓β1␈↓α;z␈↓β2␈↓α;z␈↓β3␈↓α]␈↓ ε'add1[z␈↓β1␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬/␈↓ ε'plus[first[x];z␈↓β2␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬/␈↓ ε'plus[times[first[x];first[x]];z␈↓β3␈↓α] ]
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬/[sigmasum[rest[x]]] ]]
␈↓"∀␈↓ α←␈↓Notice␈αthat␈αwe␈αuse␈αan␈αanonymous␈αλ-expression␈αto␈αspread␈αthe␈αmultiple␈αvalues
␈↓ α←␈↓at the level of the caller.
␈↓ α←␈↓␈↓4.9␈↓ λ
Alternatives to ␈↓αeval␈↓ 223␈↓α
␈↓"β␈↓ α←␈↓␈↓ β'Another␈α∩example␈α⊃is␈α∩a␈α∩solution␈α⊃to␈α∩the␈α∩␈↓αsamefringe␈↓␈α⊃problem [Hew 74]:
␈↓ α←␈↓determine␈α∩whether␈α⊃or␈α∩not␈α⊃the␈α∩terminal␈α∩nodes␈α⊃of␈α∩two␈α⊃trees␈α∩are␈α∩the␈α⊃same,
␈↓ α←␈↓respecting order, but irrespective of tree structure.␈↓π 20␈↓ Thus:
␈↓"∀␈↓ α←␈↓α␈↓ ∧jsamefringe[(A (B (C))); (A B C)] = ␈↓
t
␈↓"β␈↓ α←␈↓
␈↓but␈↓ ∧u␈↓αsamefringe[(A (B C)); (A C B)] = ␈↓
f␈↓
␈↓"∀␈↓ α←␈↓αsamefringe <= λ[[x;y]␈↓ ∧␈[null[x] → null[y];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈ ␈↓
t␈↓α →␈↓ ¬;λ[[z␈↓β1␈↓α;z␈↓β2␈↓α;z␈↓β3␈↓α;z␈↓β4␈↓α]␈↓ εo[eq[z␈↓β1␈↓α;z␈↓β3␈↓α] → samefringe[z␈↓β2␈↓α;z␈↓β4␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬;␈↓ εo ␈↓
t␈↓α → ␈↓
f␈↓α]]
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬; [fringe[x];fringe[y]] ]
␈↓" ␈↓ α←␈↓αfringe <= λ[[x]␈↓ ∧∨[atom[first[x]] → first[x]; rest[x];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ ␈↓
t␈↓α →␈↓ ∧sλ[[y;z] y;␈↓ ¬k[null[z] → rest[x];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧s␈↓ ¬k ␈↓
t␈↓α → cons[z; rest[x]] ] ]
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧s [fringe[first[x]]] ] ]
␈↓"∀␈↓ α←␈↓In␈α→this␈α_solution,␈α→␈↓αsamefringe␈↓␈α_is␈α→single-valued␈α_but␈α→uses␈α_values␈α→from␈α_a
␈↓ α←␈↓multiple-valued␈α∞function.␈α∞The␈α∞two␈α∞values␈α
from␈α∞␈↓αfringe[x]␈↓␈α∞are␈α∞spread␈α∞into␈α
␈↓αz␈↓β1␈↓
␈↓ α←␈↓and ␈↓αz␈↓β2␈↓ and the values from ␈↓αfringe[y]␈↓ are spread into ␈↓αz␈↓β3␈↓ and ␈↓αz␈↓β4␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'It␈α∂is␈α∂easy␈α⊂to␈α∂write␈α∂an␈α⊂evaluator␈α∂for␈α∂such␈α⊂multiple-valued␈α∂expressions.
␈↓ α←␈↓Here is a sketch of the basic parts:
␈↓"∀␈↓ α←␈↓αmeval <= λ[[x;e]␈↓ ∧7[isconst[x] → list[denote[x]];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 isvar[x] →list[lookup[x;e]];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 iscond[x] → mevcond[condbody[x];e];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 ␈↓
t␈↓α → mapply[fun[x];mevlist[arglist[x];e];e] ]]
␈↓"∀␈↓ α←␈↓αmapply <= λ[[fn;args;e]␈↓ ¬↔[isprim[fn] →list[apply[fn;args;e]]
␈↓"β␈↓ α←␈↓α␈↓ ¬↔ islambda[fn] → mevlist[␈↓ πObodylist[fn];
␈↓"β␈↓ α←␈↓α␈↓ ¬↔␈↓ ε≠␈↓ πOmkenv[vars[fn];args;e] ]
␈↓"β␈↓ α←␈↓α␈↓ ¬↔ ␈↓
t␈↓α → mapply[eval[fn;e];args;e] ]]
␈↓"∀␈↓ α←␈↓αmevlist <= λ[[l;e]␈↓ ∧C[null[l] → ();
␈↓"β␈↓ α←␈↓α␈↓ ∧C ␈↓
t␈↓α → append[meval[first[l];e]; mevlist[rest[l];e]] ]]
␈↓"∀␈↓ α←␈↓αmevcond <= λ[[l;e]␈↓ ∧O[null[l] → ();
␈↓"β␈↓ α←␈↓α␈↓ ∧O first[meval[pred[first[l]];e]] → mevlist[␈↓ λGconseq[first[l]];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ λGenv];
␈↓"β␈↓ α←␈↓α␈↓ ∧O ␈↓
t␈↓α → mevcond[rest[l];e] ]]
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 20␈↓There␈αare␈αmany␈α"solutions"␈αto␈αthis␈αproblem;␈αthe␈αsimplest␈αis␈αto␈αflatten
␈↓ α←␈↓the␈α∞trees␈α∞first,␈α∞then␈α∞use␈α∞␈↓αequal␈↓.␈α∞Indeed,␈α∞there␈α∞are␈α∞many␈α∞"problems";␈α∂the␈α∞most
␈↓ α←␈↓accurate␈α⊃formulation␈α⊃requires␈α⊃that␈α∩no␈α⊃␈↓αcons␈↓␈α⊃operations␈α⊃be␈α⊃done.␈α∩For␈α⊃more
␈↓ α←␈↓details␈α∩and␈α∩interesting␈α∩discussions␈α⊃see␈α∩[Gre 76],␈α∩[Hen 76],␈α∩[And 76a],␈α⊃and
␈↓ α←␈↓[Fin 76].
␈↓ α←␈↓␈↓224 Imperative Constructs in LISP␈↓
(4.9␈↓
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Complete the specification of ␈↓αeval␈↓βname␈↓.
␈↓" ␈↓ α←␈↓2. Complete␈α∂the␈α∂specification␈α⊂of␈α∂␈↓αeval␈↓βneed␈↓.␈α∂To␈α∂do␈α⊂this,␈α∂you␈α∂may␈α⊂assume␈α∂the
␈↓ α←␈↓␈↓ β∂existence␈αof␈αa␈αbinary␈αfunction␈αnamed␈α␈↓αstuff␈↓␈αwhose␈αfirst␈αargument␈α␈↓αx␈↓␈αis␈αthe
␈↓ α←␈↓␈↓ β∂a␈α∂name␈α∂in␈α∂the␈α∂symbol␈α∂table,␈α∂and␈α∂whose␈α∂second␈α∂argument␈α∂␈↓αy␈↓␈α∂is␈α∂a␈α∂value.
␈↓ α←␈↓␈↓ β∂␈↓αstuff␈↓ is to replace the current binding of ␈↓αx␈↓ with ␈↓αy␈↓.
␈↓" ␈↓ α←␈↓3. Modify ␈↓αpeval␈↓ to handle multiple-valued functions.
␈↓" ␈↓ α←␈↓4. Recall␈αproblem␈α2␈αon␈αpage 154␈α
dealing␈αwith␈αthe␈αanalysis␈αof␈α␈↓αlookup␈↓.␈α
Include
␈↓ α←␈↓␈↓ β∂call-by-name and call-by-need in your analysis.
␈↓" ␈↓ α←␈↓5. Using␈α∞the␈α∞results␈α∞of␈α
the␈α∞previous␈α∞problem,␈α∞make␈α
up␈α∞a␈α∞table␈α∞whose␈α
rows
␈↓ α←␈↓␈↓ β∂are␈α≡labeled␈α≡with␈α≡the␈α≡binding␈α≡implementations:␈α≡"deep"␈α≥"shallow",
␈↓ α←␈↓␈↓ β∂"Weizenbaum",␈α"need",␈αand␈α"name";␈α
and␈αwhose␈αcolumns␈αare␈α
labeled␈αwith
␈↓ α←␈↓␈↓ β∂the␈αprmitives:␈α␈↓αalloc␈↓,␈α␈↓αlink␈↓,␈α␈↓αsend␈↓,␈α
and␈αthe␈αprimitives␈αfor␈α␈↓αlookup␈↓.␈α
Supply␈αthe
␈↓ α←␈↓␈↓ β∂entries.
␈↓"β␈↓ α←␈↓␈↓ ¬%␈↓↓4.10 Function Definitions␈↓
␈↓"β␈↓ α←␈↓Now␈α∪that␈α∪we␈α∪have␈α∪developed␈α∪these␈α∪more␈α∪explicit␈α∪evaluators,␈α∪we␈α∪should
␈↓ α←␈↓exploit␈α
some␈αof␈α
this␈α
additional␈αdetail.␈α
In␈αparticular,␈α
more␈α
of␈αthe␈α
detail␈αof␈α
"<="
␈↓ α←␈↓should␈αbe␈αunderstandable.␈αThe␈αeffect␈αof␈α␈↓αf <= λ[[x] ␈↓λx␈↓α]␈↓␈αis␈αto␈αput␈αthe␈αdefinition
␈↓ α←␈↓of␈α␈↓αf␈↓␈αin␈αthe␈αglobal␈αenvironment,␈αwhereas␈α␈↓αlabel␈↓␈αcreates␈αa␈αnew␈αdest-block␈αwith␈α␈↓αf␈↓
␈↓ α←␈↓bound␈α∞to␈α∞a␈α
␈↓αfunarg␈↓␈α∞consisting␈α∞of␈α∞␈↓λx␈↓␈α
and␈α∞that␈α∞constructed␈α∞environment.␈α
Once
␈↓ α←␈↓we␈α∞leave␈α∂the␈α∞environment␈α∂containing␈α∞the␈α∞␈↓αlabel␈↓␈α∂definition,␈α∞that␈α∂definition␈α∞is
␈↓ α←␈↓effectively␈α∂destroyed.␈α∂ The␈α∂effect␈α∂of␈α⊂"<="␈α∂is␈α∂to␈α∂be␈α∂global.␈α⊂A␈α∂"<="-definition
␈↓ α←␈↓could␈αbe␈α
temporarily␈αsuperseded␈α
by␈αa␈α
␈↓αlabel␈↓-definition␈αto␈α
the␈αsame␈α
name␈αand
␈↓ α←␈↓therefore␈α→our␈α→search␈α→for␈α→a␈α→binding␈α→for␈α→␈↓αf␈↓␈α→may␈α→not␈α→short-circuit␈α→the
␈↓ α←␈↓environment chain.
␈↓"β␈↓ α←␈↓␈↓ β'Our␈α
search␈αstrategy␈α
is␈α
encoded␈αin␈α
the␈α
␈↓αlookup␈↓␈αfunction;␈α
using␈αthe␈α
current
␈↓ α←␈↓environment,␈α⊃we␈α⊂find␈α⊃the␈α⊃latest␈α⊂active␈α⊃binding␈α⊃for␈α⊂a␈α⊃variable.␈α⊃ The␈α⊂␈↓αprog␈↓
␈↓ α←␈↓evaluator,␈α␈↓αpeval␈↓β1␈↓␈α
of␈αpage 214␈α
is␈αa␈α
bit␈αmore␈α
complicated.␈αBesides␈α
finding␈αthe
␈↓ α←␈↓definition␈α
we␈α
must␈α
also␈α
determine␈α
whether␈α
the␈α
arguments␈α
are␈α
to␈α
be␈α
evaluated.
␈↓ α←␈↓The␈αdevice␈αof␈α␈↓αisspec␈↓␈α(page 212)␈αis␈αsufficient␈αfor␈αthe␈αevaluators,␈αbut␈αhas␈αsome
␈↓ α←␈↓difficulties␈αif␈αwe␈αwish␈αto␈αallow␈αuser-defined␈αspecial␈αforms.␈αWe␈αwill␈αdevelop␈αa
␈↓ α←␈↓syntax␈α∀for␈α∀expressing␈α∀special␈α∀forms␈α∪at␈α∀the␈α∀user␈α∀level,␈α∀and␈α∀then␈α∪discuss
␈↓ α←␈↓problems of implementation.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∞will␈α∞define␈α∞"<␈↓βf␈↓="␈α∂to␈α∞mean␈α∞"..is␈α∞defined␈α∂to␈α∞be␈α∞a␈α∞special␈α∂form...".␈α∞ A
␈↓ α←␈↓special-form␈α
definition␈αis␈α
also␈αcalled␈α
a␈α␈↓↓fexpr␈↓;␈α
and␈αa␈α
call-by-value␈αdefinition␈α
is
␈↓ α←␈↓called an ␈↓↓expr␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'A␈αfexpr␈αis␈αdefined␈αwith␈αeither␈αone␈αor␈αtwo␈αformal␈αparameters.␈αThe␈αfirst
␈↓ α←␈↓parameter␈α∞is␈α∞always␈α∞bound␈α∞to␈α∞the␈α∞list␈α∞of␈α∞␈↓¬unevaluated␈↓␈α∞actual␈α∂parameters.␈α∞ If
␈↓ α←␈↓the␈α∞definition␈α∞has␈α∞a␈α∞second␈α∞formal␈α∞parameter,␈α∞then␈α∞the␈α∞environment␈α∞at␈α
the
␈↓ α←␈↓␈↓4.10␈↓ πyFunction Definitions 225␈↓
␈↓"β␈↓ α←␈↓point␈α
of␈α
call␈α∞is␈α
assigned␈α
to␈α
the␈α∞second␈α
parameter.␈α
This␈α
distinction␈α∞needs␈α
to
␈↓ α←␈↓be␈α
made␈α
if␈α
we␈α
expect␈α
to␈α
perform␈α
some␈α
evaluation␈α
of␈α
the␈α
formal␈α
parameters
␈↓ α←␈↓within␈α∞the␈α∞fexpr.␈α∂ Using␈α∞the␈α∞implementation␈α∂of␈α∞␈↓αeval␈↓␈α∞discussed␈α∂on␈α∞page 216
␈↓ α←␈↓we␈αcan␈αwrite␈αeither␈α␈↓αeval[␈↓<form>;<env>]␈αor␈α␈↓αeval␈↓[<form>].␈αIn␈αthe␈αlatter␈αcase␈αthe
␈↓ α←␈↓environment␈α∂that␈α∂is␈α∂used␈α∂is␈α⊂the␈α∂environment␈α∂which␈α∂was␈α∂current␈α⊂when␈α∂the
␈↓ α←␈↓␈↓αeval␈↓ is performed. Sometimes this is not the desired environment. Consider:
␈↓"∀␈↓ α←␈↓α␈↓ ββf1 <␈↓βf␈↓α= λ[[x] prog[[y]
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧Cy ← 2;
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧Creturn[eval[first[x]]]]]
␈↓" ␈↓ α←␈↓If␈α⊂we␈α⊂execute␈α⊂␈↓αf1[0]␈↓,␈α⊂␈↓αx␈↓␈α⊂will␈α⊂be␈α∂bound␈α⊂to␈α⊂the␈α⊂list␈α⊂␈↓α(0)␈↓␈α⊂and␈α⊂␈↓αeval[first[x]]␈↓␈α∂will
␈↓ α←␈↓return ␈↓α0␈↓ as expected. But if we execute:
␈↓" ␈↓ α←␈↓α␈↓ ββy ← 0;
␈↓"β␈↓ α←␈↓α␈↓ ββf1[y];
␈↓"∀␈↓ α←␈↓then␈α␈↓αx␈↓␈α
gets␈αbound␈αto␈α
␈↓α(Y)␈↓,␈αand␈α
␈↓αeval[Y]␈↓␈αwill␈αfind␈α
the␈αvalue␈α
associated␈αwith␈α␈↓αY␈↓␈α
to
␈↓ α←␈↓be ␈↓α2␈↓, and the value of ␈↓αf1[y]␈↓ is ␈↓α2␈↓, rather than the expected ␈↓α0␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α_problem␈α_is␈α_that␈α_the␈α_call␈α_on␈α_␈↓αeval␈↓␈α_takes␈α_place␈α_in␈α_the␈α↔␈↓αprog␈↓
␈↓ α←␈↓environment.␈α⊗ We␈α⊗can␈α∃correct␈α⊗this␈α⊗by␈α∃making␈α⊗the␈α⊗definition␈α⊗with␈α∃␈↓¬two␈↓
␈↓ α←␈↓arguments,␈αbinding␈αthe␈αsecond␈αto␈αthe␈αenvironment␈αat␈αthe␈αpoint␈αof␈αcall␈αto␈αthe
␈↓ α←␈↓fexpr:
␈↓"∀␈↓ α←␈↓α␈↓ ββf2 <␈↓βf␈↓α= λ[[x;a] prog[[y]
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧∨y ← 2;
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧∨return[eval[first[x];a]]]]
␈↓" ␈↓ α←␈↓α␈↓ ββy ← 0;
␈↓"β␈↓ α←␈↓α␈↓ ββf2[y];
␈↓"∀␈↓ α←␈↓The call on ␈↓αf2␈↓ will use the environment with ␈↓αy␈↓ being ␈↓α0␈↓ rather than ␈↓α2␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'As a final example:
␈↓"∀␈↓ α←␈↓α␈↓ ββf3 <␈↓βf␈↓α= λ[[x;a] prog[[z]
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧∨y ← 2;
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧∨return[eval[first[x];a]]]]
␈↓" ␈↓ α←␈↓α␈↓ ββy ← 0;
␈↓"β␈↓ α←␈↓α␈↓ ββf3[y];
␈↓"∀␈↓ α←␈↓would return ␈↓α2␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'As␈αwe␈αhave␈αjust␈αseen,␈αspecial␈αforms␈αmust␈αbe␈αused␈αwith␈αcare.␈α However,
␈↓ α←␈↓they␈α≠are␈α~useful␈α≠in␈α~several␈α≠contexts.␈α~Recall␈α≠that␈α~we␈α≠restricted␈α~LISP
␈↓ α←␈↓call-by-value␈αfunctions␈αto␈αhave␈αa␈αfixed␈αnumber␈αof␈αarguments.␈αFor␈αexample␈αif
␈↓ α←␈↓we␈α∀wish␈α∪to␈α∀add␈α∀four␈α∪numbers␈α∀or␈α∪append␈α∀three␈α∀lists␈α∪we␈α∀have␈α∀to␈α∪write
␈↓ α←␈↓something like:
␈↓"∀␈↓ α←␈↓α␈↓ βLplus[x␈↓β1␈↓α;[plus[x␈↓β2␈↓α;plus[x␈↓β3␈↓α;x␈↓β4␈↓α]␈↓ or ␈↓αappend[append[l␈↓β1␈↓α;l␈↓β2␈↓α];l␈↓β3␈↓α]
␈↓"∀␈↓ α←␈↓Since ␈↓αplus␈↓ and ␈↓αappend␈↓ are associative operations we would rather write:
␈↓"∀␈↓ α←␈↓α␈↓ ∧Hplus[x␈↓β1␈↓α;x␈↓β2␈↓α;x␈↓β3␈↓α;x␈↓β4␈↓α]␈↓ or ␈↓αappend[l␈↓β1␈↓α;l␈↓β2␈↓α;l␈↓β3␈↓α]
␈↓ α←␈↓␈↓226 Imperative Constructs in LISP␈↓
_4.10␈↓
␈↓"β␈↓ α←␈↓We␈α∞discussed␈α∂the␈α∞macro␈α∞implementation␈α∂of␈α∞these␈α∞constructs␈α∂in␈α∞Section 3.12.
␈↓ α←␈↓Using a special form, we could write ␈↓αplus␈↓ as:
␈↓"∀␈↓ α←␈↓αplus <␈↓βf␈↓α= λ[␈↓ β{[l;e] prog[[sum]
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧∪sum ← 0;
␈↓"β␈↓ α←␈↓α␈↓ β{a␈↓ ∧∪[null[l] → return[sum]];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧∪sum ← *plus[sum;eval[first[l];e]];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧∪l ← rest[l];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧∪go[a]]]
␈↓"∀␈↓ α←␈↓Notice␈α
that␈α
we␈αcould␈α
have␈α
used␈α␈↓αeval␈↓␈α
with␈α
one␈αargument␈α
unless␈α
the␈αvariables␈α
␈↓αl␈↓
␈↓ α←␈↓or ␈↓αsum␈↓ appeared as constitutents of the actual parameters.
␈↓"β␈↓ α←␈↓␈↓ β'Recalling␈α⊃Section 3.12,␈α⊂we␈α⊃can␈α⊃use␈α⊂<␈↓βf␈↓=␈α⊃to␈α⊂extend␈α⊃the␈α⊃evaluator.␈α⊂ For
␈↓ α←␈↓example, ␈↓αand␈↓ could be defined as:
␈↓"∀␈↓ α←␈↓␈↓αand <␈↓βf␈↓α= λ[[l;e] evand[l;e]] ␈↓where ␈↓αevand␈↓ is defined on page 155.
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α
implementation␈αof␈α
␈↓αg <␈↓βf␈↓= ␈↓αλ[[x] ␈↓λx␈↓]␈α
requires␈αthat␈α
we␈α
represent␈αthe␈α
fact
␈↓ α←␈↓that␈α␈↓αg␈↓␈αis␈αa␈αfexpr␈αrather␈αthan␈αan␈αexpr.␈αThe␈αimplication␈αof␈α␈↓αisspec␈↓␈αof␈αpage 212
␈↓ α←␈↓is␈α
that␈α
we␈α
have␈α
two␈α
tables:␈α
one␈α
for␈α
exprs,␈α
one␈α
for␈α
fexprs.␈α
This␈αcomplicates␈α
the
␈↓ α←␈↓search␈αstrategy␈αunnecessarily.␈α
Indeed␈αthere␈αshould␈α
only␈αbe␈αone␈α
definition␈αor
␈↓ α←␈↓value␈α∞associated␈α∂with␈α∞a␈α∂name␈α∞at␈α∂any␈α∞one␈α∞time,␈α∂so␈α∞a␈α∂single␈α∞table␈α∂should␈α∞be
␈↓ α←␈↓both␈α⊃necessary␈α∩and␈α⊃sufficient.␈α⊃We␈α∩␈↓¬do␈↓␈α⊃need␈α⊃some␈α∩way␈α⊃of␈α∩determining␈α⊃the
␈↓ α←␈↓calling␈α
style␈α
to␈α
be␈α
used␈α
when␈α
applying␈α
the␈α
definition.␈α
One␈α
way␈α
is␈α
to␈α
revise
␈↓ α←␈↓the␈α␈↓αisspec␈↓␈αtechnique␈α
slightly:␈αwe␈αuse␈α
␈↓αlookup␈↓␈αfor␈α␈↓¬all␈↓␈α
searches,␈αbut␈αalso␈α
have␈αa
␈↓ α←␈↓table␈α⊂relating␈α∂function-name␈α⊂with␈α∂its␈α⊂calling␈α∂style.␈α⊂One␈α∂difficulty␈α⊂with␈α∂this
␈↓ α←␈↓scheme␈α
is␈αthat␈α
we␈α
could␈αnot␈α
handle␈αanonymous␈α
fexpr␈α
definitions.␈αTherefore
␈↓ α←␈↓some␈α⊗versions␈α⊗of␈α⊗LISP␈α⊗replace␈α⊗the␈α⊗character␈α⊗"λ"␈α⊗with␈α⊗another␈α∃special
␈↓ α←␈↓character when making fexpr definitions. For example:
␈↓"β␈↓ α←␈↓α␈↓ ¬g <␈↓βf␈↓α= λ[[x;y] ␈↓λx␈↓α] ␈↓λ≡␈↓α g <= β[[x;y] ␈↓λx␈↓α]
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∂would␈α⊂translate␈α∂such␈α∂β-expressions␈α⊂into␈α∂S-expr␈α∂form,␈α⊂and␈α∂extend
␈↓ α←␈↓the evaluator to recognize such constructs.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α
could␈αuse␈α
a␈α
similar␈αtechnique␈α
to␈αrecognize␈α
macro␈α
definitions.␈α The
␈↓ α←␈↓next chapter will discuss some alternative implementations.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Define ␈↓αlist␈↓ as a special form.
␈↓" ␈↓ α←␈↓2. Write a version of ␈↓αpeval␈↓ to handle β-expressions.
␈↓" ␈↓ α←␈↓3. Define␈α∂two␈α∂special␈α∂forms,␈α∂␈↓αde␈↓␈α∂and␈α∂␈↓αdf␈↓,␈α∂which␈α∂will␈α∂implement␈α∂<=␈α⊂and␈α∂<␈↓βf␈↓=
␈↓ α←␈↓␈↓ β∂respectively. The format of these special forms is identical. For example:
␈↓"β␈↓ α←␈↓␈↓ ∧Q␈↓αde␈↓[<name>;<formal parameters>;<body>]
␈↓"β␈↓ α←␈↓␈↓ β∂will implement
␈↓"β␈↓ α←␈↓␈↓ ∧><name> <= λ[<formal parameters> <body>]
␈↓ α←␈↓␈↓4.11␈↓ πRapprochement: In Retrospect 227␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧f␈↓↓4.11 Rapprochement: In Retrospect␈↓
␈↓"β␈↓ α←␈↓As␈αwith␈αthe␈αreview␈αsection␈αof␈αthe␈αprevious␈αchapter,␈αthis␈αsection␈αis␈α
a␈αmixture
␈↓ α←␈↓of␈α∞the␈α∞practical␈α∞and␈α∞the␈α∞theoretical.␈α∞ That␈α∞is␈α∞a␈α∞healthy␈α∞attitude␈α∂to␈α∞cultivate
␈↓ α←␈↓when␈αdiscussing␈α
programming␈αlanguages.␈α
For␈αexample,␈α
in␈αthe␈α
first␈αsection␈α
of
␈↓ α←␈↓this␈α⊂chapter␈α⊂we␈α⊂claimed␈α⊂that␈α⊂a␈α⊂theoretically␈α⊂expected␈α⊃relationship␈α⊂between
␈↓ α←␈↓call-by-name␈α∂and␈α∂call-by-value␈α∂breaks␈α⊂down␈α∂in␈α∂the␈α∂presence␈α⊂of␈α∂side-effects.
␈↓ α←␈↓The␈αidea␈αof␈αside-effects␈αis␈αa␈αdecidedly␈αpractical␈αone,␈αbased␈αon␈αthe␈α"practical"
␈↓ α←␈↓notion␈α∂of␈α∂a␈α∂"variable"␈α∂as␈α∂a␈α∞"box␈α∂which␈α∂contains␈α∂a␈α∂value",␈α∂rather␈α∂than␈α∞the
␈↓ α←␈↓mathematical␈α
notion␈α
of␈αa␈α
"variable"␈α
as␈α
a␈αdescription␈α
for␈α
an␈α
anonymous,␈αbut
␈↓ α←␈↓fixed, element of a domain.
␈↓"β␈↓ α←␈↓␈↓ β'Consider the following expression:
␈↓"∀␈↓ α←␈↓α␈↓ ∧Cprog [[ ]␈↓ ¬;y ← 0;
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬↔λ[[x] prog[[ ]
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬↔␈↓ ¬;loop␈↓ ¬w[eq[y;0] → go[loop]]
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬↔ [y ← 1]]]]
␈↓"∀␈↓ α←␈↓If␈α∩this␈α∪expression␈α∩is␈α∩evaluated␈α∪using␈α∩call-by-value,␈α∩we␈α∪bind␈α∩␈↓αy␈↓␈α∩to␈α∪␈↓α0␈↓,␈α∩and
␈↓ α←␈↓evaluate␈αthe␈αanonymous␈αλ-expression.␈αThat␈αentails␈αevaluation␈αof␈α␈↓αy ← 1␈↓,␈α␈↓¬before␈↓
␈↓ α←␈↓entering␈α∞the␈α∂␈↓αprog␈↓.␈α∞The␈α∂computation␈α∞of␈α∂the␈α∞␈↓αprog␈↓ body␈α∂terminates,␈α∞returning
␈↓ α←␈↓␈↓αNIL␈↓.␈α∨ Call-by-name␈α∨evaluation␈α∨would␈α∨not␈α∨evaluate␈α∨␈↓αy ← 1␈↓␈α∨and␈α∨the
␈↓ α←␈↓computation would not terminate.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊂issue␈α⊂of␈α⊃side-effects␈α⊂highlights␈α⊂an␈α⊂important␈α⊃distinction␈α⊂between
␈↓ α←␈↓"variables"␈α∂in␈α⊂the␈α∂mathematical␈α∂sense␈α⊂and␈α∂"variables"␈α∂in␈α⊂the␈α∂programming
␈↓ α←␈↓sense.␈α∞ In␈α∞mathematics,␈α∞we␈α∞use␈α∞the␈α∞term,␈α∞variable,␈α∞to␈α∞designate␈α∞a␈α∂fixed,␈α∞but
␈↓ α←␈↓anonymous,␈α
object:␈α
"let␈α␈↓αx␈↓␈α
be␈α
a␈α
real␈αnumber ...".␈α
In␈α
programming␈αlanguages,␈α
an
␈↓ α←␈↓object␈α⊂is␈α∂"variable"␈α⊂as␈α⊂opposed␈α∂to␈α⊂a␈α⊂"constant",␈α∂meaning␈α⊂that␈α⊂the␈α∂quantity
␈↓ α←␈↓associated␈αwith␈αthe␈αobject␈αmay␈αvary.␈αThus␈αthe␈αidea␈αof␈α"box␈αwhich␈αcontains␈αa
␈↓ α←␈↓value"␈α⊃arises␈α⊃again.␈α⊃The␈α⊃manipulation␈α⊃of␈α⊃variables␈α⊃in␈α⊃languages␈α⊃leads␈α⊃to
␈↓ α←␈↓further distinctions.
␈↓"β␈↓ α←␈↓␈↓ β'Applicative␈α↔languages,␈α↔modelled␈α↔after␈α↔the␈α↔␈↓λλ␈↓-calculus,␈α↔simulate␈α↔the
␈↓ α←␈↓reduction␈α∀rules␈α∪by␈α∀associating␈α∪an␈α∀environment␈α∪or␈α∀symbol␈α∪table␈α∀with␈α∪an
␈↓ α←␈↓expression.␈α⊃What␈α⊃is␈α⊃being␈α⊃simulated␈α⊂is␈α⊃a␈α⊃"copy rule"␈α⊃which␈α⊃says␈α⊃that␈α⊂the
␈↓ α←␈↓reduction␈αis␈αto␈αbe␈αdone␈αby␈αmaking␈αa␈αcopy␈αof␈αthe␈αexpression,␈αsubstituting␈αthe
␈↓ α←␈↓actual␈αparameters␈αinto␈αthe␈αcopy␈αwherever␈αone␈αof␈αthe␈α␈↓λλ␈↓-variables␈αappeared␈αin
␈↓ α←␈↓the␈αoriginal␈αexpression.␈αThe␈αeffect␈αof␈αthe␈αsymbol␈αtable␈αis␈αto␈α␈↓¬share␈↓␈αa␈αcommon
␈↓ α←␈↓instance␈αof␈α
the␈αactual␈αparameter.␈α
The␈αidea␈αof␈α
sharing␈αbecomes␈α
central␈αwhen
␈↓ α←␈↓we␈α
discuss␈α
assignments␈α
and␈α
the␈α∞behavior␈α
of␈α
side-effects.␈α
If␈α
␈↓¬copies␈↓␈α∞of␈α
values
␈↓ α←␈↓are␈α∩made,␈α∪the␈α∩issue␈α∪of␈α∩side-effects␈α∪vanishes.␈α∩ Important␈α∪distinctions␈α∩arise
␈↓ α←␈↓when␈α∂we␈α∞are␈α∂able␈α∞both␈α∂to␈α∂␈↓¬share␈↓␈α∞values␈α∂and␈α∞to␈α∂destructively␈α∂␈↓¬change␈↓␈α∞values.
␈↓ α←␈↓This␈α→combination␈α→of␈α_properties␈α→is␈α→present␈α_in␈α→the␈α→general␈α_assignment
␈↓ α←␈↓statement.␈α
Because␈αof␈α
the␈αclose␈α
relationship␈αbetween␈α
assignment␈α
and␈αLISP's
␈↓ α←␈↓λ-binding,␈α∨λ-binding␈α∨is␈α∨sometimes␈α∨called␈α∨a␈α∨"pushdown"␈α∨assignment
␈↓ α←␈↓([New 61]),␈α∪as␈α∩compared␈α∪with␈α∪a␈α∩"destructive"␈α∪assignment.␈α∪ The␈α∩adjective,
␈↓ α←␈↓␈↓228 Imperative Constructs in LISP␈↓
_4.11␈↓
␈↓"β␈↓ α←␈↓"pushdown",␈α
refers␈α∞to␈α
the␈α
λ-binding's␈α∞ability␈α
to␈α
save␈α∞the␈α
prior␈α
binding␈α∞of␈α
a
␈↓ α←␈↓λ-variable␈α∂in␈α∂such␈α∂a␈α∂way␈α∂that␈α∂it␈α∂may␈α∂be␈α∂restored␈α∂after␈α∂the␈α∂computation␈α∂is
␈↓ α←␈↓completed.
␈↓"β␈↓ α←␈↓␈↓ β'Differences␈α⊗in␈α↔binding␈α⊗are␈α⊗highlighted␈α↔by␈α⊗the␈α↔implementation␈α⊗of
␈↓ α←␈↓␈↓αfunction␈↓.␈α
In␈α
the␈αapplicative␈α
subset,␈α
␈↓αfunction␈↓␈αis␈α
satisfactorily␈α
implemented␈αby
␈↓ α←␈↓the␈α
␈↓αFUNARG␈↓␈α
triple,␈αwhere␈α
the␈α
substitution␈α
is␈αrepresented␈α
by␈α
a␈α
pointer␈αto␈α
the
␈↓ α←␈↓binding␈α~environment.␈α≠In␈α~the␈α~presence␈α≠of␈α~assignment␈α≠statements,␈α~this
␈↓ α←␈↓equivalence breaks down.
␈↓"β␈↓ α←␈↓␈↓ β'Consider the following example due to H. Samet:
␈↓"∀␈↓ α←␈↓␈↓ βK␈↓αf <= λ[[x] prog␈↓ ¬#[[]
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ¬# a ← a+1;
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ¬# return[[eq[a;1] → x; ␈↓
t␈↓α → -x]]]]
␈↓" ␈↓ α←␈↓␈↓ βK␈↓αg <= λ[[x;fun] prog␈↓ ¬G[[]
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ¬G a ← 0;
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ¬G return[fun[x]]]]
␈↓"∀␈↓ α←␈↓α␈↓Now evaluate ␈↓αh[1]␈↓ where ␈↓αh <= λ[[a] g[3;function[f]]]␈↓
␈↓"∀␈↓ α←␈↓If␈α∃we␈α∀implemented␈α∃␈↓αfunction␈↓␈α∀as␈α∃a␈α∀direct␈α∃substitution␈α∀of␈α∃values␈α∃for␈α∀free
␈↓ α←␈↓variables,␈α⊂then␈α⊂␈↓αh[1]␈↓␈α⊂would␈α⊂yield␈α∂␈↓α-3␈↓.␈α⊂ The␈α⊂implementation␈α⊂of␈α⊂␈↓αfunction␈↓␈α⊂as␈α∂a
␈↓ α←␈↓pointer␈α
to␈α
the␈α∞binding␈α
environment␈α
yields␈α
␈↓α3␈↓.␈α∞ It␈α
is␈α
clear␈α
where␈α∞the␈α
problem
␈↓ α←␈↓lies;␈α∂we␈α∂have␈α∂assigned␈α∂to␈α⊂a␈α∂non-local␈α∂variable␈α∂after␈α∂the␈α⊂substitution␈α∂would
␈↓ α←␈↓have␈α⊃been␈α⊃carried␈α⊃out.␈α⊃ However,␈α⊂compare␈α⊃the␈α⊃current␈α⊃situation␈α⊃with␈α⊂the
␈↓ α←␈↓example␈αon␈α
page 145.␈α The␈αlesson␈α
to␈αbe␈αlearned␈α
is␈αthat␈αassignment␈α
statements
␈↓ α←␈↓do not fit well with the substitution model.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α
enriched␈αthe␈α
LISP␈αsubset␈α
to␈αallow␈α
such␈αconstructs␈α
as␈α
iteration␈αand
␈↓ α←␈↓assignment;␈α∞therefore,␈α∞we␈α
wished␈α∞to␈α∞provide␈α
an␈α∞evaluator␈α∞which␈α
adequately
␈↓ α←␈↓reflected␈αthe␈α
implementation,␈αor␈α
pragmatics,␈αof␈α
these␈αfacilities.␈α
We␈αcould␈α
have
␈↓ α←␈↓modelled␈α∩them␈α⊃directly␈α∩in␈α∩the␈α⊃initial␈α∩LISP␈α⊃subset,␈α∩but␈α∩the␈α⊃representation
␈↓ α←␈↓would␈αnot␈αconvey␈αthe␈αintended␈αimplementation.␈αAs␈αa␈αresult,␈αwe␈αdeveloped␈αan
␈↓ α←␈↓interpreter␈α≠which␈α≠directly␈α~modelled␈α≠the␈α≠assignment␈α≠statements,␈α~␈↓αreturn␈↓
␈↓ α←␈↓statements,␈α_and␈α↔non-local␈α_␈↓αgo␈↓␈α↔statements.␈α_These␈α↔are␈α_some␈α↔of␈α_the␈α↔most
␈↓ α←␈↓troublesome areas to reflect in an applicative model.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αresult␈α
of␈αour␈α
investigations␈αwas␈α
␈↓αpeval␈↓.␈αThis␈α
evaluator␈αexpresses␈α
the
␈↓ α←␈↓implementation␈α
of␈α
the␈α∞enriched␈α
subset␈α
of␈α
LISP;␈α∞it␈α
is␈α
self-explanatory␈α∞in␈α
the
␈↓ α←␈↓sense␈αthat␈αany␈αconstruct␈αwhich␈α␈↓αpeval␈↓␈αuses␈αhas␈αa␈αdata␈αstructure␈αrepresentation
␈↓ α←␈↓which␈α∩is␈α⊃recognizable␈α∩by␈α∩␈↓αpeval␈↓.␈α⊃ That␈α∩is,␈α⊃␈↓αpeval␈↓␈α∩can␈α∩interpret␈α⊃expressions
␈↓ α←␈↓involving representations of ␈↓αpeval␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αprocess␈αof␈αdeveloping␈α␈↓αpeval␈↓␈αexposed␈αthe␈αcontrol␈αstructure␈αof␈αLISP
␈↓ α←␈↓just␈αas␈αthe␈αdevelopment␈αof␈α␈↓αeval␈↓␈αin␈αChapter 3␈αexposed␈αthe␈αaccess␈αstructure.␈αA
␈↓ α←␈↓data␈α↔structure␈α_model␈α↔for␈α↔access␈α_environments␈α↔was␈α↔necessary␈α_since␈α↔the
␈↓ α←␈↓implementation␈α⊂of␈α∂␈↓αfunction␈↓␈α⊂demanded␈α∂it.␈α⊂Traditional␈α∂LISP␈α⊂does␈α⊂not␈α∂allow
␈↓ α←␈↓such␈αgenerality␈αin␈αthe␈αarea␈αof␈αcontrol␈αstructure.␈α The␈αappearance␈αof␈α
non-local
␈↓ α←␈↓␈↓αgo␈↓s␈α⊂and␈α⊂␈↓αreturn␈↓s␈α⊂requires␈α⊃control␈α⊂behavior␈α⊂analogous␈α⊂to␈α⊂that␈α⊃of␈α⊂functional
␈↓ α←␈↓␈↓4.11␈↓ πRapprochement: In Retrospect 229␈↓
␈↓"β␈↓ α←␈↓arguments,␈αbut␈αcontrol␈αregimes␈αanalogous␈αto␈αthat␈αof␈αfunctional␈α␈↓¬values␈↓␈αdo␈αnot
␈↓ α←␈↓appear␈α∩in␈α∩LISP.␈α∩More␈α∩recent␈α∩demands␈α∩from␈α∩LISP␈α∩users␈α∩have␈α⊃prompted
␈↓ α←␈↓development␈α≠of␈α≠generalized␈α≠control␈α≠structures␈α≠in␈α≠LISP-like␈α~languages
␈↓ α←␈↓([Hew 72],␈α∃[Pre 72],␈α∀[Pre 76a],␈α∃[Bob 73a],␈α∃[Con 73]).␈α∀Now␈α∃that␈α∃we␈α∀have
␈↓ α←␈↓developed␈α∪the␈α∩data␈α∪structure␈α∩representation␈α∪for␈α∩control,␈α∪we␈α∪could␈α∩extend
␈↓ α←␈↓LISP␈α
to␈α
allow␈α
manipulation␈α
of␈α
control␈α
structures.␈α
We␈α
resist␈α∞that␈α
temptation,
␈↓ α←␈↓leaving such experimentation to the reader.
␈↓"β␈↓ α←␈↓␈↓ β'As␈α≠we␈α≠have␈α≠just␈α≠seen␈α≠there␈α≠are␈α≠alternatives␈α≠to␈α≠some␈α≠of␈α~the
␈↓ α←␈↓LISP-techniques␈αand␈αthere␈α
are␈αsome␈αthings␈α
which,␈αin␈αretrospect,␈α
LISP␈αcould
␈↓ α←␈↓have␈α_done␈α_better.␈α_ There␈α_are␈α_some␈α_conceptual␈α_difficulties␈α_with␈α_LISP
␈↓ α←␈↓evaluation.␈α∪ We␈α∪have␈α∪seen␈α∪some␈α∪computational␈α∪schemes␈α∪which␈α∪will␈α∪give
␈↓ α←␈↓values␈αwhen␈αLISP's␈αcall-by-value␈α
does␈αnot␈αterminate.␈α Whether␈αthese␈α
schemes
␈↓ α←␈↓are␈α∞better␈α∞is␈α∞a␈α∞debatable␈α∞point.␈α∞ Programmers␈α∞tend␈α∞to␈α∂think␈α∞"call-by-value,"
␈↓ α←␈↓but␈αit␈αis␈αnot␈αclear␈αwhether␈α
that␈αis␈αhabit,␈αtraining,␈αor␈αa␈αfundamental␈α
point␈αof
␈↓ α←␈↓view towards computation.␈↓π 21␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊃practical␈α⊃and␈α⊃the␈α⊃theoretical␈α⊃aspects␈α⊃of␈α⊃programming␈α⊃languages
␈↓ α←␈↓have␈α∞much␈α
common␈α∞ground.␈α
As␈α∞we␈α∞have␈α
seen,␈α∞the␈α
notion␈α∞of␈α∞"function"␈α
in
␈↓ α←␈↓mathematics␈αis␈αdifferent␈α
from␈αthe␈αnotion␈α
of␈α"LISP␈αfunction."␈α
The␈αformer␈αis␈α
a
␈↓ α←␈↓set-theoretic␈αnotion,␈αthe␈αlatter␈αis␈αan␈αalgorithmic␈αnotion.␈αWith␈αthe␈αintroduction
␈↓ α←␈↓of␈α⊂iteration,␈α⊂a␈α⊂further␈α⊂discrimination␈α⊂is␈α⊂needed.␈α⊂ A␈α⊂useful␈α⊂classification␈α⊂of
␈↓ α←␈↓"recursive" appears in [Hew 76].
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ "Recursive"␈α
in␈α∞the␈α
sense␈α
of␈α∞recursive␈α
function␈α
theory␈α∞([Rog 67])␈α
meaning
␈↓ α←␈↓␈↓ β∂that␈αthere␈α
is␈αan␈α
algorithm␈αwhich␈α
specifies␈αthe␈α
function.␈α This␈α
involves␈αa
␈↓ α←␈↓␈↓ β∂precise study of the concepts of algorithm and computability.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ "Recursive"␈αin␈αthe␈αsense␈αof␈αself-referential.␈αThe␈αdefinition␈αof␈αan␈α
algorithm
␈↓ α←␈↓␈↓ β∂makes␈α
reference␈αto␈α
the␈αalgorithm␈α
itself.␈α
The␈αidea␈α
of␈α"self-reference"␈α
needs
␈↓ α←␈↓␈↓ β∂to␈αbe␈αhandled␈αcarefully.␈α The␈αdefinition␈αmay␈αinvolve␈αmutual␈αrecursion:␈α␈↓αf␈↓
␈↓ α←␈↓␈↓ β∂calls␈α
␈↓αg␈↓,␈α
and␈α∞␈↓αg␈↓␈α
calls␈α
␈↓αf␈↓.␈α
Also,␈α∞we␈α
saw␈α
on␈α
page 149␈α∞that␈α
a␈α
function␈α∞may␈α
be
␈↓ α←␈↓␈↓ β∂dynamically self-referential, while the static text is not.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 21␈↓Compare␈α⊃[And 76]␈α⊃in␈α⊃discussing␈α⊃call-by-name␈α⊃versus␈α⊂call-by-value:
␈↓ α←␈↓"programs␈αwhich␈αdon't␈αterminate␈α
usually␈αhave␈αbugs,␈αand␈αprogrammers␈α
would
␈↓ α←␈↓rather find out sooner (call-by-value) than later (call-by-name)".
␈↓ α←␈↓␈↓230 Imperative Constructs in LISP␈↓
_4.11␈↓
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ Finally,␈α⊃"recursive"␈α⊃is␈α⊃used␈α∩meaning␈α⊃"non-iterative".␈α⊃ This␈α⊃is␈α∩the␈α⊃usual
␈↓ α←␈↓␈↓ β∂interpretation␈α⊃imposed␈α⊃in␈α⊃programming␈α⊃languages.␈α⊃ This␈α⊃sense␈α⊂implies
␈↓ α←␈↓␈↓ β∂some␈αassumptions␈αabout␈α
the␈αevaluation␈αimplementation.␈α
Basically,␈αit␈αis␈α
to
␈↓ α←␈↓␈↓ β∂mean␈αthat␈αa␈αrecursive␈αevaluation␈αwill␈αrequire␈αmore␈αbookkeeping␈αthan␈α
the
␈↓ α←␈↓␈↓ β∂iterative␈α∃evaluation.␈α∃A␈α∃problem␈α∃on␈α∃page 193␈α∃asked␈α∃for␈α∃an␈α∀iterative
␈↓ α←␈↓␈↓ β∂evaluation␈α⊃of␈α∩␈↓αfact[2]␈↓;␈α⊃fewer␈α⊃environments␈α∩were␈α⊃created␈α⊃there␈α∩than␈α⊃we
␈↓ α←␈↓␈↓ β∂required␈αfor␈αthe␈αevaluation␈αof␈αthe␈α"recursive"␈α(in␈αthe␈αsense␈αof␈α␈↓↓2␈↓)␈αversion.
␈↓ α←␈↓␈↓ β∂This␈α∩third␈α∩use␈α⊃of␈α∩the␈α∩word␈α⊃"recursive"␈α∩is␈α∩the␈α⊃least␈α∩well␈α∩defined␈α⊃and
␈↓ α←␈↓␈↓ β∂understood.␈α∂ It␈α∂is␈α∂frequently␈α∂believed␈α∞that␈α∂"recursive"␈α∂in␈α∂the␈α∂sense␈α∂of␈α∞␈↓↓2␈↓
␈↓ α←␈↓␈↓ β∂implies␈α∞the␈α
third␈α∞"recursive."␈α
The␈α∞third␈α
sense␈α∞is␈α
more␈α∞a␈α
property␈α∞of␈α
the
␈↓ α←␈↓␈↓ β∂implementation␈α_of␈α→the␈α_evaluation␈α→scheme␈α_than␈α→a␈α_property␈α→of␈α_the
␈↓ α←␈↓␈↓ β∂algorithm.␈α_ Techniques␈α_are␈α_know␈α_for␈α_evaluating␈α_several␈α_classes␈α↔of
␈↓ α←␈↓␈↓ β∂"recursive"␈α∃algorithms␈α∀using␈α∃iterative␈α∀storage␈α∃requirements;␈α∀[Hew 76],
␈↓ α←␈↓␈↓ β∂[Sus 76], [Gre 76a].
␈↓"∀␈↓ α←␈↓␈↓ β'Since␈α∞the␈α∞ideas␈α∞involved␈α∞in␈α
"recursive"␈α∞are␈α∞so␈α∞important␈α∞to␈α∞LISP␈α
and
␈↓ α←␈↓programming␈α
languages␈α
in␈α
general,␈α
we␈α
want␈α
to␈α
explore␈α
the␈α
ideas␈α
further.␈α
We
␈↓ α←␈↓will␈α∂be␈α∞most␈α∂concerned␈α∞with␈α∂"recursive"␈α∞in␈α∂sense␈α∞␈↓↓2␈↓;␈α∂we␈α∞want␈α∂to␈α∞understand
␈↓ α←␈↓what␈αis␈αinvolved␈αin␈αgiving␈αa␈αrecursive␈αdefinition.␈α We␈αnow␈αhave␈α
three␈αways
␈↓ α←␈↓to␈αdefine␈αfunctions:␈αthe␈α
␈↓αlabel␈↓␈αoperator,␈αthe␈α"<="-operator,␈αand␈α
the␈αassignment
␈↓ α←␈↓statement. We need to understand the differences between these operations.
␈↓"β␈↓ α←␈↓␈↓ β'To begin with, we were able to give counterexamples to interpreting:
␈↓"∀␈↓ α←␈↓␈↓ ¬ ␈↓αf <= λ[[x] ␈↓λx␈↓] as ␈↓αf ← λ[[x] ␈↓λx␈↓α]
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α8discussion␈α8of␈α7binding␈α8and␈α8environments␈α7made
␈↓ α←␈↓␈↓αf ← function[λ[[x] ␈↓λx␈↓α]]␈↓␈α∞a␈α
more␈α∞likely␈α
candidate;␈α∞however␈α
this␈α∞interpretation␈α
is
␈↓ α←␈↓also not adequate.
␈↓" ␈↓ α←␈↓␈↓ β'We might implement ␈↓αfact <= λ[[x][x=0 → 1; ␈↓
t␈↓α → *[x;fact[x-1]]]]␈↓ as:
␈↓" ␈↓ α←␈↓α␈↓ ∧yfact ← function[λ[[x] ... fact[x-1] ...]
␈↓"∀␈↓ α←␈↓Consider an initial environment with ␈↓αfact␈↓ defined:
␈↓" ␈↓ α←␈↓␈↓ βK␈↓ β{E␈↓βi␈↓
␈↓"β␈↓ α←␈↓␈↓ βKE␈↓βc␈↓␈↓ β{|␈↓ ∧+E␈↓βa␈↓
␈↓"β␈↓ α←␈↓␈↓ βK___________
␈↓"β␈↓ α←␈↓␈↓ βK␈↓αfact␈↓␈↓ β{| ␈↓αλ[[x] ... fact[x-1] ... ] ␈↓: E␈↓βi␈↓
␈↓"∀␈↓ α←␈↓We␈α⊗will␈α⊗demonstrate␈α⊗the␈α↔inadequacy␈α⊗of␈α⊗two␈α⊗natural␈α↔interpretations␈α⊗of
␈↓ α←␈↓function␈α
values:␈α∞direct␈α
assignment␈α∞of␈α
value,␈α
and␈α∞assignment␈α
of␈α∞␈↓αfunarg␈↓.␈α
We
␈↓ α←␈↓execute ␈↓αfoo ← fact␈↓ and ␈↓αbaz ← function[fact]␈↓, giving:
␈↓" ␈↓ α←␈↓␈↓ βK␈↓ β{E␈↓βi␈↓
␈↓"β␈↓ α←␈↓␈↓ βKE␈↓βc␈↓␈↓ β{|␈↓ ∧+E␈↓βa␈↓
␈↓"β␈↓ α←␈↓␈↓ βK___________
␈↓"β␈↓ α←␈↓␈↓ βK␈↓αfact␈↓␈↓ β{| ␈↓αλ[[x] ... fact[x-1] ... ] ␈↓: E␈↓βi␈↓
␈↓"β␈↓ α←␈↓␈↓ βK␈↓αfoo␈↓␈↓ β{| ␈↓αλ[[x] ... fact[x-1] ... ] ␈↓: E␈↓βi␈↓
␈↓"β␈↓ α←␈↓␈↓ βK␈↓αbaz␈↓␈↓ β{| ␈↓αfact ␈↓: E␈↓βi␈↓
␈↓ α←␈↓␈↓4.11␈↓ πRapprochement: In Retrospect 231␈↓
␈↓"∀␈↓ α←␈↓Things␈α≠don't␈α≠look␈α≠quite␈α≠right;␈α≠the␈α≠"intent"␈α≠of␈α≠both␈α≠␈↓αfoo ← fact␈↓␈α≠and
␈↓ α←␈↓␈↓αbaz ← function[fact]␈↓␈α⊂was␈α⊂to␈α⊂make␈α⊂␈↓αfoo␈↓␈α⊂and␈α⊂␈↓αbaz␈↓␈α⊂synonymous␈α⊂with␈α⊃␈↓αfact␈↓.␈α⊂That
␈↓ α←␈↓clearly␈α∂is␈α∂not␈α∂the␈α⊂case␈α∂though␈α∂the␈α∂right␈α∂thing␈α⊂happens␈α∂if␈α∂we␈α∂were␈α⊂now␈α∂to
␈↓ α←␈↓evaluate␈αan␈αexpression␈αinvolving␈α␈↓αfoo␈↓␈αor␈α␈↓αbaz␈↓.␈αThe␈αproblem␈αis␈αthat␈αit␈αhappens
␈↓ α←␈↓for␈α
the␈αwrong␈α
reason␈αeven␈α
though␈αthe␈α
occurrence␈αof␈α
␈↓αfact␈↓␈αin␈α
the␈αbody␈α
of␈α␈↓αfoo␈↓
␈↓ α←␈↓will␈α⊃find␈α⊃the␈α⊃right␈α⊂definition␈α⊃of␈α⊃␈↓αfact␈↓;␈α⊃an␈α⊂application␈α⊃of␈α⊃␈↓αbaz␈↓␈α⊃will␈α⊃find␈α⊂the
␈↓ α←␈↓definition of ␈↓αfact␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'One more step will lead to disaster: ␈↓αfact ← λ[[x] x]␈↓
␈↓" ␈↓ α←␈↓␈↓ βK␈↓ β{E␈↓βi␈↓
␈↓"β␈↓ α←␈↓␈↓ βKE␈↓βc␈↓␈↓ β{|␈↓ ∧+E␈↓βa␈↓
␈↓"β␈↓ α←␈↓␈↓ βK___________
␈↓"β␈↓ α←␈↓␈↓ βK␈↓αfact␈↓␈↓ β{| ␈↓αλ[[x]x]
␈↓"β␈↓ α←␈↓α␈↓ βKfoo␈↓ β{| λ[[x] ... fact[x-1] ... ] ␈↓: E␈↓βi␈↓
␈↓"β␈↓ α←␈↓␈↓ βK␈↓αbaz␈↓␈↓ β{| ␈↓αfact ␈↓: E␈↓βi␈↓
␈↓"∀␈↓ α←␈↓Now␈αwe␈αare␈αreally␈αlost.␈αThough␈αit␈α
is␈αreasonable␈αto␈αredefine␈α␈↓αfact␈↓␈α-- it␈αis␈αonly␈α
a
␈↓ α←␈↓name --␈α∂our␈α∂intent␈α∂was␈α∂to␈α∂keep␈α∂␈↓αbaz␈↓␈α∂and␈α∂␈↓αfoo␈↓␈α∂as␈α∂realizations␈α∂of␈α∂the␈α∞factorial
␈↓ α←␈↓function. This intent has not been maintained.
␈↓" ␈↓ α←␈↓␈↓ ∧∂␈↓αfact␈↓ <= ␈↓αλ[[x] ... fact[x-1] ... ]␈↓ is quite different from:
␈↓" ␈↓ α←␈↓␈↓ ¬
␈↓αfoo␈↓ <= ␈↓αλ[[x] ... fact[x-1] ... ] ␈↓π 22␈↓α
␈↓"β␈↓ α←␈↓To␈α⊗understand␈α⊗what␈α⊗has␈α⊗happened␈α⊗we␈α⊗look␈α⊗at␈α⊗assignments␈α↔to␈α⊗␈↓¬simple␈↓
␈↓ α←␈↓variables␈α∞rather␈α∞than␈α∂functional␈α∞variables.␈α∞It␈α∂is␈α∞clear␈α∞how␈α∂the␈α∞environment
␈↓ α←␈↓should change during the execution of the sequence:
␈↓"∀␈↓ α←␈↓α␈↓ βKx ← 3
␈↓" ␈↓ α←␈↓α␈↓ βKy ← x
␈↓" ␈↓ α←␈↓α␈↓ βKx ← 4
␈↓"∀␈↓ α←␈↓Let's try something slightly more complicated:
␈↓" ␈↓ α←␈↓α␈↓ βKx ← 1
␈↓"β␈↓ α←␈↓α␈↓ βKx ← x␈↓π2␈↓α - 6
␈↓"β␈↓ α←␈↓Here␈αwe␈αfirst␈αassign␈α␈↓α1␈↓␈αto␈α␈↓αx␈↓;␈αthen␈αwe␈αevaluate␈αthe␈αright␈αhand␈αside␈αof␈αthe␈αnext
␈↓ α←␈↓statement.␈α∃We␈α∀look␈α∃up␈α∃the␈α∀current␈α∃value␈α∃of␈α∀␈↓αx␈↓,␈α∃perform␈α∃the␈α∀arithmetic
␈↓ α←␈↓computations,␈α
and␈α
finally␈α
assign␈αthe␈α
value␈α
␈↓α-5␈↓␈α
to␈α␈↓αx␈↓.␈α
Compare␈α
␈↓¬this␈↓␈α
to␈α
the␈α␈↓αfact␈↓
␈↓ α←␈↓definition;␈α∞there␈α∞we␈α∞made␈α
a␈α∞point␈α∞of␈α∞not␈α
"evaluating"␈α∞the␈α∞name␈α∞␈↓αfact␈↓␈α∞in␈α
the
␈↓ α←␈↓right hand side of "<=".
␈↓"β␈↓ α←␈↓␈↓ β'Notice␈α∩that␈α∩␈↓¬after␈↓␈α∩an␈α∩assignment␈α⊃like␈α∩␈↓αy ← x␈↓␈α∩has␈α∩been␈α∩executed,␈α⊃then
␈↓ α←␈↓equality␈α∂holds␈α∂between␈α∂the␈α∂left-hand␈α∂and␈α∂right-hand␈α∂quantities.␈α∂ Let's␈α∞look
␈↓ α←␈↓more␈α∂closely␈α∂at␈α∂the␈α∂relationship␈α∂between␈α∂"←"␈α∂and␈α∂"=".␈α∂ Consider␈α∂␈↓αx ← x␈↓π2␈↓α - 6␈↓.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 22␈↓In␈α∀LISP␈α∪1␈α∀([McC 60])␈α∪considerations␈α∀were␈α∪given␈α∀to␈α∪representing
␈↓ α←␈↓recursive␈α∞definitions␈α∂as␈α∞circular␈α∂structure,␈α∞instead␈α∞of␈α∂referring␈α∞to␈α∂the␈α∞name.
␈↓ α←␈↓That would have solved the current problem.
␈↓ α←␈↓␈↓232 Imperative Constructs in LISP␈↓
_4.11␈↓
␈↓"β␈↓ α←␈↓After␈α∪the␈α∪assignment␈α∩is␈α∪made,␈α∪equality␈α∪does␈α∩␈↓¬not␈↓␈α∪hold␈α∪between␈α∪left-␈α∩and
␈↓ α←␈↓right-hand␈α∂sides.␈α∂ Now␈α∂consider␈α∂␈↓αx = x␈↓π2␈↓α - 6␈↓.␈α∂ Interpreting␈α∂this␈α⊂expression␈α∂as
␈↓ α←␈↓an␈αequation,␈αnot␈αas␈αan␈αexpression␈αwhose␈αvalue␈αis␈αtrue␈αor␈αfalse␈αdepending␈αon
␈↓ α←␈↓the␈αcurrent␈αvalue␈αof␈α␈↓αx␈↓,␈αwe␈αfind␈αtwo␈αsolutions:␈αlet␈α␈↓αx␈↓␈αhave␈αvalue␈α␈↓α-2␈↓␈αor␈α␈↓α3␈↓.␈α If␈αwe
␈↓ α←␈↓examine␈α∩our␈α⊃"definition"␈α∩of␈α∩␈↓αfact␈↓␈α⊃in␈α∩␈↓¬this␈↓␈α∩light,␈α⊃interpreting␈α∩"<="␈α∩as␈α⊃being
␈↓ α←␈↓related␈α⊂to␈α⊂"="␈α⊂rather␈α∂than␈α⊂"←",␈α⊂then␈α⊂we␈α⊂are␈α∂faced␈α⊂with␈α⊂an␈α⊂equation␈α⊂to␈α∂be
␈↓ α←␈↓solved,␈α∞only␈α∂now␈α∞the␈α∂form␈α∞of␈α∂the␈α∞solution␈α∂is␈α∞a␈α∂␈↓¬function␈↓␈α∞which␈α∂satisfies␈α∞the
␈↓ α←␈↓equation.␈α⊗There␈α⊗may␈α↔be␈α⊗many␈α⊗solutions;␈α↔there␈α⊗may␈α⊗be␈α↔no␈α⊗non-trivial
␈↓ α←␈↓solutions.␈α∪ In␈α∪our␈α∪case␈α∪there␈α∪is␈α∩at␈α∪least␈α∪one␈α∪solution:␈α∪the␈α∪usual␈α∩factorial
␈↓ α←␈↓function. So what we ␈↓¬should␈↓ write is something more like:
␈↓" ␈↓ α←␈↓α␈↓ β∨fact ← ␈↓a solution to the equation␈↓α: f = λ[[x][x=0 → 1; ␈↓
t␈↓α → *[x;f[x-1]]]].
␈↓" ␈↓ α←␈↓That␈α∂is,␈α∂the␈α∂␈↓¬real␈↓␈α∞intent␈α∂of␈α∂the␈α∂recursive␈α∂definition␈α∞of␈α∂␈↓αfact␈↓␈α∂was␈α∂to␈α∂define␈α∞a
␈↓ α←␈↓function␈α
to␈α
effect␈αthe␈α
computation␈α
of␈α
factorial␈αand␈α
then␈α
to␈α
␈↓¬name␈↓␈αthat␈α
function
␈↓ α←␈↓␈↓αfact␈↓.␈αQuestions␈αof␈αwhen␈αsolutions␈α
exist,␈αhow␈αmany,␈αand␈αwhich␈αare␈α
the␈α"best"
␈↓ α←␈↓solutions is a topic of much current research ([Man 74]).
␈↓"β␈↓ α←␈↓␈↓ β'We␈αhave␈α
seen␈αa␈αrelated␈α
result␈αin␈α
the␈αproblem␈αon␈α
page 183;␈αwe␈α
were␈αto
␈↓ α←␈↓show␈αthat␈α␈↓αfact = ␈↓εt␈↓α[fact]␈↓.␈α That␈αis,␈α␈↓αfact␈↓␈αis␈αa␈αsolution␈αto␈αthe␈αequation␈α␈↓εt␈↓α[x] = x␈↓.
␈↓ α←␈↓Solutions to such equations are called ␈↓↓fixed points␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The ␈↓αfact␈↓ result is an instance of a more general result:
␈↓"∀␈↓ α←␈↓␈↓ βKfor any ␈↓εt␈↓, define ␈↓αh <= λ[[g] ␈↓εt␈↓α[function[λ[[x] g[g[x]]]]]␈↓
␈↓"β␈↓ α←␈↓(␈↓Y␈↓)
␈↓"β␈↓ α←␈↓␈↓ βKthen ␈↓αh[function[h]] ␈↓λ≡␈↓α ␈↓εt␈↓α[h[function[h]]]␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'We␈α∂shall␈α∂not␈α∂prove␈α∂this␈α∂result␈α∂but␈α∂we␈α∂can␈α∂give␈α∂some␈α∂insight␈α∂into␈α∂its
␈↓ α←␈↓justification␈αas␈αwe␈α
develop␈αthe␈αmathematical␈α
properties␈αof␈α␈↓αlabel␈↓;␈α
we␈αcontinue
␈↓ α←␈↓our␈α⊂discussion␈α⊂of␈α⊃Section 3.13.␈α⊂ Recall␈α⊂␈↓εD␈↓βe␈↓␈α⊂and␈α⊃␈↓εD␈↓βa␈↓␈α⊂from␈α⊂page 232.␈α⊃ In␈α⊂any
␈↓ α←␈↓environment␈α␈↓λD␈↓βa␈↓␈αshould␈αmap␈α␈↓αlabel[f;g]␈↓␈αin␈αsuch␈αa␈αway␈αthat␈αthe␈αdenotation␈αof␈α
␈↓αf␈↓
␈↓ α←␈↓is␈α
synonymous␈α
with␈α
that␈α
of␈α
␈↓αg␈↓.␈α That␈α
is,␈α
␈↓f␈↓␈α
is␈α
a␈α
mapping␈α
satisfying␈αthe␈α
equation
␈↓ α←␈↓␈↓f(t␈↓β1␈↓, ..., t␈↓βn␈↓) = g(t␈↓β1␈↓, ..., t␈↓βn␈↓)␈↓. So:
␈↓"∀␈↓ α←␈↓␈↓ ¬π␈↓λD␈↓βa␈↓∞(␈↓αlabel[␈↓∧f␈↓;␈↓∧g␈↓]␈↓∞)␈↓(l)␈↓ = ␈↓λD␈↓βa␈↓∞(␈↓∧g␈↓∞)␈↓(l)␈↓
␈↓"∀␈↓ α←␈↓This␈αwill␈αsuffice␈α
for␈αour␈αcurrent␈α
λ-definitions;␈αwe␈αneed␈α
not␈αmodify␈α␈↓l␈↓␈αsince␈α
the
␈↓ α←␈↓name␈α␈↓αf␈↓␈αwill␈αnever␈αbe␈α
used␈αwithin␈αthe␈αevaluation␈αof␈αan␈α
expression␈αinvolving
␈↓ α←␈↓␈↓αg␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α⊃must␈α⊃be␈α⊃a␈α⊃bit␈α⊃careful.␈α⊃ Our␈α⊃treatment␈α⊃of␈α⊃non-local␈α⊃variables␈α⊂in
␈↓ α←␈↓LISP␈α∀(on␈α∪page 127␈α∀and␈α∀in␈α∪Section 3.8)␈α∀requires␈α∪that␈α∀these␈α∀variables␈α∪be
␈↓ α←␈↓evaluated␈α∂when␈α∂the␈α∞function␈α∂is␈α∂␈↓¬activated␈↓␈α∂rather␈α∞than␈α∂when␈α∂the␈α∂function␈α∞is
␈↓ α←␈↓defined.␈α
Thus␈α
a␈α
λ-definition␈α
generally␈αrequires␈α
an␈α
environment␈α
in␈α
which␈αto
␈↓ α←␈↓evaluate␈αits␈αnon-local␈αvariables.␈α So␈αits␈αdenotation␈αshould␈αbe␈αa␈αmapping␈αlike:
␈↓ α←␈↓␈↓env␈↓ → ␈↓[S␈↓πn␈↓ → S]␈↓␈α⊂rather␈α⊂than␈α∂simply␈α⊂␈↓[S␈↓πn␈↓ → S]␈↓.␈α⊂This␈α∂is␈α⊂consistent␈α⊂with␈α∂our
␈↓ α←␈↓understanding␈α⊗of␈α⊗the␈α⊗meaning␈α⊗of␈α∃λ-notation.␈α⊗ It␈α⊗is␈α⊗what␈α⊗␈↓αfunction␈↓␈α∃was
␈↓ α←␈↓attempting␈α
to␈α
describe.␈αWhat␈α
we␈α
previously␈α
have␈αcalled␈α
an␈α
open␈α
function␈αis
␈↓ α←␈↓of␈αthe␈αform:␈α␈↓[S␈↓πn␈↓ → S]␈↓.␈α Given␈αa␈α
name␈αfor␈αa␈αfunction␈αin␈αan␈α
environment␈αwe
␈↓ α←␈↓can expect to receive a mapping from ␈↓env␈↓ to an element of ␈↓Fn␈↓.
␈↓ α←␈↓␈↓4.11␈↓ πRapprochement: In Retrospect 233␈↓
␈↓"β␈↓ α←␈↓␈↓ β'A␈αmodification␈αof␈αour␈αhandling␈αof␈α␈↓αlabel␈↓␈αis␈αrequired␈αto␈αdescribe␈αthe␈αcase
␈↓ α←␈↓for recursion:
␈↓"∀␈↓ α←␈↓␈↓ ∧&␈↓λD␈↓βa␈↓∞(␈↓αlabel[␈↓∧f␈↓;␈↓∧g␈↓]␈↓∞)␈↓(l)␈↓ = ␈↓λD␈↓βa␈↓∞(␈↓∧g␈↓∞)␈↓(l : <f,␈↓λD␈↓βa␈↓∞(␈↓∧g␈↓∞)␈↓>)␈↓
␈↓"∀␈↓ α←␈↓Interpreting␈α∀this␈α∀equation␈α∀operationally,␈α∀it␈α∀says:␈α∀when␈α∀we␈α∀apply␈α∃a␈α∀␈↓αlabel␈↓
␈↓ α←␈↓expression␈α⊂in␈α⊂an␈α∂environment␈α⊂it␈α⊂is␈α∂the␈α⊂same␈α⊂as␈α∂applying␈α⊂the␈α⊂body␈α⊂of␈α∂the
␈↓ α←␈↓definition␈α⊗in␈α⊗an␈α∃environment␈α⊗modified␈α⊗to␈α∃associate␈α⊗the␈α⊗name␈α⊗with␈α∃its
␈↓ α←␈↓definition.␈α
This␈α
is␈α
analogous␈α
to␈α
what␈α
the␈α
LISP␈α
␈↓αapply␈↓␈α
function␈α
will␈α
do.␈α
Since
␈↓ α←␈↓this␈α
interpretation␈αof␈α
␈↓αlabel␈↓␈αis␈α
inadequate␈αin␈α
general␈αcontexts,␈α
we␈α
should␈αlook
␈↓ α←␈↓further␈α≠for␈α≠a␈α~general␈α≠reading␈α≠of␈α≠␈↓αlabel␈↓.␈α~Our␈α≠hint␈α≠comes␈α≠from␈α~our
␈↓ α←␈↓interpretation of "<=" as an equality. Recall:
␈↓"∀␈↓ α←␈↓α␈↓ β#fact ← ␈↓a solution to the equation:␈↓α f = λ[[x][x=0 → 1; ␈↓
t␈↓α → *[x;f[x-1]]]]
␈↓"∀␈↓ α←␈↓␈↓ β'What␈α∂we␈α∂need␈α∂is␈α∂a␈α∂representation␈α∂of␈α∂an␈α∂"equation-solver"␈α⊂which␈α∂will
␈↓ α←␈↓take␈α∃such␈α∃an␈α∀equation␈α∃and␈α∃will␈α∃return␈α∀a␈α∃function␈α∃which␈α∃satisfies␈α∀that
␈↓ α←␈↓equation.␈α∂In␈α∂particular␈α∞we␈α∂would␈α∂like␈α∞the␈α∂␈↓¬best␈↓␈α∂solution␈α∞in␈α∂the␈α∂sense␈α∂that␈α∞it
␈↓ α←␈↓imposes␈α
the␈α
minimal␈α
structure␈αon␈α
the␈α
function.␈↓π 23␈↓␈α
This␈α
request␈αfor␈α
minimality
␈↓ α←␈↓translates␈α⊗to␈α↔finding␈α⊗the␈α↔function␈α⊗which␈α⊗satisfies␈α↔the␈α⊗equation,␈α↔but␈α⊗is
␈↓ α←␈↓least-defined␈α∩on␈α⊃other␈α∩elements␈α∩of␈α⊃the␈α∩domain.␈α⊃This␈α∩discussion␈α∩of␈α⊃"least"
␈↓ α←␈↓brings␈α∂in␈α∂the␈α∂recent␈α⊂work␈α∂of␈α∂D.␈α∂Scott␈α⊂and␈α∂the␈α∂intuition␈α∂behind␈α⊂this␈α∂study
␈↓ α←␈↓again␈α(illuminates␈α(the␈α(distinction␈α(between␈α(mathematical␈α'meaning
␈↓ α←␈↓(denotational) and manipulative meaning (operational).
␈↓"β␈↓ α←␈↓␈↓ β'Consider the following LISP definition:
␈↓"∀␈↓ α←␈↓α␈↓ ∧,f <= λ[[n][n=0 → 0; ␈↓
t␈↓α → +[f[n-1];*[2;sub1[n]]]]]
␈↓"∀␈↓ α←␈↓When␈α∞we␈α
are␈α∞asked␈α∞what␈α
this␈α∞function␈α
is␈α∞doing,␈α∞most␈α
of␈α∞us␈α∞would␈α
proceed
␈↓ α←␈↓operationally;␈αthat␈αis,␈αwe␈αwould␈α
begin␈αcomputing␈α␈↓αf[n]␈↓␈αfor␈αdifferent␈α
values␈αof
␈↓ α←␈↓␈↓αn␈↓␈α∩--what is ␈↓αf[0]?␈↓,␈α∪what␈α∩is␈α∪␈↓αf[1]␈↓, ... .␈α∩It␈α∩is␈α∪profitable␈α∩to␈α∪look␈α∩at␈α∪this␈α∩process
␈↓ α←␈↓differently:␈αwhat␈αwe␈αare␈αdoing␈αis␈α
looking␈αat␈αa␈α␈↓¬sequence␈↓␈αof␈αfunctions,␈αcall␈α
them
␈↓ α←␈↓␈↓f␈↓βi␈↓'s .
␈↓"∀␈↓ α←␈↓␈↓ β∂f␈↓β0␈↓ =␈↓ βW␈↓{<0,␈↓λB␈↓>,<1,␈↓λB␈↓>, ... }␈↓ εc␈↓when we begin, we know nothing.
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓f␈↓β1␈↓ =␈↓ βW␈↓{<0,0>,<1,␈↓λB␈↓>, ... }␈↓ εc␈↓now we know one pair.
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓f␈↓β2␈↓ =␈↓ βW␈↓{<0,0>,<1,1>, ... }␈↓ εc␈↓now two
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓f␈↓β3␈↓ =␈↓ βW␈↓{<0,0>,<1,1>,<2,4>, ... }␈↓ εc␈↓now three
␈↓"β␈↓ α←␈↓␈↓ β∂ ...␈↓ βW thinks ...␈↓ εc␈↓Eureka!!
␈↓"∀␈↓ α←␈↓When␈α∃or␈α∃if,␈α⊗we␈α∃realize␈α∃that␈α∃the␈α⊗LISP␈α∃function␈α∃denotes␈α⊗the␈α∃"squaring
␈↓ α←␈↓function"␈αfor␈αnon-negative␈αintegers,␈αthen␈αwe␈αhave␈αmoved␈αfrom␈αpragmatics␈αto
␈↓ α←␈↓semantics.␈α∂ The␈α∂process␈α∞of␈α∂discovering␈α∂the␈α∂denotation␈α∞may␈α∂be␈α∂likened␈α∂to␈α∞a
␈↓ α←␈↓limiting process which converges to a function satisfying the LISP definition.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 23␈↓Like␈α∞a␈α
free␈α∞group␈α∞satisfies␈α
the␈α∞group␈α∞axioms,␈α
but␈α∞imposes␈α∞no␈α
other
␈↓ α←␈↓requirements.
␈↓ α←␈↓␈↓234 Imperative Constructs in LISP␈↓
_4.11␈↓
␈↓"β␈↓ α←␈↓␈↓ β'That is:
␈↓"∀␈↓ α←␈↓␈↓ ¬"␈↓λλ␈↓((n) n␈↓π2␈↓)␈↓ = limit of the ␈↓ f␈↓βi␈↓'s
␈↓"β␈↓ α←␈↓where ␈↓f␈↓βi␈↓ may also be characterised as:
␈↓" ␈↓ α←␈↓␈↓ βc␈↓ ∧s n␈↓π2␈↓ if ␈↓0␈↓λ≤␈↓n␈↓λ≤␈↓i
␈↓"β␈↓ α←␈↓␈↓ βcf␈↓βi␈↓(n)␈↓ =
␈↓"β␈↓ α←␈↓␈↓ βc␈↓ ∧s ␈↓λB␈↓ if ␈↓i␈↓λ<␈↓n␈↓ or ␈↓n␈↓␈↓λ<␈↓0␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'We␈α
may␈α
think␈α
of␈α
our␈α"equation-solver"␈α
as␈α
proceeding␈α
in␈α
such␈αa␈α
manner.
␈↓ α←␈↓As␈α
with␈α
simpler␈α∞equations,␈α
there␈α
may␈α
be␈α∞several␈α
solutions.␈α
For␈α∞example,␈α
we
␈↓ α←␈↓have␈α∞seen␈α∞that␈α∞a␈α∞function,␈α∞␈↓αg␈↓,␈α∞defined␈α∞to␈α∞be␈α∞␈↓αn!␈↓␈α∞for␈α∞␈↓αn␈↓λ≥␈↓α0␈↓␈α∞and␈α∞␈↓α0␈↓␈α∞for␈α∞␈↓αn␈↓λ<␈↓α0␈↓␈α
also
␈↓ α←␈↓satisfies␈α∪the␈α∀LISP␈α∪definition␈α∪of␈α∀␈↓αfact␈↓.␈α∪ The␈α∪expected␈α∀solution␈α∪for␈α∀␈↓αfact␈↓␈α∪is
␈↓ α←␈↓undefined␈αfor␈α␈↓αn␈↓λ<␈↓α0␈↓,␈αand␈αis␈αtherefore␈α"less␈αdefined"␈αthan␈α␈↓αg␈↓.␈αWe␈αwould␈αlike␈αour
␈↓ α←␈↓equation solver to produce a minimal solution, if possible.
␈↓"β␈↓ α←␈↓␈↓ β'That is, ␈↓Y␈↓ applied to a term ␈↓εt␈↓ gives a function ␈↓f␈↓ satisfying ␈↓f = ␈↓εt␈↓(f)␈↓.
␈↓"∀␈↓ α←␈↓␈↓ ¬n␈↓Y(␈↓εt␈↓) = ␈↓εt␈↓(Y(␈↓εt␈↓))␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Also␈α∞␈↓f␈↓␈α∞should␈α
be␈α∞minimal␈α∞in␈α
the␈α∞sense␈α∞that␈α
any␈α∞other␈α∞function␈α
which
␈↓ α←␈↓satisfies␈α∞the␈α∞equation␈α∞is␈α∞more␈α∂defined␈α∞than␈α∞␈↓f␈↓.␈α∞Compare␈α∞this␈α∂with␈α∞page 232.
␈↓ α←␈↓That result can be interpreted as saying: for any ␈↓εt␈↓
␈↓"∀␈↓ α←␈↓␈↓ ∧=␈↓Y(␈↓εt␈↓) = h(h)␈↓ where ␈↓h = ␈↓λλ␈↓((g)␈↓εt␈↓(g(g)))␈↓
␈↓"∀␈↓ α←␈↓Such␈αan␈αequation␈αsolver␈α␈↓¬does␈↓␈αexist;␈αit␈αis␈αcalled␈αthe␈α␈↓↓fixed-point␈αoperator␈↓.␈αIt␈αis
␈↓ α←␈↓designated␈α∂here␈α∂by␈α⊂␈↓Y␈↓.␈α∂To␈α∂comprehend␈α∂␈↓Y␈↓␈α⊂we␈α∂generalize␈α∂from␈α⊂the␈α∂previous
␈↓ α←␈↓example.
␈↓"β␈↓ α←␈↓␈↓ β'In terms of our example we want a solution to ␈↓f = ␈↓λt␈↓(f)␈↓, where:
␈↓"∀␈↓ α←␈↓␈↓ ∧=␈↓λt␈↓(g) = ␈↓λλ␈↓((n) if(n=0, 0, g(n-1)+2*n-1))␈↓
␈↓"∀␈↓ α←␈↓Our␈α∞previous␈α∞discussion␈α∞leads␈α∞us␈α∞to␈α∞believe␈α∞that␈α∞␈↓λλ␈↓((n) n␈↓π2␈↓)␈α∞␈↓for␈α∞␈↓n␈α∞␈↓λ≥␈↓0␈↓␈α∂is␈α∞the
␈↓ α←␈↓desired solution.
␈↓"β␈↓ α←␈↓␈↓ β'How␈α∞does␈α∞this␈α∞discussion␈α∞relate␈α∞to␈α∞the␈α∞sequence␈α∞of␈α∞functions␈α∂␈↓f␈↓βi␈↓?␈α∞ Let's
␈↓ α←␈↓look␈α
at␈α
the␈αbehavior␈α
of␈α
␈↓λt␈↓␈αfor␈α
various␈α
arguments.␈αThe␈α
simplest␈α
function␈αis␈α
the
␈↓ α←␈↓totally undefined function, ␈↓λB␈↓ .␈↓π 24␈↓
␈↓"∀␈↓ α←␈↓␈↓ ∧6␈↓λt␈↓(␈↓λB␈↓) = ␈↓λλ␈↓((n) if(n=0, 0, ␈↓λB␈↓(n-1)+2*n-1))␈↓
␈↓"∀␈↓ α←␈↓but this says ␈↓λt␈↓(␈↓λB␈↓)␈↓ is just ␈↓f␈↓β1␈↓. Similarly,
␈↓"∀␈↓ α←␈↓␈↓ ∧ ␈↓λt␈↓(␈↓λt␈↓(␈↓λB␈↓)) = ␈↓λλ␈↓((n) if(n=0, 0, f␈↓β1␈↓(n-1)+2*n-1))␈↓
␈↓"∀␈↓ α←␈↓is just ␈↓f␈↓β2␈↓. Writing ␈↓λt␈↓πi␈↓ for the composition of ␈↓λt␈↓...␈↓λt␈↓, i times, ␈↓π 25␈↓ we can say
␈↓"∀␈↓ α←␈↓␈↓ ¬z␈↓f␈↓βi␈↓ = ␈↓λt␈↓πi␈↓(␈↓λB␈↓)␈↓ or,
␈↓" ␈↓ α←␈↓␈↓ ¬;␈↓λλ␈↓((n)n␈↓π2␈↓)␈↓ = lim␈↓βi → ∞␈↓λt␈↓πi␈↓(␈↓λB␈↓)␈↓
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 24␈↓This ␈↓λB␈↓ is the totally undefined function in ␈↓[Fn → Fn]␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 25␈↓Define ␈↓λt␈↓π0␈↓ to be ␈↓λB␈↓ in ␈↓[Fn→Fn]␈↓.
␈↓ α←␈↓␈↓4.11␈↓ πRapprochement: In Retrospect 235␈↓
␈↓"β␈↓ α←␈↓␈↓ β'It␈α⊂can␈α⊃be␈α⊂shown␈α⊃that␈α⊂the␈α⊃least␈α⊂fixed-point␈α⊃of␈α⊂an␈α⊃equation␈α⊂␈↓f␈α⊃=␈α⊂␈↓λt␈↓(f)␈↓
␈↓ α←␈↓satisfies the relation:
␈↓"∀␈↓ α←␈↓␈↓ ¬N␈↓Y(␈↓λt␈↓) = lim␈↓βi→∞␈↓λt␈↓πn␈↓(␈↓λB␈↓) ␈↓
␈↓"∀␈↓ α←␈↓So the denotation for ␈↓αlabel␈↓ might be better described by:
␈↓"∀␈↓ α←␈↓␈↓ ∧≥␈↓λD␈↓βa␈↓∞(␈↓αlabel[␈↓∧f␈↓;␈↓∧g␈↓]␈↓∞)␈↓(l)␈↓ = ␈↓Y(␈↓λλ␈↓(h)␈↓λD␈↓βa␈↓∞(␈↓∧g␈↓∞)␈↓(l␈↓ : ␈↓<f,h>))␈↓
␈↓"∀␈↓ α←␈↓rather than:
␈↓"∀␈↓ α←␈↓␈↓ ∧&␈↓λD␈↓βa␈↓∞(␈↓αlabel[␈↓∧f␈↓;␈↓∧g␈↓]␈↓∞)␈↓(l)␈↓ = ␈↓λD␈↓βa␈↓∞(␈↓∧g␈↓∞)␈↓(l : <f,␈↓λD␈↓βa␈↓∞(␈↓∧g␈↓∞)␈↓>)␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α#characterizations␈α$are␈α#not␈α$equivalent.␈α#The␈α$behavior␈α#of
␈↓ α←␈↓␈↓λD␈↓βa␈↓∞(␈↓αlabel[car;car]␈↓∞)␈↓␈αwill␈αexhibit␈α
a␈αdiscrepancy.␈α The␈αfix-point␈α
characterization
␈↓ α←␈↓reduces␈α␈↓αlabel[car;car]␈↓␈αto␈αthe␈αminimal␈αsolution␈αof␈αthe␈αequation␈α␈↓car =car␈↓;␈αthat
␈↓ α←␈↓solution␈α∩is␈α∩␈↓λB␈↓.␈α∩ The␈α∩LISP␈α∩interpretation␈α∩of␈α∩␈↓αlabel[car;car]␈↓␈α∩gives␈α∩␈↓car␈↓.␈α⊃ The
␈↓ α←␈↓general␈α∂question␈α∞of␈α∂the␈α∂correctness␈α∞of␈α∂the␈α∞denotational␈α∂semantics␈α∂which␈α∞we
␈↓ α←␈↓are developing is the subject of [Gor 73].
␈↓"β␈↓ α←␈↓␈↓ β'In␈αsummary,␈α
LISP␈αhas␈αtwo␈α
ways␈αof␈αassigning␈α
values␈αto␈α
functions:␈α␈↓αlabel␈↓
␈↓ α←␈↓and␈α∂<=.␈α⊂ The␈α∂use␈α⊂of␈α∂␈↓αlabel␈↓␈α⊂manufactures␈α∂a␈α⊂new␈α∂"knotted"␈α⊂environment␈α∂but
␈↓ α←␈↓does␈α∩not␈α∩always␈α⊃find␈α∩the␈α∩mimimal␈α⊃solution␈α∩[Gor 73].␈α∩ The␈α∩evaluation␈α⊃of
␈↓ α←␈↓␈↓αf <= ␈↓λf␈↓␈α∞is␈α∞interpreted␈α
as␈α∞␈↓αf ← ␈↓λf␈↓,␈α∞where␈α
the␈α∞assignment␈α∞is␈α
made␈α∞in␈α∞the␈α
global
␈↓ α←␈↓environment.␈α LISP's␈αsolutions␈αare␈αsufficient␈αfor␈αmost␈αdefinitions,␈αbut␈αa␈αmore
␈↓ α←␈↓general␈α⊃treatment␈α⊃of␈α⊃the␈α⊃ideas␈α⊃involved␈α⊃in␈α⊃function␈α⊃definitions␈α⊃is␈α⊃needed
␈↓ α←␈↓from both practical and theoretical considerations.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α∞mathematical␈α∞treatment␈α∂of␈α∞the␈α∞imperative␈α∞features␈α∂of␈α∞programming
␈↓ α←␈↓languages␈α⊃requires␈α⊃an␈α⊃extension␈α⊃of␈α⊂our␈α⊃model.␈α⊃An␈α⊃essential␈α⊃ingredient␈α⊂of
␈↓ α←␈↓imperatives␈α∞is␈α∞their␈α∞ability␈α∞to␈α∞produce␈α∞side-effects.␈α∞This␈α∞is␈α∞usually␈α
modelled
␈↓ α←␈↓by␈α_including␈α_some␈α_notion␈α↔of␈α_"the␈α_state␈α_of␈α↔the␈α_machine".␈α_In␈α_such␈α↔a
␈↓ α←␈↓development,␈α∪the␈α∪␈↓εD␈↓␈α∪function␈α∪is␈α∀specified␈α∪in␈α∪terms␈α∪of␈α∪an␈α∀expression,␈α∪an
␈↓ α←␈↓environment, and a state.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. The␈α∂␈↓αfoo-fact-baz␈↓␈α∂example␈α∞of␈α∂page 230␈α∂is␈α∞not␈α∂described␈α∂directly␈α∂in␈α∞LISP.
␈↓ α←␈↓␈↓ β∂Can␈αyou␈α
write␈αa␈α
LISP␈αprogram␈α
without␈α␈↓αprog␈↓s␈α
which␈αwill␈α
also␈αexemplify
␈↓ α←␈↓␈↓ β∂this difficulty?
␈↓"β␈↓ α←␈↓␈↓ ¬M␈↓↓4.12 LISP Machines␈↓
␈↓"β␈↓ α←␈↓¬␈↓ β'"in the beginning was the Word"
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ ∂John, ␈↓α1:1␈↓
␈↓"β␈↓ α←␈↓¬␈↓ β'"in␈α⊂the␈α⊂beginning␈α⊂was␈α⊂the␈α⊂Word␈α⊂all␈α⊂right,␈α⊂but␈α⊂it␈α⊂wasn't␈α⊂a␈α∂fixed
␈↓ α←␈↓¬␈↓ β'number of bits"
␈↓ α←␈↓␈↓236 Imperative Constructs in LISP␈↓
_4.12␈↓
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ ε7R. S. Barton, ␈↓αSoftware Engineering␈↓
␈↓"λ␈↓ α←␈↓The␈α∪LISP␈α∪definitions␈α∩and␈α∪expressions␈α∪which␈α∩we␈α∪have␈α∪been␈α∪writing␈α∩are
␈↓ α←␈↓expressed␈αin␈αa␈αlanguage␈αcalled␈αthe␈α␈↓↓meta-language␈↓,␈αand␈αthe␈αLISP␈αexpressions
␈↓ α←␈↓are␈αcalled␈αM-expressions␈α
or␈αM-exprs.␈α The␈α
most␈αprimitive␈αdata␈α
structures␈αof
␈↓ α←␈↓LISP␈αare␈αcalled␈α
S-expressions.␈α We␈αhave␈αseen␈α
that␈αit␈αis␈αpossible␈α
to␈αrepresent
␈↓ α←␈↓M-expressions␈α⊗as␈α∃S-expressions,␈α⊗and␈α⊗indeed,␈α∃that␈α⊗significant␈α⊗results␈α∃are
␈↓ α←␈↓obtained␈α∂from␈α∂such␈α∂a␈α∂mapping.␈α∂ The␈α∂␈↓↓programming␈α∂language␈↓,␈α⊂LISP,␈α∂uses
␈↓ α←␈↓the␈α
S-expr␈α
translation␈α
of␈α
the␈α
LISP␈α
algorithms.␈α
As␈α
we␈α
move␈α
from␈α
the␈α
more
␈↓ α←␈↓formal␈αaspects␈αof␈αLISP␈αto␈αthe␈αpractical␈αdetails␈αof␈αimplementations,␈αwe␈αshould
␈↓ α←␈↓reflect␈α
on␈α
some␈α
of␈α
the␈α
features␈α
of␈α
LISP␈α
which␈α
make␈α
it␈α
a␈α
unique␈α
programming
␈↓ α←␈↓language.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αarguments␈αto␈αLISP␈αprograms␈αare␈αS-exprs␈αand␈αsince␈αwe␈αare␈αwriting
␈↓ α←␈↓LISP␈α
programs␈α
in␈α
S-expr␈α
form,␈α
then␈α
data␈α
and␈α
program␈αare␈α
indistinguishable.
␈↓ α←␈↓Programs␈αmust␈αhave␈αa␈α
very␈αspecial␈αstructure,␈αbut␈α
program␈αand␈αdata␈αare␈α
both
␈↓ α←␈↓S-exprs␈α∞just␈α∞as␈α∞in␈α∞most␈α∞hardware␈α∞machines␈α∞the␈α∞contents␈α∞of␈α∞locations␈α
which
␈↓ α←␈↓contain␈α≡data␈α≥are␈α≡not␈α≡distinguishable␈α≥from␈α≡locations␈α≡which␈α≥contain
␈↓ α←␈↓instructions.␈↓π 26␈↓␈αOn␈α
a␈αtypical␈αcontemporary␈α
machine␈αit␈αis␈α
the␈αway␈αin␈α
which␈αa
␈↓ α←␈↓location␈αis␈αaccessed␈αwhich␈αdetermines␈αthe␈αinterpretation␈αgiven␈αto␈αthe␈αlocation.
␈↓ α←␈↓If␈αa␈αprocessor␈αaccesses␈αthe␈αlocation␈αvia␈αthe␈αprogram␈αcounter,␈αthe␈αcontents␈αare
␈↓ α←␈↓executed␈α∂as␈α∂an␈α∂instruction.␈α∂ That␈α∂same␈α∂location␈α∂can␈α∂usually␈α∂be␈α∂accessed␈α∞as
␈↓ α←␈↓part␈αof␈αa␈αdata␈αmanipulating␈αinstruction.␈α In␈αthat␈αcase,␈αthe␈αcontents␈αare␈α
simply
␈↓ α←␈↓taken␈α
to␈α
be␈α
data.␈α LISP␈α
is␈α
one␈α
of␈α
the␈αfew␈α
high-level␈α
languages␈α
which␈αalso␈α
has
␈↓ α←␈↓this property␈↓π 27␈↓ It gives the programmer exceptional power.
␈↓"β␈↓ α←␈↓␈↓ β'Since␈α∂the␈α∂next␈α∂three␈α⊂chapters␈α∂delve␈α∂more␈α∂deeply␈α⊂into␈α∂implementation
␈↓ α←␈↓details␈α⊂and␈α∂machine␈α⊂organization,␈α∂it␈α⊂is␈α∂useful␈α⊂to␈α∂illuminate␈α⊂the␈α∂similarities
␈↓ α←␈↓between␈α∃LISP␈α∀machines␈α∃and␈α∃traditional␈α∀computers;␈α∃The␈α∃similarities␈α∀are
␈↓ α←␈↓striking.␈α∨ A␈α∨contemporary␈α machine␈α∨is␈α∨a␈α∨computer␈α which␈α∨executes
␈↓ α←␈↓well-behaved␈α≡programs␈α≡segments␈α≡referencing␈α≡very␈α≡well-behaved␈α≡data
␈↓ α←␈↓structures.␈α⊗A␈α∃LISP␈α⊗architecture,␈α∃catering␈α⊗to␈α∃program␈α⊗debugging,␈α⊗has␈α∃a
␈↓ α←␈↓different␈αemphasis.␈αWe␈αwill␈αsee␈αthe␈αtraditional␈αdesign␈αas␈αa␈αspecialization␈αof␈αa
␈↓ α←␈↓LISP design.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 26␈↓A␈α
few␈αmachines␈α
␈↓¬have␈↓␈α
been␈αbuilt␈α
to␈α
enforce␈αa␈α
dichotomy␈αbetween␈α
data
␈↓ α←␈↓and␈αprogram;␈αthe␈αHP3000␈αand␈α
several␈αof␈αthe␈αBurroughs␈αmachines␈α
([Org 73],
␈↓ α←␈↓[Dor 76]) follow this approach.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 27␈↓The␈α
IPL␈α
series␈α
of␈α∞languages␈α
([New 61])␈α
had␈α
this␈α∞property,␈α
however
␈↓ α←␈↓those languages were more reminiscent of assembly language.
␈↓ α←␈↓␈↓4.12␈↓ λILISP Machines 237␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Our␈α∞discussion␈α∞will␈α∞be␈α∞restricted␈α∞to␈α∞common␈α∞features␈α∞of␈α∞contemporary
␈↓ α←␈↓computers;␈αwe␈αwill␈αnot␈αsingle␈α
out␈αone␈αspecific␈αarchitecture.␈↓π 28␈↓␈αThe␈α
basic␈αitem
␈↓ α←␈↓of␈α
information␈α
in␈α
the␈α
computer␈α
will␈α∞be␈α
the␈α
␈↓↓word␈↓;␈α
That␈α
word␈α
will␈α∞consist␈α
of
␈↓ α←␈↓binary bits, zero and one:
␈↓"⊃␈↓ α←␈↓
⊂ααααααααααα⊃
␈↓"␈↓ α←␈↓
~ 01 ... 10 ~
␈↓"␈↓ α←␈↓
%ααααααααααα$
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α∀number␈α∀of␈α∀bits␈α∀in␈α∀a␈α∀word␈α∀will␈α∀not␈α∀be␈α∀too␈α∀relevant␈α∀for␈α∪this
␈↓ α←␈↓discussion,␈α
however␈α
we␈α
will␈α
assume␈α
that␈α
each␈α
word␈α
contains␈α
the␈αsame␈α
number
␈↓ α←␈↓of␈α∩bits.␈α∩ The␈α∩"word"␈α∩is␈α∩our␈α∪basic␈α∩unit␈α∩of␈α∩data.␈α∩ Since␈α∩programs␈α∪will␈α∩be
␈↓ α←␈↓specified␈α∀as␈α∀collections␈α∃of␈α∀words,␈α∀it␈α∀will␈α∃be␈α∀convenient␈α∀to␈α∃introduce␈α∀an
␈↓ α←␈↓information unit for program representation.
␈↓"β␈↓ α←␈↓␈↓ β'In␈α∞most␈α∞machines,␈α∞the␈α∞words␈α
are␈α∞not␈α∞simply␈α∞scattered␈α∞about␈α∞like␈α
loose
␈↓ α←␈↓paper␈α∂on␈α∞a␈α∂desk␈α∞top.␈α∂Rather,␈α∂they␈α∞are␈α∂arranged␈α∞neatly␈α∂like␈α∞the␈α∂pages␈α∂in␈α∞a
␈↓ α←␈↓book.␈α
Each␈α
word␈α
has␈αan␈α
associated␈α
number␈α
called␈αa␈α
its␈α
␈↓↓location␈↓.␈α
We␈αwill␈α
call
␈↓ α←␈↓a sequential collection of computer words a ␈↓↓vector␈↓. For example:
␈↓"⊃␈↓ α←␈↓
⊂αααααααααα⊃
␈↓"␈↓ α←␈↓
100 ~ 10 ... 1 ~
␈↓"␈↓ α←␈↓
εααααααααααλ
␈↓"␈↓ α←␈↓
101 ~ 01 ... 1 ~
␈↓"␈↓ α←␈↓
εααααααααααλ
␈↓"␈↓ α←␈↓
...
␈↓"␈↓ α←␈↓
εααααααααααλ
␈↓"␈↓ α←␈↓
111 ~ 11 ... 0 ~
␈↓"␈↓ α←␈↓
εααααααααααλ
␈↓"␈↓ α←␈↓
1000 ~ 10 ... 1 ~
␈↓"␈↓ α←␈↓
εααααααααααλ
␈↓"␈↓ α←␈↓
...
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α∞numbers␈α
preceeding␈α∞the␈α∞words␈α
are␈α∞the␈α∞locations␈α
of␈α∞the␈α∞words.␈α
In
␈↓ α←␈↓this␈αexample␈αwe␈αhave␈αsketched␈αthe␈αvector␈αfrom␈αlocations␈α␈↓α4␈↓β8␈↓␈α
through␈α␈↓α10␈↓β8␈↓ .␈↓π 29␈↓
␈↓ α←␈↓A program will be stored in memory as a vector of numbers.
␈↓"β␈↓ α←␈↓␈↓ β'Over␈α∂this␈α∂simple␈α∂matrix␈α∂of␈α∞words␈α∂and␈α∂vectors␈α∂the␈α∂computer␈α∞industry
␈↓ α←␈↓has␈αpresented␈αa␈αmyriad␈αof␈αrepresentations␈αfor␈αinstructions␈αand␈αdata␈αitems.␈α
A
␈↓ α←␈↓ubiquitous feature is the execution cycle of a stored program machine.
␈↓"β␈↓ α←␈↓␈↓ β'For␈α∂simplicity,␈α∞we␈α∂will␈α∂assume␈α∞that␈α∂the␈α∞architecure␈α∂is␈α∂a␈α∞single-address
␈↓ α←␈↓instruction␈α∞machine.␈α
This␈α∞implies␈α
that␈α∞each␈α
instruction␈α∞of␈α
our␈α∞machine␈α
will
␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 28␈↓Readers␈αwho␈αare␈αfamiliar␈α
with␈αcomputer␈αarchitecture␈αmay␈αfind␈α
much
␈↓ α←␈↓of␈αthis␈αdiscussion␈αtoo␈αsimple;␈αhowever␈αour␈αdiscussions␈α␈↓¬will␈↓␈αshow␈αrelationships
␈↓ α←␈↓between␈α∞LISP␈α∞and␈α∞other␈α∂architectures,␈α∞and␈α∞raise␈α∞some␈α∂interesting␈α∞questions
␈↓ α←␈↓about machine design.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 29␈↓The␈αnotation␈α"␈↓↓i␈↓β8␈↓"␈αdenotes␈αthe␈αbase␈αeight,␈αor␈α"octal",␈αrepresentation␈αof
␈↓ α←␈↓␈↓↓i␈↓. In the remainder of this section, any numeral is an octal numeral.
␈↓ α←␈↓␈↓238 Imperative Constructs in LISP␈↓
_4.12␈↓
␈↓"β␈↓ α←␈↓specify␈αtwo␈α
parts;␈αan␈α
operation,␈αand␈α
a␈αsingle␈α
operand.␈α Since␈αmany␈α
operations
␈↓ α←␈↓we␈α
wish␈α∞to␈α
perform␈α
expect␈α∞two␈α
operands,␈α
the␈α∞address␈α
format␈α∞may␈α
reference
␈↓ α←␈↓an␈α∂implicit␈α∞operand.␈α∂That␈α∞operand,␈α∂named␈α∂␈↓↓AC␈↓,␈α∞is␈α∂a␈α∞special␈α∂register␈α∂in␈α∞the
␈↓ α←␈↓machine.␈α
For␈α∞example,␈α
an␈α∞instruction␈α
␈↓αADD 100␈↓␈α
would␈α∞mean␈α
form␈α∞the␈α
sum
␈↓ α←␈↓of␈α∂the␈α∂number␈α∞represented␈α∂in␈α∂location␈α∂100␈α∞and␈α∂the␈α∂number␈α∂represented␈α∞in
␈↓ α←␈↓␈↓↓AC␈↓,␈αand␈αplace␈αthe␈αsummation␈αback␈αin␈α␈↓↓AC␈↓.␈↓π 30␈↓␈αThus␈αthe␈αresult␈αof␈αthe␈α
addition
␈↓ α←␈↓is␈α∂"accumulated"␈α∂in␈α∂␈↓↓AC␈↓;␈α∂␈↓↓AC␈↓␈α∂is␈α∂also␈α∂called␈α∂the␈α∂"accumulator."␈α∂Or␈α∞␈↓αJMZ 101␈↓
␈↓ α←␈↓might␈α∩mean␈α⊃"begin␈α∩execution␈α⊃at␈α∩location␈α⊃␈↓α101␈↓␈α∩if␈α⊃the␈α∩contents␈α⊃of␈α∩␈↓↓AC␈↓␈α∩is␈α⊃a
␈↓ α←␈↓representation␈αof␈αzero;␈αotherwise␈αexecute␈αthe␈αinstruction␈αfollowing␈αthe␈α␈↓αJMZ␈↓."
␈↓ α←␈↓We␈αwill␈αpostpone␈αmore␈αdetailed␈αexamination␈αof␈αspecific␈αmachine␈αinstructions
␈↓ α←␈↓until␈α∃Chapter 6.␈α∃ We␈α∃wish␈α∃to␈α∃concentrate␈α∃on␈α∃the␈α∃mechanisms␈α⊗which␈α∃a
␈↓ α←␈↓computer uses to execute a program.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α␈↓↓program␈αcounter␈↓,␈αdenoted␈αby␈α␈↓↓PC␈↓,␈αis␈αused␈αto␈αcontrol␈αthe␈αexecution
␈↓ α←␈↓path␈αof␈αa␈αprogram.␈αIf␈α␈↓↓PC␈↓␈α
references␈αan␈αinstruction␈αlike␈αthe␈α␈↓αADD␈↓␈αabove,␈α
then
␈↓ α←␈↓the␈α⊂machine␈α⊂executes␈α⊂the␈α⊂addition␈α⊂and␈α⊂then␈α⊂prepares␈α⊂to␈α⊂execute␈α⊃the␈α⊂next
␈↓ α←␈↓instruction␈α⊗in␈α⊗the␈α∃sequence.␈α⊗The␈α⊗details␈α∃of␈α⊗the␈α⊗␈↓αJMZ␈↓␈α∃are␈α⊗a␈α⊗bit␈α∃more
␈↓ α←␈↓complicated.␈α
If␈α
the␈α
contents␈αof␈α
the␈α
␈↓↓AC␈↓␈α
is␈α(a␈α
representation␈α
of)␈α
zero␈α
then␈αthe
␈↓ α←␈↓machine␈α∂will␈α∞place␈α∂␈↓α101␈↓␈α∂in␈α∞the␈α∂␈↓↓PC␈↓␈α∂so␈α∞that␈α∂it␈α∂can␈α∞execute␈α∂the␈α∂instruction␈α∞in
␈↓ α←␈↓location␈α
␈↓α101␈↓;␈α
otherwise␈α
the␈α∞instruction␈α
following␈α
the␈α
␈↓αJMZ␈↓␈α
is␈α∞executed.␈α
Our
␈↓ α←␈↓intent␈α⊗should␈α⊗be␈α⊗clearer␈α⊗now,␈α⊗but␈α⊗we␈α⊗can␈α⊗do␈α⊗better␈α⊗with␈α⊗a␈α∃diagram.
␈↓ α←␈↓Something like:
␈↓"∀␈↓ α←␈↓␈↓ ¬↔␈↓↓l:␈↓α␈↓ ¬;C(␈↓↓IR␈↓α) ← C(C(␈↓↓PC␈↓α))
␈↓"β␈↓ α←␈↓α␈↓ ¬↔␈↓ ¬;C(␈↓↓PC␈↓α) ← C(␈↓↓PC␈↓α) + 1
␈↓"β␈↓ α←␈↓α␈↓ ¬↔␈↓ ¬; ␈↓↓execute ␈↓αC(␈↓↓IR␈↓α)
␈↓"β␈↓ α←␈↓α␈↓ ¬↔␈↓ ¬;␈↓↓go to l
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α∪notation,␈α∪␈↓αC(x)␈↓,␈α∪is␈α∪read␈α∪"contents␈α∪of␈α∪␈↓αx␈↓";␈α∪the␈α∪arrow␈α∪"←"␈α∪is␈α∩read
␈↓ α←␈↓"replaced␈α
by".␈α The␈α
␈↓↓IR␈↓,␈α
or␈α␈↓↓Instruction␈α
register␈↓,␈α
is␈αan␈α
internal␈α
register␈αused␈α
to
␈↓ α←␈↓hold␈α
the␈α
instruction␈α
we␈α
are␈αto␈α
execute.␈α
So␈α
step-by-step␈α
the␈α
diagram␈αreads:␈α
"the
␈↓ α←␈↓contents␈αof␈α␈↓↓IR␈↓␈αare␈αreplaced␈αby␈α
the␈αcontents␈αof␈αthe␈αcontents␈αof␈α␈↓↓PC␈↓";␈α
that␈αgets
␈↓ α←␈↓the␈α
next␈α∞instruction.␈α
Next,␈α∞"contents␈α
of␈α
␈↓↓PC␈↓␈α∞are␈α
replaced␈α∞by␈α
contents␈α∞of␈α
␈↓↓PC␈↓
␈↓ α←␈↓plus␈α␈↓α1␈↓.␈α Note␈α
that␈α␈↓↓PC␈↓␈αis␈α
incremented␈α␈↓¬before␈↓␈αexecution␈α
of␈αthe␈αinstruction.␈αIf␈α
we
␈↓ α←␈↓incremented␈α∂␈↓↓PC␈↓␈α∂␈↓¬after␈↓␈α⊂the␈α∂execution␈α∂of␈α⊂the␈α∂instruction,␈α∂and␈α⊂the␈α∂instruction
␈↓ α←␈↓were␈α≥a␈α≤␈↓αJMZ␈↓-type␈α≥instruction,␈α≤then␈α≥the␈α≤␈↓↓PC␈↓␈α≥might␈α≤get␈α≥a␈α≤spurious
␈↓ α←␈↓incrementation.␈α Finally␈αwe␈αexecute␈αthe␈αinstruction␈αand␈αthen␈αgo␈αback␈αto␈αfetch
␈↓ α←␈↓the␈αnext␈αinstruction.␈αEmbellishments␈αof␈αthis␈αbasic␈αcycle␈αwill␈αget␈αus␈αto␈αa␈αLISP
␈↓ α←␈↓machine.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αwill␈αassume␈αthat␈αany␈αsuch␈αword␈αcan␈αcontain␈αan␈αinstruction␈αor␈αdata,
␈↓ α←␈↓and␈α↔the␈α⊗interpretation␈α↔of␈α⊗a␈α↔word␈α⊗depends␈α↔on␈α⊗the␈α↔way␈α↔the␈α⊗execution
␈↓ α←␈↓mechanism␈αof␈αthe␈αmachine␈αaccesses␈αthe␈α
word.␈α For␈αexample,␈αthe␈αcontents␈αof␈α
a
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 30␈↓Actually␈αthe␈αoperation␈α"␈↓αADD␈↓"␈αwill␈αnot␈αappear␈αin␈αthe␈αmemory;␈αrather,
␈↓ α←␈↓a␈αbit-pattern␈αwill␈αappear␈αthere␈α
and␈αthe␈αcomputer␈αmust␈αinterpret␈α
that␈αpattern
␈↓ α←␈↓to mean "␈↓αADD␈↓".
␈↓ α←␈↓␈↓4.12␈↓ λILISP Machines 239␈↓
␈↓"β␈↓ α←␈↓location␈α⊃can␈α⊃be␈α⊃used␈α∩both␈α⊃as␈α⊃an␈α⊃instruction␈α∩and␈α⊃as␈α⊃data;␈α⊃if␈α∩location␈α⊃␈↓α101␈↓
␈↓ α←␈↓contained␈α
the␈α
representation␈α
of␈α
the␈α
instruction␈α
␈↓αADD 101␈↓,␈α
then␈α
the␈α
execution
␈↓ α←␈↓cycle␈αfor␈αthat␈αinstruction␈αwould␈αinvolve␈αlocation␈α␈↓α101␈↓␈αin␈αboth␈αroles;␈αinstruction
␈↓ α←␈↓␈↓¬and␈↓␈α∩data.␈α⊃ This␈α∩dual␈α∩role␈α⊃of␈α∩instruction␈α⊃and␈α∩data␈α∩occurs␈α⊃in␈α∩less␈α∩ad␈α⊃hoc
␈↓ α←␈↓situations.␈α⊃ An␈α⊃assembler␈α⊃converts␈α⊃an␈α⊃external␈α⊃string␈α⊃of␈α⊃characters␈α⊃into␈α⊃a
␈↓ α←␈↓vector␈α∩of␈α∩bit␈α∩patterns␈α∩which␈α∩a␈α∩loader␈α∩finally␈α∩converts␈α∩into␈α∪real␈α∩machine
␈↓ α←␈↓instructions.␈α⊂ Many␈α⊂of␈α∂the␈α⊂bit␈α⊂patterns␈α⊂which␈α∂the␈α⊂loader␈α⊂receives␈α⊂from␈α∂an
␈↓ α←␈↓assembler␈αare␈α
simply␈αmachine␈α
"instructions",␈αhowever␈α
the␈αloader␈α
acts␈αon␈α
them
␈↓ α←␈↓as data items; it does not execute them.
␈↓"β␈↓ α←␈↓␈↓ β'Several␈α
machines␈α
allow␈α
certain␈α
embellishments␈α
of␈α
the␈α
operand␈α
field␈α
of
␈↓ α←␈↓an␈α
instruction;␈α∞indirect␈α
addressing␈α
is␈α∞one␈α
example.␈α
If␈α∞an␈α
operand␈α∞is␈α
fetched
␈↓ α←␈↓indirectly,␈αthat␈αmeans␈αthat␈αcontents␈αof␈αthe␈αoperand␈αfield␈αare␈αnot␈αused␈αdirectly
␈↓ α←␈↓as␈α∂data,␈α∂but␈α∂are␈α⊂interpreted␈α∂as␈α∂a␈α∂further␈α⊂address␈α∂and␈α∂the␈α∂contents␈α⊂of␈α∂that
␈↓ α←␈↓further␈αaddress␈αare␈αexamined.␈αIf␈αthe␈αmachine␈αallows␈αarbitrarily␈αdeep␈αindirect
␈↓ α←␈↓addressing,␈α
that␈α
further␈α
address␈α
is␈α∞examined␈α
to␈α
see␈α
if␈α
it␈α∞specifies␈α
additional
␈↓ α←␈↓indirection.␈α∞ This␈α
chain␈α∞of␈α∞indirect␈α
addressing␈α∞must␈α
terminate␈α∞with␈α∞a␈α
"real"
␈↓ α←␈↓address␈α∞if␈α∞the␈α∞instruction␈α∞which␈α∞instigated␈α∞this␈α∞mess␈α∞is␈α∞ever␈α∞to␈α∞complete␈α∞its
␈↓ α←␈↓execution.
␈↓"β␈↓ α←␈↓␈↓ β'For␈α_example,␈α_let␈α_a␈α_modifier␈α_␈↓αi␈↓␈α_indicate␈α_indirect␈α_addressing;␈α↔then
␈↓ α←␈↓␈↓αADD 10 i␈↓␈αmeans␈αdon't␈αadd␈αthe␈αcontents␈αof␈αlocation␈α␈↓α10␈↓␈αto␈α␈↓↓AC␈↓␈αbut␈αlook␈αat␈αthe
␈↓ α←␈↓contents␈αof␈α
location␈α␈↓α10␈↓␈α
to␈αdetermine␈α
the␈αaddress.␈α
If␈αthe␈α
contents␈αof␈αlocation␈α
␈↓α10␈↓
␈↓ α←␈↓is␈α⊂␈↓α2␈↓,␈α∂then␈α⊂the␈α∂contents␈α⊂of␈α∂location␈α⊂␈↓α2␈↓␈α∂will␈α⊂be␈α∂added␈α⊂to␈α∂␈↓↓AC␈↓.␈α⊂ If␈α⊂location␈α∂␈↓α10␈↓
␈↓ α←␈↓contained␈α⊂␈↓α2 i␈↓,␈α⊂then␈α∂indirect␈α⊂cycle␈α⊂continues␈α⊂and␈α∂the␈α⊂contents␈α⊂of␈α⊂location␈α∂␈↓α2␈↓
␈↓ α←␈↓would␈αbe␈αinspected␈αfor␈αan␈αaddress.␈α However␈αif␈αthe␈αcontents␈αof␈αlocation␈α␈↓α10␈↓␈αis
␈↓ α←␈↓␈↓α10 i␈↓,␈αthen␈αwe␈αwill␈αcontinue␈αto␈αfetch␈α
the␈αcontents␈αof␈αlocation␈α␈↓α10␈↓,␈αlooking␈αfor␈α
an
␈↓ α←␈↓operand which will not be forthcoming.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αshould␈αexamine␈αseveral␈αof␈αthe␈αconventions␈αof␈αthis␈α
machine,␈αasking
␈↓ α←␈↓if␈α
they␈α
are␈α
fundamental␈α
to␈α
the␈α
architecture␈α
or␈α
are␈α
more␈α
whims␈α∞or␈α
historical
␈↓ α←␈↓accidents.␈α⊂After␈α⊂all␈α⊂the␈α∂original␈α⊂Von Neumann␈α⊂machines␈α⊂were␈α∂numerically
␈↓ α←␈↓oriented.␈↓π 31␈↓␈αWhy␈αshould␈α
data␈αbe␈αnumerical?␈α
Why␈αshould␈αthe␈α
instructions␈αbe
␈↓ α←␈↓sequential?␈α≤ Why␈α≤do␈α≥we␈α≤need␈α≤accumulators?␈α≤Let's␈α≥examine␈α≤indirect
␈↓ α←␈↓addressing more closely.
␈↓"β␈↓ α←␈↓␈↓ β'Indirect␈α∩addressing␈α∩is␈α∩actually␈α∩a␈α⊃special␈α∩case␈α∩of␈α∩an␈α∩interesting␈α⊃idea:
␈↓ α←␈↓instead␈αof␈αrequiring␈αthat␈αthe␈αoperand␈αbe␈αa␈αdata␈αitem,␈αlet␈αthe␈αoperand␈αspecify
␈↓ α←␈↓a␈α∞further␈α∂operation.␈α∞There␈α∂are␈α∞several␈α∂problems␈α∞with␈α∂this␈α∞scheme,␈α∂none␈α∞of
␈↓ α←␈↓which␈α⊃are␈α⊃insurmountable␈α⊃as␈α⊃we␈α⊂shall␈α⊃see.␈α⊃ However,␈α⊃the␈α⊃most␈α⊂important
␈↓ α←␈↓problem␈α⊂for␈α⊂our␈α∂current␈α⊂concerns␈α⊂is:␈α∂if␈α⊂an␈α⊂operand␈α∂may␈α⊂specify␈α⊂a␈α∂further
␈↓ α←␈↓operation,␈α∂then␈α∂we␈α∂must␈α∂have␈α∂a␈α∂way␈α∂of␈α∂terminating␈α⊂the␈α∂"get-new-operand"
␈↓ α←␈↓fetch. We saw two solutions in the indirect address paradigm:
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ An␈αinstruction␈αwill␈αnot␈αinvoke␈α
indirection␈αif␈αthe␈α␈↓αi␈↓␈αmodifier␈αis␈α
not␈αpresent
␈↓ α←␈↓␈↓ β∂in the instruction.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 31␈↓[Car 75]␈α⊃is␈α⊃interesting␈α⊃paper␈α⊃on␈α⊃the␈α⊃practical␈α⊃design␈α∩proposed␈α⊃by
␈↓ α←␈↓Alan␈α⊂Turing␈α⊂(of␈α⊂the␈α⊃theoretical␈α⊂Turing␈α⊂machine␈α⊂fame);␈α⊃Turing's␈α⊂machine
␈↓ α←␈↓had a much more non-numerical flavor.
␈↓ α←␈↓␈↓240 Imperative Constructs in LISP␈↓
_4.12␈↓
␈↓"β␈↓ α←␈↓␈↓↓2.␈↓ An␈α⊃indirect␈α⊃addressing␈α⊃cycle␈α⊃will␈α⊃terminate␈α⊃the␈α⊃operand␈α⊃fetch␈α⊃when␈α⊂it
␈↓ α←␈↓␈↓ β∂comes across a reference which does ␈↓↓not␈↓ contain the ␈↓αi␈↓ modifier.
␈↓"∀␈↓ α←␈↓␈↓ β'These two solutions generalize nicely:
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ Have␈αoperations␈α
which␈αlook␈α
no␈αfurther.␈α
That␈αis,␈α
their␈αoperand␈α
may␈α␈↓↓look␈↓
␈↓ α←␈↓␈↓ β∂like␈αan␈αoperation,␈αbut␈αit␈αis␈αnot␈αto␈αbe␈αtaken␈αas␈αsuch␈αin␈αthe␈αcurrent␈αcontext.
␈↓ α←␈↓␈↓ β∂This is the effect of the ␈↓αquote␈↓ operator.
␈↓"∞␈↓ α←␈↓␈↓ β'For example:
␈↓"⊃␈↓ α←␈↓
⊂ααααααααααα⊃
␈↓"␈↓ α←␈↓
loc ~ ADD loc ~
␈↓"␈↓ α←␈↓
%ααααααααααα$
␈↓"∀␈↓ α←␈↓␈↓ β'When we execute ␈↓
loc␈↓, we also access ␈↓
loc␈↓ as data.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ Supply␈α_each␈α_word␈α_with␈α_an␈α_indicator␈α_that␈α_distinguishes␈α_data␈α↔from
␈↓ α←␈↓␈↓ β∂instruction.␈α∂The␈α∂operand␈α∂fetch␈α∂would␈α∂terminate␈α∂when␈α∂a␈α∂"real"␈α∂operand
␈↓ α←␈↓␈↓ β∂was fetched.
␈↓"β␈↓ α←␈↓␈↓ β'As␈αwe␈αhave␈αalready␈αnoted,␈αsome␈αmachines␈αdo␈αsupply␈αa␈αdata-instruction
␈↓ α←␈↓indicator␈α⊂(though␈α∂not␈α⊂for␈α∂the␈α⊂purpose␈α∂we␈α⊂have␈α∂ascribed␈α⊂to␈α∂it);␈α⊂and␈α⊂as␈α∂we
␈↓ α←␈↓have␈α∞further␈α∞noted,␈α∂we␈α∞do␈α∞␈↓↓not␈↓␈α∂plan␈α∞to␈α∞follow␈α∂this␈α∞policy.␈α∞ The␈α∂freedom␈α∞to
␈↓ α←␈↓move␈αfreely␈αbetween␈αdata␈αand␈αinstruction␈αis␈αvery␈αpowerful,␈αalbeit␈αdangerous.
␈↓ α←␈↓We␈αwould␈αlike␈α
uniform␈αaccess␈αto␈αprogram␈α
elements␈αand␈αdata␈α
elements.␈αThat
␈↓ α←␈↓is,␈α
an␈α
instruction␈α
can␈α
reference␈α
either␈α
data␈α
locations␈α
or␈α∞instruction␈α
locations.
␈↓ α←␈↓In terms of our current machine this means:
␈↓"∀␈↓ α←␈↓␈↓ β≠If␈αa␈αlocation␈αis␈αreferenced␈αby␈αthe␈α␈↓↓PC␈↓␈αthen␈αits␈αcontents␈αis␈αdecoded␈αas
␈↓ α←␈↓␈↓ β≠an␈α⊂instruction.␈α⊂If␈α∂a␈α⊂location␈α⊂is␈α⊂referenced␈α∂as␈α⊂an␈α⊂operand␈α⊂then␈α∂its
␈↓ α←␈↓␈↓ β≠contents is taken as data.
␈↓"∀␈↓ α←␈↓␈↓ β'We␈α∃want␈α∀similar␈α∃flexibility␈α∀in␈α∃a␈α∀LISP␈α∃machine.␈α∃What␈α∀"arbitrary"
␈↓ α←␈↓decisions␈α
in␈αthe␈α
current␈αarchitecture␈α
should␈αwe␈α
replace?␈α The␈α
data␈α
items␈αare
␈↓ α←␈↓particularly␈αpuny.␈α As␈αan␈αinitial␈αgeneralization,␈αwe␈αmight␈αconsider␈αa␈α"vector"
␈↓ α←␈↓machine.␈αHere,␈αthe␈αbasic␈αdata␈αunits␈α
would␈αbe␈αvectors␈αof␈αnumbers.␈αThe␈α
would
␈↓ α←␈↓lead␈α∞to␈α
an␈α∞interesting␈α∞generalization:␈α
the␈α∞␈↓αADD␈↓␈α
operation␈α∞might␈α∞now␈α
specify
␈↓ α←␈↓the␈α∞component-wise␈α∞addition␈α
of␈α∞two␈α∞vectors,␈α
etc... .␈α∞Also␈α∞programs␈α∞would␈α
be
␈↓ α←␈↓representable␈α∨as␈α∨data␈α∨items,␈α∨since␈α∨they␈α∨are␈α∨vectors.␈α∨ That's␈α≡some
␈↓ α←␈↓improvement.␈↓π 32␈↓␈α
But␈α
data␈α
and␈α
programs␈α
are␈α
still␈α
represented␈α
linearly,␈αand␈α
the
␈↓ α←␈↓data items are all representations of numbers. Surely we can do better.
␈↓"β␈↓ α←␈↓␈↓ β'First,␈α∩realize␈α⊃that␈α∩most␈α⊃programs␈α∩are␈α⊃␈↓¬not␈↓␈α∩linear;␈α⊃they␈α∩typically␈α⊃have
␈↓ α←␈↓jumps␈α⊃and␈α⊃subroutine␈α⊃calls.␈α∩A␈α⊃linearity␈α⊃is␈α⊃imposed␈α⊃on␈α∩program␈α⊃structure
␈↓ α←␈↓because␈α∪of␈α∪the␈α∪gratuitous␈α∪incrementation␈α∪of␈α∪the␈α∪program␈α∀counter.␈α∪ That
␈↓ α←␈↓linearity␈α⊂becomes␈α⊂particularly␈α∂bothersome␈α⊂when␈α⊂one␈α∂has␈α⊂to␈α⊂patch␈α∂machine
␈↓ α←␈↓language␈α∂programs.␈α∂ Note␈α∂also␈α∂that␈α∞much␈α∂data␈α∂is␈α∂non-numerical␈α∂in␈α∞nature;
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 32␈↓This generalization is similar to that available in APL.
␈↓ α←␈↓␈↓4.12␈↓ λILISP Machines 241␈↓
␈↓"β␈↓ α←␈↓and␈α
much␈α
of␈αthat␈α
information␈α
has␈α
more␈αcomplex␈α
structure␈α
than␈αthat␈α
supplied
␈↓ α←␈↓by␈α∞a␈α∞"vector"␈α∞machine.␈α∞"Ah,␈α∞Hah!",␈α
you␈α∞exclaim;␈α∞we␈α∞might␈α∞want␈α∞vectors,␈α
of
␈↓ α←␈↓vectors,␈α∞of␈α∞... vectors,␈α∞because␈α∞that's␈α∞a␈α∞more␈α∞general␈α∞structuring␈α∞of␈α∂data␈α∞and
␈↓ α←␈↓could be used to reflect the non-linear structure of programs, and ...
␈↓"β␈↓ α←␈↓␈↓ β'Excellent! Now you're ready for a LISP machine!
␈↓"β␈↓ α←␈↓␈↓ β' The␈α⊂processor␈α⊂of␈α⊂a␈α⊂"LISP␈α∂machine"␈α⊂is␈α⊂␈↓αeval␈↓.␈↓π 33␈↓␈α⊂If␈α⊂␈↓αeval␈↓␈α⊂references␈α∂an
␈↓ α←␈↓S-expr␈α∀via␈α∀its␈α∀"program␈α∀counter",␈α∃then␈α∀that␈α∀S-expr␈α∀is␈α∀decoded␈α∃via␈α∀the
␈↓ α←␈↓internals␈αof␈α␈↓αeval␈↓.␈α If␈αan␈αS-expr␈αis␈αreferenced␈αas␈αan␈αargument,␈αthen␈αit␈αis␈αtaken
␈↓ α←␈↓as␈α∞data.␈↓π 34␈↓␈α∞The␈α∞identity␈α∞of␈α∞program␈α∞and␈α∞data␈α∞is␈α∞not␈α∞fixed␈α∞even␈α∂within␈α∞the
␈↓ α←␈↓execution;␈α∂a␈α∂LISP␈α∂program␈α∞can␈α∂create␈α∂a␈α∂data␈α∞structure␈α∂which␈α∂can␈α∂then␈α∞be
␈↓ α←␈↓executed␈αeither␈αexplicitly,␈αby␈αappearing␈αas␈αan␈αargument␈αto␈α␈↓αeval␈↓,␈αor␈αimplicitly,
␈↓ α←␈↓by␈αappearing␈αin␈αthe␈αfunction-position␈αof␈αan␈αapplication.␈α A␈αLISP␈αmachine␈αis
␈↓ α←␈↓a␈αgeneralization␈αof␈αthe␈αsimple␈αcomputer.␈α The␈αoperations␈αwhich␈αget␈αthe␈αnext
␈↓ α←␈↓instruction␈α∪or␈α∪get␈α∪the␈α∪next␈α∪data␈α∪object,␈α∪are␈α∪more␈α∪complex␈α∪since␈α∪neither
␈↓ α←␈↓program␈α⊃nor␈α⊃data␈α∩is␈α⊃sequentially␈α⊃related.␈α∩ The␈α⊃next␈α⊃chapters␈α∩will␈α⊃discuss
␈↓ α←␈↓implementations of LISP structures on traditional computers.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∞simplest␈α∞way␈α∞to␈α∞communicate␈α∞with␈α∞such␈α∞a␈α∞machine␈α∞is␈α∞to␈α∞read␈α∞an
␈↓ α←␈↓S-expr␈α~translation␈α≠of␈α~a␈α~LISP␈α≠expression␈α~into␈α~memory,␈α≠evaluate␈α~the
␈↓ α←␈↓expression,␈α∩and␈α∩print␈α∪the␈α∩result.␈α∩Several␈α∪implementations␈α∩of␈α∩LISP␈α∪use␈α∩a
␈↓ α←␈↓variant of this "␈↓αread-eval-print␈↓" loop:
␈↓"∀␈↓ α←␈↓α␈↓ βoprog[[]
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ ∧+a␈↓ ∧Cprint[eval[read[];( )]];
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ ∧+␈↓ ∧Cgo[a]]
␈↓"∀␈↓ α←␈↓Note␈α∞the␈α∂similarity␈α∞with␈α∞␈↓αloop␈↓␈α∂on␈α∞page 210␈α∞and␈α∂the␈α∞basic␈α∞execution␈α∂cycle␈α∞of
␈↓ α←␈↓our simple computer.␈↓π 35␈↓
␈↓"β␈↓ α←␈↓␈↓ β'A␈α
LISP␈α
machine␈αis␈α
a␈α
calculator␈αusing␈α
list-notation␈α
input␈αand␈α
converting
␈↓ α←␈↓the␈α∪output␈α∪from␈α∪LISP␈α∀programs␈α∪to␈α∪list-notation␈α∪wherever␈α∀possible.␈α∪ But
␈↓ α←␈↓internally,␈α∩all␈α∩manipulations␈α∩are␈α∩done␈α∩on␈α∩the␈α∩S-expression␈α⊃representation.
␈↓ α←␈↓LISP␈αwill␈αallow␈αyou␈αto␈αmanipulate␈αthe␈α␈↓¬representation␈↓␈αof␈αthe␈αlists.␈α The␈αLISP
␈↓ α←␈↓S-expr␈α∞operations␈α
like␈α∞␈↓αcar,␈α∞cdr␈↓,␈α
and␈α∞␈↓αcons␈↓␈α∞operate␈α
without␈α∞complaint␈α∞on␈α
lists,
␈↓ α←␈↓even␈α∞though␈α∞we␈α∞have␈α∂repeatedly␈α∞said␈α∞that␈α∞these␈α∞functions␈α∂are␈α∞S-expression
␈↓ α←␈↓functions.␈α∞ LISP's␈α∞attitude␈α∞toward␈α∞␈↓αcar␈↓␈α∞and␈α∞␈↓αcdr␈↓␈α∞is␈α∞similar␈α∞to␈α∞its␈α∞treatment␈α∞of
␈↓ α←␈↓␈↓αgo␈↓s and labels: these are useful primitives from which to build tools.
␈↓"β␈↓ α←␈↓␈↓ β'One␈α
effect␈α
of␈α
this␈α
generality␈α
is␈α
to␈α
present␈α
the␈α
unwary␈α
LISP␈α
user␈αwith␈α
an
␈↓ α←␈↓incredible␈α∞potentiality␈α∂for␈α∞generating␈α∂programming␈α∞errors.␈α∂ The␈α∞alternative,
␈↓ α←␈↓to␈α≠require␈α≠declarations␈α≠for␈α≠all␈α≠data␈α≠objects␈α≠and␈α≠disallow␈α≤run␈α≠time
␈↓ α←␈↓modifications␈α⊂to␈α⊃programs␈α⊂offers␈α⊃a␈α⊂debugging␈α⊃tool␈α⊂of␈α⊃some␈α⊂power.␈α⊃ If␈α⊂we
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 33␈↓Several␈α⊗"LISP␈α∃machines"␈α⊗have␈α⊗been␈α∃proposed␈α⊗or␈α⊗actually␈α∃built
␈↓ α←␈↓including: [Got 74], [Gre 74], [Deu 73], and [Bar 71]
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 34␈↓This goes for ␈↓αfunarg␈↓s as well: until they're applied, they're data.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 35␈↓The␈α↔details␈α↔of␈α↔␈↓αread␈↓␈α↔and␈α⊗␈↓αprint␈↓,␈α↔two␈α↔of␈α↔the␈α↔input␈α↔and␈α⊗output
␈↓ α←␈↓operations in LISP, are discussed in the next chapter.
␈↓ α←␈↓␈↓242 Imperative Constructs in LISP␈↓
_4.12␈↓
␈↓"β␈↓ α←␈↓write␈αprograms␈αsuch␈αthat␈αthe␈αtype␈αof␈αeach␈αdata␈αobject␈αmust␈αbe␈αgiven,␈↓π 36␈↓␈αand
␈↓ α←␈↓if␈α⊃we␈α⊃write␈α⊃each␈α⊂function␈α⊃such␈α⊃that␈α⊃the␈α⊂process␈α⊃of␈α⊃binding␈α⊃arguments␈α⊂to
␈↓ α←␈↓values␈αmust␈αcheck␈αthat␈αthe␈αtype␈αof␈αthe␈αactual␈αparameter␈αagrees␈αwith␈αthe␈αtype
␈↓ α←␈↓of␈αthe␈αparameter␈αof␈αthe␈αfunction,␈αthen␈αa␈αvery␈αlarge␈αnumber␈αof␈αprogramming
␈↓ α←␈↓errors␈α∂can␈α∂be␈α∂located␈α∞almost␈α∂as␈α∂soon␈α∂as␈α∂they␈α∞occur.␈α∂ You␈α∂can␈α∂think␈α∂of␈α∞the
␈↓ α←␈↓parameter-passing␈α⊂mechanism␈α⊂as␈α⊂a␈α∂"fire-wall,"␈α⊂which␈α⊂will␈α⊂help␈α⊂contain␈α∂the
␈↓ α←␈↓deviant behavior to within the particular function.
␈↓"β␈↓ α←␈↓␈↓ β'Any␈α⊂function␈α∂which␈α⊂gets␈α⊂called␈α∂has␈α⊂a␈α∂right␈α⊂to␈α⊂expect␈α∂that␈α⊂it␈α⊂will␈α∂be
␈↓ α←␈↓called␈αwith␈αreasonable␈αvalues.␈αPart␈αof␈αbeing␈αreasonable␈αis␈αhaving␈αthe␈αcorrect
␈↓ α←␈↓number␈αof␈αarguments␈α
given␈αto␈αit;␈α
␈↓αcons[A; B; C]␈↓␈αis␈αas␈α
bad␈αas␈α␈↓αcons[A]␈↓.␈α
Part␈αof
␈↓ α←␈↓being␈α⊃reasonable␈α⊃is␈α⊂having␈α⊃the␈α⊃right␈α⊃kind␈α⊂of␈α⊃arguments;␈α⊃we␈α⊃don't␈α⊂expect
␈↓ α←␈↓results␈α
from␈α␈↓αsub1[A]␈↓.␈α
We␈α
should␈αnot␈α
expect␈α
that␈αthe␈α
functions␈αare␈α
sufficiently
␈↓ α←␈↓omniscient␈α
to␈αconvert␈α
an␈α
argument␈αof␈α
the␈α
wrong␈αkind␈α
into␈α
a␈αproper␈α
one.␈α If␈α
a
␈↓ α←␈↓function␈α
is␈α
written␈α
to␈α
expect␈α
an␈α
argument␈α
of␈α
type␈α
␈↓αpolynomial␈↓␈α
then␈α
it␈α
should
␈↓ α←␈↓complain␈α⊂if␈α⊃it␈α⊂receives␈α⊂an␈α⊃argument␈α⊂of␈α⊂type␈α⊃␈↓αlist␈↓␈α⊂even␈α⊂though␈α⊃the␈α⊂current
␈↓ α←␈↓representation for polynomials might be special instances of lists.
␈↓"β␈↓ α←␈↓␈↓ β'Many␈α
programming␈α
languages␈α
␈↓¬do␈↓␈αoffer␈α
such␈α
omniscience.␈α
Fortran␈αcalls
␈↓ α←␈↓this␈α∃service␈α⊗"conversion";␈α∃Algol68␈α⊗calls␈α∃it␈α⊗␈↓↓"coercion"␈↓.␈α∃ However␈α⊗if␈α∃each
␈↓ α←␈↓function␈α
accepts␈α
whatever␈α
argument␈α
it␈α
is␈α
given␈α
and␈α
attempts␈α
to␈α
use␈α
it␈α
in␈α
its
␈↓ α←␈↓computation,␈αthen␈αthe␈αfirst␈αindication␈αof␈αtrouble␈αwill␈αoccur␈αwhen␈αa␈αprimitive
␈↓ α←␈↓function␈α⊃is␈α⊂called␈α⊃and␈α⊂causes␈α⊃some␈α⊂error␈α⊃deep␈α⊂within␈α⊃the␈α⊂implementation.
␈↓ α←␈↓Typically␈α∀this␈α∀indication␈α∀of␈α∀error␈α∀is␈α∪way␈α∀past␈α∀the␈α∀actual␈α∀source␈α∀of␈α∪the
␈↓ α←␈↓difficulty.␈α⊂The␈α∂alternative␈α⊂is␈α∂to␈α⊂explicitly␈α∂code␈α⊂tests␈α∂into␈α⊂the␈α∂entry␈α⊂code␈α∂of
␈↓ α←␈↓each␈α⊂function␈α⊂definition;␈α⊂but␈α⊂that's␈α∂an␈α⊂expensive␈α⊂use␈α⊂of␈α⊂the␈α∂programmer's
␈↓ α←␈↓time␈αand␈αcomputation␈αtime.␈α What␈αtypically␈αhappens␈αis␈αthat␈αthe␈αtests␈αare␈αleft
␈↓ α←␈↓out and intensive debugging soon follows.
␈↓"β␈↓ α←␈↓␈↓ β'As␈α∂with␈α∂most␈α∂areas␈α∂of␈α∞programming,␈α∂coercion␈α∂is␈α∂not␈α∂a␈α∞black-or-white
␈↓ α←␈↓issue.␈α A␈αstrong␈αtype␈αstructure␈αcan␈αhinder␈αas␈αwell␈αas␈αhelp.␈αRequiring␈αexplicit
␈↓ α←␈↓declarations␈α⊂and␈α∂directions␈α⊂for␈α⊂conversion␈α∂is␈α⊂frequently␈α⊂annoying.␈α∂ Several
␈↓ α←␈↓important␈α∪programming␈α∪tasks␈α∪are␈α∪type-free.␈α∪In␈α∪particular,␈α∪the␈α∪debugging
␈↓ α←␈↓programs␈α⊃must␈α⊃be␈α⊃able␈α⊃to␈α⊃freely␈α⊃access␈α⊃all␈α⊃parts␈α⊃of␈α⊃the␈α∩representation␈α⊃of
␈↓ α←␈↓program␈αand␈αdata␈αwithout␈αregard␈αfor␈αtype.␈α To␈αmake␈αdebugging␈αmeaningful,
␈↓ α←␈↓such␈αprograms␈αmust␈α␈↓¬modify␈↓␈αexisting␈αstructures,␈αchanging␈αdata␈α
structures␈αand
␈↓ α←␈↓programs.␈α→When␈α→dealing␈α→with␈α~large␈α→complex␈α→computations,␈α→it␈α~is␈α→not
␈↓ α←␈↓acceptable␈α∃to␈α∀edit␈α∃programs,␈α∀recompile␈α∃them,␈α∀and␈α∃reinitialize␈α∃the␈α∀whole
␈↓ α←␈↓computation. More sophisticated debugging techniques must be developed.
␈↓"β␈↓ α←␈↓␈↓ β'LISP's␈α∞position␈α
is␈α∞that␈α
it␈α∞is␈α∞the␈α
user's␈α∞responsibility␈α
to␈α∞handle␈α∞all␈α
type
␈↓ α←␈↓restrictions␈α⊗by␈α⊗programmed␈α⊗tests.␈α⊗ LISP␈α⊗has␈α⊗no␈α⊗capability␈α⊗to␈α∃maintain
␈↓ α←␈↓abstract␈αdata␈αstructures;␈αin␈αfact,␈αthe␈αimplementation␈αof␈αLISP␈αitself␈αis␈αopen␈αto
␈↓ α←␈↓programmer␈α∩modification.␈α∩ However␈α∪people␈α∩have␈α∩begun␈α∪investigations␈α∩of
␈↓ α←␈↓"typed␈α
LISP"␈α
[Car 76],␈αand␈α
some␈α
implementations␈αof␈α
LISP␈α
[Int 75]␈αgive␈α
some
␈↓ α←␈↓aids in constructing and maintaining typed data structures.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 36␈↓For␈αexample,␈αin␈α
Section 2.3␈αthe␈αtypes␈αof␈α
the␈αarguments␈αto␈α␈↓αdiff␈↓␈α
should
␈↓ α←␈↓be <poly> and <variable>, ␈↓¬not␈↓ list and atom.
␈↓ α←␈↓␈↓4.12␈↓ λILISP Machines 243␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Symbolic␈α∞expressions␈α∞are␈α∞the␈α∞only␈α∞real␈α∞data␈α∞structure;␈α∞we␈α∞almost␈α
have
␈↓ α←␈↓sequences␈α
as␈α
a␈αdata␈α
structure,␈α
and␈α
the␈αnecessary␈α
ingredients␈α
are␈α
there␈αto␈α
build
␈↓ α←␈↓abstract␈α
data␈α
structures.␈α
But␈α
the␈α
question␈α
of␈α
integrity␈α
in␈α
using␈α∞such␈α
defined
␈↓ α←␈↓data␈αstructures␈αis␈αleft␈αin␈αthe␈αhands␈αof␈αthe␈αprogrammer.␈αIn␈αsummary,␈αLISP␈αis
␈↓ α←␈↓an␈α⊂excellent␈α⊂tool␈α⊂for␈α⊂building␈α⊂more␈α⊂complex␈α⊂systems;␈α⊂as␈α⊂a␈α⊂tool,␈α⊂it␈α⊂has␈α∂the
␈↓ α←␈↓ability␈αto␈αcause␈αinjury,␈αand␈αsince␈αit␈αis␈αa␈αtool␈αit␈αhas␈αfew␈αpreconceptions.␈αThese
␈↓ α←␈↓are␈α∀some␈α∀of␈α∀the␈α∀reasons␈α∀that␈α∀LISP␈α∀has␈α∀maintained␈α∀its␈α∀position␈α∀as␈α∀the
␈↓ α←␈↓"machine language" for Artificial Intelligence.
␈↓ α←␈↓␈↓244 Static Structure␈↓
85.␈↓
␈↓"β␈↓ α←␈↓␈↓ ∨␈↓↓CHAPTER 5␈↓
␈↓"β␈↓ α←␈↓␈↓ The Static Structure of LISP␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬g␈↓↓5.1 Introduction␈↓
␈↓"β␈↓ α←␈↓The␈α∃material␈α∀in␈α∃the␈α∀previous␈α∃chapters␈α∀has␈α∃been␈α∀rather␈α∃abstract.␈α∀ This
␈↓ α←␈↓chapter␈α!begins␈α!a␈α discussion␈α!of␈α!the␈α mechanisms␈α!which␈α!occur␈α in
␈↓ α←␈↓implementations␈αof␈αLISP.␈αHowever␈αthe␈αimportance␈αof␈αthe␈αtechniques␈αwe␈αwill
␈↓ α←␈↓describe␈α
extends␈α
far␈α
beyond␈α
the␈α
implementation␈α
of␈α
this␈α
particular␈α
language.
␈↓ α←␈↓Most␈α∀of␈α∀the␈α∀ideas␈α∃involved␈α∀in␈α∀our␈α∀implementation␈α∀are␈α∃now␈α∀considered
␈↓ α←␈↓standard␈αsystem␈αprogramming␈αtechniques␈αand␈αare␈αcommon␈αtools␈αin␈αlanguage
␈↓ α←␈↓design.␈α
LISP␈αis␈α
particularly␈αwell-suited␈α
to␈αthe␈α
task␈αof␈α
explicating␈α
these␈αideas
␈↓ α←␈↓since many find their origins in the first LISP implementation.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α⊂will␈α⊂begin␈α⊂our␈α⊃discussion␈α⊂of␈α⊂implementation␈α⊂with␈α⊂an␈α⊃analysis␈α⊂of
␈↓ α←␈↓storage␈α∞regimes␈α∞for␈α∂S-expressions.␈α∞As␈α∞with␈α∂the␈α∞more␈α∞abstract␈α∂discussions␈α∞of
␈↓ α←␈↓representations,␈α∩the␈α⊃"concrete"␈α∩representation␈α⊃which␈α∩we␈α⊃pick␈α∩for␈α∩our␈α⊃data
␈↓ α←␈↓structures␈α
(S-expressions)␈α
will␈α
have␈α∞direct␈α
bearing␈α
on␈α
the␈α∞implementation␈α
of
␈↓ α←␈↓the␈αprimitive␈α
constructor␈α(␈↓αcons␈↓),␈α
selectors␈α(␈↓αcar,␈α
cdr␈↓)␈αand␈α
predicates␈α(␈↓αatom,␈αeq␈↓)␈α
of
␈↓ α←␈↓LISP.␈α∞ We␈α∞must␈α∞also␈α∞consider␈α∞the␈α∞efficiency␈α∞of␈α∞the␈α∞implementation␈α∂and␈α∞we
␈↓ α←␈↓must␈α∂include␈α∂input␈α∂and␈α∂output␈α∂mechanisms␈α∂to␈α∂translate␈α⊂this␈α∂representation
␈↓ α←␈↓back and forth between the external S-expr notation.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αpresent␈αchapter␈αwill␈αdevelop␈αa␈αpicture␈αof␈αthe␈α␈↓¬static␈↓␈αstructure␈αof␈αan
␈↓ α←␈↓implementation,␈αor␈αto␈αbe␈αmore␈αgraphic,␈αthis␈αchapter␈αdescribes␈αthe␈αmemory␈αof
␈↓ α←␈↓a␈αLISP␈αmachine.␈α The␈αnext␈αchapter␈αdiscusses␈αthe␈α␈↓¬dynamic␈αstructure␈↓␈αof␈αLISP;
␈↓ α←␈↓␈↓5.1␈↓ λ↑Introduction 245␈↓
␈↓"β␈↓ α←␈↓that␈α∩is,␈α∩the␈α∩control␈α∩structures␈α∩necessary␈α∩to␈α∩evaluate␈α∩expressions␈α⊃involving
␈↓ α←␈↓recursive functions and other LISP control constructs.
␈↓"β␈↓ α←␈↓␈↓ β'Throughout␈α⊃these␈α⊃discussions␈α⊂we␈α⊃will␈α⊃remain␈α⊂as␈α⊃abstract␈α⊃as␈α⊂possible
␈↓ α←␈↓without␈αlosing␈αtoo␈α
much␈αdetail.␈α We␈αwill␈α
describe␈αthe␈α"logical"␈αstructure␈α
of␈αa
␈↓ α←␈↓LISP␈α∩machine,␈α∩even␈α∩though␈α∩a␈α∩more␈α∩efficient␈α∩implementation␈α∩might␈α⊃map
␈↓ α←␈↓differing␈α∩logical␈α∪structures␈α∩onto␈α∪the␈α∩same␈α∪physical␈α∩structures␈α∪by␈α∩utilizing
␈↓ α←␈↓machine-dependent techniques.
␈↓"β␈↓ α←␈↓␈↓ ∧c␈↓↓5.2 Representation of S-expressions␈↓
␈↓"β␈↓ α←␈↓We previously have expressed the dotted pair ␈↓α(A . (B . C))␈↓ as:
␈↓"⊃␈↓ α←␈↓∂ /\
␈↓"␈↓ α←␈↓∂ / \
␈↓"␈↓ α←␈↓∂ / /\
␈↓"␈↓ α←␈↓∂ / / \
␈↓"␈↓ α←␈↓∂ ␈↓αA␈↓∂ ␈↓αB␈↓∂ ␈↓αC␈↓∂
␈↓" ␈↓ α←␈↓or occasionally (see page 9) as:
␈↓"ε␈↓ α←␈↓
⊂αααπααα⊃ ⊂αααπααα⊃
␈↓"␈↓ α←␈↓
ααααα→~ # ~ #αβααααααααααα→~ # ~ # ~
␈↓"␈↓ α←␈↓
%αβα∀ααα$ %αβα∀αβα$
␈↓"␈↓ α←␈↓
↓ ↓ ↓
␈↓"␈↓ α←␈↓
␈↓αA␈↓
␈↓αB␈↓
␈↓αC␈↓
␈↓"∀␈↓ α←␈↓This␈α∞second␈α∞style␈α∞of␈α∞graphical␈α∞representation␈α∞has␈α∞a␈α∞direct␈α∞representation␈α
in
␈↓ α←␈↓the␈αstorage␈αlayout␈αof␈αour␈αmachine.␈α Each␈α"double-box"␈αwill␈αbe␈αrepresented␈αas
␈↓ α←␈↓a␈α⊂machine␈α∂location␈α⊂and␈α⊂each␈α∂arrow␈α⊂will␈α∂be␈α⊂represented␈α⊂as␈α∂a␈α⊂␈↓↓pointer␈↓␈α⊂to␈α∂a
␈↓ α←␈↓machine␈αlocation.␈α Notice␈αthat␈αeach␈αbox␈αcontains␈αtwo␈αpointers;␈αtherefore␈αeach
␈↓ α←␈↓corresponding␈α∂machine␈α∂location,␈α∂␈↓
loc␈↓,␈α∂will␈α∂be␈α∂interpreted␈α∂as␈α∂containing␈α∂two
␈↓ α←␈↓machine␈αaddresses.␈↓π 1␈↓␈αThe␈αleft-hand␈αaddress␈αwill␈αrepresent␈αthe␈α␈↓αcar␈↓-branch;␈αthe
␈↓ α←␈↓right-hand address will represent the ␈↓αcdr␈↓-branch:
␈↓"⊃␈↓ α←␈↓
⊂αααααπααααα⊃
␈↓"␈↓ α←␈↓
loc ~ car ~ cdr ~
␈↓"␈↓ α←␈↓
%ααααα∀ααααα$
␈↓"∀␈↓ α←␈↓The␈α∀pointers␈α∀will␈α∀either␈α∀reference␈α∪atoms␈α∀or␈α∀point␈α∀to␈α∀two-pointer␈α∪boxes.
␈↓ α←␈↓Literal␈α
atoms␈α
-- like␈α
␈↓αA, B, C␈↓ --␈α∞will␈α
also␈α
be␈α
represented␈α
in␈α∞machine␈α
locations,
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 1␈↓An␈α∂actual␈α⊂hardware␈α∂machine␈α⊂may␈α∂not␈α∂be␈α⊂of␈α∂sufficient␈α⊂word-size␈α∂to
␈↓ α←␈↓accomodate␈α
two␈αaddresses;␈α
in␈αthis␈α
case,␈αseveral␈α
real␈αwords␈α
may␈αbe␈α
needed␈αto
␈↓ α←␈↓represent␈α≥one␈α≤LISP␈α≥word.␈α≤For␈α≥example,␈α≤the␈α≥PDP-11␈α≤(16 bit words)
␈↓ α←␈↓implementations␈α"typically␈α"use␈α"two␈α"machine␈α"words␈α"([Har 75]),␈α!and
␈↓ α←␈↓micro-processor␈α∪versions␈α∪(8 bit words)␈α∪may␈α∪use␈α∪four␈α∪words␈α∀([Pag 76]).␈α∪In
␈↓ α←␈↓Section 7.13 we discuss a special compact representation of LISP cells.
␈↓ α←␈↓␈↓246 Static Structure␈↓
(5.2␈↓
␈↓"β␈↓ α←␈↓only␈α
here␈α∞the␈α
contents␈α
of␈α∞each␈α
location␈α
will␈α∞be␈α
an␈α
encoding␈α∞of␈α
the␈α∞name␈α
of
␈↓ α←␈↓the␈α∀atom.␈α∪ The␈α∀contents␈α∪of␈α∀such␈α∪a␈α∀location␈α∪must␈α∀not␈α∪be␈α∀interpreted␈α∪as
␈↓ α←␈↓pointers.
␈↓"⊃␈↓ α←␈↓
⊂αααααααααααααααααααααα⊃
␈↓"␈↓ α←␈↓
loc ~ rep. of literal atom ~
␈↓"␈↓ α←␈↓
%αααααααααααααααααααααα$
␈↓"∀␈↓ α←␈↓␈↓ β'To␈α∞help␈α∞keep␈α
track␈α∞of␈α∞the␈α
different␈α∞uses␈α∞of␈α
machine␈α∞locations␈α∞we␈α
will
␈↓ α←␈↓partition␈α∪our␈α∪machine's␈α∪memory␈α∪into␈α∪two␈α∪disjoint␈α∪spaces:␈α∀␈↓↓pointer␈α∪space␈↓,
␈↓ α←␈↓which␈α⊃will␈α⊃contain␈α∩two-pointer␈α⊃cells;␈α⊃and␈α∩␈↓↓atom␈α⊃space␈↓,␈α⊃which␈α∩will␈α⊃contain
␈↓ α←␈↓information␈αlike␈αatoms␈αwhich␈αshould␈αnot␈αbe␈αinterpreted␈αas␈αpointers.␈α Thus␈αif
␈↓ α←␈↓the␈α
first␈α
box␈α
in␈α
our␈α
example␈α
were␈α
represented␈α
by␈α
location␈α
␈↓
100␈↓␈α
and␈αthe␈α
second
␈↓ α←␈↓were␈α↔represented␈α↔by␈α↔location␈α↔␈↓
405␈↓,␈α↔and␈α↔the␈α↔atoms␈α↔␈↓αA␈↓,␈α↔␈↓αB␈↓,␈α↔and␈α↔␈↓αC␈↓␈α⊗were
␈↓ α←␈↓represented␈α
by␈α
the␈α
numbers␈α
␈↓
40␈↓,␈α
␈↓
41␈↓,␈α
and␈α
␈↓
42␈↓,␈α
and␈α
were␈α
to␈α
be␈α
found␈α
in␈α
locations
␈↓ α←␈↓␈↓
710␈↓,␈α∪␈↓
762␈↓,␈α∩and␈α∪␈↓
711␈↓,␈α∩respectively,␈α∪then␈α∩the␈α∪following␈α∩picture␈α∪beginning␈α∩at
␈↓ α←␈↓location ␈↓
100␈↓ could represent the dotted pair ␈↓α(A . (B . C))␈↓.
␈↓"⊃␈↓ α←␈↓
⊂αααααπααααα⊃
␈↓"␈↓ α←␈↓
100 ~ 710 ~ 405 ~
␈↓"␈↓ α←␈↓
%ααααα∀ααααα$
␈↓"ε␈↓ α←␈↓
...
␈↓"ε␈↓ α←␈↓
⊂αααααπααααα⊃
␈↓"␈↓ α←␈↓
405 ~ 762 ~ 711 ~
␈↓"␈↓ α←␈↓
%ααααα∀ααααα$
␈↓"ε␈↓ α←␈↓
...
␈↓"ε␈↓ α←␈↓
⊂ααααααααααα⊃
␈↓"␈↓ α←␈↓
710 ~ 40 ~
␈↓"␈↓ α←␈↓
εαααααααααααλ
␈↓"␈↓ α←␈↓
711 ~ 42 ~
␈↓"␈↓ α←␈↓
%ααααααααααα$
␈↓"␈↓ α←␈↓
...
␈↓"␈↓ α←␈↓
⊂ααααααααααα⊃
␈↓"␈↓ α←␈↓
762 ~ 41 ~
␈↓"␈↓ α←␈↓
%ααααααααααα$
␈↓"∀␈↓ α←␈↓The␈αleft␈αhalf␈αof␈αlocation␈α␈↓
100␈↓␈αpoints␈αto␈αthe␈αrepresentation␈αof␈αthe␈αatom␈α␈↓αA␈↓␈αand
␈↓ α←␈↓the␈αright␈αhalf␈αpoints␈αto␈αthe␈αrepresentation␈αof␈αthe␈αdotted␈αpair␈α
␈↓α(B . C)␈↓.␈α Notice
␈↓ α←␈↓too,␈α∞that␈α∞given␈α∞the␈α
entry␈α∞point␈α∞into␈α∞the␈α
representation␈α∞--location␈α∞␈↓
100␈↓␈α∞in␈α
the
␈↓ α←␈↓example-- we can discover the S-expr being represented.
␈↓"β␈↓ α←␈↓␈↓ β'This␈α∩representation␈α∩of␈α⊃S-exprs␈α∩is␈α∩a␈α∩special␈α⊃case␈α∩of␈α∩a␈α∩scheme␈α⊃called
␈↓ α←␈↓␈↓↓linked␈α∪list␈α∀structure␈↓.␈α∪The␈α∪term␈α∀"linked"␈α∪refers␈α∪to␈α∀the␈α∪fact␈α∪that␈α∀to␈α∪find
␈↓ α←␈↓succeeding␈αelements␈α
in␈αthe␈αrepresentation␈α
we␈αmust␈α
follow␈αthe␈αexplicit␈α
pointers
␈↓ α←␈↓as␈α∞opposed␈α∞to,␈α∞say,␈α∞merely␈α∞incrementing␈α∞an␈α∞array␈α∞pointer.␈α∞ The␈α∞phrase␈α∞"list
␈↓ α←␈↓structure"␈α∞describes␈α∞an␈α∞arbitrary␈α∞interconnection␈α∞of␈α∞these␈α∞two-pointer␈α∞nodes,
␈↓ α←␈↓including␈α∂self-referential␈α∂structures.␈α⊂We␈α∂will␈α∂discuss␈α∂such␈α⊂general␈α∂structures
␈↓ α←␈↓later;␈α∃for␈α∃the␈α∃moment␈α∃we␈α∃restrict␈α∃such␈α∃constructions␈α∃to␈α∃LISP␈α⊗trees:␈α∃no
␈↓ α←␈↓␈↓5.2␈↓ εoRepresentation of S-expressions 247␈↓
␈↓"β␈↓ α←␈↓intersecting branches.␈↓π 2␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈α≤particular␈α≠brand␈α≤of␈α≠linked␈α≤list␈α≠structure␈α≤which␈α≤we␈α≠have
␈↓ α←␈↓demonstrated␈αis␈αcalled␈α␈↓↓singly␈αlinked␈↓.␈α The␈αadjective␈α"singly"␈αmeans␈αthat␈αonly
␈↓ α←␈↓␈↓¬one␈↓␈αpointer␈αis␈αstored␈α
as␈αthe␈αrepresentation␈αof␈α
the␈αarrow,␈α␈↓
→␈↓.␈α This␈α
means␈αthat
␈↓ α←␈↓the␈α∃representation␈α⊗only␈α∃tells␈α∃us␈α⊗how␈α∃to␈α∃find␈α⊗successor␈α∃elements␈α⊗in␈α∃the
␈↓ α←␈↓structure.␈αFor␈αexample,␈αif␈αwe␈αwere␈αlooking␈αat␈αlocation␈α␈↓
405␈↓␈αthe␈αrepresentation
␈↓ α←␈↓tells␈αus␈αhow␈αto␈αfind␈α
the␈α␈↓αcar␈↓␈αor␈α␈↓αcdr␈↓;␈αthey're␈α
at␈α␈↓
762␈↓␈αand␈α␈↓
711␈↓␈αrespectively.␈αBut␈α
if
␈↓ α←␈↓we␈α⊃wanted␈α⊂to␈α⊃find␈α⊂the␈α⊃␈↓¬predecessor␈↓␈α⊂of␈α⊃␈↓
405␈↓␈α⊂in␈α⊃this␈α⊂representation␈α⊃it␈α⊂would
␈↓ α←␈↓require␈α
some␈α
further␈αcalculation.␈α
We␈α
would␈α
have␈αto␈α
start␈α
at␈α
the␈αbeginning␈α
of
␈↓ α←␈↓the␈α
S-expr␈α
representation␈α
and␈α
look␈α
for␈α
a␈α
location␈α
such␈α
that␈α
its␈α
␈↓αcar␈↓␈α
or␈α∞␈↓αcdr␈↓␈α
is
␈↓ α←␈↓the␈α∂desired␈α∂cell.␈α∂ If␈α∂a␈α∞particular␈α∂application␈α∂required␈α∂frequent␈α∂discovery␈α∞of
␈↓ α←␈↓such␈α⊃predecessors␈α⊃then␈α⊃we␈α⊃might␈α⊃consider␈α⊃a␈α⊃more␈α∩complex␈α⊃representation
␈↓ α←␈↓which␈α∂would␈α∞also␈α∂contain␈α∞information␈α∂about␈α∞the␈α∂predecessor␈α∞of␈α∂each␈α∞node,
␈↓ α←␈↓essentially representing ␈↓
→␈↓ as ␈↓
↔␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'For example:
␈↓"⊃␈↓ α←␈↓
⊂αααπααα⊃ ⊂αααπααα⊃
␈↓"␈↓ α←␈↓
α←→α→~ # ~ #←βααα←→αααααα→~ # ~ # ~
␈↓"␈↓ α←␈↓
%αβα∀ααα$ %αβα∀αβα$
␈↓"␈↓ α←␈↓
↑ ↑ ↑
␈↓"␈↓ α←␈↓
↓ ↓ ↓
␈↓"␈↓ α←␈↓
␈↓αA␈↓
␈↓αB␈↓
␈↓αC␈↓
␈↓"∀␈↓ α←␈↓One␈α⊗such␈α∃representation␈α⊗is␈α∃called␈α⊗␈↓↓doubly-linked␈α∃list␈α⊗structure␈↓.␈α⊗In␈α∃this
␈↓ α←␈↓representation␈α
of␈αLISP␈α
trees␈α
we␈αcould␈α
store␈α
␈↓¬three␈↓␈αpieces␈α
of␈α
information␈αwith
␈↓ α←␈↓each node:
␈↓"⊃␈↓ α←␈↓
⊂ααααααααααααα⊃
␈↓"␈↓ α←␈↓
~ predecessor ~
␈↓"␈↓ α←␈↓
loc εααααααπααααααλ
␈↓"␈↓ α←␈↓
~ car ~ cdr ~
␈↓"␈↓ α←␈↓
%αααααα∀αααααα$
␈↓"∀␈↓ α←␈↓␈↓ β'Note␈α
that␈αLISP␈α
trees␈αalways␈α
␈↓¬do␈↓␈α
have␈αunique␈α
predecessors.␈αIn␈α
the␈αcase␈α
of
␈↓ α←␈↓list-structure,␈α
unique␈α
predecessors␈α
do␈α
not␈α
always␈α
exist.␈α
Compromises␈α
exist␈α
in
␈↓ α←␈↓some␈α∂applications:␈α∂some␈α∞data␈α∂structures␈α∂can␈α∞be␈α∂doubly-linked,␈α∂allowing␈α∞fast
␈↓ α←␈↓traversal␈α⊃but␈α⊂requiring␈α⊃more␈α⊂space;␈α⊃while␈α⊂other␈α⊃data␈α⊂structures␈α⊃are␈α⊂singly
␈↓ α←␈↓linked,␈α∞requiring␈α
less␈α∞space,␈α
but␈α∞requiring␈α
more␈α∞time␈α
to␈α∞traverse␈α
([Gua 69]);
␈↓ α←␈↓still␈α∩other␈α⊃structures␈α∩may␈α⊃have␈α∩more␈α⊃compact␈α∩representation␈α⊃as␈α∩arrays␈α⊃or
␈↓ α←␈↓numbers.␈α∞ For␈α∞example,␈α
a␈α∞typical␈α∞representation␈α∞of␈α
a␈α∞vector,␈α∞or␈α∞sequence␈α
of
␈↓ α←␈↓fixed␈α⊂length,␈α∂is␈α⊂to␈α∂store␈α⊂the␈α∂elements␈α⊂sequentially␈α∂in␈α⊂memory.␈↓π 3␈↓␈α⊂Since␈α∂each
␈↓ α←␈↓element␈αin␈αthis␈αstructure␈αhas␈αat␈αmost␈αone␈αsuccessor␈αwe␈αcan␈αuse␈αthe␈αsequential
␈↓ α←␈↓addresses␈α∀as␈α∃implicit␈α∀pointers␈α∀to␈α∃retrieve␈α∀successive␈α∀elements.␈α∃A␈α∀general
␈↓ α←␈↓S-expr␈αhas␈αtwo␈αsuccessors,␈αthus␈αthe␈αimplied␈αlinear␈αaddressing␈αscheme␈αof␈αmost
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 2␈↓The␈αimplementation␈αof␈αlists␈αusing␈αlinked␈αaddresses␈αwas␈αintroduced␈αby
␈↓ α←␈↓the IPL series of languages; [New 61].
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 3␈↓We will discuss these more detailed representations in Chapter 7.
␈↓ α←␈↓␈↓248 Static Structure␈↓
(5.2␈↓
␈↓"β␈↓ α←␈↓machine␈α⊂memories␈α∂is␈α⊂insufficient␈α∂as␈α⊂it␈α∂stands;␈α⊂LISP␈α∂uses␈α⊂linked␈α∂allocation.
␈↓ α←␈↓Again␈α≤there␈α≤are␈α≤compromises.␈α≤ For␈α≤example,␈α≤the␈α≥following␈α≤memory
␈↓ α←␈↓representation␈α
is␈α
valid␈α
for␈α
LISP␈α
trees:␈α
for␈α
any␈α
location␈α
␈↓αn␈↓,␈α
find␈α
its␈αsuccessors␈α
at
␈↓ α←␈↓locations␈α␈↓α2n␈↓␈αand␈α␈↓α2n+1␈↓;␈αnote␈αthat␈αthe␈αpredecessor␈αof␈αany␈αcell␈αis␈αunique.␈α Each
␈↓ α←␈↓location␈α∂must␈α∂contain␈α∞an␈α∂indication␈α∂of␈α∞whether␈α∂or␈α∂not␈α∞it␈α∂is␈α∂an␈α∂atom.␈α∞ The
␈↓ α←␈↓remaining contents of a location is available for data; see [Ber 71].
␈↓"β␈↓ α←␈↓␈↓ β'We␈αwill␈αfrequently␈αrefer␈αto␈αseveral␈αdifferent␈αS-exprs␈αsimultaneously;␈αfor
␈↓ α←␈↓example,␈αwhen␈αwe␈αare␈αtalking␈αabout␈αthe␈αimplementation␈αof␈αthe␈αfunction␈α␈↓αcons␈↓
␈↓ α←␈↓we␈αwill␈αbe␈αmanipulating␈αthe␈αrepresentations␈αof␈αtwo␈αS-exprs.␈αSimilarly␈αwe␈αwill
␈↓ α←␈↓want␈α∞to␈α∞refer␈α∞to␈α∞several␈α∞pieces␈α∞of␈α∞a␈α∞single␈α∞complex␈α∞S-expr;␈α∞for␈α∂example␈α∞we
␈↓ α←␈↓might␈α⊂wish␈α⊂to␈α⊃"put␈α⊂a␈α⊂finger"␈α⊃at␈α⊂a␈α⊂specific␈α⊂point␈α⊃in␈α⊂a␈α⊂structure␈α⊃and␈α⊂then,
␈↓ α←␈↓depending␈α∪on␈α∀the␈α∪result␈α∀of␈α∪a␈α∀computation␈α∪on␈α∀some␈α∪sub-part,␈α∀move␈α∪the
␈↓ α←␈↓"finger"␈αeither␈αleft␈αor␈αright.␈α To␈αfacilitate␈αsuch␈αdiscussions␈αwe␈αwill␈αassume␈α
the
␈↓ α←␈↓existence␈α
of␈αa␈α
set␈αof␈α
pointer␈α
registers:␈α␈↓
Pt␈↓β1␈↓,␈α
␈↓
Pt␈↓β2␈↓, ..., ␈↓
Pt␈↓βn␈↓.␈α Thus,␈α
using␈αthe␈α
above
␈↓ α←␈↓example,␈αthe␈αfollowing␈α
represents␈α␈↓
Pt␈↓β1␈↓␈αpointing␈αat␈α
␈↓α(B . C)␈↓␈αand␈α␈↓
Pt␈↓β2␈↓␈αpointing␈α
at
␈↓ α←␈↓the atom ␈↓αA␈↓:
␈↓"⊃␈↓ α←␈↓
Pt␈↓β1␈↓
Pt␈↓β2␈↓
␈↓"␈↓ α←␈↓
⊂αααααααα⊃ ⊂αααααααα⊃
␈↓"␈↓ α←␈↓
~ 405 ~ ~ 710 ~
␈↓"␈↓ α←␈↓
%αααααααα$ %αααααααα$
␈↓"∀␈↓ α←␈↓Implicit␈α∪in␈α∪our␈α∩representation␈α∪is␈α∪the␈α∩assurance␈α∪that␈α∪we␈α∪can␈α∩differentiate
␈↓ α←␈↓between␈αlocations␈αin␈αatom␈αspace␈αand␈αlocations␈αin␈αpointer␈αspace.␈αFor␈αexample,
␈↓ α←␈↓assume␈αeach␈α
of␈αour␈α
locations␈αcan␈α
hold␈αsix␈αdigits␈α
and␈αassume␈α
we␈αwill␈α
store␈αa
␈↓ α←␈↓numeric␈αatom␈αas␈αits␈αcorresponding␈αnumber.␈αThen␈αthe␈αatom␈α␈↓α762711␈↓␈αwould␈αbe
␈↓ α←␈↓stored as:
␈↓"⊃␈↓ α←␈↓
⊂αααααααα⊃
␈↓"␈↓ α←␈↓
~ 762711 ~
␈↓"␈↓ α←␈↓
%αααααααα$
␈↓"∀␈↓ α←␈↓Since␈αthis␈αis␈αexactly␈αthe␈αcontents␈αof␈αlocation␈α␈↓
405␈↓␈↓π 4␈↓␈αsome␈αconfusion␈αis␈αpossible:
␈↓ α←␈↓is␈αthe␈αcontents␈α
a␈αnumber␈αor␈αis␈α
it␈αtwo␈αpointers?␈αA␈α
typical␈αtrick␈αis␈α
to␈αpartition
␈↓ α←␈↓memory␈α
such␈α
that␈αparticular␈α
portions␈α
of␈α
the␈αaddress␈α
space␈α
correspond␈αto␈α
each
␈↓ α←␈↓of␈α
the␈α
logical␈α
spaces:␈α
atom␈α
space␈α
or␈α
pointer␈α
space.␈α
In␈α
our␈α
example␈α
we␈α
could
␈↓ α←␈↓assume␈α↔that␈α↔addresses␈α↔less␈α↔than␈α↔␈↓
700␈↓␈α↔are␈α↔locations␈α↔for␈α↔pointers,␈α⊗while
␈↓ α←␈↓addresses␈α≥greater␈α≥than␈α≥or␈α≥equal␈α≤to␈α≥␈↓
700␈↓␈α≥contain␈α≥atoms.␈α≥Thus␈α≤the
␈↓ α←␈↓representation␈α⊂of␈α⊂␈↓α762711␈↓␈α⊂would␈α⊂appear␈α⊃in␈α⊂a␈α⊂location␈α⊂with␈α⊂address␈α⊃␈↓
700␈↓␈α⊂or
␈↓ α←␈↓greater.
␈↓"β␈↓ α←␈↓␈↓ β'Though␈α∞our␈α∞memory␈α
system␈α∞is␈α∞not␈α∞completed␈α
yet,␈α∞we␈α∞␈↓¬do␈↓␈α∞have␈α
enough
␈↓ α←␈↓structure␈α⊗to␈α∃begin␈α⊗a␈α∃discussion␈α⊗of␈α∃the␈α⊗implementation␈α∃of␈α⊗some␈α⊗of␈α∃the
␈↓ α←␈↓primitive LISP operations.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 4␈↓The vertical bar doesn't appear in the machine's memory.
␈↓ α←␈↓␈↓5.2␈↓ εoRepresentation of S-expressions 249␈↓
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. What␈α⊂problems␈α⊂do␈α⊂you␈α⊂foresee␈α⊂in␈α⊂using␈α⊂the␈α⊂double-linking␈α⊂scheme␈α⊂for
␈↓ α←␈↓␈↓ β∂representing LISP's S-exprs?
␈↓"β␈↓ α←␈↓␈↓ ∧P␈↓↓5.3 Representation of LISP Primitives␈↓
␈↓"β␈↓ α←␈↓Now␈α⊗that␈α∃we␈α⊗have␈α∃some␈α⊗of␈α∃the␈α⊗representational␈α∃problems␈α⊗for␈α∃S-exprs
␈↓ α←␈↓reasonably␈α⊂well␈α⊃in␈α⊂hand␈α⊃we␈α⊂will␈α⊂look␈α⊃at␈α⊂the␈α⊃implementation␈α⊂of␈α⊃the␈α⊂LISP
␈↓ α←␈↓primitives.␈α
We␈αwill␈α
examine␈α
␈↓αcar,␈αcdr,␈α
eq,␈↓␈αand␈α
␈↓αatom␈↓␈α
in␈αthis␈α
section,␈αleaving␈α
␈↓αcons␈↓
␈↓ α←␈↓for later.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αmust␈αunderstand␈αhow␈αthese␈αprimitives␈αobtain␈αtheir␈αparameters␈αand
␈↓ α←␈↓how␈α
they␈α∞are␈α
to␈α
return␈α∞their␈α
values.␈α
Recall␈α∞our␈α
discussion␈α∞of␈α
environments
␈↓ α←␈↓and␈α∞destinations␈α∂in␈α∞Section 4.6.␈α∂ An␈α∞environment␈α∂chain␈α∞was␈α∂constructed␈α∞by
␈↓ α←␈↓linking␈α∂destination␈α∂blocks␈α∂whose␈α∂value␈α∂slots␈α∂have␈α∂been␈α∂filled.␈α∂A␈α∞dest-block
␈↓ α←␈↓was␈α_created␈α_when␈α_we␈α_recognized␈α_a␈α_function␈α_application.␈↓π 5␈↓␈α_The␈α_name
␈↓ α←␈↓components␈α→of␈α_a␈α→block␈α_are␈α→either␈α→the␈α_λ-variables␈α→in␈α_the␈α→case␈α→of␈α_a
␈↓ α←␈↓λ-application␈α_or␈α_are␈α_system-generated␈α_names␈α_in␈α_the␈α_case␈α_of␈α_primitive
␈↓ α←␈↓application;␈α∪the␈α∪value-slots␈α∪of␈α∪a␈α∪dest-block␈α∪received␈α∪the␈α∀evaluated␈α∪actual
␈↓ α←␈↓parameters.␈α
When␈α
a␈α∞dest-block␈α
was␈α
filled,␈α
it␈α∞was␈α
chained␈α
onto␈α
the␈α∞front␈α
of
␈↓ α←␈↓the␈αenvironment␈αand␈αwe␈αwere␈αready␈αto␈αcall␈αthe␈αfunction.␈αThus␈αthe␈αfirst␈αblock
␈↓ α←␈↓on␈α⊃the␈α⊂environment␈α⊃chain␈α⊂was␈α⊃the␈α⊂local␈α⊃symbol␈α⊂table.␈α⊃ The␈α⊃function␈α⊂was
␈↓ α←␈↓expected␈α
to␈αreturn␈α
its␈αvalue␈α
to␈αa␈α
designated␈αdest-block,␈α
and␈αthen␈α
return␈αto␈α
the
␈↓ α←␈↓interrupted␈αcomputation.␈α So,␈αon␈αentrance␈αto␈αa␈αprimitive␈αwe␈αhave␈αaccess␈αto␈αat
␈↓ α←␈↓least two structures: a destination block and the environment chain.
␈↓"⊃␈↓ α←␈↓
␈↓αdest␈↓
␈↓αenv␈↓
␈↓"␈↓ α←␈↓
~ ~
␈↓"␈↓ α←␈↓
~ ⊂ααααααα⊃ ~ ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
%α→ ~ #αβα⊃ %αα→ ~ #αβαα ### →
␈↓"␈↓ α←␈↓
εαααπαααλ ↓ εαααπαααλ
␈↓"␈↓ α←␈↓
# # # # # #
␈↓"␈↓ α←␈↓
~ ~ ~←$ ~ ~ ~
␈↓"␈↓ α←␈↓
εαααβαααλ εαααβαααλ
␈↓"␈↓ α←␈↓
~ ~ ~ ~ ~ ~
␈↓"␈↓ α←␈↓
# # # %ααα∀ααα$
␈↓"␈↓ α←␈↓
εαααβαααλ
␈↓"␈↓ α←␈↓
~ ~ ~
␈↓"␈↓ α←␈↓
%ααα∀ααα$
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 5␈↓If␈α∪the␈α∩application␈α∪supplied␈α∩an␈α∪incorrect␈α∩number␈α∪of␈α∪arguments␈α∩no
␈↓ α←␈↓dest-block␈α∞is␈α∞built;␈α∞the␈α
debugging␈α∞routine␈α∞is␈α∞called.␈α∞Several␈α
implementations
␈↓ α←␈↓supply␈α~missing␈α≠arguments␈α~with␈α~␈↓αNIL␈↓␈α≠or␈α~evaluate␈α~and␈α≠discard␈α~extra
␈↓ α←␈↓arguments.␈α
This␈α
treatment␈α
of␈α
improper␈α
calling␈α
sequences␈α
ignores␈α
one␈α∞of␈α
the
␈↓ α←␈↓most common sources of bugs in LISP programs ([Mot 76]).
␈↓ α←␈↓␈↓250 Static Structure␈↓
(5.3␈↓
␈↓"∀␈↓ α←␈↓Here is how ␈↓αcar␈↓ uses these structures:
␈↓"β␈↓ α←␈↓␈↓ βWLet␈α
␈↓αval␈↓␈α
be␈α
the␈α
value-part␈α
of␈α
the␈α
first␈α
entry␈α
in␈α
the␈α
local␈α
table.␈α
If␈α␈↓αval␈↓␈α
is
␈↓ α←␈↓␈↓ βWan␈α∞atom␈α∞then␈α∂␈↓αcar␈↓␈α∞is␈α∞undefined;␈α∂the␈α∞implementation␈α∞should␈α∂send␈α∞a
␈↓ α←␈↓␈↓ βWmessage␈α∞to␈α∞the␈α∞debugging␈α∞package␈α∞(see␈α∞Section 6.23).␈α∞ If␈α∞␈↓αval␈↓␈α∂is␈α∞␈↓¬not␈↓
␈↓ α←␈↓␈↓ βWatomic,␈α⊃it␈α⊃has␈α⊃a␈α⊃left-␈α∩and␈α⊃right-hand␈α⊃side.␈α⊃ We␈α⊃should␈α∩send␈α⊃the
␈↓ α←␈↓␈↓ βW␈↓¬left␈↓-hand␈αside␈αof␈α
␈↓αval␈↓␈αto␈αthe␈α
value␈αpart␈αof␈αthe␈α
slot␈αpointed␈αto␈α
in␈αthe
␈↓ α←␈↓␈↓ βWdest-block.
␈↓"β␈↓ α←␈↓For example:
␈↓"⊃␈↓ α←␈↓
␈↓αdest␈↓
␈↓αenv␈↓
␈↓"␈↓ α←␈↓
~ ~
␈↓"␈↓ α←␈↓
~ ⊂ααααααα⊃ ~ ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
%→ ~ #αβα⊃ %→ ~ #αβαα ### →
␈↓"␈↓ α←␈↓
εαααπαααλ ↓ εαααπαααλ
␈↓"␈↓ α←␈↓
# # # ~ ~102~
␈↓"␈↓ α←␈↓
~ ~ ~←$ %ααα∀ααα$
␈↓"␈↓ α←␈↓
εαααβαααλ
␈↓"␈↓ α←␈↓
~ ~ ~
␈↓"␈↓ α←␈↓
# # #
␈↓"␈↓ α←␈↓
εαααβαααλ ⊂αααπααα⊃
␈↓"␈↓ α←␈↓
~ ~ ~ 102 ~204~ 22~
␈↓"␈↓ α←␈↓
%ααα∀ααα$ %ααα∀ααα$
␈↓"␈↓ α←␈↓↓␈↓ ε,Before
␈↓"⊃␈↓ α←␈↓
⊂ααααααα⊃ ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
~ #αβα⊃ ~ #αβαα ### →
␈↓"␈↓ α←␈↓
εαααπαααλ ↓ εαααπαααλ
␈↓"␈↓ α←␈↓
# # # ~ ~102~
␈↓"␈↓ α←␈↓
~ ~204~←$ %ααα∀ααα$
␈↓"␈↓ α←␈↓
εαααβαααλ
␈↓"␈↓ α←␈↓
~ ~ ~
␈↓"␈↓ α←␈↓
# # #
␈↓"␈↓ α←␈↓
εαααβαααλ ⊂αααπααα⊃
␈↓"␈↓ α←␈↓
~ ~ ~ 102 ~204~ 22~
␈↓"␈↓ α←␈↓
%ααα∀ααα$ %ααα∀ααα$
␈↓"β␈↓ α←␈↓↓␈↓ ε3After
␈↓"β␈↓ α←␈↓␈↓ Example for ␈↓αcar␈↓ ␈↓α
␈↓"→␈↓ α←␈↓For␈α∞successful␈α∞completion␈α∞␈↓
␈↓αcar␈↓␈α∂expects␈α∞that␈α∞its␈α∞actual␈α∞parameter␈α∂represents␈α∞a
␈↓ α←␈↓node␈α
in␈α
pointer␈α
space;␈αotherwise␈α
we␈α
get␈α
an␈αerror.␈α
If␈α
the␈α
operation␈αis␈α
successful
␈↓ α←␈↓then␈α⊂the␈α⊂dest-slot␈α⊂is␈α⊂changed␈α⊂to␈α⊃point␈α⊂to␈α⊂whatever␈α⊂was␈α⊂pointed␈α⊂to␈α⊃by␈α⊂the
␈↓ α←␈↓left-hand␈α∩side␈α∩of␈α∪the␈α∩selected␈α∩cell.␈α∩ The␈α∪description␈α∩of␈α∩␈↓αcdr␈↓␈α∪is␈α∩sufficiently
␈↓ α←␈↓similar that we leave it to the reader.
␈↓ α←␈↓␈↓5.3␈↓ ε←Representation of LISP Primitives 251␈↓
␈↓"β␈↓ α←␈↓␈↓ β'On␈αpage 248␈αwe␈αdescribed␈αthe␈αinternal␈αstructure␈αof␈αLISP␈α
atoms.␈αUsing
␈↓ α←␈↓that␈α∂representation␈α∂we␈α∂can␈α⊂give␈α∂a␈α∂simple␈α∂implementation␈α∂for␈α⊂the␈α∂predicate
␈↓ α←␈↓␈↓αatom␈↓:
␈↓"β␈↓ α←␈↓␈↓ βWDoes␈α∂the␈α∂actual␈α∂parameter␈α∂point␈α∞into␈α∂that␈α∂area␈α∂reserved␈α∂for␈α∞atom
␈↓ α←␈↓␈↓ βWnames?␈αIf␈α
so,␈αsend␈α
a␈αrepresentation␈α
of␈αtruth␈α
as␈αvalue,␈αotherwise␈α
send
␈↓ α←␈↓␈↓ βWa representation of false.
␈↓"⊃␈↓ α←␈↓
␈↓αdest␈↓
␈↓αenv␈↓
␈↓"␈↓ α←␈↓
~ ~
␈↓"␈↓ α←␈↓
~ ⊂ααααααα⊃ ~ ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
%→ ~ #αβα⊃ %→ ~ #αβαα ### →
␈↓"␈↓ α←␈↓
εαααπαααλ ↓ εαααπαααλ
␈↓"␈↓ α←␈↓
# # # ~ ~710~
␈↓"␈↓ α←␈↓
~ ~ ~←$ %ααα∀ααα$
␈↓"␈↓ α←␈↓
εαααβαααλ ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
~ ~ ~ 710 ~ "A"~
␈↓"␈↓ α←␈↓
# # # %ααααααα$
␈↓"␈↓ α←␈↓
εαααβαααλ ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
~ ~ ~ 714 ~ "T"~
␈↓"␈↓ α←␈↓
%ααα∀ααα$ %ααααααα$
␈↓"β␈↓ α←␈↓
␈↓We are writing ␈↓
"A"␈↓ instead of the numeric encoding. Thus ␈↓
"A"␈↓ is really ␈↓
40␈↓.␈↓
␈↓"␈↓ α←␈↓↓␈↓ ε,Before
␈↓"⊃␈↓ α←␈↓
⊂ααααααα⊃ ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
~ #αβα⊃ ~ #αβαα ### →
␈↓"␈↓ α←␈↓
εαααπαααλ ↓ εαααπαααλ
␈↓"␈↓ α←␈↓
# # # ~ ~710~
␈↓"␈↓ α←␈↓
~ ~714~←$ %ααα∀ααα$
␈↓"␈↓ α←␈↓
εαααβαααλ
␈↓"␈↓ α←␈↓
~ ~ ~
␈↓"␈↓ α←␈↓
# # #
␈↓"␈↓ α←␈↓
εαααβαααλ
␈↓"␈↓ α←␈↓
~ ~ ~
␈↓"␈↓ α←␈↓
%ααα∀ααα$
␈↓"β␈↓ α←␈↓↓␈↓ ε3After
␈↓"β␈↓ α←␈↓␈↓ Example for ␈↓αatom␈↓ ␈↓α
␈↓"→␈↓ α←␈↓Notice␈α∞that␈α∂we␈α∞did␈α∞not␈α∂need␈α∞to␈α∂examine␈α∞the␈α∞contents␈α∂of␈α∞location␈α∂␈↓
710␈↓,␈α∞thus
␈↓ α←␈↓saving␈αone␈αstorage␈αreference.␈α It␈αwas␈αsufficient␈αto␈αknow␈αthat␈αthe␈αlocation␈αwas
␈↓ α←␈↓between␈αpredetermined␈αbounds.␈α If␈αthe␈αactual␈αparameter␈αwas␈αnot␈α
pointing␈αat
␈↓ α←␈↓an atom we would have returned a pointer to a location containing ␈↓
"NIL"␈↓.
␈↓ α←␈↓␈↓252 Static Structure␈↓
(5.3␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Finally we describe an implementation of ␈↓αeq␈↓:
␈↓"β␈↓ α←␈↓␈↓ βWDo␈αboth␈αactual␈αparameters␈αpoint␈αinto␈αatom␈αspace?␈α If␈αnot␈αthe␈αresult
␈↓ α←␈↓␈↓ βWis␈αundefined.␈αIf␈αthey␈α␈↓¬do␈↓␈αthen␈αdo␈αthey␈αreference␈αthe␈αsame␈αatom?␈α We
␈↓ α←␈↓␈↓ βWcan␈α⊂determine␈α⊂this␈α⊂latter␈α⊂condition␈α⊂in␈α⊂two␈α⊂ways:␈α⊂first,␈α⊂they␈α∂might
␈↓ α←␈↓␈↓ βWpoint␈α∂to␈α∂two␈α∂different␈α∂locations␈α⊂in␈α∂atom␈α∂space;␈α∂we␈α∂would␈α⊂have␈α∂to
␈↓ α←␈↓␈↓ βWexamine␈α∪the␈α∪contents␈α∩of␈α∪those␈α∪locations;␈α∩if␈α∪they␈α∪agreed␈α∪then␈α∩␈↓αeq␈↓
␈↓ α←␈↓␈↓ βWshould␈αreturn␈αa␈α
representation␈αof␈αtruth.␈α
A␈αmore␈αsatisfactory␈α
solution
␈↓ α←␈↓␈↓ βWis␈αto␈αstore␈αeach␈α
atom␈α␈↓¬uniquely␈↓;␈αone␈αlocation␈α
will␈αbe␈αreserved␈αfor␈α
␈↓
"A"␈↓,
␈↓ α←␈↓␈↓ βWetc.␈αNow␈αall␈α␈↓αeq␈↓␈αneed␈αdo␈αis␈αmake␈αsure␈αthat␈αboth␈αslots␈αpoint␈αinto␈αatom
␈↓ α←␈↓␈↓ βWspace␈α∞␈↓¬and␈↓␈α∞point␈α∞to␈α∞the␈α
same␈α∞location.␈α∞Thus␈α∞no␈α∞additional␈α
memory
␈↓ α←␈↓␈↓ βWreference␈α∞is␈α∞required.␈α∞From␈α∞now␈α∞on␈α∞we␈α∞will␈α∞require␈α∞that␈α∞all␈α
atoms
␈↓ α←␈↓␈↓ βWare stored uniquely.
␈↓"⊃␈↓ α←␈↓
␈↓αdest␈↓
␈↓αenv␈↓
␈↓"␈↓ α←␈↓
~ ~
␈↓"␈↓ α←␈↓
~ ⊂ααααααα⊃ ~ ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
%→ ~ #αβα⊃ %→ ~ #αβαα ### →
␈↓"␈↓ α←␈↓
εαααπαααλ ↓ εαααπαααλ
␈↓"␈↓ α←␈↓
# # # ~ ~710~
␈↓"␈↓ α←␈↓
~ ~ ~←$ εαααβαααλ
␈↓"␈↓ α←␈↓
εαααβαααλ ~ ~710~
␈↓"␈↓ α←␈↓
~ ~ ~ %ααα∀ααα$
␈↓"␈↓ α←␈↓
# # # ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
εαααβαααλ 714 ~ "T"~
␈↓"␈↓ α←␈↓
~ ~ ~ %ααααααα$
␈↓"␈↓ α←␈↓
%ααα∀ααα$ ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
710 ~ "A"~
␈↓"␈↓ α←␈↓
%ααααααα$
␈↓"␈↓ α←␈↓↓␈↓ ε,Before
␈↓"⊃␈↓ α←␈↓
⊂ααααααα⊃ ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
~ #αβα⊃ ~ #αβαα ### →
␈↓"␈↓ α←␈↓
εαααπαααλ ↓ εαααπαααλ
␈↓"␈↓ α←␈↓
# # # ~ ~710~
␈↓"␈↓ α←␈↓
~ ~714~←$ εαααβαααλ
␈↓"␈↓ α←␈↓
εαααβαααλ ~ ~710~
␈↓"␈↓ α←␈↓
~ ~ ~ %ααα∀ααα$
␈↓"␈↓ α←␈↓
# # # ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
εαααβαααλ 714 ~ "T"~
␈↓"␈↓ α←␈↓
~ ~ ~ %ααααααα$
␈↓"␈↓ α←␈↓
%ααα∀ααα$ ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
710 ~ "A"~
␈↓"␈↓ α←␈↓
%ααααααα$
␈↓"β␈↓ α←␈↓↓␈↓ ε3After
␈↓"β␈↓ α←␈↓␈↓ Example for ␈↓αeq␈↓ ␈↓α
␈↓"
␈↓ α←␈↓␈↓ β'We␈αstill␈αhave␈αa␈αambiguity␈αto␈α
resolve␈αif␈αwe␈αrepresent␈αthe␈αnumber␈α
␈↓α40␈↓␈αas
␈↓ α←␈↓␈↓
40␈↓ and represent the atom ␈↓αA␈↓ as ␈↓
40␈↓. Section 5.6 resolves this conflict.
␈↓ α←␈↓␈↓5.4␈↓ ∞AMBIT/G 253␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬v␈↓↓5.4 AMBIT/G␈↓
␈↓"β␈↓ α←␈↓Before␈α∞investigating␈α∂the␈α∞implementation␈α∂of␈α∞atoms,␈α∂we␈α∞should␈α∂explore␈α∞other
␈↓ α←␈↓possible␈α≠descriptions␈α≠for␈α≤LISP's␈α≠primitives.␈α≠ We␈α≠have␈α≤described␈α≠the
␈↓ α←␈↓primitives␈α
by␈α
example;␈α
it␈α
would␈α
be␈αmore␈α
pleasing␈α
if␈α
we␈α
could␈α
describe␈αeach
␈↓ α←␈↓primitive␈α
in␈α
more␈α
general␈α
terms.␈α
Fortunately␈α
we␈α
can,␈α
using␈α∞a␈α
micro-version
␈↓ α←␈↓of a graphical language called AMBIT/G.␈↓π 6␈↓
␈↓"β␈↓ α←␈↓␈↓ β'When␈αdeveloping␈α
a␈αcomplex␈α
structure-manipulating␈αprogram,␈α
we␈αdraw
␈↓ α←␈↓pictures.␈α∞In␈α∞LISP␈α∞we␈α∞frequently␈α∞describe␈α∞data␈α∞structures␈α∞graphically␈α∞and␈α∞in
␈↓ α←␈↓the␈α∞previous␈α∞section␈α∞we␈α∞gave␈α∞graphical␈α∞descriptions␈α∞of␈α∞the␈α∞LISP␈α∞primitives
␈↓ α←␈↓using␈α∩examples.␈α∩AMBIT␈α∩is␈α∩an␈α∩extension␈α∩of␈α∩both␈α∩of␈α∩these␈α∩ideas;␈α∩it␈α∩is␈α∩a
␈↓ α←␈↓graphical language for the description of both data and algorithms.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α≤basic␈α≤statements␈α≥of␈α≤the␈α≤language␈α≤are␈α≥␈↓↓pattern-match␈↓␈α≤and
␈↓ α←␈↓␈↓↓replacement␈↓␈α⊂rules.␈α⊂ Several␈α⊂programming␈α⊂languages␈α⊂have␈α⊂complex␈α∂pattern
␈↓ α←␈↓matchers;␈α
AMBIT's␈α
uniqueness␈α
is␈α
its␈α
graphical␈α
presentation␈α
of␈α∞the␈α
patterns.
␈↓ α←␈↓Patterns␈α∂are␈α⊂described␈α∂as␈α∂combinations␈α⊂of␈α∂shapes␈α∂and␈α⊂solid␈α∂arrows.␈α⊂ If␈α∂an
␈↓ α←␈↓instance␈α
of␈α∞a␈α
pattern␈α∞can␈α
be␈α∞found␈α
in␈α∞the␈α
current␈α∞state␈α
of␈α∞the␈α
computation,
␈↓ α←␈↓then␈α⊂we␈α∂will␈α⊂replace␈α⊂that␈α∂instance␈α⊂with␈α∂a␈α⊂new␈α⊂pattern.␈α∂ The␈α⊂only␈α⊂kind␈α∂of
␈↓ α←␈↓replacement␈α
we␈α
will␈α
allow␈α
is␈α
the␈α␈↓↓swinging␈↓␈α
of␈α
an␈α
arrow␈α
so␈α
that␈α
its␈αhead␈α
moves
␈↓ α←␈↓from␈αone␈αnode␈α
to␈αanother;␈αthe␈αtail␈α
of␈αthe␈αarrow␈α
is␈αimmovable.␈α Thus␈αthe␈α
new
␈↓ α←␈↓pattern␈α∂differs␈α∂from␈α∂the␈α∂old␈α∂only␈α⊂in␈α∂the␈α∂positioning␈α∂of␈α∂some␈α∂of␈α⊂the␈α∂arrow
␈↓ α←␈↓heads.␈α
Where␈αthe␈α
arrow␈αhead␈α
strikes␈αa␈α
node␈αis␈α
immaterial.␈α
Dashed␈αarrows
␈↓ α←␈↓show␈αreplacements␈α
to␈αbe␈α
made␈αif␈αthe␈α
pattern␈αmatches.␈α
Portions␈αof␈αthe␈α
shapes
␈↓ α←␈↓marked with "?" are "don't-care" conditions.
␈↓"β␈↓ α←␈↓␈↓ β'For example, here's ␈↓αcar␈↓:
␈↓"⊃␈↓ α←␈↓
␈↓αdest␈↓
␈↓αenv␈↓
␈↓"␈↓ α←␈↓
~ ~
␈↓"␈↓ α←␈↓
~ ⊂ααααααα⊃ ~ ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
%→ ~ #αβα⊃ %→ ~ #αβαα ### →
␈↓"␈↓ α←␈↓
εαααπαααλ ↓ εαααπαααλ
␈↓"␈↓ α←␈↓
# # # ~ ~ # ~
␈↓"␈↓ α←␈↓
~ ~ ~←$ %ααα∀αβα$
␈↓"␈↓ α←␈↓
~ ~ # → - → ⊃ ~
␈↓"␈↓ α←␈↓
εαααβαααλ ↓ ↓
␈↓"␈↓ α←␈↓
~ ~ ~ ⊂αααπααα⊃
␈↓"␈↓ α←␈↓
# # # | ~ # ~ ? ~
␈↓"␈↓ α←␈↓
εαααβαααλ %αβα∀ααα$
␈↓"␈↓ α←␈↓
~ ~ ~ ↓ ↓
␈↓"␈↓ α←␈↓
%ααα∀ααα$ ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
%→ - → - →~ ? ~
␈↓"␈↓ α←␈↓
%ααααααα$
␈↓"β␈↓ α←␈↓␈↓ Algorithm for ␈↓αcar␈↓ ␈↓α
␈↓"λ␈↓ α←␈↓This␈α∀AMBIT␈α∃diagram␈α∀contains␈α∀equivalent␈α∃information␈α∀to␈α∃the␈α∀previous
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 6␈↓AMBIT/G␈α⊃is␈α⊃an␈α⊃acronym␈α⊃for␈α⊃Algebraic␈α⊃Manipulation␈α⊃By␈α⊃Identity
␈↓ α←␈↓Transformation/Graphical.
␈↓ α←␈↓␈↓254 Static Structure␈↓
(5.4␈↓
␈↓"β␈↓ α←␈↓␈↓¬example␈↓␈α∂of␈α∂␈↓αcar␈↓,␈α⊂but␈α∂the␈α∂extraneous␈α∂details␈α⊂of␈α∂specific␈α∂addresses␈α⊂have␈α∂been
␈↓ α←␈↓stripped␈α∀away.␈α∪ We␈α∀will␈α∪use␈α∀such␈α∪diagrams␈α∀occasionally␈α∪when␈α∀they␈α∪will
␈↓ α←␈↓contribute to clarity.
␈↓"β␈↓ α←␈↓␈↓ ε≡␈↓↓Problem␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Give an AMBIT diagram for ␈↓αeq␈↓.
␈↓"β␈↓ α←␈↓␈↓ ∧Z␈↓↓5.5 A Few Programming Techniques␈↓
␈↓"β␈↓ α←␈↓There␈α∞are␈α∞a␈α
few␈α∞useful␈α∞and␈α
practical␈α∞LISP␈α∞programming␈α∞techniques␈α
which
␈↓ α←␈↓take␈α∞advantage␈α∞of␈α∞the␈α∞implementation.␈α∞ These␈α∞tricks␈α∞are␈α∞supported␈α∂in␈α∞most
␈↓ α←␈↓implementations␈α∞and␈α
are␈α∞useful␈α∞enough␈α
that␈α∞they␈α
should␈α∞be␈α∞documented␈α
as
␈↓ α←␈↓programming language features.
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓␈αIn␈αmost␈αimplementations␈αof␈α␈↓αeq␈↓␈αthe␈αcheck␈αfor␈αatom-ness␈αis␈αsuppressed␈αand␈αa
␈↓ α←␈↓␈↓ β∂simple␈α⊃address␈α⊃comparison␈α∩is␈α⊃made.␈α⊃Non-atomic␈α⊃S-expressions␈α∩are␈α⊃not
␈↓ α←␈↓␈↓ β∂usually stored uniquely;␈↓π 7␈↓ Thus in most implementations
␈↓"∀␈↓ α←␈↓␈↓ βK␈↓αeq[(A . B);(A . B)]␈↓ is usually false, but
␈↓" ␈↓ α←␈↓␈↓ βK␈↓αeq[x;x] ␈↓is usually true for any ␈↓αx␈↓.
␈↓"∀␈↓ α←␈↓We␈αare␈α␈↓¬not␈↓␈αchanging␈αthe␈αdefinition␈αof␈α␈↓αeq␈↓;␈αit␈αis␈αstill␈αundefined␈αfor␈αnon-atomic
␈↓ α←␈↓arguments.␈α The␈αpreceding␈αremarks␈αdeal␈αonly␈αwith␈αthe␈αusual␈αimplementation
␈↓ α←␈↓of ␈↓αeq␈↓.␈↓π 8␈↓
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓␈α⊃The␈α⊃implementation␈α∩of␈α⊃the␈α⊃truth␈α⊃values␈α∩␈↓
t␈↓␈α⊃and␈α⊃␈↓
f␈↓␈α⊃is␈α∩usually␈α⊃simplified,
␈↓ α←␈↓␈↓ β∂mapping␈α␈↓
f␈↓␈αonto␈α␈↓αNIL␈↓,␈αbut␈αallowing␈αanything␈α␈↓¬but␈↓␈α␈↓αNIL␈↓␈αas␈αa␈αrepresentation
␈↓ α←␈↓␈↓ β∂of ␈↓
t␈↓. This allows several related tricks:
␈↓" ␈↓ α←␈↓␈↓ βK␈↓↓a.␈↓ Any expression may be used as a predicate, and
␈↓" ␈↓ α←␈↓␈↓ βK␈↓↓b.␈↓ Used as a predicate, ␈↓αnot[null[l]]␈↓ has the same effect as ␈↓αl␈↓.
␈↓"β␈↓ α←␈↓For␈α∃example,␈α⊗consider␈α∃the␈α∃following␈α⊗extended␈α∃version␈α∃of␈α⊗the␈α∃predicate
␈↓ α←␈↓␈↓αmember␈↓.
␈↓"∀␈↓ α←␈↓αmem␈↓λ'␈↓α <= λ[[x;l]␈↓ ∧+[null[l] → ␈↓
f␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ∧+ equal[x;first[l]] → l;
␈↓"β␈↓ α←␈↓α␈↓ ∧+ ␈↓
t␈↓α → mem␈↓λ'␈↓α[x;rest[l]]] ]
␈↓"∀␈↓ α←␈↓␈↓ β'This␈α∞"predicate",␈α
␈↓αmem␈↓λ'␈↓,␈α∞will␈α
return␈α∞␈↓
f␈↓␈α
if␈α∞no␈α
matching␈α∞element␈α∞is␈α
found,
␈↓ α←␈↓otherwise␈α∞it␈α
will␈α∞return␈α
the␈α∞list␈α∞beginning␈α
at␈α∞the␈α
match.␈α∞The␈α∞non-empty␈α
list
␈↓ α←␈↓can␈α∪be␈α∩used␈α∪as␈α∪an␈α∩indication␈α∪of␈α∩truth,␈α∪and␈α∪can␈α∩also␈α∪supply␈α∪the␈α∩calling
␈↓ α←␈↓function with the match if a match is found.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 7␈↓See the problem on hash consing on page 287.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 8␈↓Formally,␈αthe␈α
implementation␈α␈↓¬is␈↓␈α
wrong␈αin␈α
not␈αsatisfying␈αthe␈α
definition.
␈↓ α←␈↓Pragmatically, the implementation is convenient.
␈↓ α←␈↓␈↓5.5␈↓ εqA Few Programming Techniques 255␈↓
␈↓"β␈↓ α←␈↓␈↓↓3.␈↓␈α⊗Several␈α⊗implementations␈α⊗of␈α⊗conditional␈α⊗expressions␈α⊗allow␈α⊗"p␈↓βi␈↓"␈α⊗as␈α⊗an
␈↓ α←␈↓␈↓ β∂abbreviation␈αfor␈α"p␈↓βi␈↓ → p␈↓βi␈↓".␈αThe␈αcomputational␈αeffect␈αis␈αthe␈αsame,␈αbut␈αp␈↓βi␈↓␈αis
␈↓ α←␈↓␈↓ β∂only evaluated once.
␈↓"β␈↓ α←␈↓␈↓ β'Using this feature, ␈↓αmem␈↓λ'␈↓ could be written:
␈↓"∀␈↓ α←␈↓αmem␈↓λ'␈↓α <= λ[[x;l]␈↓ ∧+[null[l] → ␈↓
f␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ∧+ equal[x;first[l]] → l;
␈↓"β␈↓ α←␈↓α␈↓ ∧+ mem␈↓λ'␈↓α[x;rest[l]]] ]
␈↓"∀␈↓ α←␈↓This␈α∪feature␈α∀is␈α∪more␈α∀useful␈α∪in␈α∀contexts␈α∪where␈α∀we␈α∪wish␈α∀to␈α∪test␈α∀for␈α∪the
␈↓ α←␈↓existence␈α
of␈αan␈α
object␈α
and,␈αif␈α
a␈α
match␈αis␈α
found,␈α
do␈αsomething␈α
with␈αthe␈α
object.
␈↓ α←␈↓For example, the trick of page 213 could be written:
␈↓"∀␈↓ α←␈↓α␈↓ ∧C␈↓ ∧O[cont ← isspec[fun;spectbl];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧O isprim[] → ...; ...]
␈↓" ␈↓ α←␈↓α␈↓where:␈↓α
␈↓" ␈↓ α←␈↓α␈↓ ββisspec <= λ[[x;l]␈↓ ∧[[null[l] → ( );
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧[ eq[name[first[l]];x] → value[first[l]];
␈↓"β␈↓ α←␈↓α␈↓ ββ␈↓ ∧[ isspec[x;rest[l]]]]
␈↓"∀␈↓ α←␈↓Since␈αthe␈α
result␈αof␈α␈↓αvalue␈↓␈α
will␈αbe␈αa␈α
function␈αname,␈αand␈α
never␈α␈↓
f␈↓,␈αthere␈α
will␈αbe
␈↓ α←␈↓no ambiguity in using ␈↓αisspec␈↓ as a predicate.
␈↓"β␈↓ α←␈↓␈↓ ε≡␈↓↓Problem␈↓
␈↓"→␈↓ α←␈↓1. The␈α↔application␈α↔of␈α↔these␈α↔tricks␈α↔may␈α↔give␈α↔rise␈α↔to␈α↔some␈α⊗unaesthetic
␈↓ α←␈↓␈↓ β∂programs.␈α∩Typically␈α∪we␈α∩have␈α∪to␈α∩test␈α∩for␈α∪existence␈α∩then,␈α∪assuming␈α∩an
␈↓ α←␈↓␈↓ β∂instance␈αwas␈αdiscovered,␈αwe␈αhave␈αto␈αperform␈αfurther␈αcomputation␈αon␈α
that
␈↓ α←␈↓␈↓ β∂instance.␈↓π 9␈↓ Constructs like:
␈↓"∀␈↓ α←␈↓α␈↓ ¬⊂[it ← test[object] → smash[it]; ...]
␈↓"∀␈↓ α←␈↓␈↓ β∂arise.␈α
The␈α
objectional␈α
aspect␈α
involves␈α∞the␈α
variable␈α
␈↓αit␈↓.␈α
The␈α
variable␈α∞␈↓αit␈↓␈α
is
␈↓ α←␈↓␈↓ β∂not␈α≤local␈α≤to␈α≠the␈α≤conditional␈α≤expression.␈α≠Either␈α≤␈↓αit␈↓␈α≤is␈α≤global:␈α≠an
␈↓ α←␈↓␈↓ β∂unnecessarily␈α∃gratuitious␈α∃side-effect;␈α∃or␈α∃␈↓αit␈↓␈α∃is␈α∃bound␈α∃by␈α⊗an␈α∃enclosing
␈↓ α←␈↓␈↓ β∂λ-expression␈α∂or␈α∂a␈α∂␈↓αprog␈↓.␈α∂ In␈α⊂either␈α∂case␈α∂the␈α∂binding␈α∂is␈α∂too␈α⊂far␈α∂removed
␈↓ α←␈↓␈↓ β∂from its usage. Sussman and Steele [Sus 76] suggest the construct:
␈↓"∀␈↓ α←␈↓␈↓ ∧`␈↓αtest[<form␈↓β1␈↓>; ␈↓αλ[[x] ␈↓<form␈↓β2␈↓>]; <form␈↓β3␈↓>]
␈↓"∀␈↓ α←␈↓␈↓ β∂with␈α
the␈α
following␈α
semantics:␈α
evaluate␈α
<form␈↓β1␈↓>;␈α
if␈α
it␈α
gives␈α
a␈α
value␈αother
␈↓ α←␈↓␈↓ β∂than␈α
␈↓
f␈↓␈α
then␈α
apply␈α
the␈α
second␈α
argument,␈α
a␈α
unary␈α
function,␈α
to␈α
that␈αvalue.
␈↓ α←␈↓␈↓ β∂Otherwise evaluate <form␈↓β3␈↓>.
␈↓" ␈↓ α←␈↓␈↓ β∂Recast␈α⊗the␈α∃␈↓αisspec␈↓-argument␈α⊗of␈α⊗page 213␈α∃in␈α⊗terms␈α∃of␈α⊗␈↓αtest␈↓.␈α⊗ Give␈α∃an
␈↓ α←␈↓␈↓ β∂implementation of ␈↓αtest␈↓ by extending one of our evaluators.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 9␈↓If no further computation is necessary, trick No. ␈↓↓3␈↓ suffices.
␈↓ α←␈↓␈↓256 Static Structure␈↓
(5.6␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧Z␈↓↓5.6 Symbol Tables and Property-lists␈↓
␈↓"β␈↓ α←␈↓Since␈α∂we␈α∂are␈α∂examining␈α∂implementation␈α∂details,␈α∂and␈α∂since␈α∂manipulation␈α∞of
␈↓ α←␈↓symbol␈αtables␈α
is␈αsuch␈αa␈α
central␈αissue␈αin␈α
evaluation,␈αwe␈αshould␈α
also␈αscrutinize
␈↓ α←␈↓symbol␈α∪tables␈α∪and␈α∩their␈α∪organization.␈α∪ We␈α∩have␈α∪seen␈α∪two␈α∩fundamentally
␈↓ α←␈↓different␈αsymbol␈αtable␈αorganizations:␈α
deep␈αbinding␈αand␈αshallow␈α
binding.␈αWe
␈↓ α←␈↓should␈α⊂examine␈α⊂the␈α⊂implications␈α⊂of␈α⊂these␈α⊂organizations,␈α⊂and␈α⊂probe␈α∂deeper
␈↓ α←␈↓into␈αtheir␈αimplementation.␈α If␈αthe␈αnumber␈αof␈αentries␈αassociated␈αwith␈αan␈αatom
␈↓ α←␈↓is␈α
small␈α
then␈α
shallow␈α
binding␈α
may␈α
be␈α
advantageous.␈α
If␈α
the␈α
number␈αof␈α
entries
␈↓ α←␈↓associated␈α⊂with␈α∂an␈α⊂atom␈α∂is␈α⊂very␈α∂large␈α⊂then␈α∂the␈α⊂shallow␈α⊂binding␈α∂technique
␈↓ α←␈↓may␈α⊃be␈α⊃too␈α⊃costly␈α⊂and␈α⊃deep␈α⊃binding␈α⊃or␈α⊂yet␈α⊃another␈α⊃organization␈α⊃may␈α⊂be
␈↓ α←␈↓required ([McD 75]).
␈↓"β␈↓ α←␈↓␈↓ β'Recall␈α
our␈α
discussion␈α
of␈α
␈↓αgetval␈↓,␈α
␈↓αaddval␈↓,␈α
and␈α
␈↓αgetval_cell␈↓␈α∞in␈α
Section 3.11.
␈↓ α←␈↓These␈α⊂functions␈α∂were␈α⊂developed␈α∂to␈α⊂describe␈α∂shallow␈α⊂binding,␈α∂but␈α⊂they␈α∂are
␈↓ α←␈↓illustrative␈α⊃of␈α⊂a␈α⊃more␈α⊂general␈α⊃idea.␈α⊂In␈α⊃symbol␈α⊂manipulation␈α⊃and␈α⊂symbolic
␈↓ α←␈↓programming,␈α∂we␈α∂often␈α∞want␈α∂to␈α∂be␈α∂able␈α∞to␈α∂associate␈α∂a␈α∞set␈α∂of␈α∂data␈α∂with␈α∞an
␈↓ α←␈↓item.␈α⊃For␈α⊂example,␈α⊃an␈α⊂algebraic␈α⊃simplifier␈α⊂would␈α⊃like␈α⊂to␈α⊃know␈α⊃whether␈α⊂a
␈↓ α←␈↓specific␈α∞operator␈α
is␈α∞commutative;␈α
if␈α∞so,␈α
certain␈α∞simplifications␈α
are␈α∞valid.␈α
We
␈↓ α←␈↓could␈α⊃maintain␈α⊃a␈α⊃list␈α⊃of␈α⊂all␈α⊃commutative␈α⊃operations␈α⊃and␈α⊃require␈α⊃that␈α⊂the
␈↓ α←␈↓simplifier␈α∀check␈α∀that␈α∪list.␈α∀But␈α∀since␈α∀commutativity␈α∪is␈α∀a␈α∀property␈α∀of␈α∪the
␈↓ α←␈↓operator␈αit␈αseems␈αmore␈αnatural␈α
to␈αassociate␈αthe␈αproperty␈αwith␈α
that␈αoperation.
␈↓ α←␈↓Search considerations also arise if the list of operations is long.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α⊂generalize␈α∂the␈α⊂idea␈α⊂expressed␈α∂in␈α⊂␈↓αgetval␈↓␈α∂and␈α⊂␈↓αaddval␈↓,␈α⊂allowing␈α∂the
␈↓ α←␈↓association␈α∞of␈α∞an␈α
arbitrary␈α∞collection␈α∞of␈α
␈↓↓property-value␈↓␈α∞pairs␈α∞with␈α∞an␈α
atom.
␈↓ α←␈↓With␈α∞each␈α
atom␈α∞we␈α
will␈α∞associate␈α
a␈α∞list␈α
called␈α∞the␈α
␈↓↓property-list␈↓␈α∞or␈α
␈↓↓p-list␈↓.␈↓π 10␈↓
␈↓ α←␈↓The␈α∩names,␈α∩␈↓↓attribute␈↓␈α∪or␈α∩␈↓↓indicator␈↓,␈α∩are␈α∪sometimes␈α∩used␈α∩as␈α∪synonyms␈α∩for
␈↓ α←␈↓property. An atom is frequently called a ␈↓↓carrier␈↓ of the properties.
␈↓"β␈↓ α←␈↓␈↓ β'A␈αproperty␈αlist␈αis␈αa␈αtable␈αof␈αproperties␈αand␈αproperty values.␈αThe␈αsize␈αof
␈↓ α←␈↓the property list is not fixed, but can shrink and grow during a computation.
␈↓"⊃␈↓ α←␈↓
⊂αααααααπααααα⊃
␈↓"␈↓ α←␈↓
~ prop1 ~ val1~
␈↓"␈↓ α←␈↓
εαααααααβαααααλ
␈↓"␈↓ α←␈↓
~ prop2 ~ val2~
␈↓"␈↓ α←␈↓
# # #
␈↓"␈↓ α←␈↓
εαααααααβαααααλ
␈↓"␈↓ α←␈↓
~ propn ~ valn~
␈↓"␈↓ α←␈↓
%ααααααα∀ααααα$
␈↓"∀␈↓ α←␈↓We␈α
have␈α∞seen␈α
an␈α∞identical␈α
diagram␈α
in␈α∞Section 2.5;␈α
property␈α∞lists␈α
are␈α∞a␈α
very
␈↓ α←␈↓effective␈α⊗tool␈α↔for␈α⊗modelling␈α⊗data␈α↔bases.␈↓π 11␈↓␈α⊗Thought␈α⊗of␈α↔as␈α⊗abstractions,
␈↓ α←␈↓property␈α⊃lists␈α⊃are␈α⊃symbol␈α⊃tables.␈α⊃The␈α⊃name-entries␈α⊃are␈α⊃properties␈α⊃and␈α⊃the
␈↓ α←␈↓value entries are the property values.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 10␈↓Property␈αlists␈αwere␈αintroduced␈αto␈αprogramming␈αlanguages␈αin␈αthe␈αIPL
␈↓ α←␈↓series of languages [New 61].
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 11␈↓They␈α∞are␈α∞by␈α∞no␈α∞means␈α∞the␈α∞␈↓¬only␈↓␈α∞or␈α∞␈↓¬best␈↓␈α∞way␈α∞of␈α∞representing␈α∞a␈α∞data
␈↓ α←␈↓base. See [McD 75].
␈↓ α←␈↓␈↓5.6␈↓ εdSymbol Tables and Property-lists 257␈↓
␈↓"β␈↓ α←␈↓␈↓ β'We␈αidentify␈α
an␈αatom␈α
with␈αits␈αproperty␈α
list.␈α For␈α
example,␈αif␈α
we␈αwished
␈↓ α←␈↓to␈α
represent␈αthe␈α
"+"-operation␈αas␈α
the␈α
atom␈α␈↓αPLUS␈↓␈α
and␈αwished␈α
to␈α
signify␈αthat
␈↓ α←␈↓the␈α↔operation␈α↔is␈α_commutative␈α↔and␈α↔binary,␈α_the␈α↔atom␈α↔␈↓αPLUS␈↓␈α_might␈α↔be
␈↓ α←␈↓represented as:
␈↓"⊃␈↓ α←␈↓
PLUS ⊂αααααααπααα⊃
␈↓"␈↓ α←␈↓
~ commu ~ T ~
␈↓"␈↓ α←␈↓
εαααααααβαααλ
␈↓"␈↓ α←␈↓
~ arity ~ 2 ~
␈↓"␈↓ α←␈↓
%ααααααα∀ααα$
␈↓"∀␈↓ α←␈↓In␈αthese␈αkinds␈αof␈αapplications,␈αwe␈αare␈αusing␈αthe␈αatom␈αas␈αa␈αdata␈αstructure␈αand
␈↓ α←␈↓attaching␈α⊃properties␈α⊃to␈α⊃that␈α⊃atom␈α⊃rather␈α⊃than␈α⊃thinking␈α⊃of␈α⊃the␈α⊃atom␈α∩as␈α⊃a
␈↓ α←␈↓representation of an identifier.
␈↓"β␈↓ α←␈↓For example:
␈↓"⊃␈↓ α←␈↓
CAR ⊂αααααααπααααααα⊃
␈↓"␈↓ α←␈↓
~ MFGR ~ BUICK ~
␈↓"␈↓ α←␈↓
εαααααααβαααααααλ
␈↓"␈↓ α←␈↓
~ YEAR ~ 1959 ~
␈↓"␈↓ α←␈↓
%ααααααα∀ααααααα$
␈↓"∀␈↓ α←␈↓These␈α≤same␈α≤techniques␈α≤are␈α≤applicable␈α≤when␈α≤we␈α≤consider␈α≤atoms␈α≠as
␈↓ α←␈↓representations␈α⊂of␈α⊂variables␈α⊂as␈α⊂used␈α⊂in␈α⊂the␈α⊂evaluation␈α⊂process.␈α⊂In␈α⊃fact,␈α⊂an
␈↓ α←␈↓atom␈αcan␈αsimultaneously␈αbe␈αused␈αas␈αa␈αcarrier␈αof␈αa␈αvalue␈αand␈αcan␈αalso␈αhave␈αa
␈↓ α←␈↓property list:
␈↓"⊃␈↓ α←␈↓
E4
␈↓"␈↓ α←␈↓
⊂αααπααα⊃
␈↓"␈↓ α←␈↓
⊂←β X ~ 3 ~
␈↓"␈↓ α←␈↓
↓ εαααβαααλ
␈↓"␈↓ α←␈↓
# # # E3
␈↓"␈↓ α←␈↓
↓ %ααα∀ααα∀→αα→⊂αααπααα⊃
␈↓"␈↓ α←␈↓
~ # # #
␈↓"␈↓ α←␈↓
↓ εαααβαααλ
␈↓"␈↓ α←␈↓
%→ααα→π←αα←ααα←β X ~ A ~ E1
␈↓"␈↓ α←␈↓
↓ ↑ %ααα∀ααα∀→ααα→⊂αααπααα⊃
␈↓"␈↓ α←␈↓
⊂αααααααπααααα⊃~ # # #
␈↓"␈↓ α←␈↓
~ prop1 ~ val1~~ εαααβαααλ
␈↓"␈↓ α←␈↓
# # # %←ααααα←ααααα←ααβ X ~ B ~
␈↓"␈↓ α←␈↓
εαααααααβαααααλ εαααβαααλ
␈↓"␈↓ α←␈↓
~ propn ~ valn~ # # #
␈↓"␈↓ α←␈↓
%ααααααα∀ααααα$ %ααα∀ααα$
␈↓"␈↓ α←␈↓
␈↓p-list for ␈↓αX␈↓
␈↓ α←␈↓␈↓258 Static Structure␈↓
(5.6␈↓
␈↓"β␈↓ α←␈↓All␈αinstances␈α
of␈α␈↓αX␈↓␈αshare␈α
a␈αcommon␈αproperty␈α
list,␈αbut␈αthe␈α
value,␈αor␈αbinding␈α
of
␈↓ α←␈↓␈↓αx␈↓␈αis␈αfound␈αusing␈αthe␈αenvironment␈αchain.␈α This␈αexample␈αis␈αdescribed␈αin␈αterms
␈↓ α←␈↓of␈α∞deep␈α∞binding,␈α∞but␈α
the␈α∞property-list␈α∞idea␈α∞is␈α
also␈α∞directly␈α∞applicable␈α∞to␈α
the
␈↓ α←␈↓shallow␈α∀binding␈α∪schemes.␈α∀ To␈α∪adapt␈α∀p-lists␈α∪to␈α∀the␈α∪first␈α∀shallow␈α∪binding
␈↓ α←␈↓scheme␈α⊂of␈α⊂page 151,␈α∂we␈α⊂introduce␈α⊂a␈α∂property␈α⊂named␈α⊂␈↓αVAR␈↓␈α⊂whose␈α∂property
␈↓ α←␈↓value will be the collection of environment-value pairs:
␈↓"⊃␈↓ α←␈↓
X ⊂αααααααπαααααα⊃ ⊂ααααπααα⊃
␈↓"␈↓ α←␈↓
~ VAR ~ #αβα→αα→~ E4 ~ 3 ~
␈↓"␈↓ α←␈↓
εαααααααβααααααλ εααααβαααλ
␈↓"␈↓ α←␈↓
~ prop1 ~ val1 ~ ~ E3 ~ A ~
␈↓"␈↓ α←␈↓
εαααααααβααααααλ εααααβαααλ
␈↓"␈↓ α←␈↓
# # # ~ E1 ~ B ~
␈↓"␈↓ α←␈↓
εαααααααβααααααλ %αααα∀ααα$
␈↓"␈↓ α←␈↓
~ propn ~ valn ~
␈↓"␈↓ α←␈↓
%ααααααα∀αααααα$
␈↓"∀␈↓ α←␈↓Similarly, the second shallow binder could use:
␈↓"⊃␈↓ α←␈↓
X ⊂αααααααπαααααα⊃
␈↓"␈↓ α←␈↓
~ VALUE ~ #αβα→␈↓<current value>␈↓
␈↓"␈↓ α←␈↓
%ααααααα∀αααααα$
␈↓"∀␈↓ α←␈↓␈↓ β'In␈αsummary,␈αproperty␈αlists␈αare␈αa␈αlanguage␈αfeature␈αwhich␈αis␈αindependent
␈↓ α←␈↓of␈αthe␈αbinding␈αstrategy␈αwe␈αhave␈αimplemented.␈αA␈αproperty␈αlist␈αcan␈αcontain␈αall
␈↓ α←␈↓the␈αaspects␈α
of␈αan␈α
atom␈αwhich␈αwe␈α
wish␈αto␈α
consider;␈αthe␈α
class␈αof␈αattributes␈α
need
␈↓ α←␈↓not␈α⊃be␈α∩fixed,␈α⊃but␈α⊃can␈α∩vary␈α⊃during␈α⊃the␈α∩execution␈α⊃of␈α⊃the␈α∩program.␈α⊃Those
␈↓ α←␈↓properties␈α∃need␈α∃not␈α∃involve␈α∃the␈α∃fact␈α∃that␈α∃atoms␈α∃are␈α∃used␈α⊗to␈α∃represent
␈↓ α←␈↓identifiers␈α∞when␈α∞we␈α∞map␈α∞the␈α
meta language␈α∞onto␈α∞data␈α∞structures.␈α∞The␈α
deep
␈↓ α←␈↓binding␈αimplementation␈αemphasizes␈αthat␈αthe␈αvalue␈αof␈αa␈αvariable␈αis␈α
associated
␈↓ α←␈↓with␈α⊂the␈α⊂environment␈α⊃in␈α⊂which␈α⊂that␈α⊂value␈α⊃is␈α⊂created.␈α⊂However␈α⊃a␈α⊂shallow
␈↓ α←␈↓binding␈α
organization␈α
associates␈α
values␈α
with␈α
variables,␈α
and␈α
thus␈α
it␈α
is␈αnatural
␈↓ α←␈↓to think of property-lists when thinking of shallow binding.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α
wish␈α
to␈αlook␈α
more␈α
closely␈αat␈α
the␈α
value␈αaspects␈α
of␈α
atoms.␈α
We␈αhave
␈↓ α←␈↓seen␈α∃three␈α∃properties␈α∃related␈α∃to␈α∀the␈α∃value␈α∃of␈α∃a␈α∃variable:␈α∃simple␈α∀value,
␈↓ α←␈↓call-by-value␈αfunction␈α
(expr)␈αand␈αcall-unevaluated␈α
function␈α(fexpr).␈α
We␈αwere
␈↓ α←␈↓able␈αto␈αdistinguish␈αbetween␈αexprs␈αand␈αfexprs:␈αeither␈αplace␈αthe␈αfexpr␈αname␈αin
␈↓ α←␈↓a␈α⊗special␈α⊗list;␈α∃or␈α⊗store␈α⊗the␈α∃fexpr␈α⊗as␈α⊗a␈α∃β-expression,␈α⊗rather␈α⊗than␈α⊗as␈α∃a
␈↓ α←␈↓λ-expression.␈α⊂ We␈α⊂made␈α⊂no␈α⊂explicit␈α⊂distinction␈α⊂between␈α⊂simple␈α⊂values␈α⊂and
␈↓ α←␈↓function␈α∂values;␈α∞if␈α∂a␈α∞simple␈α∂value␈α∂appeared␈α∞in␈α∂the␈α∞function␈α∂position␈α∂of␈α∞an
␈↓ α←␈↓application,␈α∂we␈α∂evaluated␈α∂that␈α⊂expression␈α∂until␈α∂we␈α∂␈↓¬did␈↓␈α∂discover␈α⊂a␈α∂function
␈↓ α←␈↓object.␈↓π 12␈↓␈αIf␈αa␈αfunction␈αappeared␈αin␈αa␈αposition␈αexpecting␈αa␈αsimple␈αvalue,␈αthen
␈↓ α←␈↓the data structure interpretation of the function object was taken.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 12␈↓It␈α∂may␈α∂be␈α∂a␈α∂bad␈α∂idea␈α∂to␈α∂distinguish␈α∂between␈α∂the␈α∂evaluation␈α∂of␈α∞an
␈↓ α←␈↓argument position and the evaluation of a function position.
␈↓ α←␈↓␈↓5.6␈↓ εdSymbol Tables and Property-lists 259␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Since␈α⊂"<="␈α⊂and␈α⊂"<␈↓βf␈↓="␈α⊂were␈α⊂defined␈α⊂to␈α⊂place␈α⊂the␈α⊂appropriate␈α∂function
␈↓ α←␈↓definition␈αin␈αthe␈αglobal␈αtable,␈αwe␈αcan␈αinterpret␈αthese␈αoperations␈αas␈α
associating
␈↓ α←␈↓the␈α
definition␈αwith␈α
the␈αatom.␈α
That␈α
is,␈αbeing␈α
an␈αexpr␈α
or␈α
fexpr␈αis␈α
a␈αproperty␈α
of
␈↓ α←␈↓the␈αatom.␈αSimilarly,␈αglobally␈αbound␈αvariables␈α
like␈α␈↓αt␈↓␈αand␈α␈↓αnil␈↓␈αplay␈αthe␈α
roles␈αof
␈↓ α←␈↓constants␈αand␈αtherefore␈αcan␈αbe␈αinterpreted␈αas␈αhaving␈αa␈αvalue␈αassociated␈αwith
␈↓ α←␈↓them.␈α∩Primitive␈α∩functions␈α∩like␈α∩␈↓αcar␈↓,␈α∩and␈α∩primitive␈α∩special␈α∩forms␈α∩like␈α∩␈↓αcond␈↓
␈↓ α←␈↓should␈αalso␈αbe␈α
considered␈αconstants.␈αTheir␈α
"values"␈αare␈αfixed␈α
procedures␈αfor
␈↓ α←␈↓specific call-by-value and call-unevaluated operations, respectively.␈↓π 13␈↓
␈↓"β␈↓ α←␈↓␈↓ β'This␈α~discussion␈α~exemplifies␈α→five␈α~value-like␈α~properties␈α~which␈α→are
␈↓ α←␈↓properties of an atom, rather than properties of a particular environment:␈↓π 14␈↓
␈↓"∀␈↓ α←␈↓␈↓ β∂␈↓αCONST␈↓␈↓ ¬;simple value; used as a constant
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓αEXPR␈↓␈↓ ¬;call-by value definition
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓αFEXPR␈↓␈↓ ¬;call-unevaluated definition
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓αSUBR␈↓␈↓ ¬;call-by-value primitive
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓αFSUBR␈↓␈↓ ¬;call-unevaluated primitive
␈↓"∀␈↓ α←␈↓␈↓ β'In␈α
Section 6.18␈α
we␈αwill␈α
introduce␈α
another␈αprotocol␈α
for␈α
assigning␈αvalues
␈↓ α←␈↓to␈α∂variables,␈α∞but␈α∂at␈α∂any␈α∞one␈α∂time␈α∞an␈α∂atom␈α∂may␈α∞have␈α∂at␈α∞most␈α∂one␈α∂of␈α∞these
␈↓ α←␈↓value-related indicators.␈↓π 15␈↓
␈↓"β␈↓ α←␈↓For example, ␈↓αcar␈↓ might be represented as:
␈↓"⊃␈↓ α←␈↓
⊂ααααααπααα⊃
␈↓"␈↓ α←␈↓
~ SUBR ~ #αβ→␈↓To machine language code for ␈↓αcar␈↓
␈↓"␈↓ α←␈↓
%αααααα∀ααα$
␈↓"β␈↓ α←␈↓␈↓ Part of the atom-structure for ␈↓αCAR␈↓ ␈↓α
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 13␈↓Many␈α∩implementations␈α∩are␈α∩less␈α∩restrictive:␈α∩␈↓↓1.␈↓␈α∩␈↓αT␈↓␈α∩and␈α∩␈↓αNIL␈↓␈α∩can␈α∩be
␈↓ α←␈↓redefined;␈α
␈↓↓2.␈↓␈α
␈↓αcar␈↓,␈α∞␈↓αcdr␈↓␈α
and␈α
the␈α
other␈α∞primitives␈α
can␈α
be␈α
redefined.␈α∞Allowing␈α
␈↓↓1␈↓
␈↓ α←␈↓will␈α⊃always␈α⊃lead␈α⊂to␈α⊃grief.␈α⊃␈↓↓2␈↓␈α⊂is␈α⊃justified␈α⊃only␈α⊂for␈α⊃debugging␈α⊃calls␈α⊃on␈α⊂these
␈↓ α←␈↓functions;␈α∩however,␈α∩if␈α∩one␈α∩programs␈α∩abstractly,␈α∩there␈α∩will␈α∩be␈α∩no␈α∩need␈α∩to
␈↓ α←␈↓debug such low level calls.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 14␈↓We␈αwill␈αdiscuss␈αthe␈α␈↓αVAR␈↓␈αand␈α␈↓αVALUE␈↓␈αproperties␈αlater.␈α That␈αwill␈αbe
␈↓ α←␈↓the intent of Section 5.19.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 15␈↓Several␈αimplementations␈αallow␈αatoms␈αto␈αhave␈αseveral␈αof␈αthese␈αsystem
␈↓ α←␈↓properties.␈αThus␈α
expressions␈αlike␈α
␈↓αcar[car]␈↓␈αare␈α
executable.␈αThe␈α
implementation
␈↓ α←␈↓uses␈αcontext␈α
to␈αdetermine␈αwhich␈α
␈↓αcar␈↓␈αis␈αa␈α
simple␈αvariable␈αand␈α
which␈α␈↓αcar␈↓␈αis␈α
the
␈↓ α←␈↓primitive␈αprocedure.␈α The␈αcurrent␈α␈↓αeval␈↓␈α␈↓¬will␈↓␈αoperate␈αcorrectly␈αon␈αthis␈αexample
␈↓ α←␈↓since␈α
a␈α
recognizer␈α
for␈α
the␈α
function␈α
␈↓αcar␈↓␈α
is␈α
explicitly␈α
encoded␈α
in␈α
␈↓αapply␈↓,␈αbut␈α
such
␈↓ α←␈↓tricks␈α∞lead␈α∂to␈α∞unnecessarily␈α∂mysterious␈α∞programs.␈α∞ For␈α∂the␈α∞same␈α∂reason,␈α∞the
␈↓ α←␈↓LISP␈α∞obscenity␈α∞␈↓αλ[[lambda] ... ]␈↓␈α∞will␈α∞work.␈α∞Notice␈α∞its␈α∞S-expr␈α∞representation␈α∞is
␈↓ α←␈↓␈↓α(LAMBDA (LAMBDA) ␈α... )␈↓.␈αContext␈αis␈αused␈αby␈αan␈αevaluator␈αin␈αslightly␈αless
␈↓ α←␈↓obnoxious␈α∞ways.␈α∞For␈α
example,␈α∞an␈α∞evaluator␈α
for␈α∞␈↓αprog␈↓␈α∞can␈α
tell␈α∞a␈α∞reference␈α
to
␈↓ α←␈↓the␈α?␈α∪label␈α?␈α∪␈↓αx␈↓␈α?␈α∀from␈α?␈α∪the␈α?␈α∪␈↓αprog␈↓-variable␈α?␈α∀␈↓αx␈↓␈α?␈α∪in
␈↓ α←␈↓␈↓α ... prog[[x;y] ... x f[..] ... g[x ... ] ... go[x].␈↓ See page 189.
␈↓ α←␈↓␈↓260 Static Structure␈↓
(5.6␈↓
␈↓"β␈↓ α←␈↓When␈α⊂␈↓αapply␈↓␈α∂recognizes␈α⊂␈↓αCAR␈↓␈α⊂as␈α∂a␈α⊂representation␈α⊂for␈α∂the␈α⊂function␈α⊂␈↓αcar␈↓,␈α∂the
␈↓ α←␈↓machine-dependent␈α∞code␈α∞will␈α
be␈α∞executed,␈α∞but␈α
␈↓αCAR␈↓␈α∞can␈α∞also␈α
be␈α∞used␈α∞as␈α
an
␈↓ α←␈↓atomic data structure. For example:
␈↓"∀␈↓ α←␈↓in ␈↓αeval[(CAR (QUOTE (CAR BMW)));( )]␈↓ both uses of ␈↓αCAR␈↓ will appear.
␈↓"∀␈↓ α←␈↓As a further example, consider the representation of:
␈↓" ␈↓ α←␈↓␈↓ ∧≥␈↓αfact <= λ[[x][x=0 → 1; ␈↓
t␈↓α → times[x;fact[sub1[x]]]]]
␈↓" ␈↓ α←␈↓α␈↓The right-hand side would be:
␈↓" ␈↓ α←␈↓α␈↓ β∂(LAMBDA (X) (COND␈↓ ¬S((ZEROP X) 1)
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ¬S(T (TIMES␈↓ ε{X
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ ¬S␈↓ ε{(FACT (SUB1 X))))))
␈↓"∀␈↓ α←␈↓To␈α
represent␈α
the␈α
intention␈α
that␈α
␈↓αfact␈↓␈α
is␈α
to␈α
be␈α
defined␈α
as␈α
the␈α
above␈α
recursive
␈↓ α←␈↓function,␈αwe␈αmight␈αstore␈αthe␈αS-expr␈αrepresentation␈αon␈αthe␈αproperty-list␈αof␈αthe
␈↓ α←␈↓atom␈α⊂␈↓αFACT␈↓␈α⊂and␈α⊂use␈α⊂␈↓αEXPR␈↓␈α⊂as␈α∂its␈α⊂indicator.␈α⊂The␈α⊂occurrence␈α⊂of␈α⊂the␈α∂atom
␈↓ α←␈↓␈↓αFACT␈↓␈αin␈αthe␈αλ-expression␈αis␈αrepresented␈αas␈αa␈αpointer␈αto␈αthe␈αatomic␈αstructure
␈↓ α←␈↓of ␈↓
FACT␈↓.
␈↓"β␈↓ α←␈↓Here is part of the atom-structure for ␈↓αFACT␈↓:
␈↓"⊃␈↓ α←␈↓
⊂ααααααπαααα⊃
␈↓"␈↓ α←␈↓
FACT ~ EXPR ~ #αβ→⊃
␈↓"␈↓ α←␈↓
⊂αα→%αααααα∀αααα$ ~
␈↓"␈↓ α←␈↓
↑ ⊂←αααα←ααααα←αα$
␈↓"␈↓ α←␈↓
~ %→⊂ααααααααπααα⊃ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
~ ~ LAMBDA ~ #αβαα→~ # ~ #αβα→~ # ~≤'~
␈↓"␈↓ α←␈↓
~ %αααααααα∀ααα$ %αβα∀ααα$ %αβα∀αα$
␈↓"␈↓ α←␈↓
↑ ~ ↓
␈↓"␈↓ α←␈↓
~ ⊂αααααααααα$ ⊂ααααααπααα⊃
␈↓"␈↓ α←␈↓
~ ↓ ~ COND ~ # ~
␈↓"␈↓ α←␈↓
~ ⊂αααπαα⊃ %αααααα∀αβα$
␈↓"␈↓ α←␈↓
↑ ~ X ~≤'~ ↓
␈↓"␈↓ α←␈↓
~ %ααα∀αα$ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
~ ~ # ~ #αβ→~ # ~≤'~
␈↓"␈↓ α←␈↓
~ %αβα∀ααα$ %αβα∀αα$
␈↓"␈↓ α←␈↓
↑ ↓ ↓
␈↓"␈↓ α←␈↓
. . . . . . . . .
␈↓"␈↓ α←␈↓
~ ↓
␈↓"␈↓ α←␈↓
~ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
↑ ~ # ~ #αβ→~ # ~≤'~
␈↓"␈↓ α←␈↓
~ %αβα∀ααα$ %αβα∀αα$
␈↓"␈↓ α←␈↓
%←αααα←αααααα←ααααα←ααααα←ααααα←ααααα←$ ↓
␈↓"␈↓ α←␈↓
⊂←αααααα←αααα$
␈↓"␈↓ α←␈↓
↓
␈↓"␈↓ α←␈↓
⊂ααααααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
~ SUB1 ~ #αβ→~ X ~≤'~
␈↓"␈↓ α←␈↓
%αααααα∀ααα$ %ααα∀αα$
␈↓"β␈↓ α←␈↓␈↓ Atom-structure for ␈↓αFACT␈↓ ␈↓α
␈↓"→␈↓ α←␈↓Every␈α⊂occurrence␈α⊂of␈α⊃an␈α⊂atom␈α⊂--␈↓
EXPR␈↓,␈α⊂␈↓
LAMBDA␈↓,␈α⊃and␈α⊂so␈α⊂forth--␈α⊂is␈α⊃actually␈α⊂a
␈↓ α←␈↓reference␈α
to␈αthe␈α
appropriate␈αatomic␈α
structure.␈α Note␈α
that␈αboth␈α
instances␈α
of␈α␈↓
X␈↓
␈↓ α←␈↓are␈αactually␈αpointers␈αto␈αthe␈α
representation␈αof␈α␈↓αX␈↓,␈αbut␈αthat␈α
two␈αrepresentations
␈↓ α←␈↓␈↓5.6␈↓ εdSymbol Tables and Property-lists 261␈↓
␈↓"β␈↓ α←␈↓of␈α
␈↓α(X)␈↓␈α
will␈α
typically␈α
be␈α
distinct.␈α
Also␈α
keep␈α
in␈α
mind␈α
that␈α
we␈α
are␈α∞storing␈α
the
␈↓ α←␈↓data structure ␈↓¬representation␈↓ of the ␈↓αfact␈↓ function.
␈↓"∀␈↓ α←␈↓␈↓ βW␈↓α(LAMBDA (X) (COND ((ZEROP X) 1)
␈↓ α←␈↓α␈↓ βW (T (TIMES X (FACT (SUB1 X))))) )
␈↓"∀␈↓ α←␈↓is␈α∞a␈α∞perfectly␈α∞good␈α∞list.␈α∞ If␈α∞we␈α∞attached␈α∞it␈α∞to␈α∞the␈α∞indicator␈α∞␈↓αVALUE␈↓␈α∞then␈α∞we
␈↓ α←␈↓would have represented the list as the ␈↓¬simple␈↓ value of ␈↓αfact␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Representations␈α∞of␈α∞special␈α∞forms␈α∞like␈α∞␈↓αCOND␈↓␈α∞and␈α∞␈↓αQUOTE␈↓␈α∞give␈α∞rise␈α
to
␈↓ α←␈↓the␈α↔indicator␈α↔␈↓αFSUBR␈↓.␈α↔ When␈α↔an␈α↔instance␈α↔of␈α↔such␈α↔a␈α↔special␈α↔form␈α⊗is
␈↓ α←␈↓recognized,␈α_the␈α↔argument␈α_list␈α_is␈α↔passed␈α_to␈α↔the␈α_primitive␈α_without␈α↔any
␈↓ α←␈↓evaluation.␈α∩ In␈α∩a␈α∪similar␈α∩manner␈α∩we␈α∪introduce␈α∩the␈α∩indicator␈α∪␈↓αFEXPR␈↓␈α∩to
␈↓ α←␈↓designate the occurrence of a "<␈↓βf␈↓=" definition.
␈↓"β␈↓ α←␈↓␈↓ β'Our␈α∞discussion␈α∂of␈α∞property␈α∞lists␈α∂as␈α∞carriers␈α∞of␈α∂values␈α∞has␈α∂centered␈α∞on
␈↓ α←␈↓the␈α
representations␈αof␈α
constants;␈αmany␈α
identifiers␈α
in␈αLISP␈α
are␈αconstants.␈α
Even
␈↓ α←␈↓though␈α∃we␈α∃can␈α⊗redefine␈α∃functions␈α∃using␈α∃a␈α⊗version␈α∃of␈α∃"<=",␈α⊗most␈α∃such
␈↓ α←␈↓definitions␈α_are␈α↔relatively␈α_constant.␈↓π 16␈↓␈α↔The␈α_primitive␈α↔functions␈α_are␈α↔also
␈↓ α←␈↓constant.␈α~Shallow␈α→binding␈α~tries␈α→to␈α~capitalize␈α→on␈α~this␈α~observation,␈α→by
␈↓ α←␈↓associating␈α
all␈α
of␈α
the␈α
value␈α
aspects␈α
of␈α
a␈α
variable␈α
with␈α
the␈α
property␈α∞list,␈α
and
␈↓ α←␈↓we␈α∃will␈α∃soon␈α∃see␈α∃that␈α⊗for␈α∃several␈α∃interesting␈α∃subsets␈α∃of␈α∃LISP,␈α⊗we␈α∃can
␈↓ α←␈↓significantly␈α⊃simplify␈α∩the␈α⊃handling␈α⊃of␈α∩shallow␈α⊃binding.␈α∩ Before␈α⊃discussing
␈↓ α←␈↓that,␈α
we␈α
will␈α
show␈α
how␈α
an␈α
evaluator␈α
might␈α
use␈α
such␈αproperty-list␈α
information.
␈↓ α←␈↓This requires the introduction of property-list manipulating functions.
␈↓"β␈↓ α←␈↓␈↓ ¬∨␈↓↓5.7 Property-list Functions␈↓
␈↓"β␈↓ α←␈↓There are four functions for manipulating the property-list:
␈↓"β␈↓ α←␈↓␈↓αputprop[a;v;p]␈↓␈αwill␈αput␈αthe␈αvalue␈α␈↓αv␈↓␈αunder␈αthe␈αproperty␈α␈↓αp␈↓␈αon␈αthe␈αproperty-list
␈↓ α←␈↓␈↓ β'of␈α
the␈αatom␈α
␈↓αa␈↓.␈αIf␈α
the␈αproperty␈α
␈↓αp␈↓␈αalready␈α
appears␈αon␈α
the␈αp-list␈α
then␈αthe␈α
␈↓αv␈↓
␈↓ α←␈↓␈↓ β'over-writes␈αthe␈αold␈αvalue;␈αotherwise␈αa␈αnew␈αproperty-value␈αpair␈αis␈αadded
␈↓ α←␈↓␈↓ β'to the front of the p-list of ␈↓αa␈↓. The value returned by ␈↓αputprop␈↓ is ␈↓αv␈↓.␈↓π 17␈↓
␈↓"∀␈↓ α←␈↓␈↓αget[x;i]␈↓␈αwill␈αsearch␈αthe␈αproperty-list␈αof␈αthe␈αatom␈α␈↓αx␈↓␈αlooking␈αfor␈αthe␈αindicator␈α␈↓αi␈↓.
␈↓ α←␈↓␈↓ β'If␈α␈↓αi␈↓␈αis␈αfound␈αthe␈αvalue␈αassociated␈αwith␈αthat␈αindicator␈αis␈αreturned␈αby␈α␈↓αget␈↓.
␈↓ α←␈↓␈↓ β'If␈α⊂␈↓αx␈↓␈α⊂does␈α⊂not␈α⊂have␈α⊂the␈α∂indicator␈α⊂then␈α⊂␈↓
f␈↓␈α⊂is␈α⊂returned.␈α⊂ Thus␈α∂␈↓αgetval[x]␈↓
␈↓ α←␈↓␈↓ β'could be defined as ␈↓αget[x;VAR].
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 16␈↓To define a temporary function we use ␈↓αlabel␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 17␈↓In␈α
some␈αimplementations,␈α
the␈αold␈α
pair␈αis␈α
removed␈αand␈α
the␈α
new␈αpair
␈↓ α←␈↓is␈α∂added␈α∂to␈α∂the␈α∂front␈α∂of␈α∂the␈α∂p-list.␈α∂The␈α∂idea␈α∂is␈α∂that␈α∂since␈α∂short␈α⊂p-lists␈α∂are
␈↓ α←␈↓usually␈α
searched␈αlinearly,␈α
one␈α
should␈αhave␈α
the␈αmost␈α
popular␈α
properties␈αnear
␈↓ α←␈↓the␈α
front␈αof␈α
the␈αlist.␈α
This␈αreorganization␈α
of␈α
the␈αtable␈α
can␈αeven␈α
be␈αextended␈α
to
␈↓ α←␈↓include␈α∞␈↓¬references␈↓␈α∞to␈α∞properties,␈α∞always␈α∞moving␈α∞the␈α∞last␈α∂referenced␈α∞property
␈↓ α←␈↓pair to the front of the list [Riv 76].
␈↓ α←␈↓␈↓262 Static Structure␈↓
(5.7␈↓
␈↓"β␈↓ α←␈↓␈↓αgetl[x;l]␈↓␈αwill␈αsearch␈αthe␈αproperty-list␈αof␈αthe␈αatom␈α␈↓αx␈↓␈αfor␈αthe␈α␈↓¬first␈↓␈αoccurrence␈αof
␈↓ α←␈↓␈↓ β'any␈α
indicator␈α
which␈α
appears␈α
in␈α
the␈αlist,␈α
␈↓αl␈↓,␈α
of␈α
indicators.␈α
If␈α
such␈αa␈α
match
␈↓ α←␈↓␈↓ β'is␈αfound,␈αthen␈αthe␈α␈↓¬remainder␈↓␈αof␈αthe␈αp-list,␈αbeginning␈αwith␈αthe␈αmatching
␈↓ α←␈↓␈↓ β'indicator,␈αis␈αreturned.␈α If␈αno␈α
match␈αis␈αfound,␈α␈↓
f␈↓␈αis␈αreturned.␈α
The␈αvirtue
␈↓ α←␈↓␈↓ β'of␈α∀␈↓αgetl␈↓␈α∀is␈α∪that␈α∀it␈α∀can␈α∪distinguish␈α∀between␈α∀an␈α∪atom␈α∀which␈α∀has␈α∪an
␈↓ α←␈↓␈↓ β'indicator␈αwith␈αvalue␈α␈↓
f␈↓␈αand␈αan␈αatom␈αwhich␈αdoes␈αnot␈αhave␈αthe␈αindicator.
␈↓ α←␈↓␈↓ β'␈↓αget␈↓␈α∂cannot␈α∂communicate␈α∞this␈α∂distinction.␈α∂ The␈α∞disadvantage␈α∂of␈α∂␈↓αgetl␈↓␈α∞is
␈↓ α←␈↓␈↓ β'that␈α
it␈α
gives␈αaccess␈α
to␈α
the␈αinternal␈α
structure␈α
of␈αthe␈α
p-list,␈α
and␈αtherefore
␈↓ α←␈↓␈↓ β'access␈αto␈αthe␈αrepresentation␈αof␈α
the␈αatom.␈αSuch␈αp-list␈αfunctions␈αare␈α
useful
␈↓ α←␈↓␈↓ β'but dangerous ([Sam 75]).
␈↓"∀␈↓ α←␈↓␈↓αremprop[n;p]␈↓:␈α≥The␈α≤final␈α≥function␈α≤in␈α≥this␈α≤class␈α≥is␈α≤used␈α≥to␈α≤remove
␈↓ α←␈↓␈↓ β'property-value␈αpairs␈αfrom␈αthe␈αp-list␈αof␈αan␈αatom.␈αThe␈αfunction␈αis␈αnamed
␈↓ α←␈↓␈↓ β'␈↓αremprop␈↓.␈α ␈↓αremprop␈↓␈αhas␈αtwo␈αarguments:␈α␈↓αn␈↓,␈αan␈αatom;␈αand␈α␈↓αp␈↓,␈αa␈αproperty.␈α
If
␈↓ α←␈↓␈↓ β'the␈α
property␈α
is␈αfound␈α
on␈α
the␈αp-list␈α
of␈α
the␈αatom,␈α
then␈α
␈↓αremprop␈↓␈αremoves
␈↓ α←␈↓␈↓ β'the property-value pair and returns ␈↓
t␈↓; otherwise it returns ␈↓
f␈↓.
␈↓"∀␈↓ α←␈↓␈↓ ¬∂␈↓↓5.8 An ␈↓αeval␈↓↓ for Property-lists␈↓α
␈↓"β␈↓ α←␈↓The␈α
evaluators␈α
in␈α
this␈α
section␈α
do␈α
not␈α
reflect␈α
typical␈α
implementation␈αpolicies,
␈↓ α←␈↓but␈α⊃illustrate␈α∩the␈α⊃use␈α⊃of␈α∩property␈α⊃lists␈α⊃and␈α∩introduce␈α⊃the␈α∩first␈α⊃non-trivial
␈↓ α←␈↓application␈αof␈αLISP's␈α
ability␈αto␈αinterchange␈α
program␈αwith␈αdata.␈α Though␈α
this
␈↓ α←␈↓chapter␈α∞is␈α∞mostly␈α∞concerned␈α∞with␈α∞the␈α∞␈↓¬static␈↓␈α∞organization␈α∞of␈α∞LISP,␈α∞the␈α
ideas
␈↓ α←␈↓involved␈α
in␈αthe␈α
evaluator␈αare␈α
sufficiently␈α
important␈αand␈α
demonstrative␈αof␈α
the
␈↓ α←␈↓power of property lists that we include them here rather than later.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∂first␈α∂evaluator␈α⊂uses␈α∂property␈α∂names␈α⊂like␈α∂␈↓αCONST␈↓␈α∂and␈α⊂␈↓αEXPR␈↓␈α∂as
␈↓ α←␈↓representations␈α
for␈α
functions␈α
␈↓αconst␈↓␈α∞and␈α
␈↓αexpr␈↓.␈α
Discovering␈α
that␈α
an␈α∞atom␈α
has
␈↓ α←␈↓the␈α∩␈↓αCONST␈↓␈α⊃property,␈α∩the␈α⊃evaluator␈α∩␈↓¬applies␈↓␈α⊃the␈α∩function␈α⊃named␈α∩␈↓αconst␈↓␈α⊃to
␈↓ α←␈↓perform␈α⊃the␈α⊃evaluation.␈α⊃In␈α⊃this␈α⊃case,␈α⊃the␈α⊃evaluation␈α⊃is␈α⊃simple:␈α∩return␈α⊃the
␈↓ α←␈↓represented␈αconstant.␈αWe␈αwill␈αassume␈αa␈αshallow␈αbinding␈α
implementation␈αand
␈↓ α←␈↓therefore␈α∃variables␈α⊗are␈α∃handled␈α⊗by␈α∃recognizing␈α⊗the␈α∃property␈α⊗␈↓αVAR␈↓␈α∃and
␈↓ α←␈↓passing␈αthe␈αevaluation␈α
to␈αthe␈αfunction␈α
␈↓αvar␈↓.␈αIn␈αthe␈α
case␈αof␈αan␈α
application,␈αwe
␈↓ α←␈↓have␈αmore␈αwork␈α
to␈αdo;␈α␈↓αexpr␈↓␈α
handles␈αthat.␈α The␈α
actual␈αapplication,␈αis␈α
handled
␈↓ α←␈↓using LISP's computed function facility discussed on page 158.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∃will␈α∀describe␈α∃a␈α∀sequence␈α∃of␈α∀evaluators␈α∃based␈α∀on␈α∃property␈α∀list
␈↓ α←␈↓manipulation.␈α∀ We␈α∀will␈α∪not␈α∀express␈α∀all␈α∪of␈α∀the␈α∀details␈α∪of␈α∀this␈α∀family␈α∪of
␈↓ α←␈↓evaluators, but leave many of the details to the reader.
␈↓ α←␈↓␈↓5.8␈↓ π?An ␈↓αeval␈↓ for Property-lists 263␈↓α
␈↓"∀␈↓ α←␈↓αeval <= λ[␈↓ βo[exp;env]
␈↓"β␈↓ α←␈↓α␈↓ βo[atom[exp] → form_name[getl[␈↓ εoexp;
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ εo(VAR CONST)]] [exp;env];
␈↓"β␈↓ α←␈↓α␈↓ βo atom[first[exp]] → form_name[getl[␈↓ π7first[exp];
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ εo␈↓ π7(␈↓ πCEXPR
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ εo␈↓ π7␈↓ πCFEXPR)]] [exp;env];
␈↓"β␈↓ α←␈↓α␈↓ βo ... ]]
␈↓"∞␈↓ α←␈↓αvar <= λ[[form;env] lookup[form;env]]
␈↓" ␈↓ α←␈↓αconst <= λ[[form;env] denote[form;env]]
␈↓" ␈↓ α←␈↓αexpr <= λ[[form;env]␈↓ ∧sλ[[def] eval[␈↓ ε∂body[def];
␈↓"β␈↓ α←␈↓α␈↓ ∧s␈↓ ε∂mkenv[␈↓ εovars[def];
␈↓"β␈↓ α←␈↓α␈↓ ∧s␈↓ ε∂␈↓ εoevlist[args[form];env]] ]
␈↓"β␈↓ α←␈↓α␈↓ ∧s [get[func[form];EXPR]]]
␈↓"∀␈↓ α←␈↓No␈αsubstantial␈αbenefit␈αis␈αapparent␈αafter␈αall␈αthis␈αwork.␈α With␈αa␈αslight␈αchange,
␈↓ α←␈↓we␈α could␈α extract␈α a␈α small␈α improvement:␈α replace␈α the␈α!explicit␈α lists
␈↓ α←␈↓␈↓α(VAR CONST)␈↓␈αand␈α␈↓α(EXPR FEXPR)␈↓␈αwith␈α␈↓αidprop␈↓␈αand␈α␈↓αappprop␈↓.␈αBind␈αthese
␈↓ α←␈↓variables␈α
globally␈αto␈α
the␈αrespective␈α
lists.␈αThen␈α
if␈αwe␈α
wished␈αto␈α
define␈α
a␈αnew
␈↓ α←␈↓kind␈αof␈αcalling␈αsequence,␈αsay␈α␈↓αgexpr␈↓s,␈αwe␈αcould␈αadd␈α␈↓αGEXPR␈↓␈αto␈α␈↓αappprop␈↓␈αand
␈↓ α←␈↓write␈α∩a␈α∩function␈α∩named␈α∩␈↓αgexpr␈↓␈α∩to␈α∩handle␈α∩the␈α∩evaluation␈α∩of␈α∩␈↓αgexpr␈↓␈α∩forms.
␈↓ α←␈↓However with further analysis, we can do much better.
␈↓"β␈↓ α←␈↓␈↓ β'Consider␈α
simple␈α∞variables.␈α
Each␈α∞instance␈α
of␈α
a␈α∞simple␈α
variable␈α∞has␈α
the
␈↓ α←␈↓same␈αvalue␈α
property;␈αwhen␈α
we␈αsee␈α
␈↓αx␈↓␈αwe␈α
apply␈α␈↓αlookup␈↓␈α
through␈α␈↓αvar␈↓;␈α
when␈αwe
␈↓ α←␈↓see␈α∩␈↓αy␈↓␈α∩we␈α∩apply␈α⊃␈↓αlookup␈↓␈α∩through␈α∩␈↓αvar␈↓.␈α∩However␈α⊃the␈α∩association␈α∩of␈α∩a␈α⊃value
␈↓ α←␈↓property␈α∃with␈α∃each␈α∃␈↓¬instance␈↓␈α∃of␈α∀a␈α∃variable␈α∃is␈α∃discomforting.␈α∃The␈α∀value
␈↓ α←␈↓property␈αis␈αmore␈αa␈αproperty␈αof␈αthe␈αclass␈αof␈αvariables␈αthan␈αit␈αis␈αa␈αproperty␈αof
␈↓ α←␈↓an␈αinstance.␈α
That␈αis,␈α
an␈αinstance␈αinherits␈α
a␈αproperty␈α
by␈αbeing␈α
a␈αmember␈αof␈α
a
␈↓ α←␈↓certain class.
␈↓"β␈↓ α←␈↓␈↓ β'Let␈α
the␈α
atom␈α
␈↓αVAR␈↓␈α
represent␈α
the␈α
class␈α
of␈α
variables;␈α
let␈α
the␈α
atom␈α␈↓αEVAL␈↓
␈↓ α←␈↓represent␈α∃the␈α∃property␈α∃name␈α∀describing␈α∃value␈α∃properties.␈α∃ The␈α∀function
␈↓ α←␈↓␈↓αlookup␈↓␈α∪is␈α∀therefore␈α∪a␈α∀property␈α∪value␈α∪of␈α∀the␈α∪atom␈α∀␈↓αVAR␈↓,␈α∪and␈α∀should␈α∪be
␈↓ α←␈↓associated with the property name ␈↓αEVAL␈↓.
␈↓"⊃␈↓ α←␈↓
X ⊂αααααααπαααααα⊃ ⊂ααααπααα⊃ VAR ⊂ααααααπαααααααα⊃
␈↓"␈↓ α←␈↓
~ VAR ~ #αβα→αα→~ E4 ~ 3 ~ ~ EVAL ~ LOOKUP ~
␈↓"␈↓ α←␈↓
εαααααααβααααααλ εααααβαααλ %αααααα∀αααααααα$
␈↓"␈↓ α←␈↓
~ prop1 ~ val1 ~ ~ E3 ~ A ~
␈↓"␈↓ α←␈↓
εαααααααβααααααλ εααααβαααλ
␈↓"␈↓ α←␈↓
# # # ~ E1 ~ B ~
␈↓"␈↓ α←␈↓
εαααααααβααααααλ %αααα∀ααα$
␈↓"␈↓ α←␈↓
~ propn ~ valn ~
␈↓"␈↓ α←␈↓
%ααααααα∀αααααα$
␈↓"∀␈↓ α←␈↓Now␈αthe␈αevaluator␈αshould␈αdo␈αa␈αdouble␈α␈↓αget␈↓,␈αlooking␈αdown␈αthe␈αproperty␈αlist␈αof
␈↓ α←␈↓an␈α∞object,␈α∞for␈α∞a␈α∞class␈α∞name␈α∞which␈α∞has␈α∞an␈α∞␈↓αEVAL␈↓␈α∞property.␈α∂ Finding␈α∞␈↓αEVAL␈↓,
␈↓ α←␈↓the␈α⊂evaluator␈α⊃applies␈α⊂the␈α⊃associated␈α⊂property␈α⊃value␈α⊂to␈α⊃the␈α⊂object␈α⊃and␈α⊂the
␈↓ α←␈↓environment.
␈↓ α←␈↓␈↓264 Static Structure␈↓
(5.8␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Before␈α∞presenting␈α
the␈α∞next␈α
evaluator␈α∞we␈α
should␈α∞settle␈α
one␈α∞more␈α
point.
␈↓ α←␈↓In␈αthe␈α
preceeding␈α␈↓αeval␈↓␈α
we␈αignored␈αthe␈α
question␈αof␈α
anonymous␈αλ-expressions;
␈↓ α←␈↓we␈α∞assumed␈α∞that␈α∞the␈α∞function-part␈α∂of␈α∞an␈α∞application␈α∞was␈α∞an␈α∞atom.␈α∂We␈α∞did
␈↓ α←␈↓this␈α∂becaue␈α⊂we␈α∂have␈α⊂implied␈α∂that␈α∂only␈α⊂atoms␈α∂have␈α⊂property␈α∂lists.␈α⊂We␈α∂will
␈↓ α←␈↓remove␈αthis␈α
restriction␈αfor␈α
the␈αnext␈α
evaluator␈αand␈α
assume␈αthat␈α
any␈αobject␈α
can
␈↓ α←␈↓have␈α
a␈α
property␈α
list.␈α
A␈α
λ-expression␈α
will␈α
have␈α
a␈α
property␈α
list␈α
with␈α
(at␈α
least)
␈↓ α←␈↓property␈α⊃name␈α⊃␈↓αLAMBDA␈↓␈α⊂and␈α⊃property␈α⊃value␈α⊂of␈α⊃the␈α⊃repesentation␈α⊃of␈α⊂the
␈↓ α←␈↓λ-expression.␈αThe␈αatom␈α␈↓αLAMBDA␈↓␈αwill␈αhave␈αan␈α␈↓αEVAL␈↓␈αproperty␈αwhose␈αvalue
␈↓ α←␈↓is␈α_the␈α_function␈α↔␈↓αeval_λ␈↓;␈α_this␈α_function␈α↔will␈α_evaluate␈α_applications␈α↔whose
␈↓ α←␈↓function-parts are λ-expressions.
␈↓"β␈↓ α←␈↓␈↓ β'Finally,␈α
since␈α
␈↓αeval_λ␈↓␈α
handles␈α
most␈α
of␈α
the␈α
evaluation␈α
of␈α
an␈α
application,
␈↓ α←␈↓there␈α⊂is␈α∂no␈α⊂need␈α∂to␈α⊂make␈α∂␈↓αexpr␈↓␈α⊂do␈α∂it␈α⊂too.␈α∂In␈α⊂fact,␈α∂our␈α⊂previous␈α∂distinction
␈↓ α←␈↓between␈α
␈↓αVAR␈↓␈α∞and␈α
␈↓αEXPR␈↓␈α∞is␈α
unnecessarily␈α∞restrictive.␈α
We␈α∞should␈α
be␈α∞able␈α
to
␈↓ α←␈↓return␈α
functions␈α
as␈α∞values␈α
just␈α
as␈α
we␈α∞can␈α
return␈α
constants␈α
or␈α∞simple␈α
values.
␈↓ α←␈↓So␈α∀␈↓αEXPR␈↓␈α∃should␈α∀be␈α∃a␈α∀property␈α∃name,␈α∀with␈α∃property␈α∀value␈α∃being␈α∀the
␈↓ α←␈↓λ-expression,␈α
but␈α
␈↓αEXPR␈↓␈α
should␈α
now␈α
have␈α
an␈α
␈↓αEVAL␈↓␈α
property␈α
which␈α
is␈αjust
␈↓ α←␈↓␈↓αlookup␈↓.
␈↓"β␈↓ α←␈↓For example:
␈↓"⊃␈↓ α←␈↓
plist for FACT plist for (LAMBDA (X)
␈↓"␈↓ α←␈↓
((ZEROP X) ... )))
␈↓"␈↓ α←␈↓
⊂ααααααπααα⊃ ⊂ααααααααπααα⊃
␈↓"␈↓ α←␈↓
~ EXPR ~ #αβ→αα→~ LAMBDA ~ #αβ→αα→ (LAMBDA
␈↓"␈↓ α←␈↓
εααααααβαααλ εααααααααβαααλ (X)
␈↓"␈↓ α←␈↓
# # # # # # (COND ((ZEROP X)
␈↓"␈↓ α←␈↓
%αααααα∀ααα$ %αααααααα∀ααα$ ... )))
␈↓"␈↓ α←␈↓
plist for EXPR plist for LAMBDA
␈↓"␈↓ α←␈↓
⊂ααααααπαααααααα⊃ ⊂ααααααπααααααααα⊃
␈↓"␈↓ α←␈↓
~ EVAL ~ LOOKUP ~ ~ EVAL ~ EVALLAM ~
␈↓"␈↓ α←␈↓
εααααααβααααααααλ εααααααβαααααααααλ
␈↓"␈↓ α←␈↓
# # # # # #
␈↓"␈↓ α←␈↓
%αααααα∀αααααααα$ %αααααα∀ααααααααα$
␈↓"∀␈↓ α←␈↓With all this extra mechanism in place, ␈↓αeval␈↓ does absolutely nothing!
␈↓"∀␈↓ α←␈↓α␈↓ ∧_eval <= λ[[exp;env] getget[exp;EVAL] [exp;env]],
␈↓"∀␈↓ α←␈↓where␈α
␈↓αgetget␈↓␈α∞looks␈α
at␈α
property␈α∞names␈α
associated␈α
with␈α∞␈↓αexp␈↓␈α
until␈α
it␈α∞finds␈α
one
␈↓ α←␈↓which itself has a property list containing ␈↓αEVAL␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Now␈αreal␈α
progress␈αhas␈αbeen␈α
made.␈α The␈αevaluation␈α
of␈αany␈αexpression␈α
is
␈↓ α←␈↓controlled␈α∂by␈α∞a␈α∂function␈α∞associated␈α∂with␈α∞the␈α∂class␈α∞to␈α∂which␈α∂that␈α∞expression
␈↓ α←␈↓belongs.␈αIt␈α
is␈αtrivial␈α
to␈αmodify␈α
or␈αextend␈α
such␈αan␈α
evaluator:␈αsupply␈α
the␈αclass
␈↓ α←␈↓name and the appropriate ␈↓αEVAL␈↓ property value.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αtechnique␈αis␈αapplicable␈αto␈αmore␈αgeneral␈αkinds␈αof␈αcomputation␈αthan
␈↓ α←␈↓just␈α∀evaluation.␈α∀With␈α∀a␈α∀class␈α∪name␈α∀we␈α∀can␈α∀associate␈α∀arbitrary␈α∀pairs␈α∪of
␈↓ α←␈↓properties␈αand␈αfunctions.␈αFor␈αexample,␈αwe␈αmight␈αwish␈αto␈αdefine␈αspecial␈αinput
␈↓ α←␈↓or␈α⊂output␈α⊂conventions␈α∂for␈α⊂classes;␈α⊂to␈α∂do␈α⊂this␈α⊂we␈α∂simply␈α⊂associate␈α⊂a␈α∂␈↓αREAD␈↓
␈↓ α←␈↓␈↓5.8␈↓ π?An ␈↓αeval␈↓ for Property-lists 265␈↓α
␈↓"β␈↓ α←␈↓property␈αand␈αa␈α␈↓αPRINT␈↓␈αproperty␈αwith␈αthe␈αclass␈αname␈αand␈αsupply␈αroutines␈αto
␈↓ α←␈↓perform␈αthe␈αspecial␈αreading␈αor␈αprinting.␈αSimilarly,␈αwe␈αcan␈αassociate␈αa␈α
compile
␈↓ α←␈↓property,␈α↔and␈α↔a␈α⊗function␈α↔describing␈α↔how␈α⊗to␈α↔compile␈α↔instances␈α↔of␈α⊗this
␈↓ α←␈↓construct.␈↓π 18␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
net␈α
effect␈α
of␈α
this␈α
reworking␈α
of␈α
evaluation␈α
is␈α
to␈α
expose␈α
a␈α
much␈α
more
␈↓ α←␈↓general␈α⊃scheme␈α∩for␈α⊃handling␈α∩computation.␈α⊃ Such␈α⊃a␈α∩distributed␈α⊃␈↓αeval␈↓␈α∩is␈α⊃an
␈↓ α←␈↓example of a LISP technique called data-driven programming ([San 75a]).␈↓π 19␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Property␈αlist␈αrepresentations␈α
have␈αalso␈αfound␈α
extensive␈αuse␈αin␈αdata␈α
base
␈↓ α←␈↓applications (Section 2.5; see [San 75a].
␈↓"β␈↓ α←␈↓␈↓ ∧c␈↓↓5.9 Representation of Property-lists␈↓
␈↓"β␈↓ α←␈↓In␈α⊂discussing␈α⊃representations,␈α⊂we␈α⊂must␈α⊃keep␈α⊂the␈α⊂essential␈α⊃characteristics␈α⊂of
␈↓ α←␈↓property␈αlists␈αwell␈αin␈αmind.␈α A␈αproperty␈αlist␈αis␈αsimilar␈αto␈αa␈αlocal␈αenvironment
␈↓ α←␈↓block;␈α⊃each␈α∩property␈α⊃list␈α∩is␈α⊃a␈α⊃sequence␈α∩of␈α⊃names␈α∩and␈α⊃values.␈α∩However␈α⊃a
␈↓ α←␈↓property␈α∩list␈α∪can␈α∩grow␈α∪and␈α∩shrink␈α∪dynamically,␈α∩whereas␈α∪an␈α∩environment
␈↓ α←␈↓block␈αis␈αcreated␈αat␈αa␈αfixed␈αsize.␈αSince␈αwe␈αcannot␈αpredict␈αthe␈αsize␈αof␈αthe␈αblock,
␈↓ α←␈↓a natural representation is that of a list.
␈↓"⊃␈↓ α←␈↓
⊂αααααααπααααα⊃ ⊂αααααααπααα⊃ ⊂αααααπααα⊃
␈↓"␈↓ α←␈↓
~ prop1 ~ val1~ ~ prop1 ~ #αβα→~ val1~ # ~
␈↓"␈↓ α←␈↓
εαααααααβαααααλ %ααααααα∀ααα$ %ααααα∀αβα$
␈↓"␈↓ α←␈↓
~ prop2 ~ val2~ ↓
␈↓"␈↓ α←␈↓
# # # ⊂← # # # ←ααα←ααα$
␈↓"␈↓ α←␈↓
↓
␈↓"␈↓ α←␈↓
εαααααααβαααααλ ⊂αααααααπααα⊃ ⊂αααααπαα⊃
␈↓"␈↓ α←␈↓
~ propn ~ valn~ ~ propn ~ #αβα→~ valn~≤'~
␈↓"␈↓ α←␈↓
%ααααααα∀ααααα$ %ααααααα∀ααα$ %ααααα∀αα$
␈↓"␈↓ α←␈↓
␈↓↓Property list Representation␈↓
␈↓"∀␈↓ α←␈↓The␈αelements␈αof␈αthe␈αp-list␈αare␈αassociated␈αin␈αpairs.␈αThe␈αfirst␈αelement␈αof␈αa␈αpair
␈↓ α←␈↓is␈α
the␈α∞property,␈α
the␈α
next␈α∞element␈α
is␈α∞the␈α
property␈α
value.␈α∞ But␈α
atoms␈α∞can't␈α
be
␈↓ α←␈↓represented␈α∩as␈α∩just␈α⊃any␈α∩arbitrary␈α∩list.␈α⊃We␈α∩must␈α∩be␈α⊃able␈α∩to␈α∩recognize␈α⊃the
␈↓ α←␈↓occurrence of an atom so that we can implement the predicate ␈↓αatom␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α
are␈α
at␈α
least␈α
two␈α∞alternatives.␈α
We␈α
might␈α
partition␈α
memory␈α∞as␈α
we
␈↓ α←␈↓began␈αto␈α
do␈αon␈α
page 248␈αwith␈α
pointer␈αspace␈α
and␈αatom␈α
space.␈α Then␈α
the␈αtest
␈↓ α←␈↓of␈α∩atom-ness␈α∪is␈α∩a␈α∪test␈α∩on␈α∩the␈α∪location␈α∩being␈α∪referenced.␈α∩ We␈α∪might␈α∩also
␈↓ α←␈↓preface␈α⊗the␈α⊗property␈α⊗list␈α⊗with␈α∃an␈α⊗"atom header"␈α⊗which␈α⊗will␈α⊗signal␈α∃the
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 18␈↓The␈α≠language␈α≠EL1␈α≠([EL1 74])␈α≠incorporates␈α≠a␈α≠similar␈α~scheme,
␈↓ α←␈↓however␈α⊃only␈α⊃five␈α⊃designated␈α⊃properties␈α⊃can␈α⊃be␈α⊃associated␈α⊃with␈α⊃any␈α⊃class
␈↓ α←␈↓name.␈α≤The␈α≥techniques␈α≤of␈α≥syntax-directed␈α≤input-output,␈α≥developed␈α≤in
␈↓ α←␈↓Section 9.4, are also applicable to such an evaluator.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 19␈↓The␈α⊂author␈α⊂first␈α⊃saw␈α⊂this␈α⊂technique␈α⊂used␈α⊃in␈α⊂a␈α⊂non-trivial␈α⊃way␈α⊂in
␈↓ α←␈↓[Dif 71] in the Stanford LISP compiler.
␈↓ α←␈↓␈↓266 Static Structure␈↓
(5.9␈↓
␈↓"β␈↓ α←␈↓beginning␈αof␈α
the␈αatom.␈αHere␈α
the␈αtest␈α
for␈αatom-ness␈αis␈α
a␈αtest␈α
on␈αthe␈αcontents␈α
of
␈↓ α←␈↓the␈α∩location␈α⊃being␈α∩referenced.␈α⊃ In␈α∩the␈α∩first␈α⊃case␈α∩we␈α⊃dedicate␈α∩a␈α∩section␈α⊃of
␈↓ α←␈↓memory␈α
for␈α
the␈α
storage␈α
of␈α
atoms;␈↓π 20␈↓␈α∞in␈α
the␈α
second␈α
case␈α
we␈α
require␈α∞an␈α
extra
␈↓ α←␈↓memory reference.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α∞related␈α
efficiency␈α∞consideration␈α∞involves␈α
the␈α∞use␈α
of␈α∞property␈α∞lists␈α
in
␈↓ α←␈↓the␈α⊂implementation␈α⊂of␈α⊂LISP.␈α⊂Since␈α⊂the␈α⊂evaluator␈α⊂will␈α⊂be␈α⊂making␈α∂frequent
␈↓ α←␈↓examination␈α∃of␈α∃the␈α∃p-list,␈α∃it␈α∃is␈α∃often␈α∃useful␈α∃to␈α∃store␈α∃the␈α∃system-related
␈↓ α←␈↓properties␈αin␈αspecific␈αpositions␈αrelative␈αto␈α
the␈αbeginning␈αof␈αthe␈αp-list;␈αthis␈α
will
␈↓ α←␈↓eliminate a search.
␈↓"β␈↓ α←␈↓␈↓ β'Using␈α⊂a␈α∂separate␈α⊂"atom␈α∂space",␈α⊂an␈α∂atom␈α⊂would␈α∂be␈α⊂represented␈α⊂by␈α∂its
␈↓ α←␈↓property␈α
list.␈α
In␈α
this␈α
case,␈α
property␈αlists␈α
need␈α
not␈α
be␈α
stored␈α
in␈α
pointer␈αspace.
␈↓ α←␈↓Chapter 7␈α∂examines␈α∂some␈α∂of␈α∂these␈α∞techniques.␈α∂In␈α∂the␈α∂text␈α∂we␈α∂will␈α∞describe
␈↓ α←␈↓atoms␈α∞using␈α∞the␈α∞"atom␈α
header"␈α∞since␈α∞it␈α∞makes␈α
it␈α∞clearer␈α∞in␈α∞pictures.␈α
Atoms
␈↓ α←␈↓will␈αbe␈αspecial␈αlists␈αwhose␈α
␈↓αcar␈↓-part␈αcontains␈αan␈αindicator␈αused␈α
exclusively␈αfor
␈↓ α←␈↓the␈αbeginning␈α
of␈αatoms.␈α
We␈αwill␈α
use␈α␈↓
∃␈↓␈α
to␈αdesignate␈α
the␈αbeginning␈α
of␈αan␈α
atom.
␈↓ α←␈↓The␈α∪␈↓αcdr␈↓␈α∪of␈α∪the␈α∪representation␈α∀of␈α∪the␈α∪atom␈α∪is␈α∪the␈α∪representation␈α∀of␈α∪the
␈↓ α←␈↓property␈α∞list.␈α∞Such␈α∞locations␈α∞in␈α∞pointer␈α∞space␈α∞containing␈α∞␈↓
∃␈↓␈α∞in␈α∂their␈α∞left-half
␈↓ α←␈↓and a pointer to a p-list in their right-half are called ␈↓↓atom headers␈↓.
␈↓"β␈↓ α←␈↓For example, here is part of a representation for the atom for ␈↓αcar␈↓:
␈↓"⊃␈↓ α←␈↓
⊂ααπαααα⊃ ⊂ααααααπαααα⊃ ⊂αααπααα⊃
␈↓"␈↓ α←␈↓
~∃ ~ #αβαα→~ SUBR ~ #αβαα→~ # ~ # ...→
␈↓"␈↓ α←␈↓
%αα∀αααα$ %αααααα∀αααα$ %αβα∀ααα$
␈↓"␈↓ α←␈↓
~
␈↓"␈↓ α←␈↓
%→αα→ to machine code for ␈↓αcar␈↓
␈↓"β␈↓ α←␈↓␈↓ Part of the atom-structure for ␈↓αCAR␈↓ ␈↓α
␈↓"λ␈↓ α←␈↓␈↓ β'An␈α≤example␈α≤of␈α≤the␈α≤distinction␈α≤between␈α≤␈↓αget␈↓␈α≤and␈α≤␈↓αgetl␈↓␈α≥in␈α≤our
␈↓ α←␈↓representation may be useful.
␈↓"⊃␈↓ α←␈↓
␈↓αgetl[FOO;(BAZ)]␈↓
␈↓"␈↓ α←␈↓
⊗
␈↓"␈↓ α←␈↓
␈↓αgetl[FOO;(PNARF BAZ)]␈↓
␈↓αget[FOO;PNARF]␈↓
␈↓"␈↓ α←␈↓
↓ ↓
␈↓"␈↓ α←␈↓
⊂ααα←ααααααα$ ~
␈↓"␈↓ α←␈↓
⊂απααα⊃ ↓ ⊂αααααπααα⊃ ⊂αααααπααα⊃ ~
␈↓"␈↓ α←␈↓
~∃~ #αβαα→~ BAZ ~ #αβα→~ NIL ~ # ~ ↓
␈↓"␈↓ α←␈↓
%α∀ααα$ %ααααα∀ααα$ %ααααα∀αβα$ ~
␈↓"␈↓ α←␈↓
↓ ~
␈↓"␈↓ α←␈↓
⊂αααααααπααα⊃ ⊂αααπαα⊃ ~
␈↓"␈↓ α←␈↓
~ PNARF ~ #αβα→~ # ~≤'~ ↓
␈↓"␈↓ α←␈↓
%ααααααα∀ααα$ %αβα∀αα$ ~
␈↓"␈↓ α←␈↓
ε←ααα←ααα$
␈↓"␈↓ α←␈↓
↓
␈↓"␈↓ α←␈↓
⊂αααπαα⊃
␈↓"␈↓ α←␈↓
~ A ~≤'~
␈↓"␈↓ α←␈↓
%ααα∀αα$
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 20␈↓We␈α∪need␈α∩not␈α∪dedicate␈α∩a␈α∪whole␈α∩section␈α∪of␈α∩the␈α∪machine␈α∪to␈α∩"atom
␈↓ α←␈↓space".␈α_Several␈α↔techniques␈α_are␈α_available␈α↔for␈α_"mapping"␈α_a␈α↔conceptually
␈↓ α←␈↓contiguous space onto a scattered memory.[Ste 73], [Nor 76], [Pre 76a].
␈↓ α←␈↓␈↓5.9␈↓ εnRepresentation of Property-lists 267␈↓
␈↓"β␈↓ α←␈↓and␈α
␈↓αget[FOO;BAZ]␈α=␈α
get[FOO;BAR]␈α
=␈αNIL␈↓.␈α
Notice␈α
how␈αboth␈α
␈↓αget␈↓␈α
and␈α␈↓αgetl␈↓
␈↓ α←␈↓allow free access to the internal representation.
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α∩simple␈α⊃atom␈α∩is␈α∩becoming␈α⊃much␈α∩more␈α∩complex.␈α⊃It␈α∩has␈α∩a␈α⊃whole
␈↓ α←␈↓substructure␈α∞attached␈α∞to␈α∞it.␈α∞Thus␈α∞each␈α∞atom␈α∞is␈α∞like␈α∞a␈α∞word␈α∞in␈α∂a␈α∞dictionary;
␈↓ α←␈↓many␈α∂words␈α∂can␈α∂be␈α∂used␈α∂as␈α∂different␈α∂parts␈α∂of␈α∂speech␈α∂and␈α∂their␈α∞dictionary
␈↓ α←␈↓entries␈αwill␈α
reflect␈αthis␈α
by␈αhaving␈αseveral␈α
alternative␈αmeanings.␈α
Similarly,␈αan
␈↓ α←␈↓atom␈αcan␈αhave␈αseveral␈αdifferent␈α"meanings"␈αattached␈αto␈αit␈αand␈αdepending␈αon
␈↓ α←␈↓the␈αcontext,␈αwe␈αwill␈α
be␈αinterested␈αin␈αone␈α
of␈αthose␈αinterpretations.␈α Just␈α
as␈αwe
␈↓ α←␈↓will␈α
find␈α
all␈α
meanings␈α
of␈α
a␈α
specific␈α
word␈α
in␈α
one␈α
location␈α
in␈α
the␈α
dictionary,␈α
our
␈↓ α←␈↓implementation␈αof␈αLISP␈αbecomes␈αmuch␈αsimpler␈αif␈αwe␈αstore␈αeach␈αatom␈αand␈αits
␈↓ α←␈↓associated␈αp-list␈αuniquely.␈αEvery␈αreference␈αto␈αthe␈αatom␈α␈↓αA␈↓␈αis␈αactually␈αa␈αpointer
␈↓ α←␈↓to␈α∂the␈α∞same␈α∂location␈α∞in␈α∂memory.␈α∞This␈α∂location␈α∞has␈α∂a␈α∞␈↓αcar␈↓-part␈α∂which␈α∂is␈α∞the
␈↓ α←␈↓special atom indicator ␈↓
∃␈↓, and a ␈↓αcdr␈↓-part which is the p-list for the atom ␈↓αA␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Thus ␈↓α(A . A)␈↓, which we have been writing as:
␈↓"⊃␈↓ α←␈↓
⊂αααααπααααα⊃
␈↓"␈↓ α←␈↓
~ A ~ A ~
␈↓"␈↓ α←␈↓
%ααααα∀ααααα$
␈↓"∀␈↓ α←␈↓might be represented as:
␈↓"⊗␈↓ α←␈↓
⊂αααααπααααα⊃
␈↓"␈↓ α←␈↓
~ # ~ # ~
␈↓"␈↓ α←␈↓
%ααβαα∀ααβαα$
␈↓"␈↓ α←␈↓
~ ~
␈↓"␈↓ α←␈↓
~ ~ ⊂απααα⊃
␈↓"␈↓ α←␈↓
%ααααα∀α→~∃~ #αβα→ ␈↓p-list for ␈↓αA␈↓
␈↓"␈↓ α←␈↓
%α∀ααα$
␈↓"∀␈↓ α←␈↓The␈α∞internal␈α
structures␈α∞of␈α∞this␈α
implementation␈α∞of␈α∞LISP␈α
are␈α∞␈↓¬not␈↓␈α∞L-trees,␈α
but
␈↓ α←␈↓list␈α∩structure;␈α∩that␈α∩is,␈α∩there␈α∩are␈α∩intersecting␈α∩and␈α∩circular␈α∩branches.␈α∩ LISP
␈↓ α←␈↓implementations␈α!therefore␈α!involve␈α"binary␈α!list␈α!structure␈α"since␈α!each
␈↓ α←␈↓non-terminal node in our representation has exactly two branches.
␈↓"β␈↓ α←␈↓␈↓ β'Assume␈αwe␈αhave␈αthe␈αabove␈αdotted␈αpair␈αas␈αa␈αvalue␈αof␈αa␈αvariable␈α␈↓αx␈↓␈αand
␈↓ α←␈↓we␈α∞wish␈α∞to␈α∞print␈α∞the␈α∞value␈α∂of␈α∞␈↓αx␈↓.␈α∞ We␈α∞would␈α∞expect␈α∞that␈α∞an␈α∂output␈α∞routine
␈↓ α←␈↓would␈α⊃be␈α⊃given␈α⊃a␈α⊃pointer␈α⊃to␈α∩the␈α⊃dotted␈α⊃pair␈α⊃and␈α⊃we␈α⊃would␈α⊃hope␈α∩to␈α⊃see
␈↓ α←␈↓"␈↓α(A . A)␈↓"␈α⊃appear␈α⊂on␈α⊃the␈α⊃output␈α⊂device.␈α⊃ The␈α⊂LISP␈α⊃output␈α⊃routine,␈α⊂named
␈↓ α←␈↓␈↓αprint␈↓,␈αcan␈αrecognize␈αthat␈α"␈↓α(A . A)␈↓"␈αis␈αa␈αdotted␈αpair␈αsince␈αits␈α␈↓αcar␈↓␈αis␈αnot␈α␈↓
∃␈↓.␈α But
␈↓ α←␈↓how␈α∪can␈α∩␈↓αprint␈↓␈α∪distinguish␈α∪␈↓α(A␈α∩.␈α∪A)␈↓␈α∪from␈α∩␈↓α(B␈α∪.␈α∪B)␈↓ ?␈α∩ The␈α∪pointer␈α∪in␈α∩the
␈↓ α←␈↓preceding␈α∂diagram␈α∂will␈α∂point␈α∂to␈α∞␈↓αA␈↓␈α∂in␈α∂one␈α∂case␈α∂and␈α∞to␈α∂␈↓αB␈↓␈α∂in␈α∂the␈α∂other,␈α∞but
␈↓ α←␈↓nothing␈αabout␈αthe␈αatom␈αtells␈αus␈α␈↓¬what␈↓␈αto␈αprint.␈α The␈αsimplest␈αthing␈αto␈αdo␈αis␈αto
␈↓ α←␈↓store␈αa␈αrepresentation␈αof␈αthe␈αname␈αon␈αeach␈αp-list.␈α This␈αis␈αdone␈αwith␈αanother
␈↓ α←␈↓indicator␈αcalled␈α␈↓αPNAME␈↓,␈αstanding␈αfor␈α␈↓↓print-name␈↓.␈αEach␈αatom␈αis␈α
guaranteed
␈↓ α←␈↓to␈αhave␈αa␈αprint-name␈αor␈α"p-name".␈α The␈αprint-name␈αof␈αthe␈αatom␈αis␈αwhat␈αthe
␈↓ α←␈↓LISP output routine will print as the name of the atom.
␈↓ α←␈↓␈↓268 Static Structure␈↓
(5.9␈↓
␈↓"β␈↓ α←␈↓␈↓ β'For example, the atom ␈↓αBAZ␈↓ will have at least the following:␈↓π 21␈↓
␈↓"⊃␈↓ α←␈↓
⊂αααααααπααααααα⊃
␈↓"␈↓ α←␈↓
~ PNAME ~ "BAZ" ~
␈↓"␈↓ α←␈↓
%ααααααα∀ααααααα$
␈↓"∀␈↓ α←␈↓where ␈↓
"BAZ"␈↓ means a representation of the string of characters, ␈↓αB, A, Z␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'When␈α≠we␈α~represent␈α≠such␈α≠a␈α~property␈α≠pair␈α~we␈α≠must␈α≠deal␈α~with
␈↓ α←␈↓representational␈α≠problems␈α~of␈α≠character␈α≠strings.␈α~ We␈α≠desire␈α≠strings␈α~of
␈↓ α←␈↓unbounded␈α∞length,␈α∞but␈α∞must␈α∞represent␈α
them␈α∞in␈α∞a␈α∞machine␈α∞with␈α∞fixed␈α
word
␈↓ α←␈↓size.␈α∞We␈α∞will␈α∞discuss␈α∞a␈α∞more␈α∞general␈α∞representation␈α∞in␈α∞Section 7.3,␈α∂but␈α∞here
␈↓ α←␈↓we␈α∃will␈α⊗represent␈α∃the␈α⊗print␈α∃name␈α⊗by␈α∃using␈α⊗the␈α∃basic␈α⊗dotted-pair␈α∃data
␈↓ α←␈↓structure.
␈↓"⊃␈↓ α←␈↓
⊂αααπαα⊃
␈↓"␈↓ α←␈↓
~ # ~≤'~
␈↓"␈↓ α←␈↓
%αβα∀αα$
␈↓"␈↓ α←␈↓
↓
␈↓"␈↓ α←␈↓
⊂ααααααα⊃
␈↓"␈↓ α←␈↓
~ BAZ≡≡ ~
␈↓"␈↓ α←␈↓
%ααααααα$
␈↓"β␈↓ α←␈↓␈↓ p-name representation for ␈↓αBAZ␈↓ ␈↓α
␈↓"→␈↓ α←␈↓␈↓
BAZ≡≡␈↓␈αmeans␈αa␈αmemory␈αlocation␈αcontaining␈αsome␈αencoding␈αof␈αthe␈αletters␈α␈↓
B␈↓,␈α␈↓
A␈↓,
␈↓ α←␈↓and␈α∀␈↓
Z␈↓.␈α∃ The␈α∀symbol,␈α∃␈↓
≡␈↓,␈α∀represents␈α∀some␈α∃non-printing␈α∀character;␈α∃we␈α∀are
␈↓ α←␈↓therefore assuming that a location can contain five characters.
␈↓"β␈↓ α←␈↓We␈α⊃represent␈α⊃the␈α∩print-name␈α⊃as␈α⊃a␈α∩list␈α⊃so␈α⊃that␈α∩we␈α⊃may␈α⊃allow␈α∩atoms␈α⊃with
␈↓ α←␈↓p-names of unbounded length. The p-name for ␈↓αBLETCH␈↓, would be:
␈↓"⊃␈↓ α←␈↓
⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
~ # ~ #αβαααα→~ # ~≤'~
␈↓"␈↓ α←␈↓
%αβα∀ααα$ %αβα∀αα$
␈↓"␈↓ α←␈↓
↓ ↓
␈↓"␈↓ α←␈↓
⊂ααααααα⊃ ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
~ BLETC ~ ~ H≡≡≡≡ ~
␈↓"␈↓ α←␈↓
%ααααααα$ %ααααααα$
␈↓"β␈↓ α←␈↓␈↓ P-name structure for ␈↓αBLETCH␈↓ ␈↓α
␈↓"→␈↓ α←␈↓With␈α⊃such␈α⊃print-names␈α⊃on␈α⊃each␈α⊂property-list␈α⊃␈↓αprint␈↓␈α⊃can␈α⊃now␈α⊃operate.␈α⊂The
␈↓ α←␈↓␈↓αprint␈↓␈α∂routine␈α∂needs␈α∂the␈α∞print␈α∂name␈α∂and,␈α∂as␈α∞we␈α∂shall␈α∂see␈α∂shortly,␈α∂the␈α∞input
␈↓ α←␈↓routine␈αalso␈αneeds␈αthe␈αprint␈αname,␈αbut␈αotherwise␈αall␈αLISP␈αcalculation␈αis␈αdone
␈↓ α←␈↓using␈α⊃the␈α⊃internal␈α⊃pointers␈α⊃to␈α⊃the␈α⊃property␈α⊃lists.␈α∩ Several␈α⊃implementations
␈↓ α←␈↓exploit␈α∞this␈α∞observation␈α∞by␈α∞placing␈α
the␈α∞print names␈α∞in␈α∞slower␈α∞memory␈α
than
␈↓ α←␈↓that␈α
used␈α
for␈α
the␈α
main␈α
computation.␈α
Since␈α
access␈α
to␈α
print␈α
names␈α
is␈α
infrequent,
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 21␈↓Since␈α↔␈↓¬every␈↓␈α_atom␈α↔is␈α_guaranteed␈α↔to␈α↔have␈α_a␈α↔print␈α_name,␈α↔many
␈↓ α←␈↓implementations separate this property from the general p-list.
␈↓ α←␈↓␈↓5.9␈↓ εnRepresentation of Property-lists 269␈↓
␈↓"β␈↓ α←␈↓we␈αcan␈αafford␈αto␈αspend␈αmore␈αtime␈αin␈αretrieving␈αthem.␈α We␈αwill␈α
discuss␈αmore
␈↓ α←␈↓of the details of LISP input and output in Section 5.11.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∞print-names␈α∞␈↓
BAZ≡≡␈↓,␈α∞␈↓
BLETC␈↓,␈α∞and␈α∞␈↓
H≡≡≡≡␈↓,␈α∞should␈α∞be␈α∞stored␈α∞in␈α∞atom
␈↓ α←␈↓space␈α∩since␈α∩their␈α∩encodings␈α∩should␈α∩not␈α∩be␈α∩interpreted␈α∩as␈α∩pointers.␈α∩ Since
␈↓ α←␈↓"atom␈α∞space"␈α∞is␈α∞no␈α∞longer␈α∂an␈α∞appropriate␈α∞descriptor␈α∞for␈α∞that␈α∞space,␈α∂we␈α∞will
␈↓ α←␈↓give␈α∩it␈α∩a␈α∩new␈α∩name.␈α∩ We␈α∩will␈α∩call␈α∩that␈α∩area␈α∩of␈α∩memory␈α∪which␈α∩contains
␈↓ α←␈↓information␈α↔␈↓¬not␈↓␈α↔to␈α↔be␈α↔interpreted␈α⊗as␈α↔pointers,␈α↔␈↓↓Full␈α↔Word␈α↔Space␈↓,␈α⊗and
␈↓ α←␈↓abbreviate it as FWS.
␈↓"β␈↓ α←␈↓␈↓ β'In␈αsummary,␈α
we␈αhave␈α
discussed␈αthe␈αdetails␈α
of␈αa␈α
typical␈αimplementation
␈↓ α←␈↓of␈α∃the␈α∃class␈α∃of␈α∃S-exprs.␈α∀Our␈α∃non-atomic␈α∃S-exprs␈α∃have␈α∃their␈α∀branching
␈↓ α←␈↓structure␈αstored␈αin␈αpointer␈αspace.␈α
Our␈αinitial␈αdiscussion␈αof␈αatoms␈α
supposed␈αa
␈↓ α←␈↓particular␈α
simple␈α
representation:␈α
simply␈α
store␈α
the␈α
encoding␈α
of␈α
the␈α
atom␈α∞in␈α
a
␈↓ α←␈↓memory␈α⊗location␈α⊗in␈α∃a␈α⊗separate␈α⊗space␈α∃called␈α⊗atom␈α⊗space.␈α⊗Upon␈α∃further
␈↓ α←␈↓reflection␈α∪we␈α∪decided␈α∩that␈α∪atoms␈α∪should␈α∩play␈α∪a␈α∪more␈α∩active␈α∪role␈α∪in␈α∩the
␈↓ α←␈↓implementation.␈α
Since␈α∞identifiers␈α
are␈α∞to␈α
be␈α
represented␈α∞as␈α
atoms,␈α∞we␈α
needed
␈↓ α←␈↓some␈α∂way␈α∂to␈α∂represent␈α∂those␈α∂properties␈α∂typically␈α∂associated␈α∂with␈α∞identifiers.
␈↓ α←␈↓Identifiers␈αin␈αLISP␈αare,␈α
among␈αother␈αthings,␈αused␈α
for␈αnames␈αof␈αfunctions␈α
and
␈↓ α←␈↓names␈α⊂of␈α⊂variables.␈α⊂We␈α⊂needed␈α⊃the␈α⊂ability␈α⊂to␈α⊂represent␈α⊂at␈α⊂least␈α⊃these␈α⊂two
␈↓ α←␈↓kinds␈α
of␈α
"values"␈α
with␈α
a␈α∞LISP␈α
atom.␈α
We␈α
introduced␈α
the␈α
general␈α∞scheme␈α
of
␈↓ α←␈↓property-lists␈α∩and␈α∩associated␈α∩such␈α∩a␈α∩p-list␈α∩with␈α∩each␈α∩LISP␈α∩atom.␈α∩All␈α∩the
␈↓ α←␈↓things␈α∂we␈α∂know␈α∂about␈α∞a␈α∂specific␈α∂atom␈α∂are␈α∞stored␈α∂on␈α∂the␈α∂p-list.␈α∂ We␈α∞stored
␈↓ α←␈↓each␈α⊂atom␈α⊂uniquely;␈α⊂then␈α⊂to␈α⊂examine␈α⊂the␈α⊂properties␈α⊂of␈α⊂an␈α⊂atom␈α⊂only␈α⊂one
␈↓ α←␈↓structure␈αneed␈αbe␈αlocated.␈α Since␈αall␈αof␈αour␈αLISP␈αprograms␈αmust␈αbe␈αread␈αinto
␈↓ α←␈↓the␈αmemory,␈αwe␈αrequired␈αthat␈αthe␈αinput␈αfunction␈αkeep␈αatoms␈αstored␈αuniquely.
␈↓ α←␈↓On␈αreading␈αa␈αliteral␈αatom,␈αthe␈αprogram␈αchecks␈αthe␈αcurrent␈αtable␈αof␈αatoms.␈α If
␈↓ α←␈↓the␈α∞atom␈α∞appears,␈α∞the␈α∞program␈α∞returns␈α∞a␈α∞pointer␈α∞to␈α∞the␈α∞entry.␈α∞ If␈α∞the␈α
atom
␈↓ α←␈↓does␈αnot␈αappear␈αit␈αconstructs␈αa␈αnew␈αtable␈αentry␈αconsisting␈αof␈αthe␈αprint␈αname.
␈↓ α←␈↓One␈α∪effect␈α∪unique␈α∪storage␈α∪was␈α∀to␈α∪turn␈α∪our␈α∪abstract␈α∪LISP-trees␈α∀into␈α∪list
␈↓ α←␈↓structure.␈α
Indeed,␈α
the␈αrepresentation␈α
of␈α
a␈α
LISP␈αexpression␈α
is␈α
a␈α
complex␈αnet
␈↓ α←␈↓of␈α∂pointers;␈α∂even␈α∞atoms␈α∂are␈α∂now␈α∞pointers.␈α∂The␈α∂only␈α∞LISP␈α∂objects␈α∂we␈α∞have
␈↓ α←␈↓represented which are ␈↓¬not␈↓ pointers are the actual print-names like ␈↓
BAZ≡≡␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'To␈α∂reinforce␈α∂our␈α∂discussion␈α⊂we␈α∂illustrate␈α∂the␈α∂abstract␈α∂picture␈α⊂of␈α∂␈↓αNIL␈↓
␈↓ α←␈↓and,␈α∞on␈α∞the␈α∞next␈α∞page,␈α∞one␈α∞implementation␈α∞of␈α∞the␈α∞atom␈α∞␈↓αNIL␈↓.␈α∞In␈α∞all␈α∞of␈α∞the
␈↓ α←␈↓resulting␈α
worms␈α
there␈αare␈α
only␈α
three␈α
elements␈αin␈α
Full␈α
Word␈αSpace;␈α
everything
␈↓ α←␈↓else is a pointer.
␈↓"⊃␈↓ α←␈↓
⊂αααααα→αααααα→ααααααα→αααααα→ααααααα⊃
␈↓"␈↓ α←␈↓
↑ ↓
␈↓"␈↓ α←␈↓
⊂ααα←ααααα←ααααα⊃ ~
␈↓"␈↓ α←␈↓
↓ ↑ ~ ⊂α←α⊃ ↓
␈↓"␈↓ α←␈↓
~ ⊂αβαπααααα⊃ ↑ ↓ ⊂αβαπααααααα⊃ ⊂αααπααααααα⊃
␈↓"␈↓ α←␈↓
%αα→~ # ~ #α→αβ→$ ε→~ # ~ PNAME ~ ~ # ~ CONST ~
␈↓"␈↓ α←␈↓
εαααβαααααλ ↑ %ααα∀ααααααα$ %αβα∀ααααααα$
␈↓"␈↓ α←␈↓
~ # ~ NIL ~ %←αααααα←αααααα←ααααα$
␈↓"␈↓ α←␈↓
%αβα∀ααααα$ ↑
␈↓"␈↓ α←␈↓
%→αααααα→αααααα→$
␈↓ α←␈↓␈↓270 Static Structure␈↓
_5.10␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧z␈↓↓5.10 A Picture of the Atom ␈↓αNIL␈↓↓␈↓α
␈↓"β␈↓ α←␈↓␈↓ β'We have been writing the atom ␈↓αNIL␈↓ as:
␈↓"␈↓ α←␈↓
⊂απααα⊃ ⊂αααααααπααα⊃ ⊂αααπααα⊃ ⊂αααααααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
~∃~ #αβα→~ CONST ~ #αβα→~ # ~ #αβα→~ PNAME ~ #αβα→~ # ~≤'~
␈↓"␈↓ α←␈↓
%α∀ααα$ %ααααααα∀ααα$ %αβα∀ααα$ %ααααααα∀ααα$ %αβα∀αα$
␈↓"␈↓ α←␈↓
↓ ~ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
NIL %α→~ # ~≤'~
␈↓"␈↓ α←␈↓
%αβα∀αα$
␈↓"␈↓ α←␈↓
↓
␈↓"␈↓ α←␈↓
⊂ααααα⊃
␈↓"␈↓ α←␈↓
~NIL≡≡~
␈↓"␈↓ α←␈↓
%ααααα$
␈↓"β␈↓ α←␈↓␈↓ β'where the atoms for ␈↓αPNAME␈↓ and ␈↓αCONST␈↓ are represented as:
␈↓"␈↓ α←␈↓
⊂απααα⊃ ⊂αααααααπααα⊃ ⊂αααπαα⊃ ⊂απααα⊃ ⊂αααααααπααα⊃
␈↓"␈↓ α←␈↓
~∃~ #αβα→~ PNAME ~ #αβα→~ # ~≤'~ ~∃~ #αβα→~ PNAME ~ #αβα
␈↓"␈↓ α←␈↓
%α∀ααα$ %ααααααα∀ααα$ %αβα∀αα$ %α∀ααα$ %ααααααα∀ααα$
␈↓"␈↓ α←␈↓
↓
␈↓"␈↓ α←␈↓
⊂αααπαα⊃
␈↓"␈↓ α←␈↓
~ # ~≤'~
␈↓"␈↓ α←␈↓
%αβα∀αα$
␈↓"␈↓ α←␈↓
↓
␈↓"␈↓ α←␈↓
⊂ααααα⊃
␈↓"␈↓ α←␈↓
~PNAME~
␈↓"␈↓ α←␈↓
%ααααα$
␈↓"β␈↓ α←␈↓␈↓ β'In full detail, ␈↓αNIL␈↓ is represented as:
␈↓"␈↓ α←␈↓
⊂ααααααααααααααααααα←ααααααααααααααααααααααααααα←ααααααπααααπα
␈↓"␈↓ α←␈↓
↓ ↑ ~
␈↓"␈↓ α←␈↓
⊂απααα⊃ ⊂αααααααπααα⊃ ⊂αααπααα⊃ ⊂αααααααπααα⊃ ⊂αααπαβα⊃ ↑
␈↓"␈↓ α←␈↓
~∃~ #αβα→~ # ~ #αβα→~ # ~ #αβα→~ # ~ #αβα→~ # ~ # ~ ~
␈↓"␈↓ α←␈↓
%α∀ααα$ %αααβααα∀ααα$ %αβα∀ααα$ %αααβααα∀ααα$ %αβα∀ααα$ ~
␈↓"␈↓ α←␈↓
↑ %αααααααααα→ ~ →αααααααα→ ~→αα⊃ ~ ↑
␈↓"␈↓ α←␈↓
~ ↓ ~ ~ ~ ⊂αααπαβα
␈↓"␈↓ α←␈↓
~ ~ ↓ ↓ %α→~ # ~ #
␈↓"␈↓ α←␈↓
~ ↓ ~ ~ %αβα∀ααα
␈↓"␈↓ α←␈↓
~ ~ ~ ~ ↓
␈↓"␈↓ α←␈↓
%ααααααα←ααααααααα←αααααα∀αααπαααα⊃ ~ ~ ⊂ααααα⊃
␈↓"␈↓ α←␈↓
↑ ↑ ~ ~ ~NIL≡≡~
␈↓"␈↓ α←␈↓
~ ~ ↓ ~ %ααααα$
␈↓"␈↓ α←␈↓
⊂αααααααααα←ααααααααααααααα← ~ ←α ~ ←α$ ~
␈↓"␈↓ α←␈↓
↓ ~ ~ ↓
␈↓"␈↓ α←␈↓
⊂απααα⊃ ⊂αααααααπααα⊃ ⊂αααπαβα⊃ ~ ⊂απααα⊃ ⊂αααααααπααα⊃
␈↓"␈↓ α←␈↓
~∃~ #αβα→~ # ~ #αβα→~ # ~ # ~ ↑ ~∃~ #αβα→~ # ~ #αβα
␈↓"␈↓ α←␈↓
%α∀ααα$ %αααβααα∀ααα$ %αβα∀ααα$ ~ %α∀ααα$ %αααβααα∀ααα$
␈↓"␈↓ α←␈↓
↑ ~ ↓ ~ ~
␈↓"␈↓ α←␈↓
εααααα←ααααα$ ⊂αααπααα⊃ ↑ ↓
␈↓"␈↓ α←␈↓
~ ~ # ~ #αβα$ ~
␈↓"␈↓ α←␈↓
~ %αβα∀ααα$ ~
␈↓"␈↓ α←␈↓
~ ↓ ~
␈↓"␈↓ α←␈↓
↑ ⊂ααααα⊃ ~
␈↓"␈↓ α←␈↓
~ ~PNAME~ ↓
␈↓"␈↓ α←␈↓
~ %ααααα$ ~
␈↓"␈↓ α←␈↓
%αααααααα←ααααααααααααα←ααααααααααααα←αααααααααααααα$
␈↓ α←␈↓␈↓5.11␈↓ π∩Input/Output: ␈↓αread␈↓ and ␈↓αprint␈↓ 271␈↓α
␈↓"β␈↓ α←␈↓␈↓ ∧r␈↓↓5.11 Input/Output: ␈↓αread␈↓↓ and ␈↓αprint␈↓↓␈↓α
␈↓"β␈↓ α←␈↓¬␈↓ β'"...Thus␈α≤syntax␈α≠is␈α≤the␈α≤servant␈α≠of␈α≤sematics,␈α≤an␈α≠appropriate
␈↓ α←␈↓¬␈↓ β'relationship␈αsince␈αthe␈αsubstance␈αof␈αthe␈αmessage␈αis␈αconveyed␈αwith␈αthe
␈↓ α←␈↓¬␈↓ β'semantics,␈α
variations␈α
in␈α
syntax␈α
being␈α
an␈α
inessential␈αtrimming␈α
added
␈↓ α←␈↓¬␈↓ β'on human-engineering grounds. ..."
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ π5Vaughan Pratt, [Pra 73]
␈↓"λ␈↓ α←␈↓The␈α⊂implementation␈α⊂of␈α⊂LISP␈α⊂is␈α⊂simplified␈α⊂dramatically␈α⊂since␈α⊂a␈α⊂very␈α∂large
␈↓ α←␈↓part␈αof␈αthat␈αimplementation␈αcan␈αbe␈αwritten␈αin␈αLISP␈αitself.␈α We␈αhave␈αalready
␈↓ α←␈↓seen␈α
that␈α
the␈α
evaluation␈α
process␈α
is␈α
expressible␈α
this␈α
way,␈α
and␈α
we␈α
will␈α
exploit
␈↓ α←␈↓this property again in dealing with compilers (Chapter 6).
␈↓"β␈↓ α←␈↓␈↓ β'In␈α∞this␈α∞section␈α
we␈α∞will␈α∞show␈α
that␈α∞the␈α∞majority␈α
of␈α∞the␈α∞LISP␈α∞input␈α
and
␈↓ α←␈↓output␈αroutines␈α
can␈αbe␈α
written␈αas␈α
LISP␈αfunctions␈α
calling␈αa␈α
very␈αfew␈α
primitive
␈↓ α←␈↓routines.␈α⊂ The␈α⊂primitive␈α∂routines␈α⊂are␈α⊂also␈α∂described␈α⊂in␈α⊂LISP,␈α⊂though␈α∂they
␈↓ α←␈↓would normally be coded in machine language.
␈↓"β␈↓ α←␈↓␈↓ β'The primitive functions are ␈↓αratom␈↓ and ␈↓αpatom␈↓.
␈↓"β␈↓ α←␈↓␈↓αratom[␈α]␈↓␈αis␈αa␈αfunction␈αof␈αno␈αarguments.␈αIt␈αreads␈αthe␈αinput␈αstring,␈αconstructing
␈↓ α←␈↓␈↓ β'the␈α∞next␈α∞atom␈α∞or␈α∞special␈α∞character␈α∞(left␈α∞paren,␈α∞right␈α∞paren␈α∞or␈α∞dot).␈α
It
␈↓ α←␈↓␈↓ β'looks␈α∂up␈α∂that␈α∂object␈α∂in␈α∂the␈α∂atom␈α∂table␈α∂and␈α∂returns␈α∂a␈α∂pointer␈α⊂to␈α∂that
␈↓ α←␈↓␈↓ β'table␈αentry.␈↓π 22␈↓␈αIf␈αno␈α
entry␈αis␈αfound␈αan␈α
appropriate␈αentry␈αis␈αmade.␈α
␈↓αratom␈↓
␈↓ α←␈↓␈↓ β'skips␈α∞over␈α∞spaces␈α∞and␈α∞commas,␈α
only␈α∞recognizing␈α∞them␈α∞as␈α∞delimiters.␈α
It
␈↓ α←␈↓␈↓ β'returns only atoms or special characters to ␈↓αread␈↓.
␈↓"∀␈↓ α←␈↓␈↓αpatom[x]␈↓␈α
is␈α
a␈α
function␈α
of␈α
one␈αargument␈α
expecting␈α
an␈α
atom,␈α
left␈α
paren,␈αright
␈↓ α←␈↓␈↓ β'paren,␈α⊂blank,␈α⊂or␈α⊂dot␈α⊂as␈α⊂the␈α⊃value␈α⊂of␈α⊂its␈α⊂argument.␈α⊂ It␈α⊂will␈α⊃print␈α⊂the
␈↓ α←␈↓␈↓ β'p-name of that object on the output device.
␈↓"∀␈↓ α←␈↓␈↓ β'To␈α
simplify␈α∞matters,␈α
we␈α∞need␈α
to␈α∞refer␈α
to␈α∞atoms␈α
whose␈α∞print-names␈α
are
␈↓ α←␈↓the␈α∩characters␈α∩"␈↓α)␈↓",␈α∩"␈↓α(␈↓",␈α⊃".",␈α∩and␈α∩"␈α∩" (blank).␈α⊃ We␈α∩will␈α∩assume␈α∩that␈α⊃␈↓αRPAR␈↓,
␈↓ α←␈↓␈↓αLPAR␈↓,␈α␈↓αPERIOD␈↓,␈αand␈α␈↓αBLANK␈↓␈αdenote␈αsuch␈αatoms.␈α For␈αexample,␈αif␈αthe␈αnext
␈↓ α←␈↓input character is "(" then
␈↓" ␈↓ α←␈↓␈↓ β∂␈↓αeq[ratom[␈α];LPAR]␈↓␈α
is␈αtrue␈α
(and␈αthe␈α
input␈αpointer␈α
is␈αmoved␈α
to␈αthe␈α
next
␈↓ α←␈↓␈↓ β∂character!).
␈↓" ␈↓ α←␈↓␈↓αpatom[PERIOD]␈↓ will have the effect of printing a ␈↓↓"."␈↓ on the output device.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αLISP␈α␈↓↓scanner␈↓␈α
is␈α␈↓αratom␈↓.␈α A␈αscanner␈α
must␈αnegotiate␈αwith␈α
the␈αactual
␈↓ α←␈↓input␈α⊗device␈α⊗for␈α↔input␈α⊗characters.␈α⊗The␈α↔scanner␈α⊗builds␈α⊗the␈α↔most␈α⊗basic
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 22␈↓Numerals␈α⊃are␈α⊃typically␈α⊂not␈α⊃stored␈α⊃uniquely;␈α⊂also␈α⊃they␈α⊃are␈α⊃given␈α⊂a
␈↓ α←␈↓simpler␈α∞structure␈α∞than␈α∞the␈α∞atomic␈α∞p-list.␈α∞See␈α∞Section 7.9␈α∞for␈α∞more␈α∞details␈α
on
␈↓ α←␈↓LISP numbers.
␈↓ α←␈↓␈↓272 Static Structure␈↓
_5.11␈↓
␈↓"β␈↓ α←␈↓ingredients,␈αlike␈αidentifiers␈α
or␈αnumbers,␈αand␈α
only␈αafter␈αsuch␈α
a␈αbasic␈αblock␈α
has
␈↓ α←␈↓been␈α
recognized␈αis␈α
the␈α
next␈αlevel␈α
of␈α
syntax␈αanalysis␈α
attempted.␈α
The␈αunits,␈α
also
␈↓ α←␈↓called tokens, which the scanner has built are passed to the ␈↓↓parser␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α
parser␈α
determines␈α
whether␈α
or␈α
not␈α
the␈α
input␈α
stream␈α
is␈α
a␈α
well-formed
␈↓ α←␈↓expression.␈α∞ The␈α∂LISP␈α∞parser␈α∞is␈α∂␈↓αread␈↓;␈α∞it␈α∞builds␈α∂a␈α∞tree-representation␈α∂of␈α∞the
␈↓ α←␈↓input string, recognizing both S-expression and list-notation.
␈↓"∀␈↓ α←␈↓αread <=λ[[ ]␈↓ ∧πλ[[j]␈↓ ∧C[atom[j] → j;
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ∧C is_lpar[j] → read_head[ ];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ∧C ␈↓
t␈↓α → err[ ]]
␈↓"β␈↓ α←␈↓α␈↓ ∧π[ratom[ ]]]
␈↓"∀␈↓ α←␈↓The call on ␈↓αerr␈↓ will terminate the input scanning immediately.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓αread_head␈↓␈αwill␈αtranslate␈αstrings␈α
␈↓λα␈↓␈αacceptable␈αin␈αthe␈αcontext␈α
"␈↓α(␈↓λα␈↓".␈α Thus
␈↓ α←␈↓␈↓λα␈↓␈α∪being␈α∪␈↓α"A)"␈↓␈α∀or␈α∪␈↓α"A (B . C))"␈↓␈α∪would␈α∪be␈α∀suitable␈α∪for␈α∪␈↓αread_head␈↓;␈α∀␈↓α(A)␈↓␈α∪and
␈↓ α←␈↓␈↓α(A (B . C))␈↓␈α∂are␈α∞S-exprs␈α∂or␈α∞lists.␈α∂ ␈↓α. A)␈↓␈α∂would␈α∞not␈α∂be␈α∞acceptable␈α∂since␈α∂␈↓α(. A)␈↓␈α∞is
␈↓ α←␈↓neither an S-expr nor list.
␈↓"β␈↓ α←␈↓Therefore, if ␈↓αread_head␈↓ sees:
␈↓" ␈↓ α←␈↓␈↓ ∧∨an atom, then ␈↓λα␈↓ is <atom>␈↓λβ␈↓α)␈↓;
␈↓" ␈↓ α←␈↓␈↓ ∧∨a left parenthesis, then ␈↓λα␈↓ is ␈↓α(␈↓λβ␈↓α) ␈↓λ∂␈↓α)␈↓;
␈↓" ␈↓ α←␈↓␈↓ ∧∨a dot, then ␈↓λα␈↓ is ␈↓α. ␈↓λβ␈↓α)␈↓; this is an error;
␈↓" ␈↓ α←␈↓␈↓ ∧∨a right parenthesis, then ␈↓λα␈↓ is ␈↓α)␈↓
␈↓"→␈↓ α←␈↓αread_head <= λ[[ ]␈↓ ∧Oλ[[j]␈↓ ¬[atom[j] → cons[j;read_tail[]];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬ is_lpar[j] → cons[␈↓ εoread_head[ ];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬␈↓ εoread_tail[ ]];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬ is_rpar[j] → NIL;
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬ ␈↓
t␈↓α → err[ ]]]
␈↓"β␈↓ α←␈↓α␈↓ ∧O [ratom[ ]]]
␈↓"∀␈↓ α←␈↓␈↓αread_tail␈↓␈α
is␈α
looking␈α
for␈α
legal␈α␈↓λα␈↓'s␈α
in␈α
the␈α
context␈α
"␈↓α(␈↓<sexpr> ␈↓λα␈↓".␈α
The␈αstructure
␈↓ α←␈↓of␈α
this␈α
function␈α
is␈αthat␈α
of␈α
␈↓αread_head␈↓␈α
except␈αfor␈α
recognition␈α
of␈α
dots.␈α
"␈↓α. ␈↓λβ␈↓α)␈↓"␈α␈↓↓is␈↓
␈↓ α←␈↓plausible␈α∩in␈α∩the␈α∩context␈α∩"␈↓α(␈↓<sexpr> ␈↓λα␈↓".␈α∩ It␈α∪is␈α∩up␈α∩to␈α∩␈↓αread_cdr␈↓␈α∩to␈α∩see␈α∪if␈α∩its
␈↓ α←␈↓expectations are fulfilled.
␈↓"∀␈↓ α←␈↓αread_tail <= λ[[ ]␈↓ ∧Cλ[[j]␈↓ ∧␈[atom[j] → cons[j;read_tail[]];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧␈ is_lpar[j] → cons[␈↓ εoread_head[ ];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧␈␈↓ εoread_tail[ ]];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧␈ is_dot[j] → read_cdr[];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧␈ is_rpar[j] → NIL;
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧␈ ␈↓
t␈↓α → err[ ]]]
␈↓"β␈↓ α←␈↓α␈↓ ∧C [ratom[ ]]]
␈↓ α←␈↓␈↓5.11␈↓ π∩Input/Output: ␈↓αread␈↓ and ␈↓αprint␈↓ 273␈↓α
␈↓"∀␈↓ α←␈↓The␈α⊃only␈α⊃input␈α⊃legal␈α⊃after␈α⊃a␈α⊃dot␈α⊃is␈α⊃a␈α⊃S-expr␈α⊃or␈α⊃list␈α⊃followed␈α⊃by␈α⊃a␈α⊃right
␈↓ α←␈↓parenthesis. Therefore:
␈↓"∀␈↓ α←␈↓αread_cdr <= λ[[ ]␈↓ ∧Cλ[[j]␈↓ ∧␈[is_rpar[ratom[ ]] → j;
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧␈ ␈↓
t␈↓α → err[ ] ]]
␈↓"β␈↓ α←␈↓α␈↓ ∧C [read[ ]]]
␈↓"β␈↓ α←␈↓␈↓ β'Finally,␈α_here␈α_are␈α↔some␈α_of␈α_the␈α↔primitive␈α_recognizers␈α_which␈α↔these
␈↓ α←␈↓functions use.
␈↓" ␈↓ α←␈↓α␈↓ βfis_dot[x] <= eq[x;PERIOD] is_lpar[x] <= eq[x;LPAR]␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'The printing of an internalized LISP expression is straightforward.
␈↓" ␈↓ α←␈↓α␈↓ ∧oprint <= λ[[x] prin0[x]; terpri[ ]; x]
␈↓" ␈↓ α←␈↓αterpri␈↓ initiates a new output line.
␈↓"∀␈↓ α←␈↓αprin0 <= λ[[x][atom[x] → patom[x];
␈↓"β␈↓ α←␈↓α␈↓ ∧C ␈↓
t␈↓α → patom[LPAR]; prinbody[x]]]
␈↓"β␈↓ α←␈↓αprinbody <= λ[[x]␈↓ ∧Oprin0[car[x]];
␈↓"β␈↓ α←␈↓α␈↓ ∧O[null[cdr[x]] → patom[RPAR];
␈↓"β␈↓ α←␈↓α␈↓ ∧O atom[cdr[x]] →␈↓ ε∂patom[BLANK];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ε∂patom[PERIOD];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ε∂patom[BLANK];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ε∂patom[cdr[x]];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ε∂patom[RPAR];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓
t␈↓α → prinbody[cdr[x]]]]
␈↓"∀␈↓ α←␈↓Notice␈α⊗that␈α↔we␈α⊗have␈α⊗used␈α↔the␈α⊗extended␈α⊗λ-expressions␈α↔and␈α⊗conditional
␈↓ α←␈↓expression as described in Section 4.3.␈↓π 23␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈αbasic␈α␈↓αprint␈↓␈αroutine␈αallows␈αus␈αto␈αprint␈αdata␈αstructures␈αand␈αprogram
␈↓ α←␈↓representations.␈α↔However␈α↔the␈α↔printer␈α↔will␈α↔print␈α↔duplications␈α↔for␈α_a␈α↔list
␈↓ α←␈↓structure␈αwhich␈α
has␈αshared␈αbranches␈α
and,␈αworse␈α
yet,␈αwill␈αnot␈α
terminate␈αif␈αit␈α
is
␈↓ α←␈↓given␈α⊗a␈α∃circular␈α⊗structure.␈α∃Some␈α⊗implementations␈α∃of␈α⊗LISP␈α⊗remedy␈α∃this
␈↓ α←␈↓ailment; see [Ter 75] or [Int 75].
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 23␈↓Notice␈αtoo␈αthat␈α
␈↓αprint[(A .(B . C))]␈↓␈αprints␈αas␈α
␈↓α(A B . C)␈↓.␈αThis␈αis␈α
because
␈↓ α←␈↓␈↓αprint␈↓␈α∪doesn't␈α∩know␈α∪that␈α∩the␈α∪structure␈α∪is␈α∩not␈α∪a␈α∩list␈α∪until␈α∩it␈α∪sees␈α∪the␈α∩last
␈↓ α←␈↓dotted-pair.␈αThere␈αare␈αtwo␈αways␈αof␈αhandling␈αthis:␈αeither␈αrequire␈α
a␈αtype-code,
␈↓ α←␈↓telling␈αwhether␈αthe␈αstructure␈αis␈αa␈αdotted␈α
pair␈αor␈αa␈αlist,␈αrepresented␈αas␈αa␈α
dotted
␈↓ α←␈↓pair.␈α⊃Then␈α⊃␈↓¬all␈↓␈α⊃dotted␈α⊂pairs␈α⊃are␈α⊃printed␈α⊃in␈α⊂dot␈α⊃notation,␈α⊃and␈α⊃␈↓¬all␈↓␈α⊃lists␈α⊂are
␈↓ α←␈↓printed␈α⊗in␈α∃list␈α⊗notation.␈α∃The␈α⊗other␈α∃alternative␈α⊗is␈α∃to␈α⊗first␈α⊗examine␈α∃the
␈↓ α←␈↓structure;␈α
if␈α
it␈α
is␈αa␈α
list␈α
representation,␈α
then␈α
print␈αit␈α
that␈α
way,␈α
otherwise␈αprint␈α
it
␈↓ α←␈↓as␈α_a␈α_dotted␈α_pair.␈α_This␈α_problem␈α_is␈α_another␈α_indication␈α_of␈α→"object␈α_vs.
␈↓ α←␈↓representation".
␈↓ α←␈↓␈↓274 Static Structure␈↓
_5.11␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
output␈α
format␈αis␈α
a␈α
simple␈α
linear␈αstring␈α
of␈α
atoms,␈α
numbers,␈αspaces,
␈↓ α←␈↓and␈α
parentheses.␈α
For␈α
example␈α∞a␈α
␈↓αprint␈↓-based␈α
program␈α
for␈α∞printing␈α
function
␈↓ α←␈↓definitions might output the following as the definition of ␈↓αmember␈↓:
␈↓"∀␈↓ α←␈↓α␈↓ ∧O(MEMBER␈α⊂(LAMBDA␈α⊂(X␈α⊂L)␈α∂(COND
␈↓ α←␈↓α␈↓ ∧O((NULL␈α∞L)␈α∞NIL)␈α
((EQ␈α∞X␈α∞(FIRST␈α
L))
␈↓ α←␈↓α␈↓ ∧OT) (T (MEMBER X (REST L))))))
␈↓"∀␈↓ α←␈↓The␈α∂print␈α∂routine␈α∂can␈α⊂break␈α∂the␈α∂text␈α∂at␈α∂the␈α⊂end␈α∂of␈α∂any␈α∂atom;␈↓π 24␈↓␈α⊂the␈α∂only
␈↓ α←␈↓restriction␈α
we␈α
place␈αon␈α
printing␈α
of␈αexpressions␈α
is␈α
that␈αwhat␈α
is␈α
␈↓αprint␈↓-ed␈αmust
␈↓ α←␈↓be ␈↓αread␈↓-able.
␈↓"β␈↓ α←␈↓␈↓ β'Even␈α∞with␈α∞a␈α∞small␈α∞definition␈α∞like␈α∞this,␈α∞we␈α∞have␈α∞difficulty␈α∞deciphering
␈↓ α←␈↓the␈α∞structure.␈α∞When␈α∞functions␈α∞or␈α∞lists␈α∞become␈α∞large␈α∞and␈α∞deeply␈α∞nested␈α
then
␈↓ α←␈↓readability␈α→becomes␈α→impossible.␈α_Most␈α→implementations␈α→of␈α→LISP␈α_supply
␈↓ α←␈↓formatting␈α∞programs␈α
called␈α∞"pretty-printers"␈α
or␈α∞"grinders"␈α
to␈α∞supplement␈α
the
␈↓ α←␈↓basic print routine.
␈↓"β␈↓ α←␈↓␈↓ β'A pretty-printer might print ␈↓αmember␈↓ as:
␈↓"∀␈↓ α←␈↓α(MEMBER
␈↓"β␈↓ α←␈↓α (LAMBDA (X L)
␈↓"β␈↓ α←␈↓α (COND␈↓ βo((NULL L) NIL)
␈↓"β␈↓ α←␈↓α␈↓ βo((EQ X (FIRST L)) T)
␈↓"β␈↓ α←␈↓α␈↓ βo(T (MEMBER X (REST L))))))
␈↓"∀␈↓ α←␈↓See␈α≤Section 9.2␈α≠for␈α≤a␈α≠more␈α≤detailed␈α≠description␈α≤of␈α≤such␈α≠formatting
␈↓ α←␈↓functions.
␈↓"β␈↓ α←␈↓␈↓ β'So␈αfar␈α
we␈αhave␈α
thrown␈αall␈α
the␈αI/O␈α
back␈αon␈α
␈↓αratom␈↓␈αand␈α
␈↓αpatom␈↓.␈α Clearly
␈↓ α←␈↓␈↓αratom␈↓␈α
will␈α
be␈α
more␈αinteresting.␈α
All␈α
␈↓αpatom␈↓␈α
need␈α
do␈αis␈α
get␈α
the␈α
p-name␈αand␈α
print
␈↓ α←␈↓it.␈α∂ ␈↓αratom␈↓␈α∂should␈α∂perform␈α∂an␈α∂efficient␈α∂search␈α∂of␈α∂the␈α∂atom␈α∂table␈α∂and␈α⊂if␈α∂the
␈↓ α←␈↓atom␈α∂is␈α∞not␈α∂found,␈α∞add␈α∂it␈α∞to␈α∂the␈α∞table.␈α∂ All␈α∞␈↓αratom␈↓␈α∂has␈α∞to␈α∂work␈α∞with␈α∂is␈α∞the
␈↓ α←␈↓actual␈αcharacter␈αstring␈αwhich␈αwill␈αbe␈αthe␈αp-name␈αof␈αsome␈αatom.␈α What␈α␈↓αratom␈↓
␈↓ α←␈↓could␈αdo␈αis␈αlook␈αat␈αthe␈αp-name␈αof␈αeach␈αatom␈αcurrently␈αin␈αthe␈αtable␈αof␈αatoms;
␈↓ α←␈↓when␈αit␈αfinds␈αa␈αmatch␈αit␈αreturns␈αa␈αpointer␈αto␈αthat␈αatom;␈αthis␈αis␈αessentially␈αthe
␈↓ α←␈↓linear␈α∞search␈α∞scheme␈α∞of␈α∞␈↓αassoc␈↓.␈α∞ If␈α∞the␈α∞appropriate␈α∞atom␈α∞is␈α∞not␈α∞found␈α∞it␈α∞can
␈↓ α←␈↓build␈α
a␈α
new␈α
one␈α
consisting␈α
of␈α
the␈α
p-name,␈α
add␈α
it␈α
to␈α
the␈α
table,␈α
and␈α
return␈αa
␈↓ α←␈↓pointer␈α
to␈α
this␈α
new␈αentry.␈α
In␈α
the␈α
next␈αsection␈α
we␈α
will␈α
introduce␈αan␈α
alternative
␈↓ α←␈↓scheme called hashing.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 24␈↓Some␈αimplementations␈αeven␈αallow␈αthe␈αprinter␈αto␈αbreak␈αin␈αthe␈αmiddle
␈↓ α←␈↓of␈α∩an␈α∩atom.␈α⊃This␈α∩is␈α∩accomplished␈α∩by␈α⊃designating␈α∩a␈α∩special␈α∩character␈α⊃for
␈↓ α←␈↓carriage␈α∀control,␈α∪and␈α∀the␈α∪␈↓αread␈↓␈α∀routine␈α∪knows␈α∀to␈α∪ignore␈α∀the␈α∪immediately
␈↓ α←␈↓following end-of-line sequence.
␈↓ α←␈↓␈↓5.11␈↓ π∩Input/Output: ␈↓αread␈↓ and ␈↓αprint␈↓ 275␈↓α
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. You␈α∞might␈α
have␈α∞noticed␈α
that␈α∞the␈α
definitions␈α∞of␈α
␈↓αread_head␈↓␈α∞and␈α
␈↓αread_tail␈↓
␈↓ α←␈↓␈↓ β∂are␈αalmost␈αidentical:␈αthe␈αdifference␈α
involves␈αtreatment␈αof␈αdots.␈α Write␈α
new
␈↓ α←␈↓␈↓ β∂versions␈α⊂of␈α⊃these␈α⊂functions␈α⊃utilizing␈α⊂a␈α⊃common␈α⊂routine␈α⊃and␈α⊂functional
␈↓ α←␈↓␈↓ β∂arguments.
␈↓" ␈↓ α←␈↓2. Write␈αa␈αset␈αof␈αBNF␈αequations␈αthat␈αgenerate␈αthe␈αsame␈αset␈αof␈αsentences␈αthat
␈↓ α←␈↓␈↓ β∂␈↓αread␈↓ parses.
␈↓" ␈↓ α←␈↓3. Write a version of ␈↓αread␈↓ which only accepts list notation.
␈↓"β␈↓ α←␈↓␈↓ ¬ε␈↓↓5.12 Table Searching: Hashing␈↓
␈↓"β␈↓ α←␈↓Table␈α∃lookup␈α∃is␈α∃analogous␈α∃to␈α⊗the␈α∃problem␈α∃of␈α∃looking␈α∃up␈α∃words␈α⊗in␈α∃a
␈↓ α←␈↓dictionary.␈α
The␈α
scheme␈α
of␈α␈↓αassoc␈↓␈α
is␈α
analogous␈α
to␈αbeginning␈α
at␈α
the␈α
first␈αpage
␈↓ α←␈↓of␈α∂the␈α∂dictionary␈α∂and␈α∂proceeding␈α∂linearly,␈α∂word-by-word␈α∂and␈α∞page-by-page,
␈↓ α←␈↓through␈α
the␈αbook␈α
until␈αthe␈α
word␈αin␈α
question␈αis␈α
found.␈α More␈α
usually,␈αwe␈α
look
␈↓ α←␈↓at␈αthe␈α
first␈αcharacter␈α
of␈αthe␈αword␈α
and␈αgo␈α
immediately␈αto␈α
the␈αsubsection␈αof␈α
the
␈↓ α←␈↓dictionary␈α⊂which␈α⊂has␈α∂the␈α⊂words␈α⊂beginning␈α∂with␈α⊂that␈α⊂character.␈α⊂ We␈α∂know
␈↓ α←␈↓that␈αif␈αwe␈αcannot␈αfind␈αthe␈αdefinition␈αof␈αour␈αword␈αin␈αthat␈αsubsection␈αwe␈αneed
␈↓ α←␈↓look␈α↔no␈α_further.␈α↔ We␈α↔delimit␈α_our␈α↔search␈α↔even␈α_further␈α↔by␈α_keying␈α↔on
␈↓ α←␈↓subsequent␈αcharacters␈αin␈αthe␈αword.␈α Finally␈αwe␈αmay␈αresort␈αto␈αlinear␈αsearch␈αto
␈↓ α←␈↓locate␈α
the␈α
word␈α
on␈α
a␈α
specific␈α
page␈α
or␈α
column.␈α
A␈α
machine␈α
might␈α
mimic␈α
the
␈↓ α←␈↓dictionary␈α∂search␈α∂and␈α∂subdivide␈α∂the␈α∂table␈α∂into␈α∂26␈α⊂subsections.␈↓π 25␈↓␈α∂However,
␈↓ α←␈↓since␈α
it␈αis␈α
the␈α
machine␈αwhich␈α
will␈αsubdivide␈α
and␈α
index␈αinto␈α
the␈α
table,␈αthere
␈↓ α←␈↓may␈αbe␈α
schemes␈αwhich␈αare␈α
computationally␈αmore␈α
convenient␈αfor␈αthe␈α
machine.
␈↓ α←␈↓The␈α∂scheme␈α∂should␈α∞also␈α∂result␈α∂in␈α∞rather␈α∂even␈α∂distribution␈α∞of␈α∂atoms␈α∂in␈α∞the
␈↓ α←␈↓subsections.␈αIf␈αthe␈αmajority␈αof␈αthe␈αatoms␈αend␈αup␈αin␈αthe␈αsame␈αpartition␈αof␈αthe
␈↓ α←␈↓table we will have gained little improvement in the search efficiency.
␈↓"β␈↓ α←␈↓␈↓ β'An␈α∂algorithm␈α∂used␈α∂to␈α∞determine␈α∂which␈α∂partition␈α∂a␈α∂particular␈α∞element
␈↓ α←␈↓belongs␈αin␈αis␈αcalled␈αa␈α␈↓↓hashing␈αalgorithm␈↓␈αor␈αhashing␈αfunction.␈α One␈αobstacle
␈↓ α←␈↓in␈α⊃such␈α∩schemes␈α⊃is␈α⊃the␈α∩management␈α⊃of␈α⊃each␈α∩partition.␈α⊃If␈α⊃more␈α∩than␈α⊃one
␈↓ α←␈↓element␈α"hashes"␈α
to␈αa␈α
partition␈αthen␈α
we␈αhave␈α
a␈α␈↓↓collision␈↓.␈α
There␈αare␈αtwo␈α
basic
␈↓ α←␈↓strategies␈α↔available␈α↔to␈α↔resolve␈α↔such␈α↔a␈α↔collision.␈α↔The␈α↔first,␈α↔called␈α⊗␈↓↓open
␈↓ α←␈↓↓addressing␈↓␈αinvolves␈αre-hashing␈αthe␈αelement,␈αthus␈αrefining␈αthe␈αpartition,␈αuntil
␈↓ α←␈↓no␈α
collision␈α
exists.␈αIn␈α
the␈α
second,␈α
called␈α␈↓↓bucket␈α
hashing␈↓,␈α
the␈αhashing␈α
function
␈↓ α←␈↓hashes␈αto␈αa␈α"bucket".␈αAll␈αthe␈αelements␈αwith␈αthe␈αsame␈αhash␈αnumber␈αare␈αstored
␈↓ α←␈↓in␈α⊂the␈α⊂same␈α⊂"bucket";␈α⊂a␈α⊃separate␈α⊂search,␈α⊂perhaps␈α⊂linear␈α⊂search,␈α⊂is␈α⊃used␈α⊂to
␈↓ α←␈↓discover␈α
if␈αan␈α
element␈α
is␈αin␈α
the␈α
bucket;␈αand␈α
an␈α
element␈αwill␈α
appear␈α
in␈αat␈α
most
␈↓ α←␈↓one␈α∞bucket.␈α∂ Since␈α∞most␈α∂LISP␈α∞implementations␈α∞use␈α∂bucket␈α∞hashing,␈α∂we␈α∞will
␈↓ α←␈↓describe that scheme in more detail.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αsearch␈αalgorithms␈αare␈αapplied␈αwithin␈α␈↓αratom␈↓␈αafter␈αan␈αidentifier␈αhas
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 25␈↓ That is, a base 26 sort.
␈↓ α←␈↓␈↓276 Static Structure␈↓
_5.12␈↓
␈↓"β␈↓ α←␈↓been␈α
delimited.␈α
All␈α
␈↓αratom␈↓␈α
has␈α
at␈αthat␈α
time␈α
is␈α
the␈α
encoding␈α
of␈α
the␈αactual␈α
name
␈↓ α←␈↓of␈αthe␈α
atom;␈αcall␈α
that␈αstring␈α
␈↓αchr_str␈↓.␈α The␈α
hashing␈αfunction␈α
will␈αuse␈α␈↓αchr_str␈↓␈α
to
␈↓ α←␈↓determine␈α∞which␈α
bucket␈α∞must␈α
contain␈α∞the␈α
atom.␈α∞ Given␈α
the␈α∞bucket␈α
number,
␈↓ α←␈↓we␈α⊃examine␈α⊃the␈α⊃list␈α⊃of␈α⊃atoms␈α⊃in␈α⊃that␈α⊃bucket,␈α⊃comparing␈α⊃each␈α⊂print-name
␈↓ α←␈↓against␈α␈↓αchr_str␈↓.␈α If␈αa␈αprint-name␈αmatches,␈αwe␈αreturn␈αa␈αpointer␈αto␈αthe␈α
property
␈↓ α←␈↓list␈α∂of␈α∞that␈α∂atom.␈α∂ If␈α∞the␈α∂atom␈α∞with␈α∂print-name␈α∂␈↓αchr_str␈↓␈α∞does␈α∂not␈α∂appear␈α∞in
␈↓ α←␈↓that␈αbucket␈αwe␈αare␈αassured␈αthat␈αit␈αdoes␈αnot␈αappear␈αanywhere␈αin␈αthe␈αtable.␈α
In
␈↓ α←␈↓this␈αcase␈αwe␈αcreate␈αa␈αnew␈αatom␈αstructure,␈αadd␈αit␈αto␈αthat␈αbucket,␈αand␈αthe␈α
value
␈↓ α←␈↓of ␈↓αratom␈↓ is a pointer to that new structure.
␈↓"β␈↓ α←␈↓␈↓ β'Here is a simple hashing function:
␈↓"→␈↓ α←␈↓␈↓↓1.␈↓ Assume that we have N+1 buckets, numbered 0, 1, 2 ... N.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ Take␈α∂the␈α⊂numeric␈α∂representation␈α⊂of␈α∂␈↓αchr-str␈↓␈α∂and␈α⊂divide␈α∂that␈α⊂number␈α∂by
␈↓ α←␈↓␈↓ β∂N+1.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ Look at the remainder. It's a number between 0 and N.
␈↓" ␈↓ α←␈↓␈↓↓4.␈↓ Use that remainder as the index to the appropriate bucket.
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α
LISP␈αatom␈α
table,␈αusually␈α
called␈α
␈↓αOBLIST␈↓␈α(for␈α
␈↓↓object␈αlist␈↓),␈α
is␈α
a␈αlist
␈↓ α←␈↓of␈α∞buckets.␈α∞ Each␈α∞bucket␈α∞is␈α∞a␈α∞list␈α
of␈α∞the␈α∞atoms␈α∞which␈α∞`hash'␈α∞to␈α∞that␈α
bucket.
␈↓ α←␈↓We␈α∂actually␈α∞represent␈α∂the␈α∞object␈α∂list␈α∂as␈α∞an␈α∂array␈α∞named␈α∂␈↓αoblist␈↓.␈α∂ Arrays␈α∞are
␈↓ α←␈↓discussed␈α↔in␈α↔full␈α↔in␈α_Section 7.2,␈α↔but␈α↔basically␈α↔are␈α↔an␈α_efficient␈α↔storage
␈↓ α←␈↓representation␈α∞for␈α∞sequences␈α∂of␈α∞fixed␈α∞length.␈α∞In␈α∂this␈α∞case␈α∞we␈α∞can␈α∂allocate␈α∞a
␈↓ α←␈↓block␈αof␈αsequential␈αcells␈αand␈αuse␈αthe␈αaddressing␈αstructure␈αof␈αthe␈αhardware␈αto
␈↓ α←␈↓do␈α∞a␈α∞rapid␈α∂subscript␈α∞calculation.␈α∞ The␈α∞hash␈α∂number␈α∞will␈α∞give␈α∞us␈α∂the␈α∞array
␈↓ α←␈↓subscript␈αand␈αwe␈αcan␈αgo␈αto␈αthe␈αcorrect␈αbucket␈αimmediately;␈αwe␈αwon't␈αhave␈αto
␈↓ α←␈↓␈↓αcdr␈↓ down the object list to locate the bucket.
␈↓ α←␈↓␈↓5.12␈↓ πDTable Searching: Hashing 277␈↓
␈↓"⊃␈↓ α←␈↓
⊂αααπααα⊃
␈↓"␈↓ α←␈↓
⊂ααααααα←ααααααα←αααα←βα# ~ #αβα⊃
␈↓"␈↓ α←␈↓
~ εαααβαααλ ↓
␈↓"␈↓ α←␈↓
~ ~ ~ ~←$
␈↓"␈↓ α←␈↓
~ ⊂ααα←ααα←ααα←βα# ~ #αβ→⊃
␈↓"␈↓ α←␈↓
↓ ↓ εαααβαααλ ↓
␈↓"␈↓ α←␈↓
... (to bucket 2) ... ...
␈↓"␈↓ α←␈↓
↓ ↓ εαααβαααλ ↓
␈↓"␈↓ α←␈↓
~ ... ⊂α←αβα# ~ ≤'~←$
␈↓"␈↓ α←␈↓
~ ↓ εαααβααα$
␈↓"␈↓ α←␈↓
~ %αα→αα→ααα⊃
␈↓"␈↓ α←␈↓
↓ ↓
␈↓"␈↓ α←␈↓
(to bucket 1) (to bucket n)
␈↓"␈↓ α←␈↓
~ ~
␈↓"␈↓ α←␈↓
~ ~ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
~ ⊂αααπααα⊃ ⊂αααπαα⊃ %α→~ # ~ #αβ→ ...→~ # ~≤'~
␈↓"␈↓ α←␈↓
%→~ # ~ #αβ→...→~ # ~≤'~ %αβα∀ααα$ %αβα∀αα$
␈↓"␈↓ α←␈↓
%αβα∀ααα$ %αβα∀αα$ ↓ %→ ...
␈↓"␈↓ α←␈↓
~ ~ ⊂απααα⊃
␈↓"␈↓ α←␈↓
(to atom 1: (atom m: ~∃~ #αβ→(p-list of
␈↓"␈↓ α←␈↓
bucket 1) bucket 1) %α∀ααα$ atom 1: bucket n)
␈↓"␈↓ α←␈↓
~ ↓
␈↓"␈↓ α←␈↓
~ . . .
␈↓"␈↓ α←␈↓
↓
␈↓"␈↓ α←␈↓
⊂απααα⊃ ⊂αααααααπααα⊃ ⊂αααπααα⊃ ⊂ααααααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
~∃~ #αβα→~ PNAME ~ #αβα→~ # ~ #αβ→~ SUBR ~ #αβ→~ # ~≤'~
␈↓"␈↓ α←␈↓
%α∀ααα$ %ααααααα∀ααα$ %αβα∀ααα$ %αααααα∀ααα$ %αβα∀αα$
␈↓"␈↓ α←␈↓
↓ ↓
␈↓"␈↓ α←␈↓
⊂αααπαα⊃ to primitive code
␈↓"␈↓ α←␈↓
~ # ~≤'~ for ␈↓αcar␈↓
␈↓"␈↓ α←␈↓
%αβα∀αα$
␈↓"␈↓ α←␈↓
↓
␈↓"␈↓ α←␈↓
⊂ααααα⊃
␈↓"␈↓ α←␈↓
~CAR≡≡~
␈↓"␈↓ α←␈↓
%ααααα$
␈↓"β␈↓ α←␈↓␈↓ Partial Object List; where atom 1:bucket 1 is ␈↓αCAR␈↓ ␈↓α
␈↓"λ␈↓ α←␈↓Note:␈α
Though␈α
the␈αtop␈α
level␈α
of␈α␈↓αOBLIST␈↓␈α
is␈α
stored␈αsequentially␈α
for␈α
fast␈αaccess
␈↓ α←␈↓by␈α
the␈αhasher,␈α
the␈α␈↓αcdr␈↓-parts␈α
are␈αchained␈α
together␈αin␈α
a␈αsequential␈α
list␈α
so␈αthat
␈↓ α←␈↓the␈α∀table␈α∃will␈α∀have␈α∃the␈α∀same␈α∀structure␈α∃as␈α∀any␈α∃other␈α∀list.␈α∃The␈α∀chained
␈↓ α←␈↓representation␈α⊗is␈α⊗used␈α⊗by␈α⊗any␈α⊗LISP␈α⊗process␈α⊗other␈α⊗than␈α⊗the␈α∃hasher.␈↓π 26␈↓
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 26␈↓In␈α⊂particular,␈α⊂the␈α⊂garbage␈α⊂collector␈α∂uses␈α⊂this␈α⊂linking.␈α⊂As␈α⊂a␈α∂further
␈↓ α←␈↓implementation␈αnote,␈αthe␈αimplementors␈αof␈αMACLISP␈αnoted␈αthe␈αfrequent␈αuse
␈↓ α←␈↓of␈α
single␈αcharacter␈α
atoms␈α
and␈αadded␈α
a␈αspecial␈α
section␈α
to␈αthe␈α
top-level␈α
of␈αthe
␈↓ α←␈↓object␈α∪list.␈α∪A␈α∩contiguous␈α∪block␈α∪of␈α∩cells,␈α∪of␈α∪size␈α∩equal␈α∪to␈α∪the␈α∪number␈α∩of
␈↓ α←␈↓␈↓278 Static Structure␈↓
_5.12␈↓
␈↓"β␈↓ α←␈↓Whether␈α
a␈α
linear␈α
search␈α
and␈αstorage␈α
technique,␈α
like␈α
␈↓αassoc-pairlis␈↓,␈α
or␈α
a␈αmore
␈↓ α←␈↓complex␈α_technique␈α_like␈α_hashing␈α↔should␈α_be␈α_employed␈α_depends␈α_on␈α↔the
␈↓ α←␈↓application,␈α∞and␈α∂the␈α∞speed␈α∞and␈α∂size␈α∞of␈α∞the␈α∂machine.␈α∞ The␈α∞hash␈α∂table␈α∞takes
␈↓ α←␈↓extra␈αspace␈αboth␈αfor␈αstorage␈αand␈αfor␈αprogram,␈αbut␈αgives␈αa␈αfaster␈αsearch␈αtime.
␈↓ α←␈↓The␈α⊃linear␈α⊃technique␈α⊃requires␈α⊃less␈α⊃space,␈α⊃but␈α⊃can␈α⊃be␈α⊃quite␈α⊃slow.␈α⊂ Several
␈↓ α←␈↓books cover searching and sorting in great detail ([Gri 71], [Knu 72]).
␈↓"β␈↓ α←␈↓␈↓ β'MACLISP␈α∂embellishes␈α⊂the␈α∂basic␈α∂␈↓αOBLIST␈↓␈α⊂idea␈α∂in␈α∂an␈α⊂important␈α∂way.
␈↓ α←␈↓That␈α⊃system␈α⊂will␈α⊃allow␈α⊂several␈α⊃object␈α⊂lists␈α⊃to␈α⊂exist␈α⊃simultaneously␈α⊃This␈α⊂is
␈↓ α←␈↓useful␈αsince␈αseveral␈αcooperating␈α
LISP␈αsubsystems␈αmay␈αexist;␈αfor␈α
example␈αthe
␈↓ α←␈↓LISP␈αeditor,␈αdebugger,␈αand␈αcompiler␈αare␈αall␈αwritten␈αin␈αLISP␈αand␈αmay␈α
all␈αbe
␈↓ α←␈↓used␈αwithin␈αthe␈αsame␈αinteractive␈αsession.␈α There␈αis␈αa␈αpotential␈αdifficulty␈αsince
␈↓ α←␈↓each␈α∞of␈α∂those␈α∞subsystems␈α∞may␈α∂use␈α∞names␈α∞which␈α∂conflict␈α∞with␈α∞names␈α∂in␈α∞the
␈↓ α←␈↓user's␈αprograms.␈αMultiple␈αobject␈αlists␈αare␈αa␈αway␈αto␈αovercome␈α
this␈αproblem.␈↓π 27␈↓
␈↓ α←␈↓Only␈α⊃one␈α⊂object␈α⊃list␈α⊂is␈α⊃current␈α⊂at␈α⊃any␈α⊂time,␈α⊃but␈α⊂several␈α⊃may␈α⊂exist␈α⊃in␈α⊂the
␈↓ α←␈↓system. Object lists are swapped by λ-binding to the identifier ␈↓αobarray␈↓.␈↓π 28␈↓
␈↓" ␈↓ α←␈↓α␈↓Consider:␈↓α␈↓ ¬jλ[[obarray]␈↓λx␈↓α][ob␈↓β1␈↓α]␈↓
␈↓" ␈↓ α←␈↓Assuming␈αthat␈α␈↓αob␈↓β1␈↓␈αis␈αbound␈αto␈αan␈αobject␈αlist,␈αthen␈αwithin␈αthe␈αevaluation␈αof␈α␈↓λx␈↓
␈↓ α←␈↓the symbols and bindings of ␈↓αob␈↓β1␈↓ would be accessible.
␈↓"β␈↓ α←␈↓␈↓ β'Finally,␈α∩we␈α∩want␈α∩to␈α∩present␈α∩a␈α∩version␈α∩of␈α∩␈↓αratom␈↓␈α∩which␈α∩uses␈α∩a␈α∩hash
␈↓ α←␈↓organization.␈α⊂ In␈α⊂this␈α⊂discussion,␈α⊂we␈α⊂will␈α⊂restrict␈α⊂ourselves␈α⊂to␈α⊂literal␈α∂atoms,
␈↓ α←␈↓leaving the reader to supply the necessary parts for recognition of numbers.
␈↓"β␈↓ α←␈↓␈↓ β'We will recognize three classes of characters:
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ The class of letters will include the alphabetic characters.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ The␈α
class␈α
of␈αdelimiters␈α
consists␈α
of␈α
those␈αcharacters␈α
which␈α
signal␈α
the␈αend␈α
of
␈↓ α←␈↓␈↓ β∂an␈α∩atom.␈α∩For␈α∩this␈α∩scanner␈α∩we␈α∩assume␈α∩space␈α∩and␈α∩carriage␈α∩control␈α⊃are
␈↓ α←␈↓␈↓ β∂delimiters.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ The special characters will consist of "(", ")" and "." .
␈↓" ␈↓ α←␈↓␈↓ β'Special␈α∞characters␈α∞also␈α∞act␈α∞as␈α∞delimiters␈α∞in␈α∞LISP␈α∞and␈α∞this␈α∞results␈α∞in␈α∞a
␈↓ α←␈↓slight␈αcomplication.␈αConsider␈αthe␈α
partial␈αstring␈α"␈↓αAB )C␈↓".␈αOur␈α
scanner␈αshould
␈↓ α←␈↓scan␈α∪the␈α∪"␈↓αA␈↓",␈α∪scan␈α∪the␈α∪"␈↓αB␈↓",␈α∩and␈α∪scanning␈α∪the␈α∪space,␈α∪should␈α∪recognize␈α∩a
␈↓ α←␈↓delimiter.␈αIt␈αshould␈αrecognize␈αthe␈α␈↓αAB␈↓␈αas␈αan␈αatom,␈αand␈αsignal␈α␈↓αread␈↓.␈αThe␈αstring
␈↓ α←␈↓will␈αbe␈αreduced␈αto␈α"␈↓α)C␈↓".␈αThe␈αnext␈αtime␈α␈↓αread␈↓␈αcalls␈α␈↓αratom␈↓␈αthe␈αright␈αparenthesis
␈↓ α←␈↓will␈αbe␈αseen,␈αrecognized␈α
as␈αa␈αspecial␈αcharacter␈α
and␈αan␈αindication␈αof␈α
that␈αwill
␈↓ α←␈↓be returned to ␈↓αread␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Now␈αconsider␈αthe␈αstring␈α"␈↓αAB)C␈↓";␈α␈↓αratom␈↓␈αwill␈αscan␈α"␈↓αA␈↓"␈αand␈α"␈↓αB␈↓"␈αas␈αbefore.
␈↓ α←␈↓It␈α∞will␈α
then␈α∞scan␈α
the␈α∞")".␈α
It␈α∞now␈α∞needs␈α
to␈α∞do␈α
␈↓¬two␈↓␈α∞things;␈α
it␈α∞must␈α∞signal␈α
␈↓αread␈↓
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓characters,␈α
was␈α
added.␈αOn␈α
reading␈α
a␈αsingle␈α
character␈α
atom,␈αthe␈α
corresponding
␈↓ α←␈↓entry␈αin␈αthe␈α
table␈αis␈αexamined.␈αA␈α
␈↓αNIL␈↓␈αsays␈αthe␈α
atom␈αhasn't␈αbeen␈αseen␈α
before;
␈↓ α←␈↓otherwise its p-list representation resides there.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 27␈↓Static binding is another way to handle the problem.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 28␈↓An␈α
object␈α
list␈α
is␈α
called␈α
object␈α
array␈α
in␈α
MACLISP␈α
since␈α
the␈α
table␈α
is
␈↓ α←␈↓represented as an array. We discuss arrays in Section 7.2.
␈↓ α←␈↓␈↓5.12␈↓ πDTable Searching: Hashing 279␈↓
␈↓"β␈↓ α←␈↓about␈αthe␈α
atom␈αit␈α
has␈αseen,␈α
but␈αit␈αmust␈α
also␈αremember␈α
the␈α")"␈α
so␈αthat␈αthe␈α
␈↓¬next␈↓
␈↓ α←␈↓time␈α∞␈↓αread␈↓␈α∞asks␈α
for␈α∞information,␈α∞it␈α∞sees␈α
the␈α∞")"␈α∞and␈α
not␈α∞the␈α∞"␈↓αC␈↓".␈α∞ We␈α
handle
␈↓ α←␈↓this␈α⊂problem␈α⊃by␈α⊂using␈α⊃a␈α⊂global␈α⊃variable␈α⊂named␈α⊃␈↓αlst_chr␈↓.␈α⊂ This␈α⊃variable␈α⊂is
␈↓ α←␈↓initialized␈α∪to␈α∀␈↓αNIL␈↓␈α∪and␈α∀remains␈α∪that␈α∪way␈α∀until␈α∪our␈α∀anomalous␈α∪situation
␈↓ α←␈↓occurs.␈αAt␈αthat␈αtime␈αthe␈αspecial␈αcharacter␈αis␈αplaced␈αin␈α␈↓αlst_chr␈↓,␈αand␈α␈↓αratom␈↓␈α
exits
␈↓ α←␈↓normally.␈α∂So,␈α∂whenever␈α∂␈↓αratom␈↓␈α∞is␈α∂called,␈α∂the␈α∂first␈α∞thing␈α∂it␈α∂does␈α∂is␈α∂check␈α∞the
␈↓ α←␈↓contents␈α
of␈α
␈↓αlst_chr␈↓.␈α
If␈αit␈α
is␈α
non-empty,␈α
its␈α
contents␈αis␈α
returned,␈α
as␈α
␈↓αlst_chr␈↓␈αis␈α
set
␈↓ α←␈↓empty again.
␈↓"∀␈↓ α←␈↓αratom <=λ[[] prog[[chr]
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧7[lst_chr → swap[lst_chr;chr];return[chr]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨a␈↓ ∧7chr ← readch[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧7[is_let[chr] → stuf_buf[chr];return[ratom␈↓β1␈↓α[]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧7 is_delim[chr] → go[a];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧7 is_spec[chr] → return[chr]]]]
␈↓"∀␈↓ α←␈↓This␈α∩procedure␈α∩uses␈α∩tricks␈α∩advertised␈α∩in␈α∩Section 5.5;␈α∩it␈α∩uses␈α∩␈↓αlst_chr␈↓␈α∩as␈α∩a
␈↓ α←␈↓predicate,␈α
knows␈α
that␈α
␈↓αprog␈↓␈α
variables␈α
are␈α
initialized␈α
to␈α
␈↓αNIL␈↓,␈α
and␈α
knows␈αthat
␈↓ α←␈↓the␈α∀representation␈α∪of␈α∀␈↓
f␈↓␈α∪is␈α∀␈↓αNIL␈↓.␈α∪ With␈α∀that␈α∪knowledge,␈α∀␈↓αswap␈↓␈α∀swaps␈α∪the
␈↓ α←␈↓contents of ␈↓αchr␈↓ and ␈↓αlst_chr␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
routine␈α␈↓αreadch␈↓␈α
gets␈αthe␈α
next␈α
character,␈αand␈α
␈↓αstuf_buf␈↓␈αis␈α
used␈αto␈α
save
␈↓ α←␈↓the␈α∂character␈α⊂string␈α∂which␈α⊂is␈α∂to␈α∂become␈α⊂an␈α∂atom.␈α⊂ The␈α∂character␈α⊂string␈α∂is
␈↓ α←␈↓built up in ␈↓αbuf␈↓.
␈↓"∀␈↓ α←␈↓αratom␈↓β1␈↓α <= λ[[] prog[[chr;chr_str]
␈↓"β␈↓ α←␈↓α␈↓ ∧Cl␈↓ ∧[chr ← readch[];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧[[is_delim[chr] → return[intern[chr_str]];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧[ is_spec[chr] → lst_chr ← chr; return[intern[chr_str]];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧[ ␈↓
t␈↓α → stuf_buf[chr]; go[l]]]]
␈↓"∀␈↓ α←␈↓If ␈↓αratom␈↓β1␈↓ sees a special character it is saved in ␈↓αlst_chr␈↓.
␈↓"∀␈↓ α←␈↓αintern <= λ[[l] prog[[bucket]
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧[bucket ← oblist[hash[maknam[l]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧Ca␈↓ ∧[[null[bucket] → return[insert[l]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧[[right-one[get[␈↓ ε∂first[bucket];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧[␈↓ ε∂PNAME];l] → return[first[bucket]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧[bucket ← rest[bucket];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧[go[a]]]
␈↓"∀␈↓ α←␈↓␈↓αmaknam␈↓␈α∀takes␈α∪our␈α∀character␈α∀string␈α∪and␈α∀converts␈α∪it␈α∀into␈α∀an␈α∪appropriate
␈↓ α←␈↓numeric␈α∀representation;␈α∀for␈α∀example,␈α∪the␈α∀input␈α∀string␈α∀might␈α∀exceed␈α∪one
␈↓ α←␈↓machine␈α
word.␈α
␈↓αhash␈↓␈α
returns␈α
the␈α
bucket␈α
number␈α
of␈α
its␈α
argument,␈α
and␈α
␈↓αinsert␈↓
␈↓ α←␈↓builds␈αthe␈αatom␈αand␈αinserts␈αit␈αinto␈αa␈αbucket.␈α ␈↓αright-one␈↓␈αis␈αa␈αpredicate␈αused␈αto
␈↓ α←␈↓check if an atom has the right print-name.
␈↓"β␈↓ α←␈↓␈↓ β'An␈α⊂implementation␈α∂of␈α⊂␈↓αratom␈↓␈α∂may␈α⊂be␈α∂generalized␈α⊂so␈α∂that␈α⊂the␈α⊂class␈α∂of
␈↓ α←␈↓special␈α∃characters␈α∃and␈α∃delimiters␈α∃can␈α∃be␈α∃varied.␈α∃This␈α∃is␈α∃done␈α∃using␈α∀a
␈↓ α←␈↓␈↓280 Static Structure␈↓
_5.12␈↓
␈↓"β␈↓ α←␈↓representation␈α∂of␈α∂a␈α∂character␈α∂table␈α∂whose␈α∂name␈α∂entries␈α∂are␈α∂characters,␈α∂and
␈↓ α←␈↓whose␈αvalue␈αentries␈α
determine␈αthe␈α␈↓αratom␈↓␈α
properties.␈α This␈αallows␈α
LISP␈αusers
␈↓ α←␈↓to␈α∞define␈α
their␈α∞own␈α∞parsers␈α
and␈α∞scanners.␈α
LISP's␈α∞modifiable␈α∞input␈α
routine,
␈↓ α←␈↓coupled␈α⊃with␈α⊃its␈α∩data␈α⊃structures␈α⊃and␈α∩extendible␈α⊃evaluator␈α⊃make␈α∩LISP␈α⊃an
␈↓ α←␈↓excellent tool for building more sophisticated language systems.
␈↓"β␈↓ α←␈↓␈↓ β'On␈α∩page 211␈α∩we␈α∩introduced␈α∩the␈α∩abbreviation␈α∩␈↓λ`␈↓αx␈↓␈α∩for␈α∪␈↓αquote[x]␈↓.␈α∩ This
␈↓ α←␈↓␈↓αquote␈↓␈αfacility␈αis␈αan␈αinstance␈αof␈αa␈α
device␈αcalled␈αa␈α␈↓αread␈↓ macro;␈αit␈αis␈αthe␈α
duty␈αof
␈↓ α←␈↓␈↓αratom␈↓␈αto␈αrecognize␈α
such␈αconstructs.␈α Whenever␈α
␈↓αratom␈↓␈αsees␈αthe␈α
prefix␈α␈↓λ`␈↓␈αit␈α
reads
␈↓ α←␈↓the␈αnext␈αS-expr␈α␈↓λα␈↓␈αand␈αreturns␈αthe␈αlist␈α␈↓α(QUOTE␈α␈↓λα␈↓α)␈↓␈αas␈αvalue.␈αIn␈αsome␈αsystems
␈↓ α←␈↓([Moo 74])␈α↔users␈α↔may␈α_define␈α↔their␈α↔own␈α_␈↓αread␈↓␈α↔macros.␈α↔ For␈α_example␈α↔a
␈↓ α←␈↓definition like:
␈↓"∀␈↓ α←␈↓α␈↓ ¬↔␈↓λ`␈↓α <␈↓βr␈↓α= λ[[] list[QUOTE;read[]]]
␈↓"∀␈↓ α←␈↓would␈α∩signal␈α∩LISP␈α⊃to␈α∩change␈α∩the␈α⊃character␈α∩table␈α∩entry␈α⊃for␈α∩" ␈↓λ`␈↓ "␈α∩to␈α∩be␈α⊃a
␈↓ α←␈↓␈↓αread␈↓ macro.␈α∞ If␈α∞" ␈↓λ`␈↓ "␈α∞appeared␈α∞during␈α∞an␈α∞input␈α∞operation,␈α∞then␈α∞the␈α∂body␈α∞of
␈↓ α←␈↓the␈α
␈↓αread␈↓-macro␈αwould␈α
be␈α
evaluated;␈αthat␈α
would␈αcall␈α
␈↓αread␈↓,␈α
and␈αthen␈α
form␈αa␈α
list
␈↓ α←␈↓with␈α
␈↓αQUOTE␈↓␈α
and␈α
the␈α
result.␈α
The␈α
resulting␈α
list␈α
would␈α
be␈α
returned␈α
to␈α
within
␈↓ α←␈↓the original input process.
␈↓"β␈↓ α←␈↓␈↓ β'MACLISP␈α⊂also␈α∂defines␈α⊂a␈α∂comment␈α⊂facility␈α∂using␈α⊂a␈α⊂␈↓αread␈↓ macro.␈α∂ The
␈↓ α←␈↓occurrence␈αof␈αa␈αsemi-colon␈αsignals␈αthe␈αbeginning␈αof␈αa␈αcomment;␈αall␈αcharacters
␈↓ α←␈↓to the end of the line are taken as commentary.
␈↓"β␈↓ α←␈↓␈↓ β'Several␈α∀implementations␈α∃also␈α∀include␈α∃abbreviations␈α∀to␈α∃decrease␈α∀the
␈↓ α←␈↓number␈αof␈αparentheses␈αneeded.␈αFor␈α
example␈α"["␈αand␈α"]"␈αare␈αoften␈α
defined␈αto
␈↓ α←␈↓be␈α"super-parentheses".␈α The␈α"["␈αacts␈αlike␈α
a␈α"("␈αbut␈αits␈αscope␈αruns␈αto␈α
the␈αnext
␈↓ α←␈↓"]",␈α≠constructing␈α≠sufficient␈α≠")"␈α≠to␈α≠balance␈α≠the␈α≠intervening␈α~expression.
␈↓ α←␈↓Similarly,␈αthe␈αscope␈αof␈αa␈α"]"␈αextends␈αto␈αthe␈αprior␈αmatching␈α"[";␈αif␈αnone␈αexists,
␈↓ α←␈↓the expression is completed by supplying sufficient ")" to balance.
␈↓"β␈↓ α←␈↓For example:
␈↓"∀␈↓ α←␈↓α␈↓ βK((A B) ((C D E)))␈↓ ¬S= ((A B) ((C D E))]
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ¬S= [(A B) ((C D E))]
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ¬S= ((A B) ((C D E]
␈↓"∞␈↓ α←␈↓α␈↓and␈↓α␈↓ ¬9(A [B (C]) = (A (B (C)))
␈↓"∀␈↓ α←␈↓␈↓ β'Regardless␈α⊂of␈α⊂the␈α⊂specifics␈α⊂of␈α⊂the␈α⊂implementation,␈α⊂the␈α⊃input␈α⊂routines
␈↓ α←␈↓will␈α∞read␈α∞a␈α∞list␈α∞representation␈α∞of␈α∞a␈α∞LISP␈α∞expression␈α∞and␈α∞convert␈α∞it␈α∂into␈α∞an
␈↓ α←␈↓S-expr. For example, let's see what happens if we want to evaluate
␈↓" ␈↓ α←␈↓α␈↓ ε*eq[x;A]
␈↓" ␈↓ α←␈↓This will be presented to the machine as:
␈↓" ␈↓ α←␈↓α␈↓ ¬U(EQ X (QUOTE A))
␈↓" ␈↓ α←␈↓That input will be recognized with the ␈↓αread-eval-print␈↓ loop:
␈↓"∀␈↓ α←␈↓α␈↓ βoprog[[]
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ ∧+a␈↓ ∧Cprint[eval[read[];( )]];
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ ∧+␈↓ ∧Cgo[a]]
␈↓ α←␈↓␈↓5.12␈↓ πDTable Searching: Hashing 281␈↓
␈↓"β␈↓ α←␈↓␈↓αread␈↓␈αwill␈αbegin␈αparsing␈αthe␈αsequence␈α
of␈αcharacters;␈αit␈αwill␈αdepend␈αon␈α␈↓αratom␈↓␈α
to
␈↓ α←␈↓return␈α⊃indications␈α⊃of␈α⊃the␈α⊃special␈α⊃characters,␈α⊃and␈α⊃will␈α⊃depend␈α⊃on␈α⊃␈↓αratom␈↓␈α⊂to
␈↓ α←␈↓properly␈αrepresent␈αeach␈αoccurrence␈αof␈αan␈αatom.␈α The␈αparser␈αknows␈αabout␈αthe
␈↓ α←␈↓representation␈α⊂we␈α∂have␈α⊂chosen␈α⊂for␈α∂lists␈α⊂and␈α∂will␈α⊂use␈α⊂␈↓αcons␈↓␈α∂to␈α⊂build␈α⊂up␈α∂the
␈↓ α←␈↓S-expression form:
␈↓"⊃␈↓ α←␈↓
⊂ααααπααα⊃ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
~ EQ ~ #αβα→~ X ~ #αβα→~ # ~≤'~
␈↓"␈↓ α←␈↓
%αααα∀ααα$ %ααα∀ααα$ %αβα∀αα$
␈↓"␈↓ α←␈↓
~ ⊂αααααααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
%→~ QUOTE ~ #αβα→~ A ~≤'~
␈↓"␈↓ α←␈↓
%ααααααα∀ααα$ %ααα∀αα$
␈↓"∀␈↓ α←␈↓The␈α
references␈α
to␈α
the␈α
atoms␈α
␈↓αEQ,␈α∞X,␈α
A,␈↓␈α
and␈α
␈↓αQUOTE␈↓␈α
are␈α
actually␈α∞pointers␈α
to
␈↓ α←␈↓the␈αatoms.␈αEach␈αatom␈αis␈αlocated␈αonly␈αonce␈αby␈αthe␈αreader.␈αAfter␈αthat␈αwe␈αhave
␈↓ α←␈↓direct access to atom and its property list.
␈↓"β␈↓ α←␈↓␈↓ β'Since␈α⊃the␈α⊃input␈α⊃routines␈α⊃perform␈α⊃several␈α⊃␈↓αcons␈↓␈α⊃operations;␈α⊃we␈α⊂should
␈↓ α←␈↓look at the details of ␈↓αcons␈↓.
␈↓"β␈↓ α←␈↓␈↓ ¬+␈↓↓5.13 A First Look At ␈↓αcons␈↓␈↓α
␈↓"β␈↓ α←␈↓The␈α
␈↓αcons␈↓␈α
operation␈α∞is␈α
quite␈α
different␈α
from␈α∞the␈α
other␈α
LISP␈α∞primitives.␈α
The
␈↓ α←␈↓other␈α→primitives␈α→manipulate␈α_existing␈α→S-expressions,␈α→whereas␈α→␈↓αcons␈↓␈α_must
␈↓ α←␈↓construct␈α"a␈α!new␈α"S-expression␈α!from␈α"two␈α!existing␈α"S-exprs.␈α! Given
␈↓ α←␈↓representations␈αof␈αtwo␈αS-exprs,␈αsay␈α␈↓αx␈↓␈αand␈α␈↓αy,␈αcons[x;y]␈↓␈αmust␈αget␈αa␈αnew␈αcell,␈αput
␈↓ α←␈↓a␈α
pointer␈α
to␈α
the␈α
representation␈α
of␈α
␈↓αx␈↓␈α
in␈α
the␈α
␈↓αcar␈↓-part␈α
of␈α
the␈α
cell␈α
and␈α
a␈αpointer␈α
to
␈↓ α←␈↓the representation of ␈↓αy␈↓ in the ␈↓αcdr␈↓-part and return a pointer to the new cell:
␈↓"⊃␈↓ α←␈↓
␈↓result of ␈↓αcons[x;y]␈↓
␈↓"␈↓ α←␈↓
~
␈↓"␈↓ α←␈↓
~ ⊂αααααπααααα⊃
␈↓"␈↓ α←␈↓
%ααααααααα→~ # ~ # ~
␈↓"␈↓ α←␈↓
%ααβαα∀ααβαα$
␈↓"␈↓ α←␈↓
~ ~
␈↓"␈↓ α←␈↓
⊂αααα$ %αααα⊃
␈↓"␈↓ α←␈↓
~ ~
␈↓"␈↓ α←␈↓
↓ ↓
␈↓"␈↓ α←␈↓
␈↓rep. of ␈↓αx ␈↓rep. of ␈↓αy␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Before␈α∞computation␈α∞is␈α∞begun,␈α∂only␈α∞the␈α∞atomic␈α∞structure␈α∞for␈α∂the␈α∞initial
␈↓ α←␈↓LISP␈αsystem␈α
table␈αuses␈α
cells␈αin␈αthe␈α
pointer area.␈αThe␈α
remaining␈αpointer cells
␈↓ α←␈↓are␈αlinked␈αtogether␈αand␈αform␈αthe␈α␈↓↓free␈αspace␈αlist␈↓␈αor␈αFS␈αlist.␈↓π 29␈↓␈αWhenever␈α
␈↓αcons␈↓
␈↓ α←␈↓needs␈α
a␈α
cell,␈α
the␈α
first␈α
cell␈α
in␈α
the␈α
FS␈αlist␈α
is␈α
used␈α
and␈α
the␈α
FS␈α
list␈α
is␈α
set␈α
to␈αthe␈α
␈↓αrest␈↓
␈↓ α←␈↓of the FS list.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 29␈↓LISP␈α∀free␈α∪space␈α∀is␈α∪an␈α∀instance␈α∪of␈α∀"heap storage" ([Alg 75]).␈α∪The
␈↓ α←␈↓rationale␈αfor␈αheap␈αstorage␈αis␈αthat␈αstorage␈αusage␈αis␈αnot␈αsufficiently␈αdisciplined
␈↓ α←␈↓that␈α∂its␈α∂allocation␈α∞and␈α∂deallocation␈α∂can␈α∞be␈α∂predicted.␈α∂Therefore␈α∂some␈α∞more
␈↓ α←␈↓global management scheme is required.
␈↓ α←␈↓␈↓282 Static Structure␈↓
_5.13␈↓
␈↓"β␈↓ α←␈↓For example the following represents the effect of ␈↓αcons[A;B]␈↓
␈↓"⊃␈↓ α←␈↓
␈↓αenv␈↓
␈↓αdest␈↓
␈↓"␈↓ α←␈↓
↓ ↓
␈↓"␈↓ α←␈↓
⊂ααπαα⊃ ⊂ααααα⊃
␈↓"␈↓ α←␈↓
~ ~ #α→... ~ #αβα⊃
␈↓"␈↓ α←␈↓
εααβααλ εααπααλ ~
␈↓"␈↓ α←␈↓
~ ~ #β→⊃ ... ↓
␈↓"␈↓ α←␈↓
εααβααλ ↓ ~ ~ ~←$
␈↓"␈↓ α←␈↓
~ ~ #βαααα⊃ εααβααλ Pt␈↓βFS␈↓
␈↓"␈↓ α←␈↓
%αα∀αα$ ↓ ↓ ... ↓
␈↓"␈↓ α←␈↓
~ ~ %αα∀αα$ ~
␈↓"␈↓ α←␈↓
~ %→αα→α→⊃ ~
␈↓"␈↓ α←␈↓
↓ ↓ ↓
␈↓"␈↓ α←␈↓
⊂απααα⊃ ⊂απααα⊃ ⊂ααπααα⊃ ⊂ααπααα⊃ ⊂ααπαα⊃
␈↓"␈↓ α←␈↓
~∃~ #αβ→... ~∃~ #αβ→ ... ~≤'~ #αβ→~≤'~ #αβ→ ...→~≤'~≤'~
␈↓"␈↓ α←␈↓
%α∀ααα$ %α∀ααα$ %αα∀ααα$ %αα∀ααα$ %αα∀αα$
␈↓"␈↓ α←␈↓
atom A atom B
␈↓"ε␈↓ α←␈↓
␈↓ Before ␈↓
␈↓"≠␈↓ α←␈↓
␈↓αenv␈↓
␈↓αdest␈↓
␈↓"␈↓ α←␈↓
↓ ↓
␈↓"␈↓ α←␈↓
⊂ααπαα⊃ ⊂ααααα⊃
␈↓"␈↓ α←␈↓
~ ~ #α→... ~ #αβα⊃
␈↓"␈↓ α←␈↓
εααβααλ εααπααλ ~
␈↓"␈↓ α←␈↓
~ ~ #β→⊃ ... ←$
␈↓"␈↓ α←␈↓
εααβααλ ↓ ~ ~ #ααα→ααα→ααα→⊃
␈↓"␈↓ α←␈↓
~ ~ #βαααα⊃ εααβααλ ↓ Pt␈↓βFS␈↓
␈↓"␈↓ α←␈↓
%αα∀αα$ ↓ ↓ ... ~ ↓
␈↓"␈↓ α←␈↓
~ ~ %αα∀αα$ ~ %ααα→⊃
␈↓"␈↓ α←␈↓
~ %→α→α→⊃ ~ ~
␈↓"␈↓ α←␈↓
↓ ↓ ↓ ↓
␈↓"␈↓ α←␈↓
⊂απααα⊃ ⊂απααα⊃ ⊂αααπααα⊃ ⊂ααπααα⊃ ⊂ααπαα⊃
␈↓"␈↓ α←␈↓
~∃~ #αβ→... ~∃~ #αβ→ ... ~ # ~ # ~ ~≤'~ #αβ→...→~≤'~≤'~
␈↓"␈↓ α←␈↓
%α∀ααα$ %α∀ααα$ %αβα∀αβα$ %αα∀ααα$ %αα∀αα$
␈↓"␈↓ α←␈↓
↑ ↑ ↓ ↓
␈↓"␈↓ α←␈↓
%αααα←αααα←αααα←αααα←ααααα$ ~
␈↓"␈↓ α←␈↓
↑ ↓
␈↓"␈↓ α←␈↓
%←ααααα←αααα←ααααα$
␈↓"ε␈↓ α←␈↓
␈↓ After ␈↓
␈↓"λ␈↓ α←␈↓␈↓ β'As␈αthe␈αcomputation␈αcontinues,␈αcells␈αare␈α
taken␈αfrom␈αthe␈αFS␈αlist.␈α When␈α
a
␈↓ α←␈↓␈↓αcons␈↓␈α⊃operation␈α⊃needs␈α⊃a␈α∩cell␈α⊃and␈α⊃the␈α⊃FS␈α∩list␈α⊃is␈α⊃empty,␈α⊃the␈α∩computation␈α⊃is
␈↓ α←␈↓suspended␈α∞and␈α
a␈α∞␈↓↓storage␈α
reclaimer␈↓␈α∞is␈α
called.␈α∞The␈α
reclaimer␈α∞is␈α∞often␈α
known
␈↓ α←␈↓by␈α⊂a␈α⊂more␈α⊂colorful␈α⊂name: the␈α⊂␈↓↓garbage␈α⊂collector␈↓.␈α⊂ The␈α⊂job␈α⊂of␈α⊂the␈α⊂garbage
␈↓ α←␈↓collector is to locate cells for a new FS list.
␈↓"β␈↓ α←␈↓␈↓ ∧≥␈↓↓5.14 Storage Management: Garbage Collection␈↓
␈↓"β␈↓ α←␈↓During␈αthe␈αcourse␈αof␈αa␈αcomputation,␈αcontents␈αof␈αcells␈αwhich␈αwere␈αtaken␈αfrom
␈↓ α←␈↓the␈α∪FS␈α∀list␈α∪often␈α∀become␈α∪unnecessary.␈α∪For␈α∀example,␈α∪if␈α∀we␈α∪ask␈α∀LISP␈α∪to
␈↓ α←␈↓evaluate something as simple as:
␈↓" ␈↓ α←␈↓␈↓ βu␈↓α(CONS (QUOTE A) (QUOTE B)),␈↓ many cells are used:
␈↓ α←␈↓␈↓5.14␈↓ εStorage Management: Garbage Collection 283␈↓
␈↓"β␈↓ α←␈↓␈↓↓1.␈↓ At least seven cells are needed just to read in the expression:
␈↓"⊃␈↓ α←␈↓
⊂ααααααπααα⊃ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
~ CONS ~ #αβα→~ # ~ #αβα→~ # ~≤'~
␈↓"␈↓ α←␈↓
%αααααα∀ααα$ %αβα∀ααα$ %αβα∀αα$
␈↓"␈↓ α←␈↓
↓ ↓ ⊂αααααααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
~ %→~ QUOTE ~ #αβα→~ B ~≤'~
␈↓"␈↓ α←␈↓
~ %ααααααα∀ααα$ %ααα∀αα$
␈↓"␈↓ α←␈↓
~ ⊂αααααααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
%α→~ QUOTE ~ #αβα→~ A ~≤'~
␈↓"␈↓ α←␈↓
%ααααααα∀ααα$ %ααα∀αα$
␈↓"∀␈↓ α←␈↓If␈α∂some␈α∂of␈α∞the␈α∂atoms␈α∂are␈α∂not␈α∞present␈α∂in␈α∂the␈α∂atom␈α∞table,␈α∂more␈α∂cells␈α∂will␈α∞be
␈↓ α←␈↓needed.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ One␈α∞cell␈α
will␈α∞be␈α
needed␈α∞to␈α
perform␈α∞the␈α
␈↓αcons␈↓␈α∞operation.␈α
See␈α∞the␈α
previous
␈↓ α←␈↓␈↓ β∂example.
␈↓"β␈↓ α←␈↓␈↓ β'After␈αthe␈αcomputation␈α
is␈αcompleted,␈αLISP␈α
will␈αprint␈α"␈↓α(A . B)␈↓"␈α
and␈αwait
␈↓ α←␈↓for␈α⊂more␈α⊃input.␈α⊂After␈α⊂the␈α⊃␈↓αprint␈↓␈α⊂statement␈α⊂is␈α⊃completed␈α⊂none␈α⊂of␈α⊃the␈α⊂eight
␈↓ α←␈↓mentioned␈α
cells␈α
are␈αneeded.␈α
They␈α
are␈α
garbage.␈α In␈α
the␈α
current␈αexample,␈α
these
␈↓ α←␈↓"garbage␈α⊂cells"␈α⊂could␈α⊂have␈α∂been␈α⊂explicitly␈α⊂returned␈α⊂to␈α∂the␈α⊂free␈α⊂list,␈α⊂but␈α∂in
␈↓ α←␈↓general␈α↔it␈α↔is␈α↔difficult␈α↔to␈α↔know␈α↔exactly␈α↔which␈α↔cells␈α↔␈↓¬are␈↓␈α_garbage.␈↓π 30␈↓␈α↔In
␈↓ α←␈↓Section 7.7 we will see how these difficulties can arise.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∪responsibility␈α∪for␈α∪reclamation␈α∪is␈α∪therefore␈α∪passed␈α∪to␈α∀the␈α∪LISP
␈↓ α←␈↓system.␈α⊂ The␈α⊂␈↓αcons␈↓␈α⊃procedure␈α⊂removes␈α⊂cells␈α⊂from␈α⊃the␈α⊂FS␈α⊂list,␈α⊂and␈α⊃its␈α⊂FWS
␈↓ α←␈↓counterpart␈α
␈↓αfwcons␈↓,␈α
removes␈α
cells␈α∞from␈α
the␈α
FWS␈α
list␈α
when␈α∞making␈α
numbers
␈↓ α←␈↓or␈α∀print-names.␈α∀ These␈α∀two␈α∀functions␈α∪are␈α∀the␈α∀only␈α∀functions␈α∀allowed␈α∪to
␈↓ α←␈↓manipulate␈α∀the␈α∀free␈α∃storage␈α∀lists.␈α∀ When␈α∃either␈α∀list␈α∀becomes␈α∃empty,␈α∀the
␈↓ α←␈↓garbage collector is called.
␈↓"∀␈↓ α←␈↓␈↓ βi␈↓↓The fundamental assumption of garbage collection is:␈↓
␈↓"∀␈↓ α←␈↓␈↓ βWAt␈α∃any␈α∃point␈α∃in␈α∀a␈α∃LISP␈α∃computation,␈α∃all␈α∃cells␈α∀which
␈↓ α←␈↓␈↓ βWcontain␈α→parts␈α→of␈α→the␈α→computation␈α→are␈α→reachable␈α→(for
␈↓ α←␈↓␈↓ βWexample,␈α∃through␈α⊗␈↓αcar-cdr␈↓␈α∃chains)␈α∃from␈α⊗a␈α∃fixed␈α⊗set␈α∃of
␈↓ α←␈↓␈↓ βWknown cells or base registers.
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α⊃first␈α⊃phase␈α⊃of␈α⊃the␈α⊂garbage␈α⊃collector,␈α⊃called␈α⊃the␈α⊃␈↓↓marking␈α⊂phase␈↓,
␈↓ α←␈↓marks␈αall␈α
of␈αthe␈α
list␈αstructure␈α
which␈αis␈α
currently␈αactive.␈α
By␈αdefinition,␈α
a␈αcell␈α
is
␈↓ α←␈↓active␈α
if␈α
it␈α∞is␈α
reachable␈α
from␈α
the␈α∞base␈α
registers.␈α
The␈α
base␈α∞registers␈α
include:
␈↓ α←␈↓pointers␈α∂to␈α∂the␈α∂beginning␈α∞of␈α∂the␈α∂atom␈α∂table␈α∞and␈α∂the␈α∂environment␈α∂chain;␈α∞a
␈↓ α←␈↓pointer␈α∞to␈α∞the␈α∞control␈α∞chain␈α∞is␈α∞also␈α∞included␈α∞since␈α∞partial␈α∞results␈α∂are␈α∞stored
␈↓ α←␈↓there.␈α
Active␈α
cells␈α
therefore␈αinclude␈α
all␈α
the␈α
atoms␈αin␈α
the␈α
atom␈α
table␈α
and␈αall
␈↓ α←␈↓the␈α⊂associated␈α∂elements␈α⊂on␈α∂property␈α⊂lists.␈α∂ Any␈α⊂partial␈α⊂computations␈α∂which
␈↓ α←␈↓have been generated will also be marked.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 30␈↓Experiments␈α
have␈α
been␈αperformed␈α
in␈α
which␈α
LISP␈αprogrammers␈α
were
␈↓ α←␈↓allowed␈α∪to␈α∪return␈α∪"garbage"␈α∪to␈α∪the␈α∪FS␈α∪list␈α∪themselves.␈α∪The␈α∀results␈α∪were
␈↓ α←␈↓disastrous;␈α∂list␈α∂structure␈α∞thought␈α∂to␈α∂be␈α∞garbage␈α∂was␈α∂returned␈α∞to␈α∂the␈α∂FS␈α∞list
␈↓ α←␈↓even though the structure was still being used by other computations.
␈↓ α←␈↓␈↓284 Static Structure␈↓
_5.14␈↓
␈↓"β␈↓ α←␈↓␈↓ β'In␈α
terms␈α
of␈αour␈α
implementation,␈α
we␈α
mark␈αfrom␈α
the␈α
object␈α
list,␈αfrom␈α
␈↓αdest␈↓,
␈↓ α←␈↓and␈α∪from␈α∪␈↓αcontrol␈↓␈α∪(see␈α∪page 204).␈α∀If␈α∪deep␈α∪binding␈α∪is␈α∪used,␈α∪we␈α∀mark␈α∪the
␈↓ α←␈↓elements␈αreachable␈αthrough␈α␈↓αenv␈↓.␈α If␈αshallow␈αbinding␈αis␈αused,␈αthen␈αmarking␈α
of
␈↓ α←␈↓␈↓αoblist␈↓␈α
would␈α
capture␈αall␈α
the␈α
values;␈αeven␈α
the␈α
ones␈αwhich␈α
may␈α
not␈αbe␈α
accessible
␈↓ α←␈↓as␈αλ-values.␈αWhat␈αwe␈αshould␈αdo␈αinstead␈αis␈αmark␈αthe␈αnon-value␈αproperties␈αon
␈↓ α←␈↓atoms␈αusing␈α␈↓αoblist␈↓;␈αwe␈αthen␈αmark␈αthe␈αvalues␈αseparately␈αusing␈αthe␈α
current␈α␈↓αenv␈↓
␈↓ α←␈↓skeleton tree.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α∞structure␈α
might␈α∞be␈α∞referenced␈α
several␈α∞times␈α
in␈α∞the␈α∞marking␈α
process,
␈↓ α←␈↓since␈α⊗we␈α∃allow␈α⊗shared␈α∃structure,␈α⊗and␈α∃since␈α⊗the␈α∃implementation␈α⊗will␈α∃be
␈↓ α←␈↓referencing␈α∞structures␈α∂also␈α∞referenced␈α∂by␈α∞the␈α∞user's␈α∂program.␈α∞We␈α∂must␈α∞take
␈↓ α←␈↓this␈α
into␈αaccount␈α
since,␈α
though␈αnaive␈α
marking␈α
of␈αan␈α
already␈αmarked␈α
structure
␈↓ α←␈↓is␈αat␈αwasteful,␈αit␈α
is␈αfatal␈αif␈αthe␈αstructure␈α
is␈αself-referential.␈α Once␈αall␈αthe␈α
active
␈↓ α←␈↓structure has been marked, we proceed to the ␈↓↓sweep phase.␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈αsweep␈αphase␈αproceeds␈αlinearly␈αthrough␈αmemory,␈αcollecting␈αall␈αthose
␈↓ α←␈↓cells␈α∩which␈α∩have␈α⊃␈↓¬not␈↓␈α∩been␈α∩marked.␈↓π 31␈↓␈α⊃These␈α∩unmarked␈α∩cells␈α∩are␈α⊃chained
␈↓ α←␈↓together␈α
via␈α
their␈α
␈↓αcdr␈↓-parts␈α
to␈α
form␈α
a␈αnew␈α
FS␈α
list.␈α
The␈α
FS␈α
pointer␈α
is␈α
set␈αto␈α
the
␈↓ α←␈↓beginning␈α
of␈α
this␈α
list.␈α
The␈α
unmarked␈αcells␈α
in␈α
FWS␈α
comprise␈α
the␈α
new␈αFWS
␈↓ α←␈↓list.
␈↓"β␈↓ α←␈↓␈↓ β'If␈αthere␈αis␈α
sufficient␈αroom␈αin␈α
a␈αfull␈αword␈αto␈α
contain␈αa␈αpointer,␈α
then␈αwe
␈↓ α←␈↓chain␈α∂the␈α⊂words␈α∂together;␈α⊂otherwise␈α∂we␈α⊂must␈α∂designate␈α⊂the␈α∂FWS␈α⊂list␈α∂some
␈↓ α←␈↓other way.
␈↓"β␈↓ α←␈↓␈↓ β'Garbage␈αcollection␈αis␈αa␈α
very␈αgeneral␈αstorage␈αmanagement␈α
technique.␈α It
␈↓ α←␈↓has␈α⊃become␈α⊃a␈α⊃standard␈α⊃tool␈α⊃for␈α⊃implementors␈α⊃of␈α⊃complex␈α⊃systems.␈α⊃It␈α⊃was
␈↓ α←␈↓invented␈α∞by␈α
the␈α∞original␈α
LISP␈α∞implementation␈α
team.␈α∞ The␈α
basic␈α∞ideas␈α
have
␈↓ α←␈↓been␈α
embellished␈αover␈α
the␈α
years␈αto␈α
account␈α
for␈αlarger␈α
real␈α
memories,␈αvirtual
␈↓ α←␈↓memories,␈α⊃different␈α⊃implementations␈α⊃of␈α⊂LISP␈α⊃data,␈α⊃and␈α⊃different␈α⊂machine
␈↓ α←␈↓architectures;␈α
but␈α
the␈α∞basic␈α
ideas␈α
are␈α∞simple.␈α
More␈α
complex␈α∞algorithms␈α
will
␈↓ α←␈↓be discussed in Section 7.3 and on page 397.
␈↓"β␈↓ α←␈↓␈↓ ∧Q␈↓↓5.15 A Simple LISP Garbage Collector␈↓
␈↓"β␈↓ α←␈↓We␈αwill␈αnow␈α
write␈αa␈αgarbage␈α
collector␈αin␈αLISP␈αto␈α
mark␈αand␈αsweep␈α
nodes␈αin
␈↓ α←␈↓FS and FWS.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 31␈↓The␈α
sweep␈α
phase␈α∞is␈α
sometimes␈α
a␈α
good␈α∞place␈α
to␈α
unmark␈α∞the␈α
marked
␈↓ α←␈↓cells.␈α This␈αdepends␈αon␈αthe␈α
implementation.␈αIf␈αeach␈αword␈αcarries␈αa␈α
"mark bit"
␈↓ α←␈↓then,␈α∩perform␈α∩the␈α∩unmarking;␈α∩if␈α∩the␈α⊃marked␈α∩flags␈α∩are␈α∩all␈α∩localized␈α∩in␈α⊃a
␈↓ α←␈↓separate␈α∀bit␈α∪table (Section 7.5)␈α∀then␈α∀there␈α∪is␈α∀no␈α∪advantage␈α∀to␈α∀doing␈α∪the
␈↓ α←␈↓unmarking now.
␈↓ α←␈↓␈↓5.15␈↓ εwA Simple LISP Garbage Collector 285␈↓
␈↓"β␈↓ α←␈↓The algorithm will have three main functions:
␈↓"β␈↓ α←␈↓␈↓αinitialize[x;y]␈↓␈α
initializes␈αthe␈α
marking␈α
device␈αfor␈α
each␈α
cell␈αin␈α
the␈αspace␈α
between
␈↓ α←␈↓␈↓ β'␈↓αx␈↓␈αand␈α␈↓αy␈↓.␈α ␈↓αinitialize␈↓␈αwill␈αbe␈αcalled␈αtwice;␈αonce␈αfor␈αFS␈αand␈αonce␈αfor␈αFWS.
␈↓ α←␈↓␈↓ β'The next algorithm does the actual marking.
␈↓"∀␈↓ α←␈↓␈↓αmark[l]␈↓␈α⊃will␈α⊃be␈α⊃called␈α⊂for␈α⊃each␈α⊃base␈α⊃register␈α⊂␈↓αl␈↓␈α⊃which␈α⊃points␈α⊃to␈α⊃active␈α⊂list
␈↓ α←␈↓␈↓ β'structure.␈α
If␈α
the␈αword␈α
is␈α
in␈α
FWS␈α␈↓αmark␈↓␈α
will␈α
mark␈αit␈α
and␈α
return;␈α
if␈αthe
␈↓ α←␈↓␈↓ β'word␈α∞has␈α∞already␈α∂been␈α∞marked␈α∞it␈α∞simply␈α∂return,␈α∞since␈α∞we␈α∂are␈α∞assured
␈↓ α←␈↓␈↓ β'that␈α∂any␈α∂cells␈α⊂further␈α∂down␈α∂the␈α⊂structure␈α∂have␈α∂already␈α⊂been␈α∂marked.
␈↓ α←␈↓␈↓ β'Otherwise␈α
the␈α∞word␈α
is␈α∞in␈α
FS␈α
and␈α∞thus␈α
has␈α∞a␈α
␈↓αcar␈↓␈α
and␈α∞a␈α
␈↓αcdr␈↓;␈α∞mark␈α
the
␈↓ α←␈↓␈↓ β'word; recursively mark the ␈↓αcar␈↓; recursively mark the ␈↓αcdr␈↓.
␈↓"∀␈↓ α←␈↓␈↓αsweep[x;y]␈↓␈α⊂collects␈α⊂all␈α⊂inaccessible␈α⊂cells␈α⊂in␈α⊂the␈α⊂space␈α⊂delimited␈α⊂by␈α⊂␈↓αx␈↓␈α⊂and␈α⊂␈↓αy␈↓.
␈↓ α←␈↓␈↓ β'␈↓αsweep␈↓␈α∞will␈α∞be␈α∂called␈α∞twice;␈α∞once␈α∂to␈α∞generate␈α∞a␈α∂new␈α∞free␈α∞space␈α∂list␈α∞and
␈↓ α←␈↓␈↓ β'once␈αto␈αgenerate␈α
a␈αnew␈αfull␈α
word␈αspace␈αlist.␈α
Elements␈αof␈αthese␈α
free␈αlists
␈↓ α←␈↓␈↓ β'will␈α∩be␈α∩chained␈α∩together␈α∩by␈α⊃their␈α∩␈↓αcdr␈↓␈α∩parts.␈α∩ The␈α∩initialization␈α⊃and
␈↓ α←␈↓␈↓ β'sweep␈α∩phases␈α∩of␈α∩this␈α∩garbage␈α∩collector␈α∩are␈α∩very␈α∩similar␈α∩and,␈α∩as␈α∩we
␈↓ α←␈↓␈↓ β'mentioned␈αabove,␈αcan␈αsometimes␈αbe␈αcombined.␈α Both␈αthese␈αphases␈αmust
␈↓ α←␈↓␈↓ β'be assured of reaching every node in the space.
␈↓"∀␈↓ α←␈↓These main functions use several other functions and predicates:
␈↓"∀␈↓ α←␈↓␈↓αfwswrdp[x]␈↓␈αis␈αtrue␈αjust␈αin␈αthe␈αcase␈αthat␈α␈↓αx␈↓␈αis␈αa␈αword␈αin␈αFWS.␈αThis␈αis␈αused␈αas
␈↓ α←␈↓␈↓ β'one of the termination conditions of ␈↓αmark␈↓.
␈↓"∀␈↓ α←␈↓␈↓αmarkA[x]␈↓ marks word ␈↓αx␈↓ as accessible.
␈↓"∀␈↓ α←␈↓␈↓αmarkNA[x]␈↓ marks word ␈↓αx␈↓ as not accessible.
␈↓"∀␈↓ α←␈↓␈↓αAp[x]␈↓ is true if word ␈↓αx␈↓ is marked "accessible".
␈↓"∀␈↓ α←␈↓␈↓αup[x]␈↓: If ␈↓αx␈↓ is at location ␈↓
n␈↓ then ␈↓αup[x]␈↓ is location ␈↓
n+1␈↓.
␈↓"∀␈↓ α←␈↓␈↓αrplacd[x;y]␈↓␈αmodifies␈α
␈↓αx␈↓␈αby␈α
replacing␈αits␈α␈↓αcdr␈↓-part␈α
with␈α␈↓αy␈↓.␈α
The␈αvalue␈αreturned␈α
is
␈↓ α←␈↓␈↓ β'the new ␈↓αx␈↓.
␈↓"⊃␈↓ α←␈↓
␈↓αdest␈↓
␈↓αenv␈↓
␈↓"␈↓ α←␈↓
~ ~
␈↓"␈↓ α←␈↓
~ ⊂ααααααα⊃ ~ ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
%→ ~ #αβα⊃ %→ ~ #αβαα ### →
␈↓"␈↓ α←␈↓
εαααπαααλ ↓ εαααπαααλ
␈↓"␈↓ α←␈↓
# # # ←$ ~ x ~ # βα→α⊃
␈↓"␈↓ α←␈↓
~ ~ β→ - - →⊃ εαααβαααλ ~
␈↓"␈↓ α←␈↓
εαααβαααλ ~ y ~ #αβ→⊃ ↓
␈↓"␈↓ α←␈↓
~ ~ ~ ↓ %ααα∀ααα$ ↓ ~
␈↓"␈↓ α←␈↓
# # # ⊂αααπααα⊃ ⊂ααααααα⊃ ~ ↓
␈↓"␈↓ α←␈↓
εαααβαααλ ~ ? ~ # β→ - - - →~ ? ~←$ ~
␈↓"␈↓ α←␈↓
~ ~ ~ %ααα∀ααα$ %ααααααα$ ~
␈↓"␈↓ α←␈↓
%ααα∀ααα$ ↑ ↓
␈↓"␈↓ α←␈↓
%←ααααα←αααα←ααααα←ααααα←ααα$
␈↓"␈↓ α←␈↓
␈↓ Algorithm for ␈↓αrplacd␈↓ ␈↓α
␈↓ α←␈↓␈↓286 Static Structure␈↓
_5.15␈↓
␈↓"β␈↓ α←␈↓Can you write ␈↓αrplacd␈↓ as a LISP function?
␈↓"∀␈↓ α←␈↓αinitialize <= λ[[x;y] prog[[]
␈↓"β␈↓ α←␈↓α␈↓ ∧7 a␈↓ ∧[markNA[x];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧[x ← up[x];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧[[eq[x;y] → return[␈↓
t␈↓α]];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧[go [a]]]
␈↓" ␈↓ α←␈↓αmark <= λ[[l]␈↓ ∧7[Ap[l] → ␈↓
t␈↓α;
␈↓"β␈↓ α←␈↓α␈↓ ∧7 fwswrdp[l] → markA[l];
␈↓"β␈↓ α←␈↓α␈↓ ∧7 ␈↓
t␈↓α →␈↓ ∧smarkA[l];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧smark[car[l]];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ∧smark[cdr[l]] ]]
␈↓" ␈↓ α←␈↓αsweep <= λ[[x;y] prog[[z]
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ a␈↓ ∧C[not[Ap[x]] → z ← rplacd[ x;z]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧Cx ← up[x];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧C[eq[x;y] → return [z]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧Cgo[a]]]
␈↓"∀␈↓ α←␈↓␈↓ β'As␈αindicated␈αpreviously,␈αthere␈αare␈αalternatives␈αto␈αgarbage␈α
collection.␈α If
␈↓ α←␈↓the␈α∪data-structure␈α∪manipulations␈α∪are␈α∪particularly␈α∪simple␈α∪one␈α∪might␈α∪leave
␈↓ α←␈↓storage␈α⊂management␈α⊃to␈α⊂the␈α⊂programmer.␈↓π 32␈↓␈α⊃There␈α⊂is␈α⊂an␈α⊃intermediate␈α⊂area
␈↓ α←␈↓between␈α⊗garbage␈α↔collection␈α⊗and␈α⊗explicit␈α↔management.␈α⊗ First␈α↔notice␈α⊗that
␈↓ α←␈↓storage␈α
management␈α
becomes␈α
quite␈α
simple␈α∞if␈α
there␈α
is␈α
no␈α
sharing␈α∞of␈α
sublists.
␈↓ α←␈↓However␈α∂sharing␈α∂substructures␈α∂can␈α∂save␈α∂space␈α∂and␈α∂careful␈α∂modification␈α∞of
␈↓ α←␈↓shared␈α⊂structures␈α⊂can␈α⊂communicate␈α⊂global␈α⊂information␈α⊂between␈α⊂algorithms.
␈↓ α←␈↓A␈αrich␈αclass␈αof␈αsymbolic␈α
data␈αmanipulations␈αfall␈αinto␈αthe␈αcategory␈α
of␈αshared,
␈↓ α←␈↓but␈α⊃non-circular,␈α∩structures.␈α⊃In␈α∩this␈α⊃case,␈α∩storage␈α⊃can␈α∩be␈α⊃managed␈α∩by␈α⊃the
␈↓ α←␈↓␈↓↓reference counter␈↓ method.
␈↓"β␈↓ α←␈↓␈↓ β'Instead␈α
of␈α
using␈αa␈α
garbage␈α
collector,␈α
we␈αmight␈α
associate␈α
a␈αcounter,␈α
called
␈↓ α←␈↓a␈α
␈↓↓reference␈α
counter␈↓,␈α∞with␈α
each␈α
list␈α∞when␈α
it␈α
is␈α
built.␈α∞In␈α
that␈α
counter␈α∞we␈α
will
␈↓ α←␈↓store␈α
the␈αnumber␈α
of␈α
references␈αto␈α
that␈α
list.␈αThe␈α
counter␈α
will␈αbe␈α
initialized␈αto␈α
1
␈↓ α←␈↓when␈αthe␈αlist␈αis␈αcreated.␈α Whenever␈αthe␈αlist␈αis␈αshared␈αwe␈αincrease␈αthe␈αcounter
␈↓ α←␈↓by␈α
1;␈α∞whenever␈α
the␈α
list␈α∞is␈α
no␈α
longer␈α∞to␈α
be␈α
shared␈α∞by␈α
some␈α
list␈α∞structure,␈α
we
␈↓ α←␈↓decrease␈αthe␈αcounter␈αby␈α1.␈α When␈αthe␈αcount␈αgoes␈αto␈α0␈αwe␈αcan␈αput␈αthe␈αcells␈αof
␈↓ α←␈↓the list in the free space list.
␈↓"β␈↓ α←␈↓␈↓ β'A␈αdifficulty␈αwith␈αthe␈αreference␈αcounter␈αscheme␈αis␈αthe␈αinability␈αto␈αcollect
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 32␈↓Aside␈αfrom␈αthese␈α
implementation␈αconsiderations,␈αone␈αstrong␈α
point␈αof
␈↓ α←␈↓LISP␈α∪is␈α∩the␈α∪notion␈α∪that␈α∩storage␈α∪management␈α∩need␈α∪not␈α∪concern␈α∩symbolic
␈↓ α←␈↓programmers␈α↔to␈α↔any␈α↔larger␈α↔extent␈α↔than␈α↔roundoff␈α↔errors␈α↔concern␈α↔their
␈↓ α←␈↓numerical counterparts.
␈↓ α←␈↓␈↓5.15␈↓ εwA Simple LISP Garbage Collector 287␈↓
␈↓"β␈↓ α←␈↓circular␈α∩lists.␈α⊃A␈α∩circular␈α⊃list␈α∩is␈α⊃a␈α∩list␈α⊃structure␈α∩which␈α∩is␈α⊃self-referential.␈↓π 33␈↓
␈↓ α←␈↓Consider the following sequence:
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ Manufacture a list, ␈↓αx␈↓: ␈↓αx ← (B I G L I S T)␈↓. Reference count is 1.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ Circularize it: ␈↓αx ← circle[x];␈↓. Reference count is now 2.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ Delete all references to ␈↓αx␈↓: ␈↓αx ← NIL␈↓. Reference count reverts to 1.
␈↓"∀␈↓ α←␈↓The␈αlist␈α
is␈αno␈α
longer␈αreferenced,␈α
but␈αit␈αis␈α
not␈αon␈α
the␈αfree␈α
space␈αlist,␈α
and␈αhas
␈↓ α←␈↓thus been lost to the system.
␈↓"β␈↓ α←␈↓␈↓ β'Two␈α⊃less␈α⊃serious␈α⊃considerations␈α⊃should␈α⊃be␈α⊃mentioned␈α∩in␈α⊃conjunction
␈↓ α←␈↓with␈α∂reference␈α∞counters.␈α∂First,␈α∞each␈α∂node␈α∂which␈α∞is␈α∂to␈α∞be␈α∂collected␈α∂with␈α∞this
␈↓ α←␈↓scheme␈α∂must␈α∞have␈α∂an␈α∂associated␈α∞reference␈α∂field␈α∂to␈α∞contain␈α∂the␈α∂count.␈α∞That
␈↓ α←␈↓requires␈α
extra␈α
space,␈αand␈α
usually␈α
imposes␈α
a␈αmaximum␈α
size␈α
for␈α
the␈αreference
␈↓ α←␈↓count.␈α
If␈α
that␈αmaximum␈α
is␈α
reached,␈α
either␈αan␈α
additional␈α
space␈α
is␈αallocated,␈α
or
␈↓ α←␈↓the␈αfilled␈αcount␈αmay␈αnever␈αbe␈αdecremented␈αand␈αthe␈αassociated␈αstructure␈αmust
␈↓ α←␈↓be garbage collected.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
second␈α∞problem␈α
involves␈α
decrementing␈α∞counts.␈α
Whenever␈α∞a␈α
count
␈↓ α←␈↓goes␈αto␈αzero␈αthe␈αcounts␈αassociated␈αwith␈αits␈αimmediate␈αsuccessors␈αmust␈α
also␈αbe
␈↓ α←␈↓decremented.␈α∂ This␈α∂process␈α∂is␈α∂applied␈α∂recursively␈α∂until␈α∂non-zero␈α∂counts␈α∞are
␈↓ α←␈↓encountered. The bookkeeping for such a task is non-trivial.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α≤are␈α≤significant␈α≤storage␈α≤management␈α≤problems␈α≤which␈α≤are
␈↓ α←␈↓amenable␈α∞to␈α∞reference␈α∞counting.␈α∞LISP␈α∞generates␈α∞very␈α∞intertwined␈α∞structures;
␈↓ α←␈↓therefore␈α∪these␈α∪alternative␈α∪methods␈α∪are␈α∪insufficient␈α∪in␈α∪general.␈α∪However,
␈↓ α←␈↓some␈α∂parts␈α⊂of␈α∂LISP␈α⊂implementations␈α∂could␈α∂use␈α⊂reference␈α∂counting;␈α⊂we␈α∂will
␈↓ α←␈↓discuss␈α∩some␈α∩of␈α∩these␈α∩aspects␈α⊃in␈α∩Section 5.20.␈α∩For␈α∩an␈α∩excellent␈α⊃discussion
␈↓ α←␈↓and analysis of storage management schemes see [Mul 76].
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. This␈α∀problem␈α∃deals␈α∀with␈α∃what␈α∀is␈α∃known␈α∀in␈α∃LISP␈α∀as␈α∃␈↓↓hash␈α∀consing␈↓
␈↓ α←␈↓␈↓ β∂([Got 74]).␈α We␈αhave␈αbeen␈αstoring␈α
atoms␈αuniquely,␈αbut␈αit␈αshould␈α
be␈αclear
␈↓ α←␈↓␈↓ β∂from␈α⊗the␈α⊗behavior␈α⊗of␈α⊗␈↓αcons␈↓␈α⊗that␈α⊗non-atomic␈α⊗S-exprs␈α⊗are␈α⊗␈↓¬not␈↓␈α∃stored
␈↓ α←␈↓␈↓ β∂uniquely.␈α∩ Storing␈α∩single␈α∩copies␈α∩of␈α⊃any␈α∩S-expr␈α∩would␈α∩save␈α∩space.␈α⊃For
␈↓ α←␈↓␈↓ β∂example,␈α≥the␈α≥non-atomic␈α≥structure␈α≥of␈α≥␈↓α((A . B) . (A . B))␈↓␈α≡could␈α≥be
␈↓ α←␈↓␈↓ β∂represented␈α∪with␈α∪two␈α∪cells␈α∀rather␈α∪than␈α∪three.␈α∪ Unique␈α∪storage␈α∀is␈α∪not
␈↓ α←␈↓␈↓ β∂without␈α∂its␈α∂difficulties.␈α∂What␈α∂problems␈α∂do␈α∂you␈α∂foresee␈α∂in␈α∞implementing
␈↓ α←␈↓␈↓ β∂such a scheme?
␈↓" ␈↓ α←␈↓2. We␈αsaid␈αon␈αpage 267␈αthat␈αmany␈αLISP␈αcomputations␈αgenerate␈αlist␈α
structure
␈↓ α←␈↓␈↓ β∂rather than true LISP-trees. Give an example.
␈↓" ␈↓ α←␈↓3. Can␈αyou␈αwrite␈αa␈α
LISP␈αfunction␈α␈↓αcircle␈α<=␈αλ[[x]␈α
...]␈↓␈αwhich␈αwill␈αtake␈αa␈α
list␈α␈↓αx␈↓
␈↓ α←␈↓␈↓ β∂and make it circular. Thus:
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 33␈↓LISP␈α∀1␈α∪([McC 60])␈α∀disallowed␈α∀circular␈α∪list,␈α∀but␈α∀succeeding␈α∪LISP
␈↓ α←␈↓dialects have allowed arbitrary binary structure.
␈↓ α←␈↓␈↓288 Static Structure␈↓
_5.15␈↓
␈↓"⊃␈↓ α←␈↓
⊂ααααααααααααα←ααααααααααααα⊃
␈↓"␈↓ α←␈↓
⊂αααααααααπααα⊃ ⊂αααααπααα⊃ ↓ ⊂ααααπααα⊃ ⊂αααααααπααα⊃ ↑
␈↓"␈↓ α←␈↓
~ NOTHING ~ #αβα→~ CAN ~ #αβα∀→~ GO ~ #αβα→~ WRONG ~ #αβ→$
␈↓"␈↓ α←␈↓
%ααααααααα∀ααα$ %ααααα∀ααα$ %αααα∀ααα$ %ααααααα∀ααα$
␈↓"β␈↓ α←␈↓␈↓ β∂This␈αlist␈αis␈αcircular␈αon␈αits␈α"last␈αtwo"␈αelements.␈α Printing␈αsuch␈αstructures␈αis
␈↓ α←␈↓␈↓ β∂not possible using the ␈↓αprint␈↓ function.
␈↓" ␈↓ α←␈↓4. What␈α∞LISP␈α∞operations␈α∞generate␈α∂structures␈α∞such␈α∞that␈α∞a␈α∂reference␈α∞counter
␈↓ α←␈↓␈↓ β∂implementation would not suffice?
␈↓ α←␈↓␈↓5.16␈↓ ¬5A Review of the Structure of the LISP Machine 289␈↓
␈↓"β␈↓ α←␈↓␈↓ βm␈↓↓5.16 A Review of the Structure of the LISP Machine␈↓
␈↓"β␈↓ α←␈↓We␈α⊂have␈α∂a␈α⊂good␈α∂portion␈α⊂of␈α∂the␈α⊂storage␈α∂conventions␈α⊂for␈α∂LISP␈α⊂set␈α⊂out.␈α∂ A
␈↓ α←␈↓difficult␈αarea␈αinvolves␈αthe␈αorganization␈αof␈αthe␈αdata␈αstructures␈αto␈αperform␈αthe
␈↓ α←␈↓correct␈αbinding␈αand␈αunbinding␈αof␈αvariables.␈αBefore␈αwe␈αtackle␈αthat,␈αwe␈αgive␈αa
␈↓ α←␈↓diagram showing the basic structure of LISP memory.
␈↓"⊃␈↓ α←␈↓
⊂αααααααααααααααααααααααααααααααααααααααα⊃
␈↓"␈↓ α←␈↓
~␈↓ λ;~
␈↓"␈↓ α←␈↓
~ ### THE SUBCONSCIOUS ###␈↓ λ;~
␈↓"␈↓ α←␈↓
~ ␈↓αeval␈↓
and friends␈↓ λ;~
␈↓"␈↓ α←␈↓
~ ␈↓αread␈↓
and ␈↓αprint␈↓
␈↓ λ;~
␈↓"␈↓ α←␈↓
~ the garbage collector␈↓ λ;~
␈↓"␈↓ α←␈↓
~ the base registers for marking;␈↓ λ;~
␈↓"␈↓ α←␈↓
~ these include:␈↓ λ;~
␈↓"␈↓ α←␈↓
~ FS pointer␈↓ λ;~
␈↓"␈↓ α←␈↓
~ FWS pointer␈↓ λ;~
␈↓"␈↓ α←␈↓
~ atom table(␈↓αOBLIST␈↓
) pointer␈↓ λ;~
␈↓"␈↓ α←␈↓
~ registers for partial results␈↓ λ;~
␈↓"␈↓ α←␈↓
~ ␈↓αdest, control␈↓
␈↓ λ;~
␈↓"␈↓ α←␈↓
~ the access chain␈↓ λ;~
␈↓"␈↓ α←␈↓
~␈↓ λ;~
␈↓"␈↓ α←␈↓
εααααααααααααααααααααααααααααααααααααααααλ
␈↓"␈↓ α←␈↓
~␈↓ λ;~
␈↓"␈↓ α←␈↓
~ ### POINTER SPACE ###␈↓ λ;~
␈↓"␈↓ α←␈↓
~ the free space list␈↓ λ;~
␈↓"␈↓ α←␈↓
~ those parts of S-exprs containing␈↓ λ;~
␈↓"␈↓ α←␈↓
~ ␈↓αcar␈↓
- and ␈↓αcdr␈↓
-parts.␈↓ λ;~
␈↓"␈↓ α←␈↓
~␈↓ λ;~
␈↓"␈↓ α←␈↓
εααααααααααααααααααααααααααααααααααααααααλ
␈↓"␈↓ α←␈↓
~␈↓ λ;~
␈↓"␈↓ α←␈↓
~ ### FULL WORD SPACE ###␈↓ λ;~
␈↓"␈↓ α←␈↓
~ the full word space list␈↓ λ;~
␈↓"␈↓ α←␈↓
~ atom print names␈↓ λ;~
␈↓"␈↓ α←␈↓
~ numbers␈↓ λ;~
␈↓"␈↓ α←␈↓
~␈↓ λ;~
␈↓"␈↓ α←␈↓
%αααααααααααααααααααααααααααααααααααααααα$
␈↓"∀␈↓ α←␈↓␈↓ Structure of LISP memory ␈↓
␈↓"λ␈↓ α←␈↓␈↓ ∧s␈↓↓5.17 Implementations of Binding␈↓
␈↓"β␈↓ α←␈↓In␈α∀Section 3.5␈α∀and␈α∀Section 3.11␈α∃we␈α∀discussed␈α∀deep␈α∀binding␈α∃and␈α∀shallow
␈↓ α←␈↓binding␈α∩respectively.␈α∩That␈α∩discussion␈α∩took␈α∩place␈α∩at␈α∩a␈α∪reasonably␈α∩abstract
␈↓ α←␈↓level.␈α
The␈α
next␈α
few␈α
sections␈αdiscuss␈α
these␈α
binding␈α
implementations␈α
in␈αmore
␈↓ α←␈↓detail.␈α We␈αfirst␈αexamine␈αsome␈α
of␈αthe␈αpossible␈αpitfalls␈αin␈α
the␈αimplementation
␈↓ α←␈↓of␈α∞LISP;␈α∞then␈α
we␈α∞give␈α∞deep␈α∞and␈α
shallow␈α∞implementations␈α∞for␈α∞an␈α
important
␈↓ α←␈↓subset␈α∀of␈α∀LISP.␈α∀ Finally,␈α∀we␈α∀sketch␈α∀some␈α∀of␈α∀the␈α∀methods␈α∃available␈α∀for
␈↓ α←␈↓implementing the full language in an efficient manner.
␈↓"β␈↓ α←␈↓␈↓ β'Though␈α⊂much␈α⊃of␈α⊂this␈α⊃discussion␈α⊂deals␈α⊂with␈α⊃the␈α⊂binding␈α⊃stategies␈α⊂of
␈↓ α←␈↓␈↓290 Static Structure␈↓
_5.17␈↓
␈↓"β␈↓ α←␈↓LISP,␈αand␈αtherefore␈αwith␈αcontrol␈αstructure,␈αwe␈αare␈αrestricting␈αourselves␈αto␈αthe
␈↓ α←␈↓data␈α↔structure␈α⊗requirements.␈α↔The␈α⊗next␈α↔chapter␈α⊗shows␈α↔how␈α↔the␈α⊗control
␈↓ α←␈↓structures of LISP implementations manipulate these data structures.
␈↓"∀␈↓ α←␈↓α␈↓Consider the evaluation of a form: ␈↓αf[a␈↓β1␈↓α; ... ;a␈↓βn␈↓α]
␈↓" ␈↓ α←␈↓α␈↓where:␈↓α␈↓ βKf <= λ[[x␈↓β1␈↓α; ... ;x␈↓βn␈↓α] ... g[ ... ] ... ;i[ ... ]],
␈↓" ␈↓ α←␈↓α␈↓ βKg <= λ[[ ... ] ... h[ ... ] ],
␈↓" ␈↓ α←␈↓α␈↓and ␈↓α␈↓ βKi <= λ[[ ... ] ... j[ ... ] ]
␈↓"∀␈↓ α←␈↓Typically␈α
a␈α
picture␈α
like␈α
the␈αfollowing␈α
occurs,␈α
where␈α
the␈α
instance␈α
of␈αfunction
␈↓ α←␈↓name␈α⊃means␈α⊃a␈α⊃block␈α⊃of␈α⊃λ-bindings␈α⊃necessary␈α⊃to␈α⊃begin␈α⊃evaluation␈α∩of␈α⊃that
␈↓ α←␈↓function:
␈↓"⊃␈↓ α←␈↓
⊂ααα⊃ ⊂ααα⊃
␈↓"␈↓ α←␈↓
~ h ~ ~ j ~
␈↓"␈↓ α←␈↓
⊂ααα⊃ εαααλ ⊂ααα⊃ ⊂ααα⊃ εαααλ ...
␈↓"␈↓ α←␈↓
~ g ~ ~ g ~ ~ g ~ ~ i ~ ~ i ~
␈↓"␈↓ α←␈↓
⊂ααα⊃ εαααλ εαααλ εαααλ ⊂ααα⊃ εαααλ εαααλ ...
␈↓"␈↓ α←␈↓
~ f ~ ~ f ~ ~ f ~ ~ f ~ ~ f ~ ~ f ~ ~ f ~
␈↓"␈↓ α←␈↓
%ααα$ %ααα$ %ααα$ %ααα$ %ααα$ %ααα$ %ααα$
␈↓"∀␈↓ α←␈↓We␈αbuild␈αup␈αa␈αstack␈αof␈αλ-binding␈αblocks␈αas␈αwe␈αcontinue␈αto␈αenter␈αprocedures,
␈↓ α←␈↓and␈α⊂as␈α∂we␈α⊂leave␈α⊂a␈α∂procedure␈α⊂we␈α∂remove␈α⊂that␈α⊂block␈α∂of␈α⊂bindings␈α⊂from␈α∂the
␈↓ α←␈↓stack.␈α∂ When␈α∂we␈α∂wish␈α∂to␈α∂know␈α∂the␈α∂value␈α∂of␈α∂a␈α∂variable␈α∂we␈α∂look␈α∂down␈α∞the
␈↓ α←␈↓stack␈α∞for␈α
the␈α∞first␈α
occurrence␈α∞of␈α∞that␈α
variable;␈α∞the␈α
associated␈α∞binding␈α∞is␈α
the
␈↓ α←␈↓desired␈α∂value.␈α∂ However,␈α∂LISP␈α∂allows␈α∂functional␈α∂arguments␈α∂and␈α∂functional
␈↓ α←␈↓values;␈αthese␈αconstructs␈α
require␈αmodification␈αof␈α
the␈αbehavior␈αmodelled␈αin␈α
this
␈↓ α←␈↓simple blocks world.
␈↓"β␈↓ α←␈↓␈↓ β'When␈αwe␈α
recognize␈αa␈α
functional␈αargument,␈α
we␈αnote␈α
the␈αblock␈α
which␈αis
␈↓ α←␈↓currently␈α
on␈α
the␈α
top␈αof␈α
the␈α
stack.␈α
When␈αwe␈α
apply␈α
that␈α
functional␈αargument,
␈↓ α←␈↓intervening␈α
blocks␈α
will␈α
have␈α
been␈αstacked;␈α
we␈α
change␈α
the␈α
environment␈αsuch
␈↓ α←␈↓that␈α
the␈α
lookup␈α
of␈α
non-local␈α
variables␈α
takes␈α
place␈α
beginning␈α
with␈α
the␈αsaved
␈↓ α←␈↓block, rather than with the top of the stack.
␈↓"β␈↓ α←␈↓␈↓ β'However,␈αif␈α␈↓αh␈↓␈αsay,␈αgenerated␈α
a␈αfunctional␈αvalue␈αwhich␈αis␈αto␈α
be␈αapplied
␈↓ α←␈↓in␈αthe␈α
context␈αof␈α
␈↓αj␈↓␈αthen␈α
we␈αmust␈α
retain␈αthose␈α
values␈αin␈α
the␈α␈↓αf-g-h␈↓␈α
stack␈αin␈α
such
␈↓ α←␈↓a␈α∞way␈α∞that␈α∂they␈α∞may␈α∞be␈α∂used␈α∞to␈α∞restore␈α∞the␈α∂enviroment␈α∞when␈α∞we␈α∂desire␈α∞to
␈↓ α←␈↓apply the functional value in the ␈↓αf-i-j␈↓ stack.
␈↓"⊃␈↓ α←␈↓
⊂ααα⊃ ⊂ααα⊃
␈↓"␈↓ α←␈↓
↑ ~ h ~ ~ j ~ ~
␈↓"␈↓ α←␈↓
~ εαααλ εαααλ ↓
␈↓"␈↓ α←␈↓
bind ~ ~ g ~ ~ i ~ ~ unbind
␈↓"␈↓ α←␈↓
↑ εααα∀αα∀αααλ ↓
␈↓"␈↓ α←␈↓
~ ~ f ~ ↓
␈↓"␈↓ α←␈↓
%αααααααααα$
␈↓"∀␈↓ α←␈↓We␈α
will␈α
discuss␈αdata␈α
structure␈α
requirements␈αfor␈α
implementation␈α
of␈αthese␈α
three
␈↓ α←␈↓LISP␈α∂subsets:␈α∞first,␈α∂simple␈α∂function␈α∞application;␈α∂then,␈α∂functional␈α∞arguments;
␈↓ α←␈↓and finally, functional values.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
mechanism␈α
which␈α
we␈α
described␈α
in␈α
the␈α
initial␈α
blocks␈α
model␈αoccurs
␈↓ α←␈↓␈↓5.17␈↓ π%Implementations of Binding 291␈↓
␈↓"β␈↓ α←␈↓quite␈α⊂naturally␈α∂in␈α⊂computer␈α⊂science.␈α∂It␈α⊂is␈α⊂called␈α∂a␈α⊂␈↓↓stack␈↓.␈↓π 34␈↓␈α⊂The␈α∂important
␈↓ α←␈↓characteristics␈α∀of␈α∀stacks␈α∀are␈α∀that␈α∀they␈α∀are␈α∀lists␈α∀such␈α∀that␈α∀additions␈α∪and
␈↓ α←␈↓deletions can only be made at the front of the list.
␈↓"β␈↓ α←␈↓␈↓ β'What␈αis␈αof␈αinterest␈αto␈αus␈αnow␈αis␈αthat␈αstacks␈αhave␈αa␈αparticularly␈αefficient
␈↓ α←␈↓implementation;␈α≤due␈α≠to␈α≤the␈α≤very␈α≠regular␈α≤way␈α≠in␈α≤which␈α≤stacks␈α≠are
␈↓ α←␈↓manipulated,␈αthe␈αlinked␈αallocation␈αimplementation␈αis␈αnot␈αusually␈αnecessary.␈↓π 35␈↓
␈↓ α←␈↓Instead a stack can be implemented as:
␈↓"∀␈↓ α←␈↓␈↓ βW␈↓↓1.␈↓ A sequence of contiguous locations.
␈↓" ␈↓ α←␈↓␈↓ βW␈↓↓2.␈↓ A pointer initialized to point ␈↓¬before␈↓ the first of these locations.
␈↓" ␈↓ α←␈↓␈↓ βW␈↓↓3.␈↓ An␈αoperation,␈αtypically␈αcalled␈α␈↓↓push␈↓␈αwhich␈αplaces␈αa␈αnew␈αobject␈αin
␈↓ α←␈↓␈↓ ∧πthe␈αstack.␈αThis␈αcan␈α
be␈αaccomplished␈αby␈αadding␈α
1␈αto␈αthe␈αvalue␈α
of
␈↓ α←␈↓␈↓ ∧πthe␈α
stack␈α
pointer,␈α
and␈α
then␈α
putting␈α
a␈α
representation␈α
of␈α
the␈α
object
␈↓ α←␈↓␈↓ ∧πin the cell currently referenced by the pointer.
␈↓" ␈↓ α←␈↓␈↓ βW␈↓↓4.␈↓ An␈α
operation␈α
called␈α␈↓↓pop␈↓␈α
which␈α
gets␈α
the␈αfirst␈α
value␈α
in␈α
the␈αstack
␈↓ α←␈↓␈↓ ∧πand then decrements the pointer by 1.
␈↓" ␈↓ α←␈↓␈↓ βW␈↓↓5.␈↓ Though␈α∃the␈α∃abstract␈α∃structure␈α⊗of␈α∃a␈α∃stack␈α∃does␈α⊗not␈α∃involve
␈↓ α←␈↓␈↓ ∧πlimitations␈α⊗on␈α⊗the␈α⊗length␈α⊗of␈α⊗stack-space,␈α⊗any␈α∃representation
␈↓ α←␈↓␈↓ ∧πshould␈α⊃include␈α⊃techniques␈α⊃for␈α⊂assuring␈α⊃that␈α⊃the␈α⊃stack␈α⊂pointer
␈↓ α←␈↓␈↓ ∧πstays within its allotted space. See the preceding footnote.
␈↓"∀␈↓ α←␈↓␈↓ β'Notice␈αthat␈αthe␈α␈↓αconcat␈↓␈αoperation␈αcan␈αbe␈αinterpreted␈αas␈α␈↓¬pushing␈↓␈αand␈αthe
␈↓ α←␈↓␈↓αrest␈↓␈αoperation␈αas␈α␈↓¬popping␈↓.␈αIndeed␈αour␈αearlier␈αmanipulations␈αof␈αsymbol␈αtables
␈↓ α←␈↓effectively␈α∂used␈α∂such␈α∂stack␈α∂operations.␈α∂This␈α∂is␈α∂particularly␈α∂apparent␈α∂in␈α∂the
␈↓ α←␈↓representation of symbol tables given on page 124.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 34␈↓Stacks␈α
are␈α
closely␈α
related␈α
to␈α
a␈α
theoretical␈α
device␈α
called␈α∞a␈α
push-down
␈↓ α←␈↓automaton.␈αThere,␈α
only␈αthe␈αtop␈α
element␈αof␈α
the␈αstack␈αis␈α
accessible.␈αWe␈α
take␈αa
␈↓ α←␈↓more␈α∂pragmatic␈α∂position,␈α⊂allowing␈α∂access␈α∂to␈α⊂elements␈α∂within␈α∂the␈α⊂stack,␈α∂and
␈↓ α←␈↓indeed␈α∂modification␈α∂of␈α⊂elements␈α∂within␈α∂the␈α⊂stack;␈α∂but␈α∂removal␈α⊂of␈α∂elements
␈↓ α←␈↓from␈α∞within␈α∞the␈α∞stack␈α∞is␈α∞not␈α∞allowed.␈α∞To␈α∞remove␈α∞an␈α∞element,␈α∞we␈α∞must␈α∞first
␈↓ α←␈↓remove the elements above it.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 35␈↓The␈α
typical␈α∞model␈α
for␈α
a␈α∞stack␈α
is␈α
a␈α∞contiguous␈α
block␈α
of␈α∞memory␈α
but
␈↓ α←␈↓that␈αignores␈αthe␈αquestion␈αof␈αexceeding␈αthe␈αbounds␈αof␈αthat␈αmemory␈αallocation.
␈↓ α←␈↓A␈αstack␈αcan␈αbe␈αimplemented␈αin␈αa␈αdiscontinuous␈αfashion␈α([Bis 74])␈αas␈αlong␈αas
␈↓ α←␈↓the␈α∞stack␈α
manipulating␈α∞functions␈α∞are␈α
able␈α∞to␈α∞cope␈α
with␈α∞such␈α∞behavior.␈α
The
␈↓ α←␈↓degenerate case of such discontinuous stacks is a linked allocation scheme.
␈↓ α←␈↓␈↓292 Static Structure␈↓
_5.17␈↓
␈↓"β␈↓ α←␈↓␈↓ β'We␈α⊂will␈α∂discuss␈α⊂the␈α∂binding␈α⊂process␈α∂in␈α⊂terms␈α∂of␈α⊂a␈α∂sequence␈α⊂of␈α∂three
␈↓ α←␈↓events:
␈↓"∀␈↓ α←␈↓␈↓↓1.␈↓ ␈↓αbind␈↓␈αdescribes␈αwhat␈αthe␈αimplementation␈α
does␈αwhen␈αwe␈αare␈αready␈αto␈α
call␈αa
␈↓ α←␈↓␈↓ β'procedure.␈α∂The␈α∂actual␈α∂parameters␈α∂are␈α∂evaluated␈α∂and␈α∂we␈α∂are␈α∂ready␈α∞to
␈↓ α←␈↓␈↓ β'add them to the environment and evaluate the body of the procedure.
␈↓"∀␈↓ α←␈↓␈↓↓2.␈↓ ␈↓αlookup␈↓ will determine how values are located in the current environment.
␈↓"∀␈↓ α←␈↓␈↓↓3.␈↓ ␈↓αunbind␈↓␈α∞will␈α∞describe␈α∞what␈α∞has␈α∞to␈α∂be␈α∞done␈α∞as␈α∞we␈α∞prepare␈α∞to␈α∞exit␈α∂from␈α∞a
␈↓ α←␈↓␈↓ β'procedure.
␈↓"∀␈↓ α←␈↓␈↓ βN␈↓↓5.18 Stack Implementation of a LISP subset: Deep Bound␈↓
␈↓"β␈↓ α←␈↓The␈αstack␈αimplementation␈αof␈αsimple␈αfunction␈αapplication␈αis␈αa␈α
straightforward
␈↓ α←␈↓implementation␈α⊂of␈α⊂our␈α∂blocks␈α⊂picture.␈α⊂ We␈α∂have␈α⊂two␈α⊂stacks␈α⊂which␈α∂operate
␈↓ α←␈↓synchronously.␈α∂One␈α∂stack␈α∂is␈α∂called␈α∂the␈α∂␈↓↓name␈α∂stack␈↓;␈α∂the␈α∂other␈α∂is␈α⊂called␈α∂the
␈↓ α←␈↓␈↓↓value␈α∀stack␈↓.␈α∀ The␈α∀name␈α∪stack␈α∀maintains␈α∀the␈α∀λ-variables␈α∀(and␈α∪generated
␈↓ α←␈↓names,␈αif␈αa␈αprimitive␈αis␈αcalled).␈αThe␈αtop␈αof␈αthe␈αname␈αstack␈αdefines␈αthe␈αorigin
␈↓ α←␈↓of␈α∪the␈α∪environment.␈α∪When␈α∪the␈α∀value␈α∪of␈α∪a␈α∪variable␈α∪is␈α∀requested␈α∪␈↓αlookup␈↓
␈↓ α←␈↓proceeds␈α∀down␈α∃the␈α∀name␈α∀stack,␈α∃looking␈α∀for␈α∀the␈α∃first␈α∀occurrence␈α∃of␈α∀the
␈↓ α←␈↓variable.␈αThe␈αcorresponding␈αposition␈α
in␈αthe␈αvalue␈αstack␈αcontains␈α
the␈αdesired
␈↓ α←␈↓value.
␈↓"β␈↓ α←␈↓␈↓ β'When␈α
we␈α
recognize␈αa␈α
function␈α
application,␈αwe␈α
begin␈α
the␈α
evaluation␈αof
␈↓ α←␈↓the␈α∞actual␈α
parameters.␈α∞As␈α
each␈α∞parameter␈α
is␈α∞evaluated,␈α
the␈α∞result␈α∞is␈α
pushed
␈↓ α←␈↓into␈α
the␈α∞value␈α
stack.␈α∞When␈α
all␈α
the␈α∞parameters␈α
have␈α∞been␈α
evaluated,␈α∞we␈α
are
␈↓ α←␈↓ready␈α
to␈αevaluate␈α
the␈α
body␈αof␈α
the␈αexpression.␈α
At␈α
that␈αpoint␈α
␈↓αbind␈↓␈α
pushes␈αthe
␈↓ α←␈↓λ-variables␈α∞onto␈α∞the␈α∂name␈α∞stack.␈α∞ When␈α∞we␈α∂complete␈α∞the␈α∞evaluation␈α∂of␈α∞the
␈↓ α←␈↓body␈α
of␈αthe␈α
expression␈α␈↓αunbind␈↓␈α
pops␈α
the␈αλ-variables␈α
from␈αthe␈α
name␈αstack,␈α
and
␈↓ α←␈↓pops their values from the value stack.
␈↓"β␈↓ α←␈↓␈↓ β'Since␈α∂the␈α∞λ-variables␈α∂are␈α∞removed␈α∂from␈α∂the␈α∞stack␈α∂as␈α∞a␈α∂group␈α∂we␈α∞can
␈↓ α←␈↓sometimes␈α∞speed␈α∞up␈α∞the␈α∞operation␈α∞by␈α∞storing␈α∞block␈α∞sizes␈α∞in␈α∞the␈α∂stack.␈α∞Also,
␈↓ α←␈↓the␈αword␈αsize␈αof␈αthe␈αmachine␈αmay␈αallow␈αusing␈αone␈αstack␈αfor␈αboth␈αnames␈αand
␈↓ α←␈↓values. For example:
␈↓"⊃␈↓ α←␈↓
⊂ααααααααααααα⊃
␈↓"␈↓ α←␈↓
~ #ααα→ααβ→⊃
␈↓"␈↓ α←␈↓
εαααααααπαααααλ ↓
␈↓"␈↓ α←␈↓
~ namen ~ valn~ ~
␈↓"␈↓ α←␈↓
εαααααααβαααααλ ~
␈↓"␈↓ α←␈↓
# # # ↓
␈↓"␈↓ α←␈↓
~ name2 ~ val2~ ~
␈↓"␈↓ α←␈↓
εαααααααβαααααλ ~
␈↓"␈↓ α←␈↓
~ name1 ~ val1~ ↓
␈↓"␈↓ α←␈↓
εααααααα∀αααααλ ~
␈↓"␈↓ α←␈↓
~ ~←$
␈↓"␈↓ α←␈↓
~ #ααα→ααβ→⊃
␈↓"␈↓ α←␈↓
↓
␈↓"∀␈↓ α←␈↓where␈α⊃␈↓
val1␈↓␈α⊂is␈α⊃furthest␈α⊂down␈α⊃the␈α⊂stack␈α⊃since␈α⊂it␈α⊃was␈α⊂pushed␈α⊃on␈α⊃first,␈α⊂and
␈↓ α←␈↓␈↓5.18␈↓ ¬Stack Implementation of a LISP subset: Deep Bound 293␈↓
␈↓"β␈↓ α←␈↓where␈α∂all␈α∂references␈α⊂to␈α∂␈↓
namei␈↓␈α∂and␈α∂␈↓
vali␈↓␈α⊂are␈α∂really␈α∂pointers␈α⊂to␈α∂appropriate
␈↓ α←␈↓S-expressions␈α∃(atoms␈α∃or␈α⊗dotted␈α∃pairs).␈α∃ The␈α⊗"back␈α∃pointer"␈α∃is␈α⊗used␈α∃for
␈↓ α←␈↓removing␈α
blocks␈α
of␈α
bindings,␈α
but␈α
it␈αis␈α
also␈α
a␈α
representation␈α
of␈α
the␈αcontrol␈α
link
␈↓ α←␈↓discussed in Section 3.8.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α≤slight␈α≥modification␈α≤of␈α≤this␈α≥scheme␈α≤is␈α≤sufficient␈α≥to␈α≤support
␈↓ α←␈↓implementation␈αof␈α
functional␈αarguments.␈αAn␈α
additional␈αpiece␈α
of␈αinformation
␈↓ α←␈↓is␈α
added␈αto␈α
guide␈α
␈↓αlookup␈↓␈αin␈α
its␈α
search␈αfor␈α
the␈α
next␈αblock␈α
of␈αbindings.␈α
Instead
␈↓ α←␈↓of␈α⊂proceeding␈α⊂linearly␈α⊂down␈α⊂the␈α∂stack,␈α⊂␈↓αlookup␈↓␈α⊂proceeds␈α⊂linearly␈α⊂through␈α∂a
␈↓ α←␈↓block␈α
and␈α∞at␈α
the␈α∞end␈α
of␈α∞each␈α
block␈α∞is␈α
information␈α∞telling␈α
␈↓αlookup␈↓␈α∞where␈α
the
␈↓ α←␈↓next␈α
block␈α∞of␈α
bindings␈α
is␈α∞to␈α
be␈α∞found.␈α
Recall␈α
that␈α∞information␈α
is␈α∞called␈α
the
␈↓ α←␈↓access link (Section 3.8).
␈↓"⊃␈↓ α←␈↓
⊂ααααααααααααα⊃
␈↓"␈↓ α←␈↓
⊂←β←αα# #αα→β→⊃
␈↓"␈↓ α←␈↓
↓ εαααααααπαααααλ ↓
␈↓"␈↓ α←␈↓
~ ~ namen ~ valn~ ~
␈↓"␈↓ α←␈↓
~ εαααααααβαααααλ ~
␈↓"␈↓ α←␈↓
access links ↓ # # # ↓ control links
␈↓"␈↓ α←␈↓
~ ~ name2 ~ val2~ ~
␈↓"␈↓ α←␈↓
~ εαααααααβαααααλ ~
␈↓"␈↓ α←␈↓
↓ ~ name1 ~ val1~ ↓
␈↓"␈↓ α←␈↓
~ εααααααα∀αααααλ ~
␈↓"␈↓ α←␈↓
%→~ ~←$
␈↓"␈↓ α←␈↓
⊂←β←αα# #αα→β→⊃
␈↓"␈↓ α←␈↓
↓ ↓
␈↓"∀␈↓ α←␈↓␈↓ β'In␈α⊂simple␈α⊂function␈α∂application,␈α⊂the␈α⊂access␈α∂links␈α⊂and␈α⊂control␈α⊂links␈α∂are
␈↓ α←␈↓identical.␈α∞ The␈α∞evaluation␈α∞of␈α∂a␈α∞functional␈α∞argument␈α∞will␈α∞generate␈α∂an␈α∞access
␈↓ α←␈↓link,␈α↔pointing␈α↔to␈α↔the␈α↔current␈α↔stack.␈α↔ That␈α↔is,␈α↔the␈α↔␈↓αfunction␈↓-construct␈α↔is
␈↓ α←␈↓responsible␈α∞for␈α∞saving␈α∞the␈α∞binding␈α∞environment␈α∞by␈α∞saving␈α∞a␈α∞pointer␈α∞to␈α
the
␈↓ α←␈↓current␈α∂top␈α∂of␈α∞the␈α∂name␈α∂and␈α∞value␈α∂stack.␈α∂ When␈α∞a␈α∂functional␈α∂argument␈α∞is
␈↓ α←␈↓applied,␈α∀the␈α∀access␈α∀link␈α∀will␈α∀be␈α∀set␈α∀to␈α∀that␈α∀saved␈α∃binding␈α∀environment
␈↓ α←␈↓(page 146).␈α Since␈αwe␈αare␈αrestricting␈αattention␈αto␈αfunctional␈αarguments,␈αwe␈α
are
␈↓ α←␈↓assured␈α
that␈αthe␈α
application␈αof␈α
that␈αargument␈α
will␈αoccur␈α
within␈αan␈α
expression
␈↓ α←␈↓which␈α
dynamically␈α∞surrounds␈α
the␈α∞creation␈α
of␈α∞the␈α
functional␈α∞argument.␈α
This
␈↓ α←␈↓means␈αthat␈αour␈αenvironment␈αpointer␈αwill␈α
indeed␈αbe␈αa␈αpointer␈αdown␈αthe␈α
stack.
␈↓ α←␈↓The␈αuse␈αof␈αfunctional␈αvalues␈αinvalidates␈αthat␈αassumption.␈α Before␈αexamining
␈↓ α←␈↓that problem we will discuss shallow binding for these same LISP subsets.
␈↓"β␈↓ α←␈↓␈↓ β:␈↓↓5.19 Stack Implementation of a LISP Subset: Shallow Bound␈↓
␈↓"β␈↓ α←␈↓A␈α∂stack␈α∂implementation␈α∂of␈α∞shallow␈α∂binding␈α∂allows␈α∂some␈α∂elegant␈α∞economies.
␈↓ α←␈↓We␈α_can␈α_use␈α_a␈α_stack␈α_implementation␈α_for␈α_the␈α_first␈α_shallow␈α→binder␈α_of
␈↓ α←␈↓Section 3.11.␈α∩ There␈α∩we␈α∩had␈α∩a␈α∩collection␈α∩of␈α∩bindings␈α∩associated␈α∪with␈α∩the
␈↓ α←␈↓identifier.␈α
Without␈α
loss␈α∞of␈α
generality,␈α
we␈α
can␈α∞organize␈α
␈↓αmkenv␈↓␈α
such␈α∞that␈α
the
␈↓ α←␈↓new␈αbinding␈αis␈αadded␈αin␈αfront␈αof␈αany␈αprevious␈αbinding.␈α Now␈αif␈αwe␈αare␈αonly
␈↓ α←␈↓evaluating␈αsimple␈αfunction␈αapplications,␈α␈↓αlookup␈↓␈αwill␈αfind␈αthe␈αdesired␈αbinding
␈↓ α←␈↓␈↓294 Static Structure␈↓
_5.19␈↓
␈↓"β␈↓ α←␈↓provided␈α∞the␈α∞unbinding␈α∞operation␈α∞removes␈α∞the␈α∞first␈α∞binding␈α∞as␈α∞it␈α∞exits␈α
the
␈↓ α←␈↓procedure.␈α In␈αthis␈αway,␈αbinding␈αand␈αunbinding␈αact␈αon␈αthe␈αvalue␈αentries␈αin␈αa
␈↓ α←␈↓stack-like␈αfashion.␈α Instead␈αof␈αassociating␈αa␈αseparate␈αstack␈αwith␈αeach␈αvariable
␈↓ α←␈↓and␈α∞accessing␈α∞the␈α∞value␈α∞through␈α∞the␈α∞top␈α∞of␈α∞the␈α∞stack,␈α∞we␈α∞associate␈α∞a␈α∞single
␈↓ α←␈↓value␈αcell␈αwith␈αeach␈αvariable␈αand␈αstore␈α
the␈αsaved␈αvalues␈αof␈αall␈αvariables␈αon␈α
a
␈↓ α←␈↓common␈αstack.␈α
The␈α"shallowest"␈αof␈α
the␈αshallow␈αschemes,␈α
which␈αonly␈αused␈α
the
␈↓ α←␈↓value cell, can also be extended to handle functional arguments.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αimplementation␈αof␈α␈↓αlookup␈↓␈αwill␈αbe␈αsimple␈αfor␈αeither␈αshallow␈αscheme:
␈↓ α←␈↓take␈α
the␈α∞value␈α
in␈α∞the␈α
value␈α∞cell.␈α
Since␈α
the␈α∞value␈α
cell␈α∞will␈α
be␈α∞maintained␈α
to
␈↓ α←␈↓␈↓¬always␈↓␈α
contain␈αthe␈α
proper␈αbinding␈α
of␈αa␈α
variable,␈αthe␈α
distinction␈αbetween␈α
local
␈↓ α←␈↓and␈αnon-local␈α
variables␈αvanishes.␈α
The␈αcontents␈α
of␈αthe␈α
value␈αcell␈α
is␈α␈↓¬the␈↓␈α
current
␈↓ α←␈↓value for any variable.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α⊃first␈α⊃review␈α⊃the␈α⊃process␈α⊃of␈α⊃shallow␈α⊃binding␈α⊃with␈α⊃the␈α⊃value␈α⊃cell,
␈↓ α←␈↓including␈α∃the␈α∃details␈α∃we␈α∃added␈α∀in␈α∃Section 4.6.␈α∃When␈α∃an␈α∃application␈α∀is
␈↓ α←␈↓recognized␈αwe␈αallocate␈αa␈αblock␈αto␈αcontain␈αthe␈αvalues␈αof␈αthe␈αactual␈αparameters;
␈↓ α←␈↓that␈αis␈αthe␈α␈↓αdest␈↓␈αblock.␈αAs␈αthe␈αarguments␈αare␈αevaluated,␈αthe␈αresults␈αare␈αsent␈αto
␈↓ α←␈↓the␈α∀appropriate␈α∪slots␈α∀in␈α∀the␈α∪␈↓αdest␈↓␈α∀block.␈↓π 36␈↓␈α∀When␈α∪all␈α∀the␈α∀arguments␈α∪are
␈↓ α←␈↓evaluated,␈α
we␈α
link␈α
the␈α∞␈↓αdest␈↓␈α
block␈α
onto␈α
the␈α∞front␈α
of␈α
the␈α
environment,␈α∞but␈α
as
␈↓ α←␈↓we␈αdo␈αthat␈αwe␈α
␈↓¬swap␈↓␈αthe␈αcurrent␈αcontents␈αof␈α
the␈αaffected␈αvalue␈αcells␈α
with␈αthe
␈↓ α←␈↓new␈α
values.␈α∞This␈α
establishes␈α∞the␈α
new␈α
values␈α∞in␈α
the␈α∞value␈α
cells␈α∞while␈α
saving
␈↓ α←␈↓the␈α↔prior␈α↔bindings.␈α↔We␈α↔are␈α↔now␈α↔ready␈α↔to␈α↔evaluate␈α↔the␈α↔body␈α_of␈α↔the
␈↓ α←␈↓application.␈α
When␈α
evaluation␈α
is␈α
completed␈α
we␈α
swap␈α
␈↓¬back␈↓,␈α
using␈α
the␈α
first␈α
block
␈↓ α←␈↓of␈α∞the␈α∞saved␈α∂environment␈α∞chain;␈α∞then␈α∞we␈α∂remove␈α∞that␈α∞first␈α∞block␈α∂from␈α∞the
␈↓ α←␈↓chain.␈αSince␈αwe␈αare␈αassuming␈αa␈αsimple␈αfunction␈αcall,␈αthat␈αold␈α␈↓αdest␈↓␈αblock␈αis␈αno
␈↓ α←␈↓longer␈α⊃accessible␈α⊂and␈α⊃can␈α⊂be␈α⊃collected.␈↓π 37␈↓␈α⊂The␈α⊃allocation␈α⊃and␈α⊂de-allocation
␈↓ α←␈↓process␈αis␈αstack-like;␈αwe␈αwill␈αdevelop␈αour␈αimplementation␈αusing␈αa␈αstack␈αcalled
␈↓ α←␈↓the␈αSpecial␈αPushdown␈α
stack.␈α This␈αstack␈α
will␈αbe␈αreferenced␈α
by␈αa␈αstack␈α
pointer
␈↓ α←␈↓called␈α∞␈↓
SP␈↓␈α∞or␈α∂called␈α∞␈↓
ENV␈↓␈α∞when␈α∂we␈α∞wish␈α∞to␈α∞reinforce␈α∂its␈α∞relation␈α∞to␈α∂the␈α∞more
␈↓ α←␈↓general environment structures.
␈↓"β␈↓ α←␈↓␈↓ β'In␈α↔the␈α⊗spirit␈α↔of␈α⊗the␈α↔evaluator␈α⊗of␈α↔Section 4.6,␈α⊗we␈α↔would␈α⊗evaluate
␈↓ α←␈↓␈↓αλ[[x;y] ␈↓λx␈↓α][C;D]␈↓ as follows:
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ Allocate␈α
space␈α
for␈α
the␈αparameters␈α
␈↓αx␈↓␈α
and␈α
␈↓αy␈↓.␈αThis␈α
space␈α
is␈α
reserved␈α
on␈αthe
␈↓ α←␈↓␈↓ β∂top of the ␈↓
ENV␈↓ stack, and is referenced by a pointer named ␈↓
DEST␈↓.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ Evaluate the actual parameters and send the results to the ␈↓αdest␈↓ block.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ Swap␈αthe␈α
contents␈αof␈αthe␈α
value␈αcells␈αfor␈α
␈↓αx␈↓␈αand␈α␈↓αy␈↓␈α
with␈αthe␈αcontents␈α
of␈αthe
␈↓ α←␈↓␈↓ β∂␈↓αdest␈↓ block. Move ␈↓
ENV␈↓ to point to the ␈↓αdest␈↓ block.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 36␈↓This scheme will also work with multi-valued functions.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 37␈↓Since␈α⊂the␈α⊂␈↓αdest␈↓␈α⊂block␈α⊂is␈α⊂no␈α⊂longer␈α⊂accessible,␈α⊂it␈α⊂was␈α⊂unnecessary␈α⊂to
␈↓ α←␈↓␈↓¬swap␈↓␈α⊂back;␈α⊃we␈α⊂could␈α⊃have␈α⊂simply␈α⊂␈↓¬restored␈↓␈α⊃the␈α⊂value␈α⊃cells.␈α⊂The␈α⊃swap␈α⊂was
␈↓ α←␈↓described in preparation for more general implementations.
␈↓ α←␈↓␈↓5.19␈↓ ∧`Stack Implementation of a LISP Subset: Shallow Bound 295␈↓
␈↓" ␈↓ α←␈↓␈↓↓4.␈↓ Evaluate␈α∂␈↓λx␈↓.␈α∂Within␈α∂␈↓λx␈↓,␈α∂␈↓αlookup␈↓␈α∂will␈α∂go␈α∂to␈α∂the␈α∂value␈α∂cells␈α∂for␈α⊂all␈α∂variable
␈↓ α←␈↓␈↓ β∂references.
␈↓" ␈↓ α←␈↓␈↓↓5.␈↓ Restore␈αthe␈α
old␈αenvironment.␈α
Swap␈αthe␈αcontents␈α
of␈αthe␈α
first␈αblock␈α
of␈α␈↓
ENV␈↓
␈↓ α←␈↓␈↓ β∂with the contents of the appropriate value cells.
␈↓" ␈↓ α←␈↓␈↓↓6.␈↓ Set ␈↓
ENV␈↓ to point at the prior block.
␈↓" ␈↓ α←␈↓To␈α∞reinforce␈α∞these␈α∞notions␈α∂we␈α∞supply␈α∞a␈α∞more␈α∞detailed␈α∂implementation.␈α∞ We
␈↓ α←␈↓will␈αimplement␈α
our␈αvalue␈α
cells␈αas␈α
elements␈αof␈α
the␈αproperty␈α
lists.␈α The␈α
property
␈↓ α←␈↓name␈α
will␈α
be␈α
␈↓αVALUE␈↓,␈α
and␈α
the␈α
corresponding␈α
property␈α
value␈α
will␈α
be␈α
the␈α
value
␈↓ α←␈↓cell.␈α⊃ Assume␈α⊃␈↓αx␈↓␈α⊃and␈α⊃␈↓αy␈↓␈α⊃are␈α⊃currently␈α⊃bound␈α⊃to␈α⊃␈↓αA␈↓␈α⊃and␈α⊃␈↓αB␈↓␈α⊃respectively;␈α⊃and
␈↓ α←␈↓assume we wish to evaluate:
␈↓" ␈↓ α←␈↓␈↓ ¬y␈↓αλ[[x;y] ␈↓λx␈↓α][C;D]␈↓
␈↓"β␈↓ α←␈↓We assume ␈↓
ENV␈↓ is in some well-defined state:
␈↓"⊃␈↓ α←␈↓
part of atom for X part of atom for Y
␈↓"␈↓ α←␈↓
⊂αααααααπααα⊃ ⊂αααπαα ⊂αααααααπααα⊃ ⊂αααπαα
␈↓"␈↓ α←␈↓
~ VALUE ~ #αβ→~ A ~ #→... ~ VALUE ~ #αβ→~ B ~ #→...
␈↓"␈↓ α←␈↓
%ααααααα∀ααα$ %ααα∀αα %ααααααα∀ααα$ %ααα∀αα
␈↓"⊃␈↓ α←␈↓
ENV
␈↓"␈↓ α←␈↓
⊂αααα⊃ εααααααααααααλ
␈↓"␈↓ α←␈↓
~ #αβα→~*MARK* #ααβ→⊃
␈↓"␈↓ α←␈↓
%αααα$ εααααααααααααλ ↓
␈↓"␈↓ α←␈↓
~ last entry ~ ~
␈↓"␈↓ α←␈↓
εααααααααααααλ ~
␈↓"␈↓ α←␈↓
...
␈↓"␈↓ α←␈↓
↓
␈↓"␈↓ α←␈↓
εααααααααααααλ←$
␈↓"␈↓ α←␈↓
~*MARK* #ααβ→⊃
␈↓"␈↓ α←␈↓
εααααααααααααλ ↓
␈↓"␈↓ α←␈↓
...
␈↓"∀␈↓ α←␈↓In␈α∩this␈α∩implementation,␈α∩the␈α∩stack␈α∩is␈α∩organized␈α∩in␈α∩blocks.␈α∪ The␈α∩allocation
␈↓ α←␈↓operation␈αclaims␈αspace␈αfrom␈α
the␈αtop␈αof␈αthe␈αstack␈α
and␈αputs␈αa␈αspecial␈α
mark␈αin
␈↓ α←␈↓the␈α∞top␈α∂of␈α∞the␈α∞␈↓
ENV␈↓␈α∂stack␈α∞to␈α∂delimit␈α∞the␈α∞block␈α∂of␈α∞λ-rebindings;␈α∂that␈α∞marked
␈↓ α←␈↓entry␈αwill␈α
also␈αcontain␈α
the␈α␈↓αdest␈↓␈α
pointer.␈α The␈α
implementation␈αwill␈αindicate␈α
the
␈↓ α←␈↓new␈α⊗block␈α∃by␈α⊗pointing␈α∃to␈α⊗it␈α⊗by␈α∃␈↓
DEST␈↓.␈α⊗ The␈α∃allocation␈α⊗routine␈α⊗is␈α∃also
␈↓ α←␈↓responsible␈α
for␈α∞filling␈α
the␈α∞name-entries␈α
of␈α∞the␈α
␈↓αdest␈↓␈α∞block.␈α
Those␈α∞entries␈α
will
␈↓ α←␈↓be␈α
represented␈α
as␈αpointers␈α
to␈α
the␈α
value␈αcell␈α
entry␈α
on␈αthe␈α
property␈α
list␈α
of␈αthe
␈↓ α←␈↓atom.
␈↓ α←␈↓␈↓296 Static Structure␈↓
_5.19␈↓
␈↓"⊃␈↓ α←␈↓
part of atom for X part of atom for Y
␈↓"␈↓ α←␈↓
⊂αααααααπααα⊃ ⊂αααπαα ⊂αααααααπααα⊃ ⊂αααπαα
␈↓"␈↓ α←␈↓
~ VALUE ~ #αβ→~ A ~ #→... ~ VALUE ~ #αβ→~ B ~ #→...
␈↓"␈↓ α←␈↓
%ααααααα∀ααα$ %ααα∀αα %ααααααα∀ααα$ %ααα∀αα
␈↓"␈↓ α←␈↓
↑
␈↓"␈↓ α←␈↓
DEST ⊂→αααα→αααα$
␈↓"␈↓ α←␈↓
⊂αααα⊃↑ ⊂αααααααααααα⊃
␈↓"␈↓ α←␈↓
~ #αβα→~*MARK* #ααβ→⊃
␈↓"␈↓ α←␈↓
%αααα$↑ εαααααααπααααλ ~
␈↓"␈↓ α←␈↓
%←β←α# ~ ~←$
␈↓"␈↓ α←␈↓
εαααααααβααααλ
␈↓"␈↓ α←␈↓
ENV ~←to Y←#~ ~
␈↓"␈↓ α←␈↓
⊂αααα⊃ εααααααα∀ααααλ
␈↓"␈↓ α←␈↓
~ #αβα→~*MARK* #ααβ→⊃
␈↓"␈↓ α←␈↓
%αααα$ εααααααααααααλ ↓
␈↓"␈↓ α←␈↓
~ last entry ~ ~
␈↓"␈↓ α←␈↓
εααααααααααααλ ~
␈↓"␈↓ α←␈↓
...
␈↓"␈↓ α←␈↓
↓
␈↓"␈↓ α←␈↓
εααααααααααααλ←$
␈↓"␈↓ α←␈↓
~*MARK* #ααβ→⊃
␈↓"␈↓ α←␈↓
εααααααααααααλ ↓
␈↓"␈↓ α←␈↓
...
␈↓"∀␈↓ α←␈↓The␈α␈↓αsend␈↓␈αoperation␈αwill␈αfill␈αthe␈α␈↓αdest␈↓␈αentries␈αfor␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓.␈α The␈α␈↓αnext␈↓␈αoperation
␈↓ α←␈↓will␈α
increment␈αthe␈α
␈↓αdest␈↓␈αpointer␈α
so␈αthat␈α
after␈α
all␈αentries␈α
have␈αbeen␈α
made␈αto␈α
the
␈↓ α←␈↓␈↓αdest␈↓ block, the ␈↓αdest␈↓ pointer will indicate the next block of saved bindings.
␈↓"β␈↓ α←␈↓␈↓ β'With␈α
␈↓αx␈↓␈α
and␈α
␈↓αy␈↓␈α
bound␈α
to␈α
␈↓αC␈↓␈α
and␈α
␈↓αD␈↓␈α
in␈α
the␈α
␈↓αdest␈↓␈α
block,␈α
we␈α
are␈α
ready␈αto␈α
swap
␈↓ α←␈↓the value cells. After the swap the picture is:
␈↓"⊃␈↓ α←␈↓
part of atom for X part of atom for Y
␈↓"␈↓ α←␈↓
⊂αααααααπααα⊃ ⊂αααπαα ⊂αααααααπααα⊃ ⊂αααπαα
␈↓"␈↓ α←␈↓
~ VALUE ~ #αβ→~ C ~ #→... ~ VALUE ~ #αβ→~ D ~ #→...
␈↓"␈↓ α←␈↓
%ααααααα∀ααα$ %ααα∀αα %ααααααα∀ααα$ %ααα∀αα
␈↓"␈↓ α←␈↓
↑
␈↓"␈↓ α←␈↓
ENV ⊂αααα→αααα$
␈↓"␈↓ α←␈↓
⊂αααα⊃↑ ⊂αααααααααααα⊃
␈↓"␈↓ α←␈↓
~ #αβα→~*MARK* #ααβ→⊃
␈↓"␈↓ α←␈↓
%αααα$↑ εαααααααπααααλ ~
␈↓"␈↓ α←␈↓
%←β←α# ~ A ~ ~
␈↓"␈↓ α←␈↓
εαααααααβααααλ ↓
␈↓"␈↓ α←␈↓
~←to Y←#~ B ~←$
␈↓"␈↓ α←␈↓
εααααααα∀ααααλ
␈↓"␈↓ α←␈↓
~*MARK* #ααβ→⊃
␈↓"␈↓ α←␈↓
εααααααααααααλ ↓
␈↓"␈↓ α←␈↓
~ last entry ~ ~
␈↓"␈↓ α←␈↓
εααααααααααααλ ~
␈↓"␈↓ α←␈↓
...
␈↓"␈↓ α←␈↓
↓
␈↓"␈↓ α←␈↓
εααααααααααααλ←$
␈↓"␈↓ α←␈↓
~*MARK* #ααβ→⊃
␈↓"␈↓ α←␈↓
εααααααααααααλ ↓
␈↓"␈↓ α←␈↓
...
␈↓ α←␈↓␈↓5.19␈↓ ∧`Stack Implementation of a LISP Subset: Shallow Bound 297␈↓
␈↓"β␈↓ α←␈↓Now␈α∞␈↓αx␈↓␈α
and␈α∞␈↓αy␈↓␈α
have␈α∞values␈α
␈↓αC␈↓␈α∞and␈α
␈↓αD␈↓␈α∞respectively␈α
and␈α∞the␈α∞previous␈α
bindings
␈↓ α←␈↓are␈αsaved␈α
on␈αthe␈α
␈↓
ENV␈↓␈αstack.␈α
We␈αmay␈αnow␈α
begin␈αthe␈α
evaluation␈αof␈α
␈↓λx␈↓␈αassured
␈↓ α←␈↓that␈α∩we␈α∩will␈α⊃get␈α∩the␈α∩expected␈α∩values␈α⊃for␈α∩␈↓αx␈↓␈α∩and␈α⊃␈↓αy␈↓.␈α∩ We␈α∩have␈α∩also␈α⊃saved
␈↓ α←␈↓sufficient␈α
information␈α
to␈α
restore␈α
the␈α
previous␈α
values␈α
afterwards.␈α
Since␈αwe␈α
are
␈↓ α←␈↓assuming␈α∀simple␈α∀function␈α∀composition,␈α∪the␈α∀unbind␈α∀operation␈α∀can␈α∪simply
␈↓ α←␈↓"pop"␈α
entries␈αoff␈α
of␈αthe␈α
top␈αof␈α
␈↓
ENV␈↓␈αinto␈α
the␈αvalue␈α
cells␈αrather␈α
than␈αswap␈α
them
␈↓ α←␈↓with the value cells.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
unbinder␈αrestores␈α
the␈αfirst␈α
block␈αof␈α
saved␈αvalues␈α
using␈αthe␈α
pointers
␈↓ α←␈↓to␈α∂the␈α⊂value␈α∂cells␈α∂as␈α⊂destinations␈α∂for␈α∂the␈α⊂values.␈↓π 38␈↓␈α∂This␈α∂stack␈α⊂of␈α∂previous
␈↓ α←␈↓values␈αis␈αalso␈α
visited␈αby␈αthe␈α
garbage␈αcollector;␈αit␈α
may␈αbe␈αthat␈α
the␈αonly␈αcopy␈α
of
␈↓ α←␈↓some␈α∪value␈α∀is␈α∪accessible␈α∪only␈α∀through␈α∪the␈α∪␈↓
ENV␈↓␈α∀stack.␈α∪It␈α∪would␈α∀be␈α∪most
␈↓ α←␈↓unfortunate␈α⊂if␈α⊂the␈α⊂garbage␈α⊂collector␈α⊂neglected␈α⊂to␈α⊂mark␈α⊂that␈α⊂entry␈α⊃and␈α⊂the
␈↓ α←␈↓unbinding mechanism later tried to restore the value.
␈↓"β␈↓ α←␈↓␈↓ β'This␈αimplementation␈αworks␈αquite␈α
well␈αfor␈αsimple␈αλ-binding␈αand␈α
lookup.
␈↓ α←␈↓Changing␈α⊂environments␈α⊂is␈α⊃a␈α⊂bit␈α⊂of␈α⊃work,␈α⊂but␈α⊂the␈α⊃access␈α⊂to␈α⊂the␈α⊃values␈α⊂of
␈↓ α←␈↓variables␈α
is␈α
relatively␈α
rapid,␈α
particularly␈α
if␈αwe␈α
make␈α
sure␈α
that␈α
the␈α
value␈αcell␈α
is
␈↓ α←␈↓always␈α
stored␈α
at␈α
a␈α
known␈α
position␈α
relative␈α
to␈α
the␈α
beginning␈α
of␈α
the␈α
property
␈↓ α←␈↓list.␈α⊂ In␈α⊃describing␈α⊂this␈α⊃implementation␈α⊂we␈α⊃have␈α⊂used␈α⊃more␈α⊂representation
␈↓ α←␈↓than␈α∞might␈α∂seem␈α∞appropriate.␈α∂In␈α∞particular␈α∞the␈α∂representation␈α∞of␈α∂the␈α∞value
␈↓ α←␈↓cell␈αas␈αa␈αlinked␈αlist␈αseems␈αunnecessarily␈αexplicit.␈αThis␈αwas␈αdone␈α
to␈αilluminate
␈↓ α←␈↓the pointer modifications involved in binding and unbinding.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∀would␈α∀like␈α∀to␈α∪implement␈α∀functional␈α∀arguments␈α∀in␈α∀this␈α∪shallow
␈↓ α←␈↓binder.␈α
Recall␈αhow␈α
deep␈α
binding␈αcoped.␈α
When␈α
we␈αrecognized␈α
an␈αinstance␈α
of
␈↓ α←␈↓a␈αfunctional␈αargument␈αwe␈αsaved␈αa␈αpointer␈αto␈αthe␈αcurrent␈αenvironment.␈αWhen
␈↓ α←␈↓we␈α
␈↓¬applied␈↓␈α
the␈α∞functional␈α
argument␈α
we␈α
restored␈α∞the␈α
symbol␈α
table␈α
in␈α∞such␈α
a
␈↓ α←␈↓way␈αthat␈αglobal␈αvariables␈αwere␈αaccessed␈αin␈αthe␈αsaved␈αenvironment␈αwhile␈α
local
␈↓ α←␈↓variables␈α∂were␈α∂found␈α∂in␈α∂the␈α⊂current␈α∂environment.␈α∂ We␈α∂must␈α∂try␈α∂to␈α⊂do␈α∂the
␈↓ α←␈↓same␈α→with␈α_the␈α→shallow-binding.␈α_The␈α→action␈α_taken␈α→when␈α→a␈α_functional
␈↓ α←␈↓argument␈α∀is␈α∀recognized␈α∀is␈α∃quite␈α∀similar␈α∀to␈α∀our␈α∀previous␈α∃solution:␈α∀when
␈↓ α←␈↓␈↓αfunction␈↓␈αis␈α
seen,␈αsave␈α
the␈αcurrent␈α
␈↓
ENV␈↓␈αpointer.␈α
This␈αsetting␈α
of␈α␈↓
ENV␈↓␈α
establishes
␈↓ α←␈↓the␈α∃binding␈α∀environment,␈α∃and␈α∀is␈α∃therefore␈α∀callled␈α∃the␈α∃␈↓↓binding␈α∀context
␈↓ α←␈↓↓pointer␈↓.␈α?␈α↓ The␈α?␈ααaction␈α?␈α↓therefore,␈α?␈α↓manufactures␈α?␈ααa␈α?␈α↓triple
␈↓ α←␈↓␈↓α(FUNARG ␈↓<function> <binding context pointer>␈↓α)␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'However,␈α⊂the␈α⊂action␈α⊂required␈α⊂when␈α⊂we␈α⊂wish␈α⊂to␈α⊂apply␈α⊂the␈α⊂functional
␈↓ α←␈↓argument␈αis␈αmuch␈αmore␈αcomplicated.␈αIn␈αthe␈αdeep␈αbinding␈αimplementation␈αwe
␈↓ α←␈↓just␈α
set␈α
up␈α
a␈α∞new␈α
access␈α
chain␈α
such␈α
that␈α∞the␈α
local␈α
table␈α
referred␈α∞to␈α
binding
␈↓ α←␈↓environment␈α∂saved␈α⊂by␈α∂the␈α∂␈↓αFUNARG␈↓␈α⊂construction.␈α∂ The␈α∂problem␈α⊂with␈α∂the
␈↓ α←␈↓shallow-binder␈α∀is␈α∀that␈α∃␈↓
ENV␈↓␈α∀only␈α∀reflects␈α∃the␈α∀␈↓¬incremental␈↓␈α∀changes␈α∃in␈α∀the
␈↓ α←␈↓environments␈α∞during␈α∞the␈α∞computation.␈α∞To␈α∞retrieve␈α∞the␈α∞environment␈α
current
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 38␈↓An␈α⊃alternative␈α⊂implementation␈α⊃would␈α⊃only␈α⊂store␈α⊃the␈α⊃saved␈α⊂values,
␈↓ α←␈↓without␈α
explicitly␈αsaving␈α
the␈α
locations,␈αand␈α
without␈α
marking␈αthe␈α
stack.␈αIn␈α
this
␈↓ α←␈↓implementation␈α∞the␈α∂unbinder␈α∞would␈α∞require␈α∂an␈α∞argument␈α∂describing␈α∞which
␈↓ α←␈↓variables needed restoring.
␈↓ α←␈↓␈↓298 Static Structure␈↓
_5.19␈↓
␈↓"β␈↓ α←␈↓when␈αthe␈αfunctional␈αargument␈αwas␈α
bound,␈αwe␈αmust␈αunwind␈α␈↓
ENV␈↓␈αback␈α
to␈αthe
␈↓ α←␈↓binding␈α
environment;␈α
we␈α
must␈α
also␈α
save␈α
the␈α
current␈α
activation␈αenvironment
␈↓ α←␈↓so␈α⊂that␈α⊂we␈α⊂may␈α⊂return␈α⊂to␈α⊂␈↓¬it␈↓␈α⊂when␈α⊂finished␈α⊂with␈α⊂the␈α⊂functional␈α⊂argument.
␈↓ α←␈↓Since␈α∞we␈α∂are␈α∞dealing␈α∂with␈α∞a␈α∂functional␈α∞argument,␈α∂rather␈α∞than␈α∂a␈α∞functional
␈↓ α←␈↓value,␈α
we␈αcan␈α
easily␈αlocate␈α
the␈αbinding␈α
context␈αpointer.␈α
The␈αpointer␈α
is␈α␈↓¬below␈↓
␈↓ α←␈↓the␈α∩current␈α∩␈↓
ENV␈↓␈α∩in␈α∩the␈α∩stack.␈α∩We␈α∩search␈α∩down␈α∩the␈α∩stack␈α∩for␈α∩that␈α⊃saved
␈↓ α←␈↓pointer,␈α∂swapping␈α∂back␈α∂the␈α∂saved␈α∞value␈α∂cells.␈α∂When␈α∂we␈α∂reach␈α∂the␈α∞binding
␈↓ α←␈↓context␈α
pointer␈α
we␈α
stop.␈α
At␈α
that␈αtime␈α
the␈α
value␈α
cells␈α
have␈α
been␈α
restored␈αto␈α
the
␈↓ α←␈↓binding␈α∂environment␈α⊂and␈α∂the␈α⊂segment␈α∂of␈α∂the␈α⊂stack␈α∂between␈α⊂the␈α∂activation
␈↓ α←␈↓environment␈α⊂and␈α∂the␈α⊂binding␈α∂environment␈α⊂accurately␈α∂reflects␈α⊂the␈α∂bindings
␈↓ α←␈↓which␈α
were␈α
made␈α
between␈α
binding␈α
and␈α
activation;␈α
that␈α
is,␈α
that␈α
segment␈α
of␈α
the
␈↓ α←␈↓stack␈α∃is␈α∃deep␈α⊗bound.␈α∃ That␈α∃stack␈α∃segment␈α⊗must␈α∃be␈α∃saved␈α∃so␈α⊗that␈α∃the
␈↓ α←␈↓activation␈α∪environment␈α∪can␈α∩be␈α∪restored,␈α∪thus␈α∪␈↓
ENV␈↓␈α∩is␈α∪not␈α∪restored␈α∪to␈α∩the
␈↓ α←␈↓binding context, but remains where it was.
␈↓"β␈↓ α←␈↓␈↓ β'This process is complex enough to warrant an example.
␈↓"β␈↓ α←␈↓␈↓ ∧≠␈↓↓An example of shallow binding and ␈↓αFUNARG␈↓↓␈↓α
␈↓"∀␈↓ α←␈↓␈↓↓I␈↓ Assume that ␈↓αx␈↓ initially has value ␈↓α1␈↓ and the ␈↓
SP␈↓ pointer is at location ␈↓
SP1␈↓,
␈↓" ␈↓ α←␈↓␈↓↓II␈↓ then assume that a λ-binding rebinds ␈↓αx␈↓ to ␈↓α2␈↓;
␈↓" ␈↓ α←␈↓␈↓↓III␈↓␈αin␈αthis␈αnew␈αcontext,␈αassume␈αa␈αfunctional␈αargument,␈α␈↓αg␈↓,␈αis␈αto␈αbe␈αbound␈αto␈αa
␈↓ α←␈↓␈↓ β≠function-variable ␈↓αf␈↓.
␈↓" ␈↓ α←␈↓␈↓↓IV-V␈↓␈α
As␈α
the␈α
computation␈α
continues␈α
␈↓αx␈↓␈αis␈α
rebound␈α
first␈α
to␈α
␈↓α3␈↓␈α
and␈α
within␈α␈↓¬that␈↓
␈↓ α←␈↓␈↓ β≠context rebound again to ␈↓α4␈↓.
␈↓" ␈↓ α←␈↓␈↓↓VI␈↓␈αFinally␈α␈↓αf␈↓␈αis␈αapplied;␈αthis␈αwill␈αresurrect␈α␈↓αg␈↓␈↓π 39␈↓␈αrequiring␈αa␈αrestoration␈αof␈αthe
␈↓ α←␈↓␈↓ β≠environment current at step ␈↓↓III␈↓.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 39␈↓From the value cell of ␈↓αf␈↓ as ␈↓α(FUNARG G SP2)␈↓.
␈↓ α←␈↓␈↓5.19␈↓ ∧`Stack Implementation of a LISP Subset: Shallow Bound 299␈↓
␈↓" ␈↓ α←␈↓Steps ␈↓↓I␈↓ through ␈↓↓V␈↓ would lead to the following sequence.
␈↓"⊃␈↓ α←␈↓
F: (FUNARG G #)
␈↓"␈↓ α←␈↓
X: 1 X: 2 X: 2 ↓
␈↓"␈↓ α←␈↓
εααα∀αααλ εαααβαααλ ⊂ααα←ααα$
␈↓"␈↓ α←␈↓
SP1 α→~*MARK* ~ =II=> SP2 α→~ X ~ 1 ~ =III=> ↓
␈↓"␈↓ α←␈↓
εαααπαααλ εααα∀αααλ SP2 ∀α→~ X ~ 1 ~
␈↓"␈↓ α←␈↓
. . . ~*MARK* ~ . . .
␈↓"␈↓ α←␈↓
εαααπαααλ
␈↓"␈↓ α←␈↓
. . .
␈↓"␈↓ α←␈↓
SP1 α→~ . . . ~
␈↓"␈↓ α←␈↓
X: 3 X: 4
␈↓"␈↓ α←␈↓
=IV=> SP3 αα→~ X ~ 2 ~ =V=> SP4 εαααβαααλ
␈↓"␈↓ α←␈↓
εαααβαααλ %α→~ X ~ 3 ~
␈↓"␈↓ α←␈↓
. . . εαααβαααλ
␈↓"␈↓ α←␈↓
SP2 αα→~ X ~ 1 ~ . . .
␈↓"␈↓ α←␈↓
εαααβαααλ
␈↓"␈↓ α←␈↓
. . .
␈↓"∀␈↓ α←␈↓Now␈α⊗to␈α⊗apply␈α↔the␈α⊗functional␈α⊗argument:␈α↔␈↓α(FUNARG␈α⊗G␈α⊗SP2)␈↓.␈α↔ This␈α⊗is
␈↓ α←␈↓accomplished␈αby␈αtracing␈αdown␈αthe␈α␈↓
SP␈↓␈αstack␈αwith␈αa␈αpointer␈α␈↓
SP*␈↓,␈αmoving␈αfrom
␈↓ α←␈↓␈↓
SP4␈↓␈α≠--the current stack pointer--␈α≠down␈α≠to␈α≤␈↓
SP2␈↓␈α≠--the ␈↓αFUNARG␈↓ pointer--,
␈↓ α←␈↓reversing␈α∞all␈α∞the␈α∞intervening␈α∞bindings␈α∞on␈α∞SP␈α∞and␈α∞putting␈α∞the␈α∞saved␈α
values
␈↓ α←␈↓back␈αinto␈α
the␈αvalue cell.␈α
The␈αpattern␈α
of␈αthese␈α
reversals␈αmust␈α
be␈αsaved;␈αwe␈α
do
␈↓ α←␈↓this␈α∞by␈α∂swapping␈α∞the␈α∞values␈α∂back␈α∞into␈α∞the␈α∂stack␈α∞segment␈α∞between␈α∂␈↓
SP4␈↓␈α∞and
␈↓ α←␈↓␈↓
SP2␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Thus, steps ␈↓↓VII␈↓ and ␈↓↓VIII␈↓:
␈↓"⊃␈↓ α←␈↓
X:3 X: 2
␈↓"␈↓ α←␈↓
. . . . . .
␈↓"␈↓ α←␈↓
SP4 α→~ X ~ 4 ~ SP4 α→~ X ~ 4 ~
␈↓"␈↓ α←␈↓
εαααβαααλ εαααβαααλ
␈↓"␈↓ α←␈↓
. . . . . .
␈↓"␈↓ α←␈↓
=VII=> SP3 α→~ X ~ 2 ~←α SP* =VIII=> SP3 α→~ X ~ 3 ~
␈↓"␈↓ α←␈↓
. . . . . .
␈↓"␈↓ α←␈↓
εαααβαααλ εαααβαααλ
␈↓"␈↓ α←␈↓
SP2 α→~ X ~ 1 ~ SP2 α→~ X ~ 1 ~←α SP*
␈↓"␈↓ α←␈↓
. . . . . .
␈↓"∀␈↓ α←␈↓Now␈α∞we␈α∂are␈α∞in␈α∞a␈α∂position␈α∞to␈α∞evaluate␈α∂the␈α∞call␈α∞on␈α∂␈↓αg␈↓;␈α∞when␈α∞we␈α∂are␈α∞finished
␈↓ α←␈↓with␈α⊂␈↓αg␈↓␈α⊂we␈α⊂will␈α⊂use␈α⊂the␈α⊂unbinding␈α⊂mechanism␈α⊂to␈α⊂reinstate␈α⊂the␈α⊂world␈α⊂as␈α∂it
␈↓ α←␈↓existed␈αat␈α␈↓
SP4␈↓.␈αThis␈αprocess␈αwill␈αrestore␈αthe␈αvalue cells␈αusing␈αthe␈αareas␈αof␈αthe
␈↓ α←␈↓stack between ␈↓
SP2␈↓ and ␈↓
SP4␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Functional␈αarguments␈α
are␈αmore␈α
difficult␈αsince␈αthere␈α
is␈αonly␈α
one␈αsymbol
␈↓ α←␈↓␈↓300 Static Structure␈↓
_5.19␈↓
␈↓"β␈↓ α←␈↓table,␈α∂not␈α∂the␈α⊂stack␈α∂of␈α∂tables␈α∂implicit␈α⊂in␈α∂the␈α∂deep␈α⊂binding␈α∂implementation.
␈↓ α←␈↓True,␈αthe␈α
information␈αnecessary␈α
to␈αrecover␈α
an␈αenvironment␈α
is␈αpresent␈α
in␈α␈↓
SP␈↓,
␈↓ α←␈↓but now it is more expensive to retrieve it.
␈↓"β␈↓ α←␈↓␈↓ β'Though␈α
the␈α
stack␈α
implementation␈α
of␈α
shallow␈α
binding␈α
␈↓¬will␈↓␈α
perform␈αfor
␈↓ α←␈↓functional␈α⊂arguments,␈α⊃it␈α⊂will␈α⊂involve␈α⊃even␈α⊂more␈α⊂complexity␈α⊃if␈α⊂we␈α⊃wish␈α⊂to
␈↓ α←␈↓handle␈α∂functional␈α∂values.␈α∂ The␈α∂difficulty␈α∂is␈α∂the␈α∂same␈α∂as␈α∂that␈α∂for␈α⊂the␈α∂stack
␈↓ α←␈↓implementation␈αof␈α
deep␈αbinding:␈α
the␈α␈↓αFUNARG␈↓␈α
will␈αpoint␈α
"up"␈αthe␈α
␈↓
SP␈↓␈αstack
␈↓ α←␈↓rather␈αthan␈α"down".␈α
A␈αstraightforward␈αapplication␈α
of␈αthe␈αtechnique␈αused␈α
for
␈↓ α←␈↓functional␈α⊃arguments␈α⊃will␈α⊃not␈α⊃work.␈α⊃At␈α⊂the␈α⊃time␈α⊃we␈α⊃wished␈α⊃to␈α⊃apply␈α⊂the
␈↓ α←␈↓functional␈αvalue␈αits␈αsaved␈α
␈↓
SP␈↓-pointer␈αwill␈αbe␈αpointing␈α
into␈αa␈αsection␈αof␈αthe␈α
␈↓
SP␈↓
␈↓ α←␈↓stack␈α∪which␈α∩no␈α∪longer␈α∩reflects␈α∪the␈α∪proper␈α∩state.␈α∪For␈α∩when␈α∪we␈α∪leave␈α∩the
␈↓ α←␈↓environment␈α∃which␈α∃created␈α∃the␈α∃functional␈α∃value␈α∃the␈α⊗current␈α∃unbinding
␈↓ α←␈↓mechanism␈α∩will␈α∩cut␈α∩the␈α∩stack␈α∩back␈α∩to␈α∩the␈α∩point␈α∩which␈α∩existed␈α∪when␈α∩we
␈↓ α←␈↓entered that environment.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αgeneralization␈αof␈αthis␈αshallow␈αbinding␈αscheme␈αto␈αfunctional␈αvalues
␈↓ α←␈↓is␈α∞possible.␈α∞There␈α
are␈α∞two␈α∞problems␈α
to␈α∞be␈α∞solved.␈α
First,␈α∞the␈α∞storage␈α∞for␈α
␈↓
ENV␈↓
␈↓ α←␈↓and␈α⊂␈↓
DEST␈↓␈α⊃must␈α⊂be␈α⊂generalized␈α⊃to␈α⊂be␈α⊂tree-like␈α⊃rather␈α⊂than␈α⊃stack-like.␈α⊂This
␈↓ α←␈↓generalization␈α∃is␈α∃not␈α∃simply␈α∃a␈α∃problem␈α∃of␈α∃shallow␈α∃binding.␈α∃ The␈α∃deep
␈↓ α←␈↓binding␈α∃scheme␈α∀builds␈α∃a␈α∀tree␈α∃isomorphic␈α∀to␈α∃the␈α∀shallow␈α∃tree;␈α∃only␈α∀the
␈↓ α←␈↓information␈α∂saved␈α⊂in␈α∂the␈α⊂respective␈α∂trees␈α∂differs.␈α⊂ The␈α∂problem␈α⊂which␈α∂the
␈↓ α←␈↓shallow␈αbinder␈αmust␈αsolve␈αis␈αhow␈αto␈αrebind␈αfrom␈αthe␈αactivation␈αenvironment
␈↓ α←␈↓to␈α∂the␈α∂binding␈α∞environment.␈α∂ The␈α∂functional␈α∞argument␈α∂case␈α∂was␈α∞simplified
␈↓ α←␈↓since␈α
the␈α
binding␈αenvironment␈α
was␈α
on␈α
the␈αsame␈α
branch␈α
of␈α
the␈αenvironment
␈↓ α←␈↓tree.␈αIn␈αthe␈αmore␈α
general␈αcase␈αthe␈αbinding␈α
environment␈αwill␈αbe␈αon␈αa␈α
different
␈↓ α←␈↓branch.␈α∩ We␈α∩will␈α∩investigate␈α∩some␈α∪solutions␈α∩to␈α∩this␈α∩problem␈α∩in␈α∪the␈α∩next
␈↓ α←␈↓section.
␈↓"β␈↓ α←␈↓␈↓ ∧ ␈↓↓5.20 Strategies for Full LISP Implementation␈↓
␈↓"β␈↓ α←␈↓The␈α⊃discussion␈α⊃of␈α⊃the␈α⊃last␈α⊃three␈α⊃sections␈α⊃should␈α⊃be␈α⊃related␈α⊃to␈α∩the␈α⊃earlier
␈↓ α←␈↓discussion␈α→of␈α→binding␈α→strategies,␈α→Weizenbaum␈α→environments,␈α→functional
␈↓ α←␈↓arguments␈α∞and␈α∞the␈α∞non-recursive␈α∂evaluators.␈α∞ Our␈α∞mapping␈α∞of␈α∂the␈α∞binding
␈↓ α←␈↓implementations␈α⊃(shallow␈α⊂or␈α⊃deep)␈α⊂onto␈α⊃a␈α⊂stack␈α⊃is␈α⊂sufficient␈α⊃for␈α⊃the␈α⊂great
␈↓ α←␈↓majority␈α
of␈α
LISP␈α
programs.␈α
However␈α
as␈α
the␈α
LISP␈α
community␈α
explores␈α
new
␈↓ α←␈↓ways␈α⊂of␈α∂using␈α⊂the␈α∂language,␈α⊂they␈α⊂expect␈α∂that␈α⊂the␈α∂full␈α⊂power␈α⊂of␈α∂functional
␈↓ α←␈↓values␈α∪be␈α∪available;␈α∪and␈α∪have␈α∪proposed␈α∪extensions␈α∪in␈α∪the␈α∀LISP␈α∪control
␈↓ α←␈↓structure␈α⊃to␈α∩allow␈α⊃non-recursive␈α∩control.␈α⊃Since␈α⊃these␈α∩topics␈α⊃are␈α∩of␈α⊃current
␈↓ α←␈↓research␈α⊂interest␈α⊃it␈α⊂is␈α⊂not␈α⊃clear␈α⊂how␈α⊃lasting␈α⊂their␈α⊂impact␈α⊃will␈α⊂be.␈α⊃We␈α⊂will
␈↓ α←␈↓sketch␈αa␈αfew␈αof␈αthe␈αideas␈αinvolved␈αand␈αindicate␈αhow␈αthe␈αtechniques␈αwe␈αhave
␈↓ α←␈↓discussed in this chapter can be applied.
␈↓"β␈↓ α←␈↓␈↓ β'In␈α∩the␈α∩implementation␈α⊃of␈α∩␈↓αeval␈↓␈α∩of␈α⊃Section 3.5␈α∩we␈α∩represented␈α⊃symbol
␈↓ α←␈↓tables␈αas␈αlist-structure.␈αLater,␈αwhen␈αwe␈αintroduced␈αthe␈α␈↓αfunction␈↓␈αconstruct,␈αthis
␈↓ α←␈↓generality␈α⊂became␈α∂necessary.␈α⊂ As␈α⊂long␈α∂as␈α⊂a␈α∂␈↓αFUNARG␈↓␈α⊂construct␈α⊂accessed␈α∂a
␈↓ α←␈↓␈↓5.20␈↓ ε→Strategies for Full LISP Implementation 301␈↓
␈↓"β␈↓ α←␈↓table,␈α∀then␈α∀that␈α∪table␈α∀was␈α∀retained;␈α∀symbol␈α∪tables␈α∀were␈α∀then␈α∀a␈α∪garbage
␈↓ α←␈↓collectible␈αcommodity.␈αEssentially␈αwe␈αhad␈αremoved␈αthe␈αstack-like␈α
behavior␈αof
␈↓ α←␈↓symbol-table␈α∞accesses␈α∞which␈α∞occurs␈α∞most␈α∞of␈α∞the␈α∞time␈α∞and␈α∞replaced␈α∞it␈α∞with␈α
a
␈↓ α←␈↓general␈α
scheme␈αwhich␈α
works␈α
for␈αall␈α
cases␈α
but␈αincurs␈α
a␈α
significant␈αoverhead␈α
in
␈↓ α←␈↓even the most simple of function calls.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α
would␈α
like␈αan␈α
intermediate␈α
solution:␈α
one␈αwhich␈α
works␈α
for␈α
all␈αcases
␈↓ α←␈↓and␈α∂minimizes␈α∂overhead␈α∂in␈α∂the␈α∂typical␈α∂call.␈α∂Such␈α∂a␈α∂scheme␈α∂can␈α⊂indeed␈α∂be
␈↓ α←␈↓implemented.␈α∩ Recall␈α∩our␈α∪discussion␈α∩of␈α∩garbage␈α∩collection␈α∪in␈α∩Section 5.14.
␈↓ α←␈↓There␈α↔we␈α⊗said␈α↔that␈α⊗a␈α↔garbage␈α⊗collector␈α↔was␈α⊗used␈α↔in␈α⊗LISP␈α↔since␈α⊗the
␈↓ α←␈↓interrelationships␈α⊂which␈α⊃we␈α⊂generated␈α⊂in␈α⊃the␈α⊂data␈α⊃structure␈α⊂manipulations
␈↓ α←␈↓were␈αsufficiently␈αintertwined␈αthat␈αit␈αwas␈αnot␈αpossible␈αto␈αuse␈αless␈αsophisticated
␈↓ α←␈↓methods to determine whether a structure was still active.
␈↓"β␈↓ α←␈↓␈↓ β'Symbol␈α↔tables␈α⊗␈↓¬are␈↓␈α↔data␈α⊗structures;␈α↔the␈α⊗discussion␈α↔of␈α⊗Weizenbaum
␈↓ α←␈↓environments␈α∂in␈α∞Section 3.8␈α∂should␈α∞have␈α∂convinced␈α∞you␈α∂of␈α∞that.␈α∂ They␈α∞are
␈↓ α←␈↓chained␈α
together␈α
in␈α∞a␈α
manner␈α
reminiscent␈α
of␈α∞that␈α
of␈α
our␈α∞implementation␈α
of
␈↓ α←␈↓S-exprs;␈α≠indeed␈α≠as␈α≠we␈α≠have␈α≠just␈α≠mentioned␈α≠LISP's␈α≠attitude␈α≠toward
␈↓ α←␈↓management␈α
of␈α
such␈α
tables␈α
was␈αto␈α
garbage␈α
collect␈α
them.␈α
However␈αthe␈α
␈↓¬behavior␈↓
␈↓ α←␈↓of␈αtables␈αduring␈αthe␈α
execution␈αof␈αa␈αprogram␈α
is␈αmuch␈αless␈αcomplex␈α
than␈αthat
␈↓ α←␈↓of␈α
arbitrary␈α
list␈α
structure.␈α
As␈α
we␈αhave␈α
just␈α
seen,␈α
the␈α
behavior␈α
is␈αpredictable
␈↓ α←␈↓except␈α⊗for␈α⊗procedure-valued␈α⊗variables.␈α∃ A␈α⊗solution␈α⊗giving␈α⊗a␈α∃reasonable
␈↓ α←␈↓implementation␈α∀based␈α∀on␈α∀the␈α∪alternative␈α∀storage␈α∀management␈α∀scheme␈α∪of
␈↓ α←␈↓reference␈α→counters,␈α→which␈α→we␈α~described␈α→on␈α→page 286,␈α→is␈α~described␈α→in
␈↓ α←␈↓[Bob 73a].␈α∂ Several␈α∂other␈α∂generalized␈α∂control␈α∂schemes␈α∂have␈α∂been␈α∂proposed;
␈↓ α←␈↓for␈αexample␈α[Con 73],␈α[Gre 74],␈α[Mon 75],␈αand␈α[Wegb 75].␈α The␈αintent␈αof␈αall
␈↓ α←␈↓these␈α
schemes␈α
is␈α
that␈α
minimal␈α
overhead␈α
be␈α
experienced␈α
if␈α
a␈α
program␈α
does␈α
not
␈↓ α←␈↓use␈α
the␈αmore␈α
exotic␈αfeatures:␈α
a␈αstack-like␈α
device␈αresults.␈α
A␈αlarger␈α
toll␈α
is␈αpaid
␈↓ α←␈↓for use of more general control regimes.
␈↓"β␈↓ α←␈↓␈↓ β'It␈α␈↓¬is␈↓␈αpossible␈αto␈α
combine␈αthe␈αuse␈αof␈αthe␈α
value␈αcell␈αwith␈αeither␈αshallow␈α
or
␈↓ α←␈↓deep␈α∂strategies.␈α∂ We␈α∂have␈α∞␈↓¬both␈↓␈α∂a␈α∂value␈α∂cell␈α∞and␈α∂either␈α∂name-value␈α∂trees␈α∞or
␈↓ α←␈↓shallow␈α
bound␈α
p-lists.␈α
We␈α
will␈α
try␈α
to␈α
use␈α
the␈α
value cell␈α
as␈α
much␈α
as␈αpossible.
␈↓ α←␈↓We␈α
associate␈αan␈α
extra␈αpiece␈α
of␈αinformation␈α
with␈αeach␈α
value␈αattached␈α
to␈αany
␈↓ α←␈↓value cell,␈α∪telling␈α∪the␈α∪binding␈α∪time␈α∩of␈α∪that␈α∪variable.␈α∪ We␈α∪have␈α∪a␈α∩global
␈↓ α←␈↓indicator␈α
telling␈α
the␈α
current␈α
context␈α
which␈α
we␈α
are␈α
using:␈α
E␈↓βcurrent␈↓,␈α
say.␈α
When
␈↓ α←␈↓we␈α
want␈α
the␈α
value␈α
of␈α
a␈α
variable,␈α
we␈α
first␈α
go␈α
to␈α
the␈α
augmented␈α
value cell;␈α
if
␈↓ α←␈↓the␈α
binding␈α
time␈α
indication␈α
is␈α
that␈α
of␈α
E␈↓βcurrent␈↓,␈α
then␈α
the␈α
value␈α
is␈α∞correct␈α
and
␈↓ α←␈↓we␈α
take␈α
it.␈α
If␈α∞the␈α
indicators␈α
disagree,␈α
we␈α∞use␈α
the␈α
full␈α
␈↓αlookup␈↓␈α∞strategy;␈α
when
␈↓ α←␈↓we␈α
find␈α
the␈α
variable␈α
we␈α
update␈α
the␈α
value cell␈α
and␈α
change␈α
the␈α
binding␈α
time
␈↓ α←␈↓indicator␈αto␈αE␈↓βcurrent␈↓.␈αThis␈αway␈αwe␈αonly␈α
search␈αthe␈αstacks␈αfor␈αthe␈αfirst␈αaccess␈α
to
␈↓ α←␈↓a␈α∞variable;␈α∞after␈α∞that␈α∂we␈α∞can␈α∞justifiably␈α∞use␈α∂the␈α∞value␈α∞cell␈α∞until␈α∂we␈α∞change
␈↓ α←␈↓context␈α∞again.␈α∂This␈α∞scheme,␈α∞called␈α∂␈↓↓cache value cells␈↓,␈α∞has␈α∂been␈α∞implemented
␈↓ α←␈↓in [Mud 75].
␈↓"β␈↓ α←␈↓␈↓ β'Finally,␈α
we␈α
describe␈α
a␈αfull␈α
shallow␈α
binding␈α
implementation␈αof␈α
functional
␈↓ α←␈↓values␈α∞([Gre 74]).␈α∂We␈α∞identified␈α∂the␈α∞critical␈α∞problem␈α∂as␈α∞that␈α∂of␈α∞discovering
␈↓ α←␈↓the␈α
path␈α
between␈α
the␈α
activation␈α
environment␈α
and␈α
the␈α
binding␈α
environment.
␈↓ α←␈↓␈↓302 Static Structure␈↓
_5.20␈↓
␈↓"β␈↓ α←␈↓Let␈α∩us␈α∩call␈α∩the␈α∩two␈α∩nodes␈α∩E␈↓βact␈↓␈α⊃and␈α∩E␈↓βbind␈↓.␈α∩With␈α∩any␈α∩node␈α∩in␈α∩the␈α∩tree␈α⊃is
␈↓ α←␈↓associated␈αa␈αflag␈αnamed␈α␈↓αactive␈↓;␈αonly␈αnodes␈αon␈αthe␈αcurrently␈αactive␈αbranch␈αare
␈↓ α←␈↓marked␈α⊂␈↓αactive␈↓.␈α⊂It␈α⊂will␈α⊂be␈α⊂the␈α⊂responsibility␈α⊂of␈α⊂the␈α⊂binding␈α⊂and␈α⊂unbinding
␈↓ α←␈↓routines␈α
to␈α
maintain␈α
this␈α∞flag.␈α
In␈α
the␈α
current␈α∞situation,␈α
E␈↓βact␈↓␈α
is␈α
on␈α∞the␈α
active
␈↓ α←␈↓branch␈α
and␈α
E␈↓βbind␈↓␈α
is␈α
not.␈α
We␈α
go␈α
to␈α
the␈α
node␈α
E␈↓βbind␈↓␈α
and␈α
search␈α
back␈α∞down␈α
its
␈↓ α←␈↓branch,␈α∃looking␈α∃for␈α∃a␈α∃node␈α∃marked␈α∃␈↓αactive␈↓.␈α∃Call␈α∃this␈α∃node␈α∃E␈↓βinter␈↓.␈α∃E␈↓βinter␈↓
␈↓ α←␈↓represents␈α∂the␈α∂intersection␈α∂of␈α∂the␈α∂active␈α∂branch␈α∂with␈α∂the␈α∂branch␈α⊂tipped␈α∂in
␈↓ α←␈↓E␈↓βbind␈↓.
␈↓"⊃␈↓ α←␈↓∂ ␈↓E␈↓βact␈↓∂ ␈↓E␈↓βbind␈↓∂
␈↓"␈↓ α←␈↓∂ ␈↓∞H␈↓∂ ␈↓∞G␈↓∂
␈↓"␈↓ α←␈↓∂ ␈↓∞H␈↓∂ ␈↓∞G␈↓∂
␈↓"␈↓ α←␈↓∂ ␈↓∞H␈↓∂ ␈↓∞G␈↓∂
␈↓"␈↓ α←␈↓∂ ␈↓∞HG␈↓∂
␈↓"␈↓ α←␈↓∂ ␈↓E␈↓βinter␈↓∂
␈↓"␈↓ α←␈↓∂ ↓
␈↓"␈↓ α←␈↓∂ . . .
␈↓"∀␈↓ α←␈↓We␈α
now␈α
go␈α
E␈↓βact␈↓␈α
and␈α
unbind␈α
down␈α
to␈α
E␈↓βinter␈↓,␈α
swapping␈α
the␈α
value␈α
cells␈α
as␈α
we
␈↓ α←␈↓go.␈α
At␈α
E␈↓βinter␈↓␈α
we␈α
bind␈α
up␈α
to␈α
E␈↓βbind␈↓,␈α
still␈α
swapping␈α
value␈α
cells.␈α
When␈α
we␈αreach
␈↓ α←␈↓E␈↓βbind␈↓␈α∂the␈α∂binding␈α∂environment␈α∂has␈α∂been␈α∂restored,␈α∂and␈α∂the␈α∂path␈α⊂from␈α∂E␈↓βbind␈↓
␈↓ α←␈↓through␈α
E␈↓βinter␈↓␈αto␈α
E␈↓βact␈↓␈α
contains␈αthe␈α
necessary␈α
information␈αto␈α
allow␈α
us␈αto␈α
rebind
␈↓ α←␈↓to E␈↓βact␈↓ if desired.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αwinding␈α
and␈αunwinding␈αof␈α
value␈αcells␈αis␈α
a␈αtime␈αconsuming␈α
process,
␈↓ α←␈↓much␈α
more␈α
so␈α
than␈α
the␈α
context␈α
swap␈α
used␈α
in␈α
a␈α
deep␈α
binding␈α
scheme.␈α
One
␈↓ α←␈↓objection␈α∞to␈α∞this␈α∞implementation␈α∞of␈α
the␈α∞shallow␈α∞scheme␈α∞is␈α∞that␈α∞it␈α
optimized
␈↓ α←␈↓for␈α
the␈α
case␈α
that␈α∞many␈α
of␈α
the␈α
references␈α
in␈α∞the␈α
new␈α
environment␈α
will␈α∞be␈α
to
␈↓ α←␈↓free␈α∪variables:␈α∪those␈α∪variables␈α∪which␈α∪are␈α∪non-local,␈α∪but␈α∪are␈α∪not␈α∩globally
␈↓ α←␈↓bound.␈α
If␈α
a␈α
variable␈α
is␈α
local␈α
or␈α
global,␈α
its␈α
access␈α
is␈α
trivial.␈α
If␈α
there␈α
are␈αno␈α
free
␈↓ α←␈↓references␈α⊂in␈α⊂the␈α⊂new␈α⊂environment,␈α∂then␈α⊂this␈α⊂shallow␈α⊂swap␈α⊂is␈α⊂not␈α∂needed.
␈↓ α←␈↓Alternative␈αschemes␈αexist␈αwhich␈αallow␈αthe␈αfast␈αaccess␈αof␈αshallow␈αbinding␈αand
␈↓ α←␈↓allow the fast context swap of deep binding.
␈↓"β␈↓ α←␈↓␈↓ ¬z␈↓↓5.21 Epilogue␈↓
␈↓"β␈↓ α←␈↓Most␈αprogramming␈αlanguages␈α
are␈αmuch␈αless␈αcomplex␈α
than␈αLISP;␈αthey␈αare␈α
far
␈↓ α←␈↓less␈α∂flexible␈α∞in␈α∂their␈α∞control␈α∂regimes␈α∞or␈α∂their␈α∞symbol␈α∂accessing␈α∞mechanisms.
␈↓ α←␈↓In␈α∩Fortran␈α⊃there␈α∩is␈α∩a␈α⊃simple␈α∩relationship␈α⊃between␈α∩variables␈α∩and␈α⊃memory
␈↓ α←␈↓locations␈α∩which␈α⊃will␈α∩contain␈α⊃their␈α∩values;␈α⊃a␈α∩Fortran␈α⊃evaluator␈α∩can␈α⊃assign
␈↓ α←␈↓fixed␈α∂locations␈α∂to␈α∂the␈α∂variables␈α∂in␈α∂a␈α∂program.␈α∂ In␈α∂Algol,␈α∂there␈α∂is␈α⊂a␈α∂simple
␈↓ α←␈↓relationship␈α
between␈αvariables␈α
and␈α
positions␈αin␈α
the␈αrun-time␈α
stack;␈α
an␈αAlgol
␈↓ α←␈↓evaluator␈α
cannot␈α
assign␈α
fixed␈α
locations,␈α
but␈α
it␈α
can␈α
replace␈α
the␈αvariable␈α
lookup
␈↓ α←␈↓with␈α
a␈α
simple␈α
address␈α
calculation.␈α
This␈α
is␈α
partly␈α
due␈α
to␈α
Algol's␈α
use␈α
of␈α
static
␈↓ α←␈↓␈↓5.21␈↓ ⊂Epilogue 303␈↓
␈↓"β␈↓ α←␈↓binding␈↓π 40␈↓␈α∂and␈α∞partly␈α∂due␈α∞to␈α∂its␈α∞restrictions␈α∂on␈α∂procedure-valued␈α∞variables.
␈↓ α←␈↓These␈α⊂kinds␈α⊂of␈α⊃restrictions␈α⊂allow␈α⊂Fortran␈α⊃and␈α⊂Algol␈α⊂compilers␈α⊃to␈α⊂produce
␈↓ α←␈↓efficient code.
␈↓"β␈↓ α←␈↓␈↓ β'In␈αthe␈αmost␈αgeneral␈αuses␈αof␈αLISP,␈αboth␈αthe␈αquality␈αand␈αthe␈αquantity␈αof
␈↓ α←␈↓variables␈α
can␈α
change.␈α∞ Arbitrary␈α
properties␈α
can␈α∞be␈α
associated␈α
with␈α∞atoms␈α
at
␈↓ α←␈↓run-time.␈α
Indeed,␈α
the␈α
symbol␈α
table␈α
mechanism␈α
of␈α
LISP␈α
is␈α
more␈α
reminiscent
␈↓ α←␈↓of␈α↔that␈α↔associated␈α↔with␈α↔the␈α↔compilers␈α↔for␈α↔other␈α↔languages.␈α↔ For␈α↔these
␈↓ α←␈↓languages␈α∃it␈α∀is␈α∃the␈α∀compiler␈α∃which␈α∀performs␈α∃the␈α∀mapping␈α∃from␈α∀source
␈↓ α←␈↓language␈α∩to␈α∩running␈α∩machine␈α∪code.␈α∩ It␈α∩is␈α∩the␈α∩compiler's␈α∪responsibility␈α∩to
␈↓ α←␈↓discover␈α
the␈α
properties␈α
associated␈α
with␈α
each␈α
variable.␈α
The␈α
compiler␈α
can␈α
do
␈↓ α←␈↓this␈αbecause␈αthe␈αsemantics␈α
of␈αthe␈αlanguage␈αis␈αsuch␈α
that␈αat␈αcompile␈αtime␈αall,␈α
or
␈↓ α←␈↓almost␈αall,␈αof␈αthe␈αproperties␈αof␈αthe␈αvariables␈αare␈αknown.␈α This␈αis␈αnot␈αtrue␈αfor
␈↓ α←␈↓LISP.␈α⊂ In␈α⊂general␈α⊂you␈α⊂cannot␈α⊂tell␈α⊂until␈α⊂run␈α⊂time␈α⊂what␈α⊂the␈α⊂attributes␈α⊂of␈α⊂a
␈↓ α←␈↓particular␈α⊃atom␈α⊃are.␈α⊃ The␈α⊃situation␈α⊃is␈α⊃really␈α⊃even␈α⊃worse␈α⊃than␈α⊃this.␈α⊂ Since
␈↓ α←␈↓programs␈α∂and␈α∂data␈α∞are␈α∂indistinguishable,␈α∂we␈α∂can␈α∞construct␈α∂a␈α∂list␈α∂using␈α∞the
␈↓ α←␈↓data␈αstructure␈αfacilities␈αand␈αthe␈αturn␈αright␈αaround␈αand␈αevaluate␈αthat␈αlist␈αas␈αa
␈↓ α←␈↓representation of a LISP expression.
␈↓"β␈↓ α←␈↓␈↓ β'However,␈αa␈αlarge␈αmajority␈αof␈αLISP␈αcomputations␈αfall␈αinto␈αa␈αmuch␈αmore
␈↓ α←␈↓disciplined␈α∞set,␈α∞and␈α∞for␈α∞those␈α
computations,␈α∞some␈α∞of␈α∞the␈α∞ideas␈α∞available␈α
for
␈↓ α←␈↓Fortran␈α∀or␈α∃Algol␈α∀translators␈α∀are␈α∃applicable.␈α∀If␈α∀we␈α∃don't␈α∀use␈α∀all␈α∃of␈α∀the
␈↓ α←␈↓generality␈α⊂available␈α⊂in␈α⊂the␈α⊂language,␈α⊃we␈α⊂can␈α⊂reduce␈α⊂some␈α⊂of␈α⊃the␈α⊂run-time
␈↓ α←␈↓overhead.␈α∪ For␈α∪these␈α∀kinds␈α∪of␈α∪computations,␈α∀it␈α∪might␈α∪be␈α∀appropriate␈α∪to
␈↓ α←␈↓compile␈α⊂out␈α⊂the␈α⊂unneeded␈α⊂generality.␈α⊂ There␈α⊂are␈α⊂LISP␈α⊂compilers,␈α⊂typically
␈↓ α←␈↓written␈α∞in␈α∞LISP.␈α
They␈α∞can␈α∞make␈α
many␈α∞decisions␈α∞at␈α
compile␈α∞time␈α∞about␈α
the
␈↓ α←␈↓properties␈αof␈α
variables;␈αand␈α
given␈αcomparable␈α
information␈αabout␈αa␈α
program's
␈↓ α←␈↓characteristics␈α
can␈α
produce␈α
code␈α∞comparable␈α
to␈α
that␈α
produced␈α
by␈α∞Algol␈α
and
␈↓ α←␈↓Fortran ([Fat 73]).
␈↓"β␈↓ α←␈↓␈↓ β'In␈α
the␈α
most␈α
general␈α
cases,␈α
the␈α
compiled␈α
code␈α
may␈α
be␈α
interspersed␈αwith
␈↓ α←␈↓calls␈αon␈α␈↓αeval␈↓.␈α
This␈αimplies␈αthat␈αcompiled␈α
and␈αinterpreted␈αcode␈αmust␈α
be␈αable
␈↓ α←␈↓to␈α∪communicate␈α∪with␈α∀each␈α∪other.␈α∪ A␈α∪piece␈α∀of␈α∪compiled␈α∪code␈α∪can␈α∀call␈α∪a
␈↓ α←␈↓λ-expression␈α
or␈α
conversely.␈α The␈α
execution␈α
of␈αthe␈α
program␈α
should␈α
be␈αtotally
␈↓ α←␈↓transparent␈αas␈αto␈αwhether␈αany,␈αor␈αall,␈αor␈αnone␈αof␈αthe␈αfunctions␈α
are␈αcompiled.
␈↓ α←␈↓This␈α∞means␈α∞that␈α∞the␈α∞calling␈α∞sequences␈α
for␈α∞both␈α∞kinds␈α∞of␈α∞functions␈α∞must␈α
be
␈↓ α←␈↓compatible.␈αLess␈αobvious␈αand␈αby␈αfar␈αmore␈αtroublesome,␈αis␈αthe␈αcommunication
␈↓ α←␈↓of␈α∩the␈α∩values␈α⊃of␈α∩free␈α∩variables.␈α∩The␈α⊃next␈α∩chapter␈α∩discusses␈α∩the␈α⊃run-time
␈↓ α←␈↓behavior␈α∂required␈α⊂for␈α∂implementations␈α∂of␈α⊂LISP-like␈α∂languages␈α⊂including␈α∂a
␈↓ α←␈↓discussion of LISP compilers.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 40␈↓This is another benefit of referential transparency (page 171).
␈↓ α←␈↓␈↓304 Dynamic Structure␈↓
86.␈↓
␈↓"β␈↓ α←␈↓␈↓ ␈↓↓CHAPTER 6␈↓
␈↓"β␈↓ α←␈↓␈↓ The Dynamic Structure of LISP␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬g␈↓↓6.1 Introduction␈↓
␈↓"β␈↓ α←␈↓We␈α∀have␈α∀now␈α∀developed␈α∀the␈α∀basic␈α∀static␈α∀structure␈α∀of␈α∀a␈α∀LISP␈α∀machine
␈↓ α←␈↓consisting␈α∞of␈α
␈↓αeval␈↓␈α∞and␈α
its␈α∞subfunctions;␈α
we␈α∞have␈α
discussed␈α∞the␈α∞I/O␈α
routines,
␈↓ α←␈↓the␈α⊃garbage␈α⊃collector,␈α⊃and␈α⊃the␈α⊂organization␈α⊃of␈α⊃a␈α⊃symbol␈α⊃table.␈α⊃This␈α⊂table
␈↓ α←␈↓contains␈α
the␈α
primitive␈α
functions,␈α
constants␈αlike␈α
␈↓αT␈↓␈α
and␈α
␈↓αNIL␈↓,␈α
and␈α
a␈αcollection
␈↓ α←␈↓of␈αutility␈αfunctions␈αlike␈α␈↓αappend␈↓,␈αand␈α␈↓αreverse␈↓.␈αWe␈αhave␈αalso␈αisolated␈αtwo␈αareas
␈↓ α←␈↓of memory: pointer space, and full word space.
␈↓"β␈↓ α←␈↓␈↓ β'Expressions␈α∂are␈α∞read␈α∂in,␈α∂converted␈α∞to␈α∂list␈α∞structure,␈α∂and␈α∂evaluated␈α∞by
␈↓ α←␈↓␈↓αeval␈↓.␈α∂ The␈α∂evaluator␈α∂traverses␈α⊂the␈α∂S-expression␈α∂representation␈α∂of␈α⊂the␈α∂form,
␈↓ α←␈↓interpreting␈α⊂the␈α⊂information␈α⊂found␈α⊂there␈α⊂as␈α⊂LISP␈α⊂"instructions".␈α⊂We␈α⊂have
␈↓ α←␈↓discussed␈α∂some␈α∂basic␈α∂data␈α∂structures␈α∂for␈α∂implementation␈α∂of␈α∂λ-bindings␈α∂and
␈↓ α←␈↓evaluation,␈α⊂but␈α⊂we␈α∂have␈α⊂said␈α⊂little␈α∂about␈α⊂how␈α⊂the␈α∂actual␈α⊂execution␈α⊂of␈α∂the
␈↓ α←␈↓expression␈α
takes␈α
place.␈α
The␈αessential␈α
ingredient␈α
involved␈α
here␈α
is␈αthe␈α
handling
␈↓ α←␈↓of␈α⊂control␈α⊂--␈α⊃the␈α⊂dynamics␈α⊂of␈α⊂LISP␈α⊃execution.␈α⊂ For␈α⊂example,␈α⊂how␈α⊃can␈α⊂we
␈↓ α←␈↓implement␈α≠call-by-value,␈α~parameter␈α≠passing,␈α~recursion,␈α≠and␈α~conditional
␈↓ α←␈↓expressions?␈α At␈αan␈αabstract␈α
level,␈αthe␈αoriginal␈α␈↓αeval-apply␈↓␈αpair␈α
of␈αSection 3.5
␈↓ α←␈↓describes␈αthe␈α
evaluation␈αmechanism.␈αGiven␈α
the␈αdata␈α
structure␈αrepresentation
␈↓ α←␈↓of␈α∞an␈α∞expression,␈α
we␈α∞can␈α∞mechanically␈α
perform␈α∞the␈α∞transformations␈α
implied
␈↓ α←␈↓in␈α
the␈α␈↓αeval-apply␈↓␈α
pair.␈α
Even␈αmore␈α
of␈α
the␈αdetail␈α
is␈α
made␈αexplicit␈α
in␈α
the␈αlater
␈↓ α←␈↓evaluators␈α→of␈α→Section 4.6␈α→through␈α_Section 4.8.␈α→ However␈α→we␈α→must␈α_still
␈↓ α←␈↓␈↓6.1␈↓ λ↑Introduction 305␈↓
␈↓"β␈↓ α←␈↓implement␈α
these␈α
evaluators␈αon␈α
a␈α
"real"␈α
machine␈αand,␈α
unless␈α
the␈α
evaluator␈αis
␈↓ α←␈↓built␈α⊂into␈α⊂the␈α⊂hardware,␈α⊂we␈α⊃must␈α⊂express␈α⊂the␈α⊂evaluator␈α⊂in␈α⊂terms␈α⊃of␈α⊂more
␈↓ α←␈↓primitive␈αoperations.␈αFor␈αexample,␈α
we␈αcannot␈α"implement"␈αrecursion␈αby␈α
␈↓¬using␈↓
␈↓ α←␈↓recursion;␈α∩we␈α∪must␈α∩express␈α∩that␈α∪idea␈α∩in␈α∩terms␈α∪of␈α∩lower␈α∪level␈α∩operations.
␈↓ α←␈↓Obviously␈α⊂this␈α⊃decomposition␈α⊂must␈α⊂stop␈α⊃somewhere.␈α⊂As␈α⊃J. McCarthy␈α⊂once
␈↓ α←␈↓said:␈α⊗"Nothing␈α∃can␈α⊗be␈α⊗explained␈α∃to␈α⊗a␈α⊗stone";␈α∃we␈α⊗must␈α⊗assume␈α∃certain
␈↓ α←␈↓primitives are known.
␈↓"β␈↓ α←␈↓␈↓ β'In␈α∞this␈α∞chapter␈α∞we␈α∞will␈α
discuss␈α∞two␈α∞layers␈α∞of␈α∞"primitive␈α∞operations"␈α
or
␈↓ α←␈↓␈↓↓instructions␈↓.␈αOne␈αlayer␈αwill␈αcorrespond␈αto␈αtraditional␈αhardware,␈αand␈αanother
␈↓ α←␈↓layer␈α
will␈α
correspond␈αto␈α
the␈α
primitives␈α
which␈αwe␈α
derive␈α
from␈α
the␈αevaluator␈α
of
␈↓ α←␈↓Section 4.8.␈α Here␈αwe␈αdiscuss␈αthe␈αprimitives␈αof␈αthat␈αsection␈αas␈αthe␈αbasis␈αfor␈αa
␈↓ α←␈↓machine␈α
which␈α∞executes␈α
LISP␈α
expressions.␈α∞ We␈α
can␈α
describe␈α∞the␈α
evaluation
␈↓ α←␈↓of␈α∂a␈α∂LISP␈α∂expression␈α∂as␈α∂the␈α∞execution␈α∂of␈α∂a␈α∂sequence␈α∂of␈α∂these␈α∞instructions.
␈↓ α←␈↓Both␈αoperations␈αare␈αequivalent:␈αeither␈αevaluate␈αthe␈αexpression␈αor␈αexecute␈αthe
␈↓ α←␈↓instruction sequence.
␈↓"β␈↓ α←␈↓␈↓ β'There␈αare␈αcommon␈αinstances␈αin␈αwhich␈αthe␈αexecution␈αof␈αthe␈αinstructions
␈↓ α←␈↓can␈α
be␈α
considered␈α
"more␈α
efficient"␈αthan␈α
the␈α
evaluation␈α
of␈α
the␈αexpression.␈α
For
␈↓ α←␈↓example,␈α∞consider␈α∞the␈α∞access␈α∂to␈α∞a␈α∞local␈α∞variable.␈α∂Each␈α∞such␈α∞access␈α∞is␈α∂to␈α∞the
␈↓ α←␈↓same␈αlocation␈αrelative␈αto␈αthe␈αlocal␈αenvironment.␈αThat␈αrelative␈αlocation␈αcan␈αbe
␈↓ α←␈↓computed␈α⊂easily,␈α⊂but␈α⊂the␈α⊂evaluator␈α⊂will␈α⊂use␈α⊂a␈α⊂version␈α⊂of␈α⊂␈↓αlookup␈↓␈α⊂for␈α∂every
␈↓ α←␈↓access.␈α We␈αresort␈αto␈α␈↓αlookup␈↓␈αfor␈αnon-local␈αvariables,␈αsince␈αLISP␈αuses␈αdynamic
␈↓ α←␈↓binding␈αand␈αthe␈αactivation␈αenvironment␈α
will␈αtypically␈αeffect␈αwhich␈αbinding␈α
is
␈↓ α←␈↓accessible,␈α∩but␈α⊃since␈α∩the␈α⊃location␈α∩of␈α⊃any␈α∩local␈α⊃variable␈α∩is␈α∩computable,␈α⊃we
␈↓ α←␈↓should exploit that knowledge when executing our programs.
␈↓"β␈↓ α←␈↓␈↓ β'Several␈αexamples␈αalso␈αarise␈αin␈αthe␈αevaluation␈αof␈αa␈α␈↓αprog␈↓.␈αFor␈αexample␈αa
␈↓ α←␈↓loop␈αtypically␈α
consists␈αof␈αa␈α
static␈↓π 1␈↓␈αsequence␈αof␈α
statements.␈αEach␈α
time␈αaround
␈↓ α←␈↓the␈αloop␈α
an␈αevaluator␈α
will␈αexecute␈α
the␈αsame␈α
sequence␈αof␈α
instructions.␈αIt␈α
would
␈↓ α←␈↓be␈α_faster␈α_to␈α_simply␈α_execute␈α_the␈α_sequence␈α_of␈α_instructions␈α→rather␈α_than
␈↓ α←␈↓re-interpret␈α
each␈α
expression.␈αA␈α
related␈α
efficiency␈αinvolves␈α
the␈α
execution␈αof␈α
␈↓αgo␈↓.
␈↓ α←␈↓We␈αassumed␈αin␈αSection 4.8␈αthat␈αthe␈αevaluator␈αwill␈αeither␈αlookup␈αthe␈αlabel␈αby
␈↓ α←␈↓searching␈α⊃the␈α⊃body␈α∩of␈α⊃the␈α⊃␈↓αprog␈↓␈α⊃or,␈α∩perhaps␈α⊃more␈α⊃efficiently,␈α∩searching␈α⊃a
␈↓ α←␈↓computed␈α
␈↓αgo_list␈↓.␈α
Either␈αcase␈α
requires␈α
a␈αsearch.␈α
If␈α
we␈αcan␈α
replace␈α
the␈αbody
␈↓ α←␈↓of␈αa␈αloop␈αwith␈αa␈αsequence␈αof␈αprimitive␈αinstructions,␈αthen␈αwe␈αcan␈αreplace␈αa␈α␈↓αgo␈↓
␈↓ α←␈↓with␈α∪a␈α∪transfer␈α∪of␈α∪control␈α∪to␈α∩the␈α∪beginning␈α∪of␈α∪an␈α∪appropriate␈α∪block␈α∩of
␈↓ α←␈↓instructions. Such a transfer operation should be one of our instructions.␈↓π 2␈↓
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 1␈↓By␈α
static␈α∞we␈α
mean␈α∞that␈α
the␈α∞actual␈α
expressions␈α∞do␈α
not␈α∞change␈α
during
␈↓ α←␈↓execution.␈α
Using␈α
the␈αfact␈α
that␈α
LISP␈α
programs␈αare␈α
data␈α
structures␈α
and␈αusing
␈↓ α←␈↓some␈α∂marginal␈α∂programming␈α∂techniques␈α∂␈↓αrplaca␈↓␈α∂and␈α∂␈↓αrplacd␈↓␈α∂(Section 7.7)␈α∞we
␈↓ α←␈↓can␈α⊃in␈α⊃fact␈α⊃write␈α⊃self␈α⊃modifying␈α⊃programs.␈α⊃However,␈α⊃such␈α⊃practice␈α⊃is␈α⊂not
␈↓ α←␈↓common.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 2␈↓A␈α∂problem␈α∂related␈α∂to␈α∂the␈α⊂execution␈α∂of␈α∂loops␈α∂is␈α∂the␈α∂␈↓¬recognition␈↓␈α⊂of␈α∂a
␈↓ α←␈↓loop.␈α⊃The␈α⊂extent␈α⊃of␈α⊂--or␈α⊃even␈α⊂the␈α⊃presence␈α⊂of--␈α⊃a␈α⊂loop␈α⊃which␈α⊂the␈α⊃user␈α⊂is
␈↓ α←␈↓controlling␈α∪by␈α∪tests␈α∪and␈α∪␈↓αgo␈↓'s␈α∪may␈α∀be␈α∪difficult␈α∪to␈α∪discover.␈α∪ If␈α∪a␈α∀loop␈α∪is
␈↓ α←␈↓␈↓306 Dynamic Structure␈↓
(6.1␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
translation␈αof␈α
an␈α
expression␈αinto␈α
a␈α
sequence␈αof␈α
instructions␈α
is␈αnot
␈↓ α←␈↓without␈α
cost.␈α
If␈α
we␈αwish␈α
to␈α
evaluate␈α
a␈αsimple␈α
expression␈α
only␈α
once,␈αthen␈α
direct
␈↓ α←␈↓evaluation␈α⊃is␈α⊂may␈α⊃be␈α⊂less␈α⊃time-consuming␈α⊂than␈α⊃translation␈α⊃plus␈α⊂execution.
␈↓ α←␈↓However␈α∀expressions␈α∪subjected␈α∀to␈α∀repeated␈α∪evaluation␈α∀can␈α∀profitably␈α∪be
␈↓ α←␈↓translated into instructions and then executed.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
translation␈α∞part␈α
of␈α∞the␈α
process␈α∞which␈α
we␈α∞have␈α
been␈α∞describing␈α
is
␈↓ α←␈↓called␈α∂␈↓↓compilation␈↓;␈α∂the␈α∂translator␈α∂is␈α∞called␈α∂a␈α∂␈↓↓compiler␈↓.␈α∂ The␈α∂compiler␈α∂is␈α∞a
␈↓ α←␈↓mapping␈α∃from␈α∃the␈α∃LISP␈α⊗expressions␈α∃to␈α∃a␈α∃sequence␈α∃of␈α⊗instructions.␈α∃ A
␈↓ α←␈↓compiler␈αis␈αa␈αuseful␈αtool␈αfor␈αincreasing␈αthe␈αspeed␈αof␈αexecution.␈α J.␈αMcCarthy
␈↓ α←␈↓says␈αa␈αcompiler␈αallows␈αyou␈αto␈αlook␈α␈↓¬before␈↓␈αyou␈αleap;␈αwe␈αwill␈αshow␈αthat␈αwe␈αcan
␈↓ α←␈↓look␈α∃␈↓¬as␈↓␈α∃we␈α∃leap.␈α∃That␈α∃is,␈α∃we␈α∃can␈α∃compile␈α∃instructions␈α∃as␈α∃we␈α∃evaluate
␈↓ α←␈↓expressions;␈α∞if␈α
the␈α∞expression␈α∞is␈α
evaluated␈α∞again␈α∞then␈α
we␈α∞execute␈α∞the␈α
faster
␈↓ α←␈↓compiled version.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∪relationship␈α∪between␈α∀compilation␈α∪and␈α∪interpretation␈α∀should␈α∪be
␈↓ α←␈↓more␈α⊂apparent␈α⊃now:␈α⊂the␈α⊃interpreter␈α⊂performs␈α⊃the␈α⊂evaluation;␈α⊃the␈α⊂compiler
␈↓ α←␈↓emits␈α∪instructions␈α∩which␈α∪when␈α∩executed␈α∪produce␈α∩the␈α∪same␈α∩computational
␈↓ α←␈↓effect␈αas␈αthe␈αevaluator.␈α Since␈αthe␈αcode␈αproduced␈αby␈αthe␈αcompiler␈αis␈αeither␈αin
␈↓ α←␈↓machine␈α∪language␈α∀or␈α∪in␈α∀a␈α∪form␈α∪closer␈α∀to␈α∪the␈α∀machine␈α∪than␈α∀the␈α∪source
␈↓ α←␈↓program,␈αwe␈αcan␈αexecute␈αthe␈αcode␈αmuch␈αfaster.␈αA␈αspeed-up␈αfactor␈αof␈αthirty␈α
to
␈↓ α←␈↓fifty␈α∞is␈α∞not␈α∞uncommon.␈α∞ Compilation␈α∞may␈α∞also␈α∞reduce␈α∂storage␈α∞requirements;
␈↓ α←␈↓interpreted␈α
programs␈α∞are␈α
stored␈α∞as␈α
S-expressions,␈α
but␈α∞compiled␈α
code␈α∞will␈α
be
␈↓ α←␈↓machine␈α⊃language.␈α⊃The␈α⊃machine␈α⊃code␈α⊃will␈α⊃require␈α⊃significantly␈α∩less␈α⊃space
␈↓ α←␈↓than␈α∪the␈α∪interpreted␈α∪version,␈α∀the␈α∪S-expr␈α∪storage␈α∪may␈α∪be␈α∀reclaimed,␈α∪and
␈↓ α←␈↓garbage␈α∞collection␈α∂time␈α∞will␈α∂be␈α∞decreased␈α∞since␈α∂the␈α∞compiled␈α∂code␈α∞is␈α∂not␈α∞in
␈↓ α←␈↓free space.
␈↓"β␈↓ α←␈↓␈↓ β'Why␈α⊃not␈α⊃compile␈α⊃all␈α⊃programs?␈α⊃We␈α⊃already␈α⊃have␈α⊃seen␈α⊃that␈α⊃we␈α⊃can
␈↓ α←␈↓␈↓αcons␈↓-up␈α
new␈α
expressions␈αto␈α
be␈α
evaluated␈αwhile␈α
we␈α
are␈αrunning.␈α
Even␈α
so,␈αwe
␈↓ α←␈↓can␈αcompile␈αthose␈αexpressions␈αbefore␈αexecution.␈↓π 3␈↓␈αThe␈αanswer,␈αrather,␈αis␈αthat
␈↓ α←␈↓for␈αdebugging␈α
and␈αediting␈αof␈α
programs␈αit␈αis␈α
extremely␈αconvenient␈αto␈α
have␈αa
␈↓ α←␈↓structured␈α→representation␈α_of␈α→the␈α→program␈α_in␈α→memory.␈α→This␈α_structured
␈↓ α←␈↓representation␈α∂also␈α∂simplifies␈α∂the␈α⊂discussion␈α∂of␈α∂compilation.␈α∂ It␈α∂is␈α⊂true␈α∂that
␈↓ α←␈↓compilers␈αcan␈α
translate␈αdirectly␈αfrom␈α
M-expression␈αrepresentation␈α
to␈αinternal
␈↓ α←␈↓machine␈αcode.␈↓π 4␈↓␈αConventional␈αcompiler␈αdiscussions␈αinclude␈αdescription␈αof␈αthe
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓controlled␈α∞by␈α∞language␈α∂constructs␈α∞(␈↓αwhile, do, repeat␈↓, etc.)␈α∞then␈α∂the␈α∞interpreter
␈↓ α←␈↓should␈α⊃have␈α∩some␈α⊃chance␈α∩of␈α⊃improving␈α∩the␈α⊃execution␈α∩of␈α⊃the␈α∩loop.␈α⊃This,
␈↓ α←␈↓perhaps,␈α∞is␈α∞reason␈α∞for␈α∞removing␈α∞control␈α∞of␈α∞iteration␈α∞from␈α∞the␈α∞hands␈α∞of␈α∞the
␈↓ α←␈↓programmer.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 3␈↓There␈αare,␈αhowever,␈αprograms␈αwhich␈αsimply␈α␈↓¬cannot␈↓␈αbe␈αcompiled.␈αThe
␈↓ α←␈↓most␈α⊂obscene␈α⊂examples␈α⊂involve␈α⊂self-modifying␈α⊂programs;␈α⊂that␈α⊃is,␈α⊂programs
␈↓ α←␈↓which␈α~modify␈α~their␈α~representation␈α~in␈α~order␈α~to␈α~affect␈α~the␈α≠course␈α~of
␈↓ α←␈↓interpretation. An example is reluctantly given on page 388.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 4␈↓The␈α∃compilers␈α∃which␈α∃perform␈α⊗in␈α∃this␈α∃manner␈α∃are␈α⊗called␈α∃sytnax
␈↓ α←␈↓directed␈α∂compilers.␈α∂They␈α∂are␈α∂an␈α∂instance␈α∂of␈α∂a␈α∂computational␈α∂scheme␈α∞called
␈↓ α←␈↓␈↓6.1␈↓ λ↑Introduction 307␈↓
␈↓"β␈↓ α←␈↓syntax␈α
analysis␈αproblems,␈α
for␈α
we␈αcannot␈α
compile␈α
code␈αuntil␈α
we␈α
know␈αwhat␈α
the
␈↓ α←␈↓syntactic␈α∪structure␈α∪of␈α∪the␈α∪program␈α∪is.␈α∪However,␈α∪syntax␈α∪analysis␈α∀is␈α∪really
␈↓ α←␈↓irrelevant␈α→for␈α_a␈α→clear␈α_understanding␈α→of␈α_the␈α→behavior␈α_of␈α→a␈α_compiler.
␈↓ α←␈↓Assuming␈α∪the␈α∪existence␈α∪of␈α∀the␈α∪structured␈α∪representation,␈α∪the␈α∀compiler␈α∪is
␈↓ α←␈↓conceptually␈αvery␈α
simple.␈α The␈αS-expr␈α
representation␈αin␈αLISP␈α
and␈αresembles
␈↓ α←␈↓the␈α∪parse␈α∪tree␈α∪of␈α∪other␈α∪language␈α∪processors.␈α∪When␈α∪we␈α∪wish␈α∪to␈α∪run␈α∪the
␈↓ α←␈↓program␈αat␈αtop␈αspeed,␈αwe␈αcan␈αcompile␈αthe␈αprograms.␈α The␈αcompiler␈αcan␈αthen
␈↓ α←␈↓translate␈α
the␈αabstract␈α
representation␈α
of␈αthe␈α
program␈αinto␈α
machine␈α
code.␈α We
␈↓ α←␈↓shall say more about this view of programming later.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∂shall␈α∞exploit␈α∂the␈α∞analogy␈α∂between␈α∞compilers␈α∂and␈α∂evaluators␈α∞when
␈↓ α←␈↓we␈α
write␈α
the␈α
function,␈α
␈↓αcompile␈↓,␈α
which␈α
will␈α
implement␈α
the␈α
compiler.␈α∞ We␈α
will
␈↓ α←␈↓also␈α∂abstract␈α∂from␈α∂the␈α∂specific␈α∂compiler,␈α∂the␈α∂essence␈α∂of␈α∂all␈α∂LISP␈α∂compilers.
␈↓ α←␈↓We␈α
will␈α
have␈α
to␈α
separate␈α
two␈α
representations␈α
from␈α
the␈α
specific␈α
compiler;␈αwe
␈↓ α←␈↓are␈α∂representing␈α∂one␈α∂specific␈α∂compiling␈α∞algorithm,␈α∂and␈α∂we␈α∂are␈α∂also␈α∞dealing
␈↓ α←␈↓with␈α∞the␈α∞representation␈α∞of␈α∞a␈α∞specific␈α∞machine.␈α∞ The␈α∞task␈α∞is␈α∂worth␈α∞pursuing
␈↓ α←␈↓since␈α∂we␈α∂wish␈α∂to␈α∂write␈α∂different␈α∂compilers␈α∂for␈α∂a␈α∂specific␈α∂machine␈α∂and␈α∞also
␈↓ α←␈↓would like a single compiler capable of easy transportation to other machines.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊃input␈α∩to␈α⊃␈↓αcompile␈↓␈α∩is␈α⊃the␈α∩representation␈α⊃of␈α∩a␈α⊃LISP␈α∩function;␈α⊃the
␈↓ α←␈↓output␈αis␈αa␈αlist␈αwhich␈αrepresents␈αa␈αsequence␈αof␈αmachine␈αinstructions.␈α
Assume
␈↓ α←␈↓that␈α⊃we␈α⊃have␈α⊃LISP␈α⊃running␈α⊃on␈α⊃␈↓↓Brand␈α⊃X␈↓␈α⊃machine,␈α⊃and␈α⊃we␈α⊃have␈α⊂written
␈↓ α←␈↓␈↓αcompile␈↓␈α∀which␈α∪produces␈α∀code␈α∀for␈α∪␈↓↓Brand␈α∀X␈↓␈α∪machine.␈α∀Then␈α∀perform␈α∪the
␈↓ α←␈↓following sequence of steps:
␈↓"∀␈↓ α←␈↓␈↓ β' ␈↓↓1.␈↓ Create the S-expression form of ␈↓αcompile␈↓.
␈↓" ␈↓ α←␈↓␈↓ β' ␈↓↓2.␈↓ Introduce this translation into the machine, defining ␈↓αcompile␈↓.
␈↓" ␈↓ α←␈↓␈↓ β' ␈↓↓3.␈↓ Ask LISP to evaluate: ␈↓αcompile[COMPILE]␈↓.
␈↓"∀␈↓ α←␈↓␈↓ β'Since␈α∩␈↓αcompile␈↓␈α⊃compiles␈α∩code␈α∩for␈α⊃␈↓↓Brand␈α∩X␈↓␈α⊃machine,␈α∩it␈α∩translates␈α⊃the
␈↓ α←␈↓S-expression␈αrepresentation␈αof␈αits␈α
argument␈αinto␈αmachine␈αcode.␈αTherefore␈α
the
␈↓ α←␈↓output␈α∪of␈α∪step␈α∪3␈α∪is␈α∪a␈α∩list␈α∪of␈α∪instructions␈α∪representing␈α∪the␈α∪translation␈α∩of
␈↓ α←␈↓␈↓αcompile␈↓. That is, step 3 compiles the compiler.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α
technique␈α∞called␈α
␈↓↓bootstrapping␈↓␈α∞is␈α
closely␈α
related␈α∞to␈α
the␈α∞process␈α
just
␈↓ α←␈↓described.␈α
To␈α
illustrate␈α
this␈α
idea,␈α
assume␈α
that␈α
we␈α
have␈α
LISP␈α
and␈α
its␈α
compiler
␈↓ α←␈↓running␈α∂on␈α∞␈↓↓Brand X␈↓,␈α∂and␈α∞we␈α∂wish␈α∞to␈α∂implement␈α∞LISP␈α∂on␈α∞␈↓↓Brand␈α∂Y␈↓.␈α∂If␈α∞we
␈↓ α←␈↓have␈α⊃been␈α⊃careful␈α∩in␈α⊃our␈α⊃encoding␈α⊃of␈α∩the␈α⊃␈↓αcompile␈↓␈α⊃function␈α⊃then␈α∩␈↓¬most␈↓␈α⊃of
␈↓ α←␈↓␈↓αcompile␈↓␈αis␈αmachine␈α
independent;␈αthat␈αis,␈αit␈α
deals␈αmostly␈αwith␈αthe␈α
structure␈αof
␈↓ α←␈↓the␈α
LISP␈α
expression␈α∞and␈α
only␈α
in␈α∞a␈α
few␈α
places␈α
deals␈α∞with␈α
the␈α
structure␈α∞of␈α
a
␈↓ α←␈↓particular␈αmachine␈αarchitecture;␈αthis␈αis␈αnot␈αan␈αunrealisitc␈αassumption.␈α Notice
␈↓ α←␈↓that␈αthis␈αis␈αone␈αof␈αour␈αearly␈αprogramming␈αadmonitions:␈αencode␈αalgorithms␈αin
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓syntax␈αdirected␈αcomputation;␈αthe␈αidea␈αis␈αbased␈αon␈αthe␈αobservation␈αthat␈αmany
␈↓ α←␈↓algorithms␈α_parallel␈α_the␈α_underlying␈α_data␈α_structure.␈α_We␈α_have␈α_seen␈α↔this
␈↓ α←␈↓behavior␈α∪frequently␈α∪in␈α∪our␈α∩data␈α∪structure␈α∪algorithms.␈α∪For␈α∪application␈α∩to
␈↓ α←␈↓compiling and parsing see [Gri 71] or [Aho 72].
␈↓ α←␈↓␈↓308 Dynamic Structure␈↓
(6.1␈↓
␈↓"β␈↓ α←␈↓a␈αrepresentation-independent␈αstyle␈αand␈αthen␈αinclude␈α
representation-dependent
␈↓ α←␈↓routines␈αas␈αthe␈αinterface.␈αChanging␈αrepresentations␈αsimply␈αrequires␈αchanging
␈↓ α←␈↓those␈α∂simpler␈α∂subfunctions.␈α∂Here␈α∂the␈α∂representations␈α∂are␈α∂machines␈α∂and␈α∂the
␈↓ α←␈↓algorithm is a compiling algorithm for LISP functions.
␈↓"β␈↓ α←␈↓␈↓ β'Let␈α
us␈αcall␈α
those␈α
parts␈αof␈α
the␈αcompiler␈α
which␈α
deal␈αwith␈α
the␈αmachine,␈α
the
␈↓ α←␈↓code␈αgenerators␈αor␈αinstruction␈αgenerators.␈αNow␈αif␈αwe␈αunderstand␈αthe␈αmachine
␈↓ α←␈↓organization␈α∂of␈α∂brands␈α∂␈↓↓X␈↓␈α∂and␈α∂␈↓↓Y␈↓␈α∂then␈α∂for␈α∂any␈α∂instruction␈α∂on␈α⊂␈↓↓Brand X␈↓␈α∂we
␈↓ α←␈↓should␈α
be␈αable␈α
to␈α
give␈αa␈α
sequence␈α
of␈αinstructions␈α
having␈α
the␈αequivalent␈α
effect
␈↓ α←␈↓on␈α␈↓↓Brand Y␈↓.␈αWe␈αcan␈αchange␈αthe␈αinstruction␈αgenerators␈αin␈α␈↓αcompile␈↓␈αto␈αgenerate
␈↓ α←␈↓instructions␈αwhich␈α
run␈αon␈α
␈↓↓Brand Y␈↓.␈αWe␈α
would␈αhave␈α
a␈α␈↓αcompile␈↓␈α
function,␈αcall
␈↓ α←␈↓it␈α
␈↓αcompile*␈↓,␈α
running␈α
on␈α
␈↓↓X␈↓␈αand␈α
producing␈α
instructions␈α
for␈α
␈↓↓Y␈↓.␈α
Take␈αthe␈α
S-expr
␈↓ α←␈↓representations␈α
of␈α
␈↓αeval,␈α
apply,␈α
read,␈α
print,␈α
compile*,...␈↓␈α
etc.␈α
and␈α
compile␈αthese
␈↓ α←␈↓with␈α∞␈↓αcompile*␈↓;␈α∞we␈α∞will␈α∂generate␈α∞a␈α∞large␈α∞segment␈α∂of␈α∞code␈α∞for␈α∞a␈α∂LISP␈α∞system
␈↓ α←␈↓which␈α
will␈αrun␈α
on␈α
␈↓↓Y␈↓.␈αCertain␈α
primitives␈α
will␈αhave␈α
to␈α
be␈αsupplied␈α
to␈αrun␈α
these
␈↓ α←␈↓instructions␈αon␈α␈↓↓Y␈↓,␈α
but␈αa␈αvery␈αlarge␈α
part␈αof␈αLISP␈α
can␈αbe␈αbootstrapped␈αfrom␈α
␈↓↓X␈↓
␈↓ α←␈↓to ␈↓↓Y␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Given␈α∩a␈α∩compiler␈α∪and␈α∩interpreter␈α∩for␈α∩a␈α∪language␈α∩␈↓εL␈↓β1␈↓␈α∩we␈α∪can␈α∩often
␈↓ α←␈↓bootstrap␈α∩␈↓εL␈↓β1␈↓␈α∩to␈α∩a␈α∩language␈α∩␈↓εL␈↓β2␈↓.␈α∩We␈α∩express␈α∩the␈α∩interpreter␈α∩for␈α∩␈↓εL␈↓β2␈↓␈α∩as␈α⊃a
␈↓ α←␈↓program␈αin␈α␈↓εL␈↓β1␈↓.␈αWe␈αcan␈αthen␈αexecute␈αprograms␈αin␈α␈↓εL␈↓β2␈↓␈αby␈αinterpreting␈αthe␈α␈↓εL␈↓β2␈↓
␈↓ α←␈↓interpreter.␈α∃We␈α⊗can␈α∃improve␈α∃efficiency␈α⊗by␈α∃compiling␈α∃the␈α⊗␈↓εL␈↓β2␈↓␈α∃evaluator.
␈↓ α←␈↓Perhaps␈α
we␈α
can␈αexpress␈α
the␈α
␈↓εL␈↓β2␈↓␈αcompiler␈α
in␈α
␈↓εL␈↓β1␈↓␈αor␈α
␈↓εL␈↓β2␈↓;␈α
in␈αeither␈α
case␈α
we␈αcan
␈↓ α←␈↓then compile that compiler.␈↓π 5␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊃purpose␈α⊂of␈α⊃this␈α⊃chapter␈α⊂is␈α⊃to␈α⊂discuss␈α⊃implementation␈α⊃of␈α⊂control
␈↓ α←␈↓structures␈α
for␈α∞LISP,␈α
yet␈α∞most␈α
of␈α
this␈α∞introduction␈α
has␈α∞been␈α
a␈α∞description␈α
of
␈↓ α←␈↓compilers. These positions can reconciled easily.
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ ␈α⊃The␈α⊃instructions␈α⊃generated␈α∩by␈α⊃the␈α⊃compiler␈α⊃will␈α⊃reference␈α∩the␈α⊃control
␈↓ α←␈↓␈↓ β∂primitives␈α∞of␈α∞the␈α∞machine.␈α∂ The␈α∞control␈α∞structures␈α∞of␈α∞the␈α∂evaluator␈α∞will
␈↓ α←␈↓␈↓ β∂also␈α
be␈α
implemented␈α
from␈α
these␈α
primitives.␈α
The␈α
machine␈α
code␈αproduced
␈↓ α←␈↓␈↓ β∂by␈α
an␈α
implementor␈α
might␈α
be␈α
more␈α
highly␈α
optimized␈α
than␈α
that␈αproduced
␈↓ α←␈↓␈↓ β∂by␈α∂a␈α∂compiler,␈α⊂by␈α∂the␈α∂essential␈α⊂structure␈α∂of␈α∂the␈α⊂code␈α∂will␈α∂will␈α⊂be␈α∂quite
␈↓ α←␈↓␈↓ β∂similar.␈α∀ We␈α∪are␈α∀initially␈α∀interested␈α∪in␈α∀understanding.␈α∪It␈α∀is␈α∀easier␈α∪to
␈↓ α←␈↓␈↓ β∂understand␈α→an␈α_abstract␈α→general␈α→algorithm␈α_than␈α→to␈α→understand␈α_the
␈↓ α←␈↓␈↓ β∂implementation of one specific evaluator.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ It␈α
will␈α
clearly␈α
show␈α
the␈α
relationship␈α
between␈α
compilation␈α∞and␈α
evaluation.
␈↓ α←␈↓␈↓ β∂That␈α⊂is,␈α⊂the␈α⊃LISP␈α⊂function␈α⊂representing␈α⊃the␈α⊂compiler␈α⊂will␈α⊃very␈α⊂closely
␈↓ α←␈↓␈↓ β∂parallel␈α
the␈α
structure␈α
of␈α
the␈αinterpreter,␈α
␈↓αeval␈↓.␈α
If␈α
you␈α
understand␈α␈↓αeval␈↓,␈α
then
␈↓ α←␈↓␈↓ β∂the compiler is easy.
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α∀design␈α∀of␈α∀the␈α∃compiler␈α∀will␈α∀also␈α∀illustrate␈α∃another␈α∀non-trivial
␈↓ α←␈↓application␈αof␈αabstract␈αcomputation,␈αshowing␈α
how␈αsimple␈αit␈αis␈αto␈α
describe␈αan
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 5␈↓The␈α∃first␈α∃bootstrapped␈α∃compiler␈α∃was␈α∃the␈α∃LISP␈α∃compiler␈α∃for␈α∃the
␈↓ α←␈↓IBM704.␈α
Bootstrapping␈α∞is␈α
a␈α∞common␈α
practice␈α
in␈α∞A.I.␈α
now,␈α∞using␈α
LISP␈α∞as␈α
a
␈↓ α←␈↓base language and extending it to any number of specialized A.I. languages.
␈↓ α←␈↓␈↓6.1␈↓ λ↑Introduction 309␈↓
␈↓"β␈↓ α←␈↓apparently␈α
complex␈αalgorithm.␈α
As␈α
in␈αthe␈α
previous␈α
chapter,␈αwe␈α
will␈αremain␈α
as
␈↓ α←␈↓abstract␈α∃as␈α⊗possible␈α∃without␈α∃losing␈α⊗the␈α∃necessary␈α∃details.␈α⊗A␈α∃meaningful
␈↓ α←␈↓description␈αof␈αcompilers␈α
entails␈αan␈αunderstanding␈αof␈α
a␈αmachine,␈αso␈αbefore␈α
the
␈↓ α←␈↓actual␈α
construction␈α
of␈αthe␈α
compilers,␈α
we␈α
will␈αdescribe␈α
a␈α
simple␈α
machine␈αwith␈α
a
␈↓ α←␈↓sufficient␈α∞instruction␈α∞set␈α∞to␈α∞handle␈α∞the␈α∞control␈α∞structures␈α∞of␈α∞LISP.␈α∞ First␈α
we
␈↓ α←␈↓will␈α∪review␈α∪and␈α∪expand␈α∪the␈α∪primitives␈α∪of␈α∪Section 4.8,␈α∪emphasizing␈α∩their
␈↓ α←␈↓interpretation as machine instructions.
␈↓"β␈↓ α←␈↓␈↓ ¬4␈↓↓6.2 Primitives for LISP␈↓
␈↓"β␈↓ α←␈↓In␈α⊃our␈α⊃discussion␈α⊃of␈α⊃the␈α⊃evaluators␈α⊃in␈α⊃Section 4.6␈α⊃through␈α⊃Section 4.8␈α⊂we
␈↓ α←␈↓uncovered␈α∞more␈α∞details␈α∞involved␈α∞in␈α∞evaluation␈α∞of␈α∞LISP␈α∞expressions.␈α∂In␈α∞the
␈↓ α←␈↓final␈αevaluator␈αwe␈α
identified␈αa␈αdozen␈α
or␈αso␈αactions.␈α
The␈αunderlying␈αidea␈α
was
␈↓ α←␈↓to␈α⊃remove␈α⊃recursion␈α⊃and␈α∩replace␈α⊃that␈α⊃implicit␈α⊃control␈α⊃structure␈α∩with␈α⊃very
␈↓ α←␈↓explicit actions, controlled by a simple loop:
␈↓"∀␈↓ α←␈↓α␈↓ ∧Cloop <= λ[[]prog[[]
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬←l␈↓ εβcont[]
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬←␈↓ εβgo[l] ]]
␈↓"∀␈↓ α←␈↓The␈α
variable␈α
␈↓αcont␈↓␈α∞was␈α
a␈α
functional␈α∞variable,␈α
bound␈α
to␈α∞states␈α
and␈α
set␈α∞to␈α
the
␈↓ α←␈↓next␈α⊂state␈α∂by␈α⊂the␈α∂action␈α⊂of␈α⊂the␈α∂current␈α⊂state.␈α∂ This␈α⊂observation␈α⊂marks␈α∂the
␈↓ α←␈↓beginning␈α⊂of␈α⊂a␈α⊂traditional␈α⊂machine␈α∂description.␈α⊂It␈α⊂remains␈α⊂to␈α⊂separate␈α∂the
␈↓ α←␈↓actions␈αof␈αthe␈αmachine␈αfrom␈αthe␈αinstructions␈αit␈αis␈αexecuting.␈αThat␈αis,␈αsome␈αof
␈↓ α←␈↓the␈αdetails␈αof␈αthe␈αstate␈αtransformations␈αdeal␈αwith␈αthe␈αbookkeeping␈αwhich␈αthe
␈↓ α←␈↓machine␈α∀is␈α∪doing␈α∀to␈α∪discover␈α∀what␈α∀the␈α∪expression␈α∀is,␈α∪and␈α∀some␈α∀of␈α∪the
␈↓ α←␈↓transformations␈α→perform␈α→the␈α→actual␈α→evaluation␈α→of␈α→the␈α→expression.␈α_For
␈↓ α←␈↓example,␈αthe␈αmanipulation␈αof␈α␈↓αfun␈↓␈αand␈α␈↓αargs␈↓␈αis␈αpart␈αof␈αthe␈αactivity␈αto␈αdiscover
␈↓ α←␈↓the␈αform␈αof␈αthe␈αexpression.␈α
The␈αexecution␈αof␈α␈↓αsend␈↓␈αand␈α␈↓αreceive␈↓␈α
are␈αinvolved
␈↓ α←␈↓with␈αthe␈αevaluation.␈α The␈αparts␈αof␈αthe␈αevaluator␈αinvolved␈αwith␈αthe␈αexecution
␈↓ α←␈↓of␈αthe␈αexpression␈αwill␈αbecome␈αthe␈αinstructions␈αof␈αthe␈αmachine.␈αSupplied␈αwith
␈↓ α←␈↓an␈α
appropriate␈αexecution␈α
device,␈αa␈α
sequence␈α
of␈αthese␈α
instructions␈αcaptures␈α
the
␈↓ α←␈↓meaning␈αof␈αthe␈αevaluation␈αof␈αan␈αexpression.␈αIt␈αis␈αthe␈αbusiness␈αof␈αthis␈αsection
␈↓ α←␈↓to␈α⊃review␈α⊃the␈α∩evaluators␈α⊃and␈α⊃extract␈α⊃a␈α∩sufficient␈α⊃set␈α⊃of␈α∩instructions.␈α⊃ We
␈↓ α←␈↓begin␈α
that␈α∞task␈α
with␈α
some␈α∞examples,␈α
using␈α
␈↓αpeval␈↓␈α∞of␈α
Section 4.8␈α
as␈α∞the␈α
basic
␈↓ α←␈↓interpreter.
␈↓"β␈↓ α←␈↓␈↓ β'First,␈α∞the␈α∞evaluation␈α∞of␈α∞a␈α∞constant␈α∞␈↓αA␈↓␈α∞involves␈α∞the␈α∞recognition␈α∂that␈α∞we
␈↓ α←␈↓have␈α∩seen␈α∪a␈α∩constant;␈α∪that␈α∩is␈α∪part␈α∩of␈α∪the␈α∩control␈α∪of␈α∩the␈α∪evaluator.␈α∩ We
␈↓ α←␈↓evaluate␈αthat␈αconstant␈αby␈α␈↓αsend[denote[]]␈↓.␈αThe␈α␈↓αdenote␈↓␈αoperation␈αis␈αstill␈α
part␈αof
␈↓ α←␈↓the␈α⊂evaluator,␈α∂but␈α⊂the␈α⊂␈↓αsend␈↓␈α∂operation␈α⊂is␈α⊂an␈α∂instruction.␈α⊂ The␈α⊂execution␈α∂of
␈↓ α←␈↓␈↓αsend[A]␈↓␈αperforms␈αthe␈αevaluation.␈αThe␈α␈↓αrestore␈↓␈αoperation␈αreturns␈αthe␈αevaluator
␈↓ α←␈↓to␈αits␈α
previous␈αstate.␈α
We␈αmust␈α
allow␈αfor␈α
some␈αstate-saving␈α
in␈αour␈αrepertoire␈α
of
␈↓ α←␈↓instructions.␈αThe␈αevaluation␈αof␈αa␈αfunction␈αapplication,␈αlike␈α␈↓αg[A]␈↓,␈αinvolves␈αthe
␈↓ α←␈↓evaluation␈α↔of␈α⊗␈↓αA␈↓,␈α↔the␈α↔calling␈α⊗of␈α↔␈↓αg␈↓,␈α⊗and␈α↔a␈α↔means␈α⊗of␈α↔returning␈α↔to␈α⊗the
␈↓ α←␈↓␈↓310 Dynamic Structure␈↓
(6.2␈↓
␈↓"β␈↓ α←␈↓computation␈αsurrounding␈α␈↓αg[A]␈↓.␈α
Function␈αcalls␈αinvolve␈α
several␈αthings:␈αwe␈α
need
␈↓ α←␈↓space␈α∞to␈α∞contain␈α∞the␈α∞evaluated␈α∞arguments;␈α∞we␈α∞need␈α∞a␈α∞control␈α∂mechanism␈α∞to
␈↓ α←␈↓describe␈α→which␈α→argument␈α_is␈α→being␈α→evaluated;␈α_we␈α→need␈α→to␈α→suspend␈α_a
␈↓ α←␈↓computation␈α∀such␈α∀that␈α∀we␈α∀can␈α∀execute␈α∀the␈α∀function␈α∀with␈α∀the␈α∀evaluated
␈↓ α←␈↓arguments;␈α⊃and␈α⊂we␈α⊃must␈α⊂be␈α⊃able␈α⊂to␈α⊃return␈α⊂to␈α⊃the␈α⊃suspended␈α⊂computation
␈↓ α←␈↓when the function has completed its task.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∂necessary␈α∂ingredients␈α∂are␈α∂already␈α∞present␈α∂in␈α∂␈↓αpeval␈↓;␈α∂we␈α∂need␈α∞only
␈↓ α←␈↓extract␈αand␈αpackage␈αthem.␈αClearly␈α
␈↓αalloc_dest␈↓␈αis␈αinvolved␈αin␈αgetting␈αnew␈α
space
␈↓ α←␈↓for␈α∀the␈α∀evaluated␈α∀arguments.␈α∃There␈α∀is␈α∀a␈α∀second␈α∀required␈α∃activity␈α∀since
␈↓ α←␈↓␈↓αalloc_dest␈↓␈α∪always␈α∪occurs␈α∩in␈α∪conjunction␈α∪with␈α∪a␈α∩␈↓αsave␈↓␈α∪of␈α∪the␈α∪current␈α∩␈↓αdest␈↓.
␈↓ α←␈↓Therefore␈α∩we␈α⊃define␈α∩an␈α∩instruction␈α⊃named␈α∩␈↓αalloc␈↓␈α⊃which␈α∩saves␈α∩the␈α⊃current
␈↓ α←␈↓destination ␈↓¬and␈↓ intitializes a new ␈↓αdest␈↓ block.
␈↓"β␈↓ α←␈↓␈↓ β'Each␈α∩slot␈α∩of␈α∩the␈α∩destination␈α⊃block␈α∩is␈α∩filled␈α∩by␈α∩an␈α∩appropriate␈α⊃␈↓αsend␈↓
␈↓ α←␈↓operation.␈α↔Examination␈α↔of␈α↔the␈α↔sub-states␈α↔of␈α_␈↓αevalargs␈↓ (page 215)␈α↔reveals
␈↓ α←␈↓another␈α∪machine␈α∀instruction:␈α∪␈↓αnext[]␈↓␈α∀is␈α∪used␈α∀to␈α∪increment␈α∀the␈α∪destination
␈↓ α←␈↓pointer.
␈↓"β␈↓ α←␈↓␈↓ β'Finally,␈α∩after␈α⊃all␈α∩the␈α⊃arguments␈α∩are␈α⊃evaluated,␈α∩the␈α∩destination␈α⊃block
␈↓ α←␈↓must␈α
become␈α
the␈αlocal␈α
environment,␈α
and␈α
the␈αfunction␈α
can␈α
be␈α
called.␈αThus␈α
two
␈↓ α←␈↓more␈α_instructions:␈α_␈↓αlink␈↓␈α_will␈α→attach␈α_the␈α_destination␈α_block␈α_as␈α→the␈α_local
␈↓ α←␈↓environment␈α∞and␈α∞restore␈α∞the␈α∞previous␈α∂dest␈α∞block;␈α∞␈↓αcall␈↓␈α∞will␈α∞call␈α∂the␈α∞function
␈↓ α←␈↓after␈α∀saving␈α∀sufficient␈α∀control␈α∀information␈α∀so␈α∀that␈α∀we␈α∀may␈α∀return␈α∀after
␈↓ α←␈↓execution of the function is completed.
␈↓"β␈↓ α←␈↓␈↓ β'For␈αexample,␈αconsider␈α␈↓αf[g[A];h[B]]␈↓.␈αAssuming␈α␈↓αf␈↓␈αand␈α␈↓αg␈↓␈αare␈αλ-definitions
␈↓ α←␈↓with␈α
formal␈αparameters␈α
␈↓α[x;y]␈↓␈αand␈α
␈↓α[z]␈↓␈α
respectively,␈αand␈α
␈↓αh␈↓␈αis␈α
a␈α
primitive,␈αthen
␈↓ α←␈↓an instruction sequence might be:
␈↓"∀␈↓ α←␈↓αalloc[(X Y)]; alloc[(Z)]; send[A];
␈↓"β␈↓ α←␈↓α␈↓ ∧Olink[]; call[G]; next[];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ∧␈alloc[(G1)]; send[B]; link[];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ∧␈␈↓ ¬kcall[H]; link[]; call[F]
␈↓"∀␈↓ α←␈↓␈↓ β'There␈α
are␈α
two␈α
classes␈α
of␈α
instructions␈α
to␈α
break␈α
the␈α
sequential␈α
flow␈α
of␈αa
␈↓ α←␈↓machine␈α∃program:␈α∃we␈α∃transfer␈α∃control␈α∃when␈α∃we␈α∃call␈α∃or␈α∃return␈α⊗from␈α∃a
␈↓ α←␈↓function; and we transfer control when we execute a conditional expression.
␈↓"β␈↓ α←␈↓␈↓ β'Examination␈αof␈α␈↓αev2␈↓,␈α␈↓αev5␈↓,␈αand␈α␈↓αev6␈↓␈α(page 214)␈αreveals␈αsome␈αof␈αthe␈α
details
␈↓ α←␈↓of␈α⊂a␈α⊂function␈α⊂call-return␈α⊂sequence.␈α⊂ After␈α⊂saving␈α⊂the␈α⊃current␈α⊂environment,
␈↓ α←␈↓restoring␈α∞the␈α
saved␈α∞destination,␈α
and␈α∞saving␈α
a␈α∞continuation␈α
point,␈α∞we␈α
passed
␈↓ α←␈↓control␈α∞to␈α
the␈α∞body␈α∞of␈α
the␈α∞function.␈α
The␈α∞instruction␈α∞sequence,␈α
representing
␈↓ α←␈↓the␈αbody␈αof␈αthe␈αfunction,␈αwill␈αbe␈αterminated␈αby␈αa␈αcall␈αon␈α␈↓αret␈↓.␈αThis␈α
instruction
␈↓ α←␈↓will␈α∩restore␈α∩the␈α∪saved␈α∩environment␈α∩and␈α∪return␈α∩control␈α∩to␈α∪the␈α∩instruction
␈↓ α←␈↓immediately␈α~following␈α~the␈α~␈↓αcall␈↓␈α~instruction.␈α~The␈α~saved␈α~information␈α→is
␈↓ α←␈↓governed␈α∞by␈α∞the␈α∞variable␈α∞named␈α
␈↓αcontrol␈↓,␈α∞with␈α∞␈↓αcall␈↓␈α∞adding␈α∞information,␈α
and
␈↓ α←␈↓␈↓αret␈↓␈αremoving␈αinformation.␈αBefore␈αshowing␈αhow␈αinstructions␈αare␈αexecuted␈αand
␈↓ α←␈↓how␈α∂␈↓αcontrol␈↓␈α∂is␈α∂manipulated,␈α∂we␈α∂will␈α∂describe␈α∂the␈α∂primitives␈α∂for␈α∞conditional
␈↓ α←␈↓expressions.
␈↓ α←␈↓␈↓6.2␈↓ λ∪Primitives for LISP 311␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Examination␈α∃of␈α∃the␈α∃details␈α∃of␈α∃␈↓αevcond␈↓␈α∃and␈α∃its␈α⊗associated␈α∃functions
␈↓ α←␈↓(page 215),␈α⊂exhibits␈α∂more␈α⊂instructions.␈α⊂We␈α∂use␈α⊂the␈α∂evaluator␈α⊂to␈α⊂evaluate␈α∂a
␈↓ α←␈↓predicate;␈α∂we␈α∞then␈α∂␈↓αreceive␈↓␈α∞the␈α∂result␈α∂from␈α∞the␈α∂␈↓αdest␈↓-block.␈α∞ If␈α∂that␈α∂answer␈α∞is
␈↓ α←␈↓true,␈αwe␈α
evaluate␈αone␈αpath;␈α
otherwise␈αwe␈αevaluate␈α
another␈αpath.␈αWe␈α
see␈αtwo
␈↓ α←␈↓instructions␈αhere:␈α
a␈αtest␈α
and␈αjump␈α
instruction,␈αwhich␈α
we␈αshall␈αcall␈α
␈↓αreceive_test␈↓,
␈↓ α←␈↓which␈α⊂tests␈α⊂the␈α⊂contents␈α⊂of␈α⊃the␈α⊂current␈α⊂destination␈α⊂slot␈α⊂and␈α⊂jumps␈α⊃to␈α⊂one
␈↓ α←␈↓instruction␈α⊃sequence␈α∩if␈α⊃the␈α⊃result␈α∩is␈α⊃true,␈α⊃and␈α∩jumps␈α⊃to␈α∩(usually)␈α⊃another
␈↓ α←␈↓instruction␈α
sequence␈α
if␈α
the␈αresult␈α
is␈α
false.␈α
The␈αsecond␈α
instruction␈α
is␈α
a␈αmeans
␈↓ α←␈↓of␈αjumping␈αunconditionally␈αto␈αa␈αprescribed␈αinstruction␈αsequence.␈αThis␈αsecond
␈↓ α←␈↓instruction is named ␈↓αgoto␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'For␈α
example,␈α
a␈α∞conditional␈α
expression␈α
[␈↓↓p␈↓β1␈↓␈α∞→␈α
␈↓↓e␈↓β1␈↓;␈α
...;␈↓↓p␈↓βn␈↓␈α∞→␈α
␈↓↓e␈↓βn␈↓]␈α
has␈α∞a␈α
code
␈↓ α←␈↓sequence like:
␈↓"∀␈↓ α←␈↓␈↓ ∧∪␈↓ ∧C<instructions for p␈↓β1␈↓>
␈↓"β␈↓ α←␈↓␈↓ ∧∪␈↓ ∧C[␈↓αreceive_test␈↓[] → <code for ␈↓↓e␈↓β1␈↓>;␈↓αgoto[a0]␈↓]
␈↓"β␈↓ α←␈↓␈↓ ∧∪␈↓ ∧C<instructions for ␈↓↓p␈↓β2␈↓>
␈↓"β␈↓ α←␈↓␈↓ ∧∪␈↓ ∧C ...
␈↓"β␈↓ α←␈↓␈↓ ∧∪␈↓ ∧C<instructions for ␈↓↓p␈↓βn␈↓>
␈↓"β␈↓ α←␈↓␈↓ ∧∪␈↓ ∧C[␈↓αreceive_test␈↓[] → <code for ␈↓↓e␈↓βn␈↓>;␈↓αgoto[a0]␈↓]
␈↓"β␈↓ α←␈↓␈↓ ∧∪␈↓ ∧C␈↓αerr[NO_TRUE_COND_CLAUSE]
␈↓"β␈↓ α←␈↓α␈↓ ∧∪a0 . . .
␈↓"∀␈↓ α←␈↓Whenever␈α␈↓αreceive_test␈↓␈αis␈αtrue␈αwe␈αexecute␈αa␈αsequence␈αof␈αinstructions␈αand␈αthen
␈↓ α←␈↓transfer␈α∀out␈α∀of␈α∀the␈α∀conditional␈α∀using␈α∀the␈α∀␈↓αgoto␈↓.␈α∀ We␈α∀could␈α∀have␈α∪treated
␈↓ α←␈↓conditional␈α≠expressions␈α~like␈α≠special␈α~function␈α≠calls,␈α~saving␈α≠␈↓αa0␈↓␈α≠as␈α~the
␈↓ α←␈↓continuation␈α∂and␈α⊂restoring␈α∂it␈α⊂from␈α∂␈↓αcontrol␈↓␈α∂instead␈α⊂of␈α∂using␈α⊂␈↓αgoto␈↓.␈α∂ However
␈↓ α←␈↓conditional expressions don't require that extra generality.␈↓π 6␈↓
␈↓"β␈↓ α←␈↓␈↓ β'We␈α
can␈α
now␈αgive␈α
a␈α
more␈αdetailed␈α
picture␈α
of␈αa␈α
device␈α
which␈αcan␈α
execute
␈↓ α←␈↓this␈α↔instruction␈α⊗set.␈α↔A␈α⊗program␈α↔will␈α⊗be␈α↔represented␈α⊗as␈α↔a␈α↔sequence␈α⊗of
␈↓ α←␈↓instructions.␈α
Some␈α
of␈α
these␈α
instructions␈α
may␈α
be␈α
prefaced␈α
with␈α∞labels.␈α
These
␈↓ α←␈↓labels␈α∂either␈α∂represent␈α∂function␈α⊂names␈α∂or␈α∂names␈α∂used␈α∂within␈α⊂a␈α∂conditional
␈↓ α←␈↓expression.␈α
Given␈αa␈α
sequence␈αof␈α
instructions␈αnamed␈α
␈↓αinst_seq␈↓,␈αwe␈α
expect␈αthat
␈↓ α←␈↓they␈α⊂be␈α⊂executed␈α⊂in␈α⊂sequence,␈α∂unless␈α⊂some␈α⊂transfer␈α⊂of␈α⊂control␈α⊂occurs.␈α∂ For
␈↓ α←␈↓example,␈αthe␈αfollowing␈αprogram␈αsuffices␈αfor␈αthe␈αexecution␈αof␈αsuch␈αinstruction
␈↓ α←␈↓sequences:
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 6␈↓Our␈α∪treatment␈α∀of␈α∪conditionals␈α∀is␈α∪an␈α∪instance␈α∀of␈α∪"open␈α∀coding"␈α∪a
␈↓ α←␈↓function␈α⊂call.␈α⊂That␈α⊃means␈α⊂we␈α⊂replace␈α⊂a␈α⊃possible␈α⊂␈↓αcall-ret␈↓␈α⊂with␈α⊃the␈α⊂"in-line"
␈↓ α←␈↓instruction␈α∂sequence␈α∂which␈α∂makes␈α∂up␈α∂the␈α∂body␈α∂of␈α∂the␈α∂function.␈α⊂This␈α∂trick
␈↓ α←␈↓gives␈αfaster␈αexecution,␈αbut␈αtakes␈αmore␈αspace.␈αWe␈αwill␈αsee␈αanother␈αinstance␈αof
␈↓ α←␈↓"open-coding" when we compile macros in Section 6.18.
␈↓ α←␈↓␈↓312 Dynamic Structure␈↓
(6.2␈↓
␈↓"∀␈↓ α←␈↓α␈↓ βoloop <= λ[[inst_seq]prog[[i_s;pc]
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ ε∂␈↓ ε'i_s ← inst_seq;
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ ε∂l␈↓ ε'[null[i_s] → return[␈↓λ`␈↓αhalt]];
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ ε∂␈↓ ε'pc ← first[i_s];
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ ε∂␈↓ ε'i_s ← rest[i_s];
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ ε∂␈↓ ε'[not[is_label[pc]] → pc[]];
␈↓"β␈↓ α←␈↓α␈↓ βo␈↓ ε∂␈↓ ε'go[l] ]]
␈↓"∀␈↓ α←␈↓If␈α
␈↓αloop␈↓␈α∞returns␈α
␈↓αHALT␈↓,␈α∞then␈α
the␈α∞result␈α
of␈α∞our␈α
computation␈α∞is␈α
found␈α∞in␈α
␈↓αdest␈↓.
␈↓ α←␈↓Labels␈αare␈αnot␈αexecutable␈αinstructions,␈αand␈αare␈αtherefore␈αignored.␈α The␈αeffect
␈↓ α←␈↓of␈α␈↓αgoto␈↓␈αis␈α
to␈αreplace␈αthe␈α
current␈αinstruction␈αsequence␈α
with␈αthe␈αsequence␈α
which
␈↓ α←␈↓begins␈α∂immediately␈α∂after␈α∞the␈α∂label␈α∂which␈α∞is␈α∂the␈α∂argument␈α∞to␈α∂the␈α∂␈↓αgoto␈↓.␈α∞The
␈↓ α←␈↓effect␈αof␈α␈↓αcall-ret␈↓␈αis␈αa␈αbit␈αmore␈αcomplex.␈α We␈αdescribe␈α␈↓¬only␈↓␈αthe␈αcontrol␈αaspects
␈↓ α←␈↓of␈α∞␈↓αcall␈↓,␈α
leaving␈α∞the␈α
other␈α∞details␈α
until␈α∞later.␈α
Let␈α∞an␈α
instance␈α∞␈↓αcall[fn]␈↓␈α∞be␈α
the
␈↓ α←␈↓current␈αinstruction;␈αand␈αlet␈α␈↓αis␈↓λ'␈↓␈αbe␈αthe␈αcurrent␈αinstruction␈αsequence.␈αNote␈αthat
␈↓ α←␈↓␈↓αis␈↓λ'␈↓␈α⊃is␈α⊃the␈α⊃sequence␈α⊃immediately␈α⊃after␈α⊃the␈α⊃call.␈α⊃We␈α⊃save␈α⊃␈↓αis␈↓λ'␈↓␈α⊃on␈α⊃␈↓αcontrol␈↓␈α⊃by
␈↓ α←␈↓␈↓αcontrol ← concat[is␈↓λ'␈↓α;control]␈↓;␈α
then␈α
we␈α∞set␈α
␈↓αi_s␈↓␈α
to␈α∞the␈α
sequence␈α
beginning␈α∞at␈α
␈↓αfn␈↓.
␈↓ α←␈↓Execution of ␈↓αgo[l]␈↓ sends us to label ␈↓αl␈↓ and we begin executing the body of ␈↓αfn␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'We leave ␈↓αfn␈↓ by executing ␈↓αret␈↓. This instruction performs
␈↓"∀␈↓ α←␈↓␈↓ ∧M␈↓αi_s ← first[control]; control ← rest[control]␈↓;
␈↓"∀␈↓ α←␈↓and we are back at the instruction following the ␈↓αcall␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Part␈α⊃of␈α⊃the␈α⊂execution␈α⊃of␈α⊃␈↓αcall␈↓␈α⊃and␈α⊂␈↓αgoto␈↓␈α⊃involves␈α⊃locating␈α⊃the␈α⊂desired
␈↓ α←␈↓label.␈α∞Since␈α∞we␈α∞have␈α∞saved␈α∞the␈α∞original␈α∞instruction␈α∞sequence␈α∞we␈α∞can␈α∞search
␈↓ α←␈↓that␈α∞list␈α∞for␈α∞the␈α∂desired␈α∞label.␈α∞We␈α∞will␈α∂see␈α∞more␈α∞effective␈α∞ways␈α∂for␈α∞locating
␈↓ α←␈↓labels in Section 6.5.
␈↓"β␈↓ α←␈↓␈↓ ¬ ␈↓↓6.3 SM: A Simple Machine
␈↓"β␈↓ α←␈↓This␈α
section␈αdescribes␈α
a␈αsimple␈α
machine␈αwhich␈α
has␈αa␈α
sufficient␈αinstruction␈α
set
␈↓ α←␈↓to␈α
describe␈α
the␈α
LISP␈α
primitives␈α
in␈α
terms␈α
of␈α
a␈α
more␈α
conventional␈α
machine.␈↓π 7␈↓
␈↓ α←␈↓Note␈α
that␈α∞this␈α
machine␈α
is␈α∞␈↓¬not␈↓␈α
necessary␈α
for␈α∞our␈α
understanding␈α
of␈α∞␈↓αeval␈↓.␈α
The
␈↓ α←␈↓evaluator␈α⊃is␈α⊃self-descriptive.␈α∩We␈α⊃need␈α⊃describe␈α∩a␈α⊃machine␈α⊃only␈α∩to␈α⊃discuss
␈↓ α←␈↓lower␈α
level␈α
implementation␈α
and␈α
compilation.␈α
Indeed,␈α
this␈α
is␈α
an␈α∞objection␈α
to
␈↓ α←␈↓describing␈α∞meaning␈α∞of␈α∞programming␈α∞languages␈α∞in␈α∞terms␈α∞of␈α∞a␈α∂compiler:␈α∞you
␈↓ α←␈↓must then understand ␈↓¬two␈↓ things, the language ␈↓¬and␈↓ a machine.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αsimple␈α
machine,␈α␈↓↓SM␈↓,␈αhas␈α
a␈αslight␈αsimilarity␈α
to␈αthe␈α
organization␈αof
␈↓ α←␈↓the␈αPDP-10␈α
[DEC 69],␈αhowever␈α
we␈αneed␈α
very␈αfew␈α
features␈αto␈α
illuminate␈αthe
␈↓ α←␈↓interesting␈α∂facets␈α⊂of␈α∂our␈α∂primitives.␈α⊂If␈α∂we␈α∂were␈α⊂to␈α∂implement␈α⊂a␈α∂production
␈↓ α←␈↓LISP,␈αmany␈αmore␈αinstructions␈αwould␈αbe␈αnecessary.␈αSimilarly,␈αour␈α␈↓↓SM␈↓␈αsuffices
␈↓ α←␈↓for␈α⊃a␈α⊂description␈α⊃of␈α⊂compilation␈α⊃algorithms,␈α⊂but␈α⊃if␈α⊂we␈α⊃wished␈α⊃to␈α⊂perform
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 7␈↓See also [Deu 73].
␈↓ α←␈↓␈↓6.3␈↓ πe␈↓↓SM␈↓: A Simple Machine 313␈↓↓
␈↓"β␈↓ α←␈↓highly␈αefficient␈αcompilation␈αfor␈αa␈αproduction␈αLISP␈αsystem,␈αwe␈αwould␈αrequire
␈↓ α←␈↓a␈α∩full␈α⊃instruction␈α∩set.␈α∩ The␈α⊃point␈α∩now␈α∩is␈α⊃to␈α∩understand␈α∩basic␈α⊃algorithms.
␈↓ α←␈↓When␈αthat␈α
is␈αaccomplished␈αit␈α
is␈αreasonable␈αto␈α
examine␈αproblems␈αof␈α
efficiency
␈↓ α←␈↓and␈αdetails␈αof␈αimplementation.␈α We␈αaddress␈αsome␈αof␈αthe␈αtechniques␈αavailable
␈↓ α←␈↓for optimization of compiler code in later sections.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓↓SM␈↓␈α∞has␈α∂a␈α∞conventional␈α∞addressable␈α∂main␈α∞memory,␈α∂including␈α∞registers,
␈↓ α←␈↓␈↓αAC1,␈α∩AC2,␈α∩...,␈α∩ACn␈↓␈α∩addressable␈α∩as␈α∩memory␈α∩locations␈α∩␈↓α0␈↓␈α∩through␈α∪␈↓αn␈↓.␈α∩These
␈↓ α←␈↓registers,␈α
called␈α
␈↓↓accumulators␈↓,␈α
will␈α
be␈α
used␈α
as␈α
pointer␈α
registers.␈α
Each␈α
memory
␈↓ α←␈↓location␈αis␈αassumed␈αto␈αbe␈αlarge␈αenough␈αto␈αcontain␈αtwo␈αaddresses.␈α For␈αsake␈αof
␈↓ α←␈↓discussion,␈α∞assume␈α∞the␈α∞word␈α∞size␈α∞is␈α
36␈α∞bits.␈α∞ One␈α∞mapping␈α∞of␈α∞a␈α
dotted-pair
␈↓ α←␈↓onto␈α
an␈α
␈↓↓SM␈↓␈α
location␈α∞is␈α
straightforward:␈α
the␈α
␈↓αcar␈↓␈α∞maps␈α
to␈α
the␈α
left-half␈α∞of␈α
the
␈↓ α←␈↓word;␈α
the␈α
␈↓αcdr␈↓,␈α
to␈α
the␈α
right.␈α The␈α
addressing␈α
space␈α
for␈α
dotted␈α
pairs␈αis␈α
therefore
␈↓ α←␈↓2␈↓π18␈↓.␈α
A␈αmemory␈α
area␈α
is␈αset␈α
aside␈α
to␈αcontain␈α
such␈α
dotted␈αpairs.␈α
A␈αmemory␈α
area
␈↓ α←␈↓is␈α⊃also␈α⊃dedicated␈α⊃to␈α⊃full-word␈α⊃space;␈α⊃all␈α⊃p-names␈α⊃and␈α⊃numbers␈α⊃are␈α⊃stored
␈↓ α←␈↓there.
␈↓"β␈↓ α←␈↓␈↓ β'Parts␈α∩of␈α∩␈↓↓SM␈↓␈α∩memory␈α∩can␈α∪be␈α∩designated␈α∩as␈α∩stacks.␈α∩Each␈α∩stack␈α∪is␈α∩a
␈↓ α←␈↓contiguous␈α
area␈α
of␈α
memory,␈α∞and␈α
the␈α
current␈α
top␈α∞of␈α
a␈α
stack␈α
is␈α∞referenced␈α
by
␈↓ α←␈↓one␈αof␈αthe␈αregisters,␈α␈↓αP1,␈α...,␈αPj␈↓;␈αthese␈αregisters␈αare␈αcalled␈α␈↓↓stack-pointers␈↓.␈↓π 8␈↓␈αThe
␈↓ α←␈↓stacks␈α⊃will␈α⊃be␈α∩used␈α⊃to␈α⊃contain␈α⊃the␈α∩partial␈α⊃results␈α⊃of␈α⊃calculations␈α∩and␈α⊃will
␈↓ α←␈↓contain␈α∂the␈α∞information␈α∂necessary␈α∞to␈α∂implement␈α∞the␈α∂function␈α∂exit␈α∞sequence.
␈↓ α←␈↓In␈α
our␈α
compilers,␈α
a␈α
single␈α∞stack␈α
will␈α
suffice␈α
for␈α
saving␈α∞partial␈α
computations,
␈↓ α←␈↓environments,␈α∃as␈α∀well␈α∃as␈α∃control␈α∀information.␈α∃This␈α∀single␈α∃stack␈α∃will␈α∀be
␈↓ α←␈↓referred to by ␈↓αP␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α∂are␈α∂only␈α⊂three␈α∂classes␈α∂of␈α⊂instructions␈α∂necessary␈α∂to␈α⊂describe␈α∂our
␈↓ α←␈↓implementation:␈α⊃instructions␈α∩for␈α⊃constant␈α∩generation,␈α⊃instructions␈α∩for␈α⊃stack
␈↓ α←␈↓manipulation, and instructions for flow of control.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αcontrol␈αinstructions␈αand␈αsome␈αof␈αthe␈αstack␈αinstructions␈αrefer␈αto␈αthe
␈↓ α←␈↓program␈α
counter␈α
of␈α
␈↓↓SM␈↓.␈α
This␈α
counter␈α
is␈α
designated␈α
as␈α
␈↓↓PC␈↓.␈α
In␈α
the␈α
following,␈α
␈↓αC␈↓
␈↓ α←␈↓means␈α"contents␈αof...";␈α␈↓αac␈↓␈αmust␈αdenote␈αan␈αaccumulator;␈α␈↓αloc␈↓␈αmeans␈αany␈αmemory
␈↓ α←␈↓location.
␈↓"β␈↓ α←␈↓␈↓ β'Here are the instructions:
␈↓"∀␈↓ α←␈↓αMOVEI ac const␈↓ ∧CC(ac) ← const
␈↓"∀␈↓ α←␈↓αPUSH P ac␈↓ ∧CC(P) ← C(P)+1␈↓ ε?␈↓Increment stack pointer␈↓α.
␈↓"β␈↓ α←␈↓α␈↓ ∧CC(C(P)) ← C(ac)␈↓ ε?␈↓Copy contents of ␈↓αac␈↓ to top of stack.␈↓α
␈↓"∀␈↓ α←␈↓αPOP P ac␈↓ ∧CC(ac) ← C(C(P))␈↓ ε?␈↓Copy top of stack into ␈↓αac.
␈↓"β␈↓ α←␈↓α␈↓ ∧CC(P) ← C(P)-1␈↓ ε?␈↓Decrement stack pointer.␈↓α
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 8␈↓On the PDP-10 a stack pointer must be one of the ␈↓αAC␈↓ registers.
␈↓ α←␈↓␈↓314 Dynamic Structure␈↓
(6.3␈↓
␈↓"∀␈↓ α←␈↓α␈↓The next two instructions implement a function calling mechanism.␈↓α
␈↓"∀␈↓ α←␈↓αPUSHJ P loc␈↓ ∧CC(P) ← C(P)+1␈↓ ε?␈↓Increment stack pointer␈↓α.
␈↓"β␈↓ α←␈↓α␈↓ ∧CC(C(P)) ← C(␈↓↓PC␈↓α)␈↓ ε?␈↓Place address following the ␈↓αPUSHJ␈↓.
␈↓"β␈↓ α←␈↓␈↓ ∧CC(␈↓↓PC␈↓α) ← loc␈↓ ε?␈↓in the stack. Then change control to
␈↓"β␈↓ α←␈↓␈↓ ∧C␈↓ ε?location ␈↓αloc␈↓.
␈↓"∀␈↓ α←␈↓␈↓αPOPJ P␈↓ ∧CC(␈↓↓PC␈↓α) ← C(C(P))␈↓ ε?␈↓Copy top of stack into ␈↓↓PC␈↓.
␈↓"β␈↓ α←␈↓␈↓ ∧C␈↓αC(P) ← C(P)-1␈↓ ε?␈↓Decrement stack pointer.
␈↓"∀␈↓ α←␈↓We␈α⊗have␈α⊗ignored␈α⊗some␈α⊗of␈α⊗the␈α⊗details␈α⊗of␈α⊗stack␈α⊗operations;␈α↔each␈α⊗stack
␈↓ α←␈↓operations␈α
must␈α
consider␈α
boundary␈α
conditions␈α
on␈α
the␈α
storage␈α
allocated␈αfor␈α
the
␈↓ α←␈↓stack.␈α
Any␈α
condition␈α
which␈α
would␈α
violate␈α
these␈α
bounds␈α
must␈α∞be␈α
detectable.
␈↓ α←␈↓If␈α⊂a␈α∂stack␈α⊂is␈α∂allocated␈α⊂in␈α∂a␈α⊂discontinuous␈α∂fashion ([Bis 74])␈α⊂then␈α⊂a␈α∂storage
␈↓ α←␈↓management␈α
decision␈α
must␈α
be␈α
made;␈α
if␈α
the␈α
stacks␈α
are␈α
of␈α
fixed␈α
size,␈α
then␈α
an
␈↓ α←␈↓error must be signaled.
␈↓"∀␈↓ α←␈↓αMOVE ac loc ␈↓ ∧CC(ac) ← C(loc) ␈↓␈↓ ε?This␈α
is␈α
an␈αinstruction␈α
to␈α
load␈αa␈α
specified
␈↓ α←␈↓␈↓ εK␈↓αac␈↓␈α
with␈α
the␈α
contents␈α
of␈α
␈↓αloc␈↓.␈α
Note␈α
␈↓αloc␈↓␈α
may
␈↓ α←␈↓␈↓ εKbe an ␈↓αac␈↓; e.g. ␈↓αMOVE AC␈↓β1␈↓α AC␈↓β2␈↓.
␈↓" ␈↓ α←␈↓αMOVEM ac loc ␈↓ ∧CC(loc) ← C(ac) ␈↓ ε?␈↓Copy␈α≠contents␈α≠of␈α≠␈↓αac␈↓␈α≠into␈α≠␈↓αloc␈↓.␈α≠ For
␈↓ α←␈↓␈↓ εKexample,
␈↓ α←␈↓␈↓ εK␈↓αMOVEM AC␈↓β1␈↓α AC␈↓β2␈↓α=MOVE AC␈↓β2␈↓α AC␈↓β1␈↓.
␈↓"∀␈↓ α←␈↓αSUB ac loc␈↓ ∧CC(ac) ← C(ac) - C(loc)
␈↓"∀␈↓ α←␈↓αJUMP loc␈↓ ∧CC(␈↓↓PC␈↓α) ← loc␈↓ ε?␈↓Go to location ␈↓αloc␈↓.
␈↓"∀␈↓ α←␈↓␈↓αJUMPF ac loc␈↓ ∧C␈↓↓if ␈↓αC(ac)=␈↓
f␈↓ ␈↓↓then␈↓α C(␈↓↓PC␈↓α) ← loc
␈↓"∀␈↓ α←␈↓αJUMPT␈α→ac␈α→loc ␈↓↓if␈α_␈↓αC(ac)␈↓λ≠␈↓
f␈↓␈α→␈↓↓then␈α→␈↓αC(␈↓↓PC␈↓α)␈α_←␈α→loc. ␈↓Note␈α→that␈α_␈↓αJUMPT␈↓
␈↓ α←␈↓␈↓ εKimplements␈α)the␈α(coding␈α)trick␈α(of
␈↓ α←␈↓␈↓ εKSection 5.5␈α*which␈α)maps␈α*␈↓
t␈↓␈α)onto
␈↓ α←␈↓␈↓ εKeverything which is not false.
␈↓"∀␈↓ α←␈↓␈↓ β'These␈α⊂instructions␈α⊂are␈α∂executed␈α⊂by␈α⊂a␈α∂machine␈α⊂whose␈α⊂basic␈α∂execution
␈↓ α←␈↓cycle is essentially:
␈↓"∀␈↓ α←␈↓α␈↓ ∧C␈↓↓l:␈↓α␈↓ ∧gC(␈↓↓IR␈↓α) ← C(C(␈↓↓PC␈↓α))
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧gC(␈↓↓PC␈↓α) ← C(␈↓↓PC␈↓α) + 1
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧g ␈↓↓execute ␈↓αC(␈↓↓IR␈↓α)
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ∧g␈↓↓go to l
␈↓"∀␈↓ α←␈↓The␈α⊃␈↓↓IR␈↓,␈α⊃or␈α⊃␈↓↓Instruction␈α⊃register␈↓,␈α⊃is␈α⊂an␈α⊃internal␈α⊃register␈α⊃used␈α⊃to␈α⊃hold␈α⊂the
␈↓ α←␈↓current␈αinstruction.␈αNote␈αthat␈αthe␈α␈↓↓PC␈↓␈αregister␈αis␈αincremented␈α␈↓¬before␈↓␈αexecution
␈↓ α←␈↓of␈α↔the␈α_instruction.␈α↔If␈α↔we␈α_incremented␈α↔␈↓↓PC␈↓␈α↔␈↓¬after␈↓␈α_the␈α↔execution␈α_of␈α↔the
␈↓ α←␈↓instruction,␈α
and␈α∞the␈α
instruction␈α∞were␈α
a␈α
JUMP-type␈α∞instruction,␈α
then␈α∞the␈α
␈↓↓PC␈↓
␈↓ α←␈↓would get a spurious incrementation.
␈↓"β␈↓ α←␈↓␈↓ β'A␈αcritical␈αpart␈αof␈αLISP␈αevaluation␈αinvolves␈αprocedure␈αcalls␈αand␈α
returns.
␈↓ α←␈↓Since␈α∀we␈α∀expect␈α∃to␈α∀handle␈α∀recursive␈α∃calling␈α∀sequences,␈α∀the␈α∃␈↓αcall-ret␈↓␈α∀pair
␈↓ α←␈↓␈↓6.3␈↓ πe␈↓↓SM␈↓: A Simple Machine 315␈↓↓
␈↓"β␈↓ α←␈↓(page 310),␈α∩represented␈α∩as␈α∪␈↓αCALL␈↓␈α∩and␈α∩␈↓αRET␈↓,␈α∪must␈α∩take␈α∩this␈α∪into␈α∩account.
␈↓ α←␈↓However,␈α∞there␈α∞is␈α∞a␈α∞more␈α∂fundamental␈α∞requirement␈α∞of␈α∞this␈α∞pair:␈α∂they␈α∞must
␈↓ α←␈↓make␈α∩sure␈α∪that,␈α∩on␈α∪completion␈α∩of␈α∪a␈α∩␈↓αCALL␈↓,␈α∪the␈α∩␈↓αRET␈↓␈α∪can␈α∩return␈α∪to␈α∩the
␈↓ α←␈↓instruction␈α∀which␈α∃directly␈α∀follows␈α∃the␈α∀␈↓αCALL␈↓.␈α∃ This␈α∀requirement␈α∃can␈α∀be
␈↓ α←␈↓accomplished␈αby␈αa␈αless␈αcomprehensive␈αcall,␈αsay␈α␈↓αJSR␈↓,␈α(for␈αJump␈αSubRoutine),
␈↓ α←␈↓which␈α⊂stores␈α⊂the␈α⊂current␈α⊂value␈α⊂of␈α∂the␈α⊂␈↓↓PC␈↓␈α⊂in␈α⊂a␈α⊂known␈α⊂location.␈α⊂Then␈α∂the
␈↓ α←␈↓return,␈α∂␈↓αJRTH␈↓,␈α∂(for␈α⊂Jump␈α∂THrough),␈α∂need␈α∂only␈α⊂pick␈α∂up␈α∂that␈α⊂saved␈α∂value
␈↓ α←␈↓and␈α∩restore␈α∩it␈α⊃into␈α∩the␈α∩␈↓↓PC␈↓.␈α⊃We␈α∩could␈α∩implement␈α⊃this␈α∩instruction␈α∩on␈α⊃our
␈↓ α←␈↓machine.␈α⊂ Recall␈α⊂that␈α⊂in␈α⊂the␈α∂basic␈α⊂machine␈α⊂cycle␈α⊂the␈α⊂␈↓↓PC␈↓␈α⊂was␈α∂incremented
␈↓ α←␈↓␈↓¬before␈↓␈α∞the␈α
execution␈α∞of␈α∞the␈α
instruction.␈α∞Thus␈α
if␈α∞we␈α∞were␈α
about␈α∞to␈α∞execute␈α
a
␈↓ α←␈↓␈↓αJSR␈↓␈αthe␈α␈↓↓PC␈↓␈αis␈αalready␈αpointing␈αat␈αthe␈αnext␈αinstruction;␈αall␈αwe␈αneed␈αto␈αdo␈αis
␈↓ α←␈↓save␈α
the␈αcurrent␈α
␈↓↓PC␈↓.␈α
So␈αlet's␈α
assume␈αthat␈α
␈↓αJSR␈α
loc␈↓␈αstores␈α
the␈α␈↓↓PC␈↓␈α
in␈α
␈↓αloc␈↓␈αand
␈↓ α←␈↓begins execution in location ␈↓αloc+1␈↓. Then:
␈↓"∀␈↓ α←␈↓␈↓αJSR loc␈↓ ∧CC(loc) ← C(␈↓↓PC␈↓α)␈↓ ε?␈↓Save the ␈↓↓PC␈↓ in ␈↓αloc␈↓.
␈↓"β␈↓ α←␈↓␈↓ ∧C␈↓αC(␈↓↓PC␈↓α) ← loc + 1␈↓ ε?␈↓Jump to location ␈↓αloc + 1␈↓.
␈↓" ␈↓ α←␈↓␈↓αJRTH loc␈↓␈↓ ∧C␈↓αC(␈↓↓PC␈↓α) ← C(loc)
␈↓"∀␈↓ α←␈↓This␈α∞pair␈α∞is␈α∞sufficient␈α∞to␈α∞implement␈α∞simple␈α∞non-recursive␈α∂calling␈α∞sequences.
␈↓ α←␈↓It's␈αfast␈αand␈αefficient;␈αhowever␈αit␈αis␈αnot␈αsufficient␈αfor␈αrecursive␈αcontrol.␈α If␈αwe
␈↓ α←␈↓always␈α∂store␈α∂in␈α∂a␈α∂␈↓¬fixed␈↓␈α∂location,␈α∂only␈α∂the␈α∂result␈α∂of␈α∂the␈α∂␈↓¬last␈↓␈α∂store␈α∂would␈α∞be
␈↓ α←␈↓available␈αand␈αprevious␈αvalues␈αset␈αby␈αprior␈αrecursions␈αwould␈αhave␈αbeen␈αlost.␈↓π 9␈↓
␈↓ α←␈↓What␈αwe␈α
need␈αis␈αan␈α
implementation␈αof␈α
the␈αactions␈αof␈α
␈↓αcontrol␈↓.␈α For␈αpurpose␈α
of
␈↓ α←␈↓our␈αdiscussion␈αwe␈αcan␈αassume␈αthat␈α␈↓αcontrol␈↓␈αoperates␈αin␈αa␈αstack-like␈αfashion.␈↓π 10␈↓
␈↓ α←␈↓What␈α⊂the␈α∂␈↓αCALL␈↓␈α⊂will␈α∂do␈α⊂is␈α⊂␈↓¬push␈↓␈α∂the␈α⊂current␈α∂contents␈α⊂of␈α∂the␈α⊂␈↓↓PC␈↓␈α⊂onto␈α∂the
␈↓ α←␈↓control␈αstack;␈α
and␈α␈↓αRET␈↓␈α
will␈αpop␈αoff␈α
the␈αtop␈α
element␈αand␈αput␈α
it␈αinto␈α
the␈α␈↓↓PC␈↓
␈↓ α←␈↓register.␈↓π 11␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈α_behavior␈α_we␈α_have␈α_just␈α↔described␈α_is␈α_that␈α_attributed␈α_to␈α↔the
␈↓ α←␈↓␈↓αPUSHJ-POPJ␈↓␈α∂pair␈α∂when␈α∂they␈α∂are␈α∂applied␈α∂to␈α∂the␈α∂control␈α∂stack.␈α∂We␈α∞have
␈↓ α←␈↓separated␈α
out␈αthe␈α
␈↓αCALL-RET␈↓␈αpair␈α
since␈α
the␈αcalling␈α
process␈αis␈α
not␈α
always␈αas
␈↓ α←␈↓simple as ␈↓αPUSHJ-POPJ␈↓. Several things impinge on our decision:
␈↓"∀␈↓ α←␈↓␈↓ β3␈↓↓1.␈↓ We␈α
want␈α
to␈α
be␈α
able␈α
to␈α
supply␈α
detailed␈α
debugging␈α
information␈αto␈α
the
␈↓ α←␈↓␈↓ βcuser.␈α~How␈α~this␈α~will␈α~be␈α→accomplished␈α~will␈α~be␈α~the␈α~topic␈α→of
␈↓ α←␈↓␈↓ βcSection 6.23.
␈↓" ␈↓ α←␈↓␈↓ β3␈↓↓2.␈↓ We␈αwant␈α
to␈αbe␈α
able␈αto␈α
freely␈αreplace␈α
functions␈αwith␈αnew␈α
definitions.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 9␈↓The␈αprogrammer␈αcould␈αhave␈α
saved␈αand␈αrestored␈αthe␈αreturn␈α
points␈αin
␈↓ α←␈↓an␈α∂internal␈α∂stack,␈α∂but␈α∂that␈α∂is␈α∂wasteful␈α∂of␈α∂programmer␈α∂time,␈α⊂memory␈α∂space,
␈↓ α←␈↓and succeptible to errors.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 10␈↓Unless␈α∪we␈α∪consider␈α∪extensions␈α∪of␈α∪LISP,␈α∪a␈α∪stack␈α∪is␈α∀sufficient␈α∪for
␈↓ α←␈↓LISP's control environment.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 11␈↓What␈αwill␈α
be␈αfound␈α
on␈αthe␈αcontrol␈α
stack␈αis␈α
a␈αtime-sequence␈α
of␈αthose
␈↓ α←␈↓procedures␈αwhich␈αhave␈αbeen␈αentered␈αbut␈αhave␈αnot␈αyet␈αbeen␈α
completed.␈αSuch
␈↓ α←␈↓information is exceptionally useful in debugging programs.
␈↓ α←␈↓␈↓316 Dynamic Structure␈↓
(6.3␈↓
␈↓"β␈↓ α←␈↓␈↓ βcA␈α≡␈↓αPUSHJ␈↓␈α≡transfers␈α≡control␈α≡to␈α≡a␈α≡particular␈α≡sequence␈α≥of
␈↓ α←␈↓␈↓ βcinstructions.
␈↓" ␈↓ α←␈↓␈↓ β3␈↓↓3.␈↓ We␈α
want␈αto␈α
be␈αable␈α
to␈α
intermix␈αcompiled␈α
and␈αinterpreted␈α
programs.
␈↓ α←␈↓␈↓ βcCompiled␈αprograms␈αmay␈αcall␈αinterpreted␈αprograms,␈αand␈αvice␈αversa.
␈↓ α←␈↓␈↓ βcIndeed␈α∩we␈α∩may␈α∩even␈α∩wish␈α∩to␈α∩replace␈α∩an␈α∩interpreted␈α∩(compiled)
␈↓ α←␈↓␈↓ βcdefinition with a compiled (interpreted) version.
␈↓" ␈↓ α←␈↓␈↓ β3␈↓↓4.␈↓ In␈α
dealing␈α
with␈α
functional␈α
arguments,␈α
we␈α
must␈α
be␈α
able␈α
to␈α
transfer
␈↓ α←␈↓␈↓ βccontrol␈αto␈α
a␈αfunction␈α
variable.␈αWe␈α
cannot␈αknow␈α
where␈αthe␈α
␈↓αPUSHJ␈↓
␈↓ α←␈↓␈↓ βcshould transfer.
␈↓"∀␈↓ α←␈↓␈↓ β'When␈α
an␈α
interpreted␈α
function␈α
calls␈α
a␈α
compiled␈α
(or␈α
primitive)␈αfunction,
␈↓ α←␈↓␈↓αeval␈↓␈αwill␈αlook␈αfor␈αthe␈αindicator,␈α␈↓αSUBR␈↓;␈αthen␈αretrieve␈αthe␈αmachine␈α
address␈αof
␈↓ α←␈↓the␈αcode␈αand␈αenter␈αvia␈α
a␈α␈↓αPUSHJ␈↓.␈αThat␈αcode␈αshould␈α
exit␈α(back␈αto␈α␈↓αeval␈↓)␈αvia␈α
a
␈↓ α←␈↓␈↓αPOPJ␈↓,␈α∀after␈α∃assuring␈α∀that␈α∀any␈α∃internal␈α∀stacks␈α∀have␈α∃been␈α∀appropriately
␈↓ α←␈↓restored.
␈↓"β␈↓ α←␈↓␈↓ β'Compiled␈α∂functions␈α∂call␈α∂other␈α∂functions␈α∂via␈α∂␈↓αCALL␈↓.␈α∂ The␈α∂␈↓αCALL␈↓␈α∂must
␈↓ α←␈↓discover␈α∂how␈α∂to␈α∂call␈α∂the␈α∂function:␈α⊂is␈α∂it␈α∂a␈α∂␈↓αSUBR,␈α∂EXPR␈↓,␈α∂an␈α⊂␈↓αFEXPR␈↓,␈α∂etc?
␈↓ α←␈↓The␈α
function␈α
is␈α
called␈α
and␈α
on␈α
completion␈α
control␈α
is␈α
returned␈α
to␈α
the␈α
address
␈↓ α←␈↓immediately␈α∞following␈α∞the␈α∞␈↓αCALL␈↓.␈α
For␈α∞example,␈α∞␈↓αCALL␈↓␈α∞can␈α∞be␈α
implemented
␈↓ α←␈↓as␈α␈↓α(PUSHJ␈αP␈αDECODE)␈↓,␈αwhere␈α␈↓αP␈↓␈αrepresents␈αthe␈αcontrol␈αstack␈αpointer,␈αand
␈↓ α←␈↓␈↓αDECODE␈↓␈α∂represents␈α∂a␈α∂routine␈α∂to␈α∂decode␈α∂the␈α∂actual␈α∂procedure␈α∂call.␈α∞Within
␈↓ α←␈↓␈↓αdecode␈↓␈αwe␈α
know␈αthat␈α␈↓αC(C(P)-1)␈↓␈α
is␈αthe␈αactual␈α
call␈αinstruction;␈α␈↓αdecode␈↓␈α
then␈αcan
␈↓ α←␈↓access␈α∂the␈α∂function␈α∂definition␈α∂associated␈α∂with␈α∂␈↓αfn␈↓,␈α∂set␈α∂up␈α∂the␈α∂call,␈α∂and␈α∂then
␈↓ α←␈↓return via a ␈↓αPOPJ␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Within␈αany␈α␈↓αCALL␈↓␈αor␈α␈↓αPUSHJ␈↓␈αwe␈αmay␈αcall␈αany␈αfunction,␈αincluding␈α
that
␈↓ α←␈↓function␈αitself.␈α This␈αbrings␈αus␈αto␈αone␈αof␈αthe␈αmost␈αimportant␈αconventions␈αfor
␈↓ α←␈↓␈↓¬any␈↓␈α
stack-like␈α
call-return␈α
sequence:␈α
Whatever␈αwe␈α
push␈α
onto␈α
a␈α
stack␈αwithin␈α
the
␈↓ α←␈↓body␈α∂of␈α⊂a␈α∂function␈α⊂␈↓¬must␈↓␈α∂be␈α⊂popped␈α∂off␈α⊂before␈α∂we␈α⊂exit␈α∂from␈α⊂the␈α∂function
␈↓ α←␈↓body.␈αThat␈α
is,␈αthe␈αstate␈α
of␈αany␈α
stack␈αmust␈αbe␈α
transparent␈αto␈αany␈α
computations
␈↓ α←␈↓which occur within the function. This is called ␈↓↓stack synchronization␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Usually␈α↔the␈α⊗effect␈α↔of␈α⊗␈↓αRET␈↓␈α↔is␈α⊗identical␈α↔to␈α⊗␈↓αPOPJ␈↓,␈α↔however␈α↔it␈α⊗is
␈↓ α←␈↓conceivable␈α∞that␈α
we␈α∞might␈α
expect␈α∞that␈α
complex␈α∞returns␈α
require␈α∞special␈α
care.
␈↓ α←␈↓The␈α⊗basic␈α⊗idea␈α∃in␈α⊗this␈α⊗discussion␈α∃is␈α⊗that␈α⊗we␈α∃will␈α⊗supply␈α⊗two␈α∃similar,
␈↓ α←␈↓compatible,␈α⊃but␈α⊃not␈α∩identical␈α⊃call-return␈α⊃sequences:␈α⊃␈↓αPUSHJ-POPJ␈↓␈α∩is␈α⊃fast
␈↓ α←␈↓and simple; the other, ␈↓αCALL-RET␈↓, is more general but more costly to invoke.
␈↓"β␈↓ α←␈↓␈↓ β'In␈α_the␈α_next␈α_section␈α_we␈α_will␈α_reconcile␈α_LISP␈α_primitives␈α_with␈α_the
␈↓ α←␈↓instruction set supplied on ␈↓↓SM␈↓.
␈↓"β␈↓ α←␈↓␈↓ ∧U␈↓↓6.4 Implementation of the Primitives␈↓
␈↓"β␈↓ α←␈↓As␈α
with␈α∞any␈α
representation␈α∞problem,␈α
several␈α
choices␈α∞are␈α
available.␈α∞ We␈α
will
␈↓ α←␈↓begin␈αour␈αuse␈α
of␈α␈↓↓SM␈↓␈αwith␈αa␈α
study␈αof␈αcall-by-value␈α
function␈αcalls;␈αlater␈αwe␈α
will
␈↓ α←␈↓discuss␈α
other␈α
calling␈αsequences.␈α
We␈α
will␈αdiscuss␈α
two␈α
general␈αimplementation
␈↓ α←␈↓␈↓6.4␈↓ εdImplementation of the Primitives 317␈↓
␈↓"β␈↓ α←␈↓techniques.␈α
The␈α
first␈α
is␈α
applicable␈α
to␈α
machines␈α
without␈α
the␈α
special␈α
␈↓αAC␈↓'s␈αof␈α
the
␈↓ α←␈↓␈↓↓SM␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'First,␈αwe␈α
will␈αassume␈α
only␈αthat␈α
we␈αare␈αable␈α
to␈αsimulate␈α
a␈αstack.␈α
All␈αthe
␈↓ α←␈↓operations␈α∂occur␈α∂on␈α∂the␈α∂stack.␈α∂Constants␈α∂will␈α∂be␈α∂generated␈α∂by␈α∂pushing␈α∂the
␈↓ α←␈↓representation␈α∂on␈α∞the␈α∂top␈α∞of␈α∂the␈α∂stack,␈α∞essentially␈α∂creating␈α∞a␈α∂␈↓αdest␈↓␈α∂block.␈α∞ A
␈↓ α←␈↓function␈α∞call,␈α
␈↓αf[t␈↓β1␈↓α; ... ;t␈↓βn␈↓α]␈↓,␈α∞expects␈α
its␈α∞arguments␈α
as␈α∞the␈α
top␈α∞␈↓αn␈↓␈α
elements␈α∞of␈α
the
␈↓ α←␈↓stack,␈α
with␈α
the␈α
value␈α
of␈α
␈↓αt␈↓βn␈↓␈α
on␈α
the␈α
top␈α
of␈α
the␈α
stack,␈α
and␈α
the␈α
other␈α
values␈α
below.
␈↓ α←␈↓As␈αthe␈αfunction␈αis␈αcalled,␈αthe␈α␈↓αdest␈↓␈α
block␈αon␈αthe␈αtop␈αof␈αthe␈αstack␈α
becomes␈αthe
␈↓ α←␈↓local␈α
environment.␈α
The␈α
function␈α
replaces␈α
the␈α
top␈α
␈↓αn␈↓␈α
elements␈α
with␈α
the␈α
value␈α
of
␈↓ α←␈↓the␈α⊂function,␈α⊂thus␈α∂␈↓αsend␈↓-ing␈α⊂its␈α⊂value␈α∂to␈α⊂the␈α⊂destination␈α∂of␈α⊂the␈α⊂caller.␈α∂This
␈↓ α←␈↓model␈αis␈αa␈α
restricted,␈αbut␈αvery␈α
useful,␈αsubset␈αof␈αLISP.␈α
It␈αwill␈αdevelop␈α
into␈αa
␈↓ α←␈↓more␈αrobust␈αexample␈αas␈αthe␈αchapter␈αprogresses.␈α The␈αtechnique␈αis␈αextendible
␈↓ α←␈↓to support the implementation model we developed in Section 5.18.
␈↓"β␈↓ α←␈↓␈↓ β'Here's␈α≡an␈α≡example␈α≡of␈α≡the␈α≡implementation␈α≡for␈α≡the␈α≥expression
␈↓ α←␈↓␈↓αf[g[A];C;h[B]]␈↓:
␈↓"∀␈↓ α←␈↓α␈↓ β∂ (PUSH P (QUOTE A))␈↓ ¬w␈↓; make argument for call on ␈↓αg
␈↓"β␈↓ α←␈↓α␈↓ β∂ (CALL G)␈↓ ¬w␈↓; call the function␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂ (PUSH P (QUOTE C))␈↓ ¬w␈↓; place second argument␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂ (PUSH P (QUOTE B))
␈↓"β␈↓ α←␈↓α␈↓ β∂ (CALL H)␈↓ ¬w␈↓; ␈↓αh␈↓ only uses (and removes) ␈↓αB
␈↓"β␈↓ α←␈↓α␈↓ β∂ (CALL F)␈↓ ¬w␈↓; after the call, ␈↓αf[g[A];C;h[B]]␈↓ is
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ ¬w; on the top of the stack.
␈↓"∀␈↓ α←␈↓␈↓ β'Now␈αwe␈αwill␈αgive␈αimplementations␈αof␈αthe␈αLISP␈αprimitives␈αwhich␈αresult
␈↓ α←␈↓in␈α∀reasonably␈α∀efficient␈α∀code␈α∀on␈α∀the␈α∀␈↓↓SM␈↓,␈α∀and␈α∀which␈α∀also␈α∀reflect␈α∪several
␈↓ α←␈↓practices␈α
applied␈α
in␈αcurrent␈α
LISP␈α
implementations.␈α We␈α
will␈α
take␈αadvantage
␈↓ α←␈↓of␈α∞the␈α∂existence␈α∞of␈α∂the␈α∞special␈α∂␈↓αAC␈↓'s;␈α∞the␈α∂usual␈α∞hardware␈α∂implementation␈α∞of
␈↓ α←␈↓such␈α
special␈α
registers␈α
allows␈αaccess␈α
to␈α
their␈α
contents␈αin␈α
less␈α
time␈α
than␈αtypical
␈↓ α←␈↓stack references.␈↓π 12␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Since␈α∂the␈α∂creating,␈α∂saving,␈α∂and␈α∞restoring␈α∂of␈α∂destination␈α∂blocks␈α∂can␈α∞be
␈↓ α←␈↓expensive,␈αwe␈α
will␈αtry␈α
to␈αminimize␈α
those␈αkinds␈α
of␈αactivities.␈α
We␈αwill␈α
use␈αour
␈↓ α←␈↓special␈α↔registers␈α↔␈↓αAC1␈↓,␈α↔through␈α↔␈↓αACn␈↓␈α⊗to␈α↔build␈α↔parameter␈α↔lists␈α↔and␈α⊗pass
␈↓ α←␈↓parameters.␈α↔This␈α↔entails␈α↔several␈α↔conventions.␈α↔ We␈α↔will␈α↔try␈α↔to␈α_use␈α↔the
␈↓ α←␈↓accumulators␈α~as␈α~the␈α~destination␈α≠block.␈α~ Our␈α~early␈α~compilers␈α≠will␈α~be
␈↓ α←␈↓sufficiently␈α∞weak␈α
that␈α∞this␈α∞desire␈α
can␈α∞be␈α
met.␈α∞Later␈α∞we␈α
will␈α∞have␈α∞to␈α
modify
␈↓ α←␈↓our stand slightly.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αactual␈αparameters␈αfor␈αa␈αfunction␈αcall,␈α␈↓αf[t␈↓β1␈↓α; ... ;t␈↓βn␈↓α]␈↓,␈αwill␈αbe␈αdeveloped
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 12␈↓There␈α_is␈α↔a␈α_question␈α↔whether␈α_such␈α↔special␈α_registers␈α_should␈α↔be
␈↓ α←␈↓considered␈α∀good␈α∀architecture␈α∀or␈α∀a␈α∀trick.␈α∀The␈α∀Burroughs␈α∀6700-7700␈α∪uses
␈↓ α←␈↓special␈α⊂hardware␈α⊂to␈α⊂decrease␈α⊂access␈α⊂time␈α⊂to␈α⊂the␈α⊂initial␈α⊂stack␈α⊂segment.␈α⊂The
␈↓ α←␈↓PDP-10␈α
uses␈αspecial␈α
registers.␈αOne␈α
can␈αargue␈α
that␈αsuch␈α
special␈α
tricks␈αbelong
␈↓ α←␈↓in␈α⊃the␈α⊃hardware,␈α∩and␈α⊃that␈α⊃the␈α⊃machine␈α∩presented␈α⊃to␈α⊃the␈α∩programmer␈α⊃be
␈↓ α←␈↓correspondingly more uniform [Dor 76].
␈↓ α←␈↓␈↓318 Dynamic Structure␈↓
(6.4␈↓
␈↓"β␈↓ α←␈↓in␈α␈↓αAC1␈↓␈αthrough␈α␈↓αACn␈↓.␈α In␈αthe␈αearly␈αcompilers␈αwe␈αwill␈αalso␈αpass␈α
the␈αevaluated
␈↓ α←␈↓parameters␈α∂to␈α∞the␈α∂called␈α∞function␈α∂using␈α∞the␈α∂accumulators.␈α∞Thus␈α∂values␈α∞will
␈↓ α←␈↓tend␈α∞to␈α
stay␈α∞in␈α
the␈α∞␈↓αAC␈↓'s␈α
unless␈α∞forced␈α
out.␈α∞They␈α
can␈α∞be␈α
forced␈α∞out␈α∞by␈α
␈↓αalloc␈↓
␈↓ α←␈↓since␈α⊃a␈α⊃call␈α⊃to␈α∩␈↓αalloc␈↓␈α⊃is␈α⊃supposed␈α⊃to␈α∩save␈α⊃the␈α⊃current␈α⊃␈↓αdest␈↓.␈α∩ The␈α⊃interplay
␈↓ α←␈↓between ␈↓αnext␈↓, ␈↓αlink␈↓, and ␈↓αsend␈↓ requires care.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αwill␈αassume␈αthat␈αwe␈αare␈αcompiling␈αfor␈αsingle␈αvalued␈α
functions,␈αand
␈↓ α←␈↓therefore␈α∩we␈α∪must␈α∩resolve␈α∩the␈α∪question␈α∩of␈α∪where␈α∩to␈α∩put␈α∪the␈α∩value␈α∪of␈α∩a
␈↓ α←␈↓function.␈α⊗ Again␈α⊗consider␈α∃␈↓αf[t␈↓β1␈↓α; ... ;t␈↓βn␈↓α]␈↓;␈α⊗we␈α⊗might␈α∃expect␈α⊗that␈α⊗each␈α⊗␈↓αt␈↓βi␈↓␈α∃be
␈↓ α←␈↓responsible␈α
for␈αplacing␈α
its␈αresult␈α
in␈αthe␈α
proper␈α␈↓αACi␈↓.␈α
Indeed␈αthat␈α
is␈α
the␈αspirit
␈↓ α←␈↓of␈α⊂the␈α⊃␈↓αsend␈↓␈α⊂operation;␈α⊃it␈α⊂knows␈α⊂where␈α⊃the␈α⊂result␈α⊃should␈α⊂be␈α⊃placed.␈α⊂ This
␈↓ α←␈↓strategy␈α⊂requires␈α⊂some␈α⊃careful␈α⊂register␈α⊂allocation␈α⊂if␈α⊃it␈α⊂is␈α⊂to␈α⊂be␈α⊃carried␈α⊂out
␈↓ α←␈↓successfully. We will postpone this discussion for a while.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α∩is␈α∩a␈α∩simpler␈α∩solution␈α⊃available:␈α∩a␈α∩function␈α∩always␈α∩returns␈α⊃its
␈↓ α←␈↓value␈α∂in␈α∂␈↓αAC1␈↓␈α⊂and␈α∂leaves␈α∂the␈α∂register␈α⊂allocation␈α∂up␈α∂to␈α∂the␈α⊂calling␈α∂function.
␈↓ α←␈↓There are at least two strategies here:
␈↓"∀␈↓ α←␈↓␈↓ β'We␈αtry␈αto␈αbuild␈α
the␈α␈↓αdest␈↓␈αblock␈αin␈α
the␈α␈↓αAC␈↓'s␈αand␈αalso␈α
use␈αthe␈α␈↓αAC␈↓'s␈αto␈α
pass
␈↓ α←␈↓parameters and values.
␈↓" ␈↓ α←␈↓␈↓ βWA␈α∩function␈α∩call,␈α∩␈↓αf[t␈↓β1␈↓α;␈α∩...;t␈↓βn␈↓α]␈↓,␈α∩expects␈α∩its␈α∩arguments␈α∩to␈α∩be
␈↓ α←␈↓␈↓ βWpresented␈α⊃in␈α⊃␈↓αAC1␈↓␈α⊃through␈α⊃␈↓αACn␈↓.␈α⊃We␈α⊃try␈α⊃to␈α⊃compute␈α⊃the
␈↓ α←␈↓␈↓ βWvalues␈αof␈α␈↓αt␈↓βi␈↓␈αdirectly␈αin␈α␈↓αACi␈↓.␈α This␈αis␈αeasy␈αif␈α␈↓αt␈↓βi␈↓␈αis␈αa␈αconstant;
␈↓ α←␈↓␈↓ βWif␈α␈↓αt␈↓βi␈↓␈αis␈αa␈αfunction␈αcall␈α
on␈α␈↓αg␈↓,␈αwe␈αsave␈α␈↓αAC1␈↓␈αthrough␈α␈↓αACi-1␈↓;␈α
set
␈↓ α←␈↓␈↓ βWup␈α∩the␈α∩arguments␈α∩to␈α∩␈↓αg␈↓;␈α∩perform␈α∩the␈α∩call,␈α∩returning␈α⊃the
␈↓ α←␈↓␈↓ βWresult␈α
in␈α
␈↓αAC1␈↓;␈α
move␈α
the␈α
result␈α
to␈α
␈↓αACi␈↓;␈α
and␈α
restore␈αthe␈α
saved
␈↓ α←␈↓␈↓ βWvalues of the ␈↓αt␈↓'s.
␈↓"β␈↓ α←␈↓␈↓ Convention 1 ␈↓
␈↓"→␈↓ α←␈↓␈↓ β'We␈α
try␈α
to␈α
build␈α
the␈α
␈↓αdest␈↓␈α
block␈αin␈α
the␈α
top␈α
of␈α
the␈α
stack,␈α
using␈α
the␈α␈↓αAC␈↓'s␈α
for
␈↓ α←␈↓passing parameters and returning values.
␈↓" ␈↓ α←␈↓␈↓ βWA␈α∩function␈α∩call,␈α∩␈↓αf[t␈↓β1␈↓α;␈α∩...;t␈↓βn␈↓α]␈↓,␈α∩expects␈α∩its␈α∩arguments␈α∩to␈α∩be
␈↓ α←␈↓␈↓ βWpresented␈α
in␈α␈↓αAC1␈↓␈α
through␈α␈↓αACn␈↓.␈α
As␈α
we␈αcompute␈α
each␈α␈↓αt␈↓βi␈↓,␈α
we
␈↓ α←␈↓␈↓ βWstore␈α↔the␈α↔result␈α⊗on␈α↔the␈α↔stack␈α⊗␈↓αP␈↓.␈α↔Thus␈α↔the␈α⊗execution
␈↓ α←␈↓␈↓ βWsequence should be:
␈↓" ␈↓ α←␈↓␈↓ ∧πcompute value of ␈↓αt␈↓β␈↓, push onto stack ␈↓αP␈↓.
␈↓" ␈↓ α←␈↓␈↓ ∧π . . .
␈↓"β␈↓ α←␈↓␈↓ ∧πcompute value of ␈↓αt␈↓βn-1␈↓, push onto stack ␈↓αP␈↓.
␈↓"β␈↓ α←␈↓␈↓ ∧πcompute value of ␈↓αt␈↓βn␈↓, move into ␈↓αACn␈↓.
␈↓"β␈↓ α←␈↓␈↓ βWAfter␈α∪this␈α∩computation␈α∪the␈α∩values,␈α∪V␈↓βn-1␈↓,␈α∩...,␈α∪V␈↓β1␈↓,␈α∪of␈α∩the
␈↓ α←␈↓␈↓ βWarguments␈αare␈αstored␈α
from␈αtop␈αto␈αbottom␈α
in␈α␈↓αP␈↓␈αwith␈α
V␈↓βn␈↓␈αin
␈↓ α←␈↓␈↓ βW␈↓αACn␈↓.␈α∞ Thus␈α∞to␈α∞complete␈α
the␈α∞function␈α∞invocation,␈α∞we␈α
need
␈↓ α←␈↓␈↓ βWonly␈α
pop␈αthe␈α
arguments␈α
into␈αthe␈α
␈↓αAC␈↓'s␈αin␈α
the␈α
correct␈αorder
␈↓ α←␈↓␈↓ βWand␈αcall␈α␈↓αf␈↓.␈α We␈αdid␈αnot␈αpush␈αV␈↓βn␈↓␈αsince␈αwe␈αexpected␈αto␈αpass
␈↓ α←␈↓␈↓ βWthe parameters to ␈↓αf␈↓ in ␈↓αAC1␈↓ through ␈↓αACn␈↓.
␈↓"β␈↓ α←␈↓␈↓ Convention 2 ␈↓
␈↓ α←␈↓␈↓6.4␈↓ εdImplementation of the Primitives 319␈↓
␈↓"β␈↓ α←␈↓␈↓ βWWhen␈α⊂a␈α⊃function␈α⊂completes␈α⊃evaluation,␈α⊂it␈α⊃is␈α⊂to␈α⊃place␈α⊂its
␈↓ α←␈↓␈↓ βWvalue␈αin␈α␈↓αAC1␈↓.␈αNothing␈α
can␈αbe␈αassumed␈αabout␈α
the␈αcontents
␈↓ α←␈↓␈↓ βWany␈αother␈α␈↓αAC␈↓.␈αIf␈αan␈α
␈↓αAC␈↓␈αcontains␈αinformation␈αwe␈αneed␈α
then
␈↓ α←␈↓␈↓ βWit must be saved on the stack ␈↓¬before␈↓ calling the function.
␈↓" ␈↓ α←␈↓␈↓ βWInstead␈α∂of␈α∂referring␈α∂to␈α∂␈↓αAC1,␈α∂AC2,␈α∂...,␈α∂ACn␈↓␈α∂we␈α∂will␈α∞simply
␈↓ α←␈↓␈↓ βWuse the numbers, ␈↓α1, 2, ..., n␈↓ in the instructions.
␈↓"β␈↓ α←␈↓␈↓ General conventions ␈↓
␈↓"→␈↓ α←␈↓␈↓ β'We␈α∀now␈α∪give␈α∀an␈α∀example␈α∪of␈α∀both␈α∪conventions␈α∀for␈α∀the␈α∪expression
␈↓ α←␈↓␈↓αf[g[A];C;h[B]]␈↓.␈α⊃ We␈α⊃use␈α∩a␈α⊃list␈α⊃representation␈α∩of␈α⊃the␈α⊃instructions␈α∩and␈α⊃code
␈↓ α←␈↓sequences in preparation for future discussions.
␈↓"∀␈↓ α←␈↓α␈↓ β∂((MOVEI 1 (QUOTE A))␈↓ ¬w␈↓; make argument for call on ␈↓αg
␈↓"β␈↓ α←␈↓α␈↓ β∂ (CALL G)␈↓ ¬w␈↓; call the function␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂ (MOVEI 2 (QUOTE C))␈↓ ¬w␈↓; place second argument␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂ (PUSH P 1)␈↓ ¬w␈↓; but now we have to save the values␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂ (PUSH P 2)␈↓ ¬w␈↓; since we must compute ␈↓αh[B]
␈↓"β␈↓ α←␈↓α␈↓ β∂ (MOVEI 1 (QUOTE B))
␈↓"β␈↓ α←␈↓α␈↓ β∂ (CALL H)
␈↓"β␈↓ α←␈↓α␈↓ β∂ (MOVE 3 1)␈↓ ¬w␈↓; move the result to ␈↓αAC3
␈↓"β␈↓ α←␈↓α␈↓ β∂ (POP P 2)␈↓ ¬w␈↓; restore ␈↓αAC2␈↓ and ␈↓αAC1␈↓ in the correct order␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂ (POP P 1)
␈↓"β␈↓ α←␈↓α␈↓ β∂ (CALL F) )
␈↓" ␈↓ α←␈↓α␈↓ Example of Convention 1 ␈↓α
␈↓"→␈↓ α←␈↓α␈↓ β∂((MOVEI 1 (QUOTE A))␈↓ ¬w␈↓; make argument for call on ␈↓αg
␈↓"β␈↓ α←␈↓α␈↓ β∂ (CALL G)␈↓ ¬w␈↓; call the function␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂ (PUSH P 1)␈↓ ¬w␈↓; save the value␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂ (MOVEI 1 (QUOTE C))
␈↓"β␈↓ α←␈↓α␈↓ β∂ (PUSH P 1)␈↓ ¬w␈↓; save the value␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂ (MOVEI 1 (QUOTE B))
␈↓"β␈↓ α←␈↓α␈↓ β∂ (CALL H)
␈↓"β␈↓ α←␈↓α␈↓ β∂ (MOVE 3 1)␈↓ ¬w␈↓; don't need to save the value␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂ (POP P 2)␈↓ ¬w␈↓; since this is the last argument ␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂ (POP P 1)
␈↓"β␈↓ α←␈↓α␈↓ β∂ (CALL F) )
␈↓" ␈↓ α←␈↓α␈↓ Example of Convention 2 ␈↓α
␈↓"→␈↓ α←␈↓Neither␈αcompiling␈αconvention␈αproduces␈αoptimal␈αcode␈αfor␈αall␈αoccasions.␈α If␈αthe
␈↓ α←␈↓parameter␈α∀list␈α∀to␈α∀a␈α∀function␈α∃call␈α∀contains␈α∀only␈α∀constants,␈α∀then␈α∃the␈α∀first
␈↓ α←␈↓convention␈αproduces␈αbetter␈αcode.␈αIf␈αthere␈αare␈αmany␈αnested␈αfunction␈αcalls␈αthen
␈↓ α←␈↓it␈αmay␈αproduce␈αvery␈αbad␈αcode.␈αWe␈αwill␈αworry␈αmore␈αabout␈αefficiency␈αafter␈αwe
␈↓ α←␈↓develop the basic compiling algorithms.
␈↓ α←␈↓␈↓320 Dynamic Structure␈↓
(6.4␈↓
␈↓"β␈↓ α←␈↓␈↓ β'At␈α~the␈α~highest␈α≠level,␈α~our␈α~compiler␈α≠will␈α~generate␈α~code␈α≠for␈α~the
␈↓ α←␈↓␈↓αalloc-link-call-...␈↓␈αmachine;␈αbut␈αfrequently␈αwe␈αwill␈αexpress␈αthe␈αcode␈αin␈αterms␈αof
␈↓ α←␈↓one of our more traditional representations.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
output␈αfrom␈α
the␈α
compiler␈αis␈α
to␈α
be␈αa␈α
list␈α
of␈αinstructions,␈α
in␈αthe␈α
order
␈↓ α←␈↓which␈α∪we␈α∪would␈α∪expect␈α∪to␈α∪execute␈α∪them.␈α∪ Each␈α∪instruction␈α∪is␈α∪a␈α∪list:␈α∩an
␈↓ α←␈↓operation␈α∞followed␈α∞by␈α∂as␈α∞many␈α∞elements␈α∞as␈α∂are␈α∞required␈α∞by␈α∂that␈α∞operation.
␈↓ α←␈↓We␈αcan␈αexecute␈αthe␈αcompiled␈αcode␈αby␈αsimulating␈αthe␈αactions␈αof␈αour␈αmachine
␈↓ α←␈↓on␈α
each␈α
element␈αof␈α
the␈α
sequence.␈αHowever␈α
it␈α
is␈αmore␈α
efficient␈α
to␈αtranslate␈α
this
␈↓ α←␈↓compiler␈α
output␈αfurther,␈α
producing␈α
a␈αsequence␈α
of␈α
actual␈αmachine␈α
instructions,
␈↓ α←␈↓placed␈α⊂in␈α⊃memory␈α⊂and␈α⊃suitable␈α⊂for␈α⊂execution␈α⊃by␈α⊂the␈α⊃hardware␈α⊂processing
␈↓ α←␈↓unit.␈α In␈αpreparation␈αfor␈αthis,␈αwe␈α
will␈αallocate␈αan␈αarea␈αof␈αmemory␈α
which␈αcan
␈↓ α←␈↓receive␈α⊂the␈α⊂processed␈α⊂compiler␈α⊂output.␈α∂ This␈α⊂area␈α⊂is␈α⊂usually␈α⊂called␈α∂␈↓↓Binary
␈↓ α←␈↓↓Program␈α⊃Space␈↓␈α⊃(BPS).␈α⊃ The␈α⊃translation␈α⊃program␈α⊃which␈α⊃takes␈α⊃the␈α⊃output
␈↓ α←␈↓from␈αthe␈αcompiler␈αand␈αconverts␈αit␈αinto␈αactual␈αmachine␈αinstructions␈αin␈αBPS␈αis
␈↓ α←␈↓called an assembler.
␈↓"β␈↓ α←␈↓␈↓ ¬p␈↓↓6.5 Assemblers␈↓
␈↓"β␈↓ α←␈↓In␈αSection 6.2␈αwe␈αgave␈αan␈αabstract␈αdescription␈αof␈αan␈αalgorithm␈αfor␈αexecuting
␈↓ α←␈↓sequences␈α∪of␈α∪instructions.␈α∪ In␈α∪this␈α∪section␈α∪we␈α∪discuss␈α∪the␈α∪mechanism␈α∪for
␈↓ α←␈↓translating␈α→the␈α→LISP␈α~list,␈α→which␈α→represents␈α→instructions,␈α~into␈α→machine
␈↓ α←␈↓instructions␈α∩in␈α∩Binary␈α∩Program␈α∪Space.␈α∩ Part␈α∩of␈α∩the␈α∩process␈α∪involves␈α∩the
␈↓ α←␈↓actual␈α∞instructions;␈α∂before␈α∞a␈α∞machine␈α∂can␈α∞execute␈α∞an␈α∂instruction␈α∞it␈α∂must␈α∞be
␈↓ α←␈↓transformed␈αinto␈αa␈α
numerical␈αcode␈αwhich␈α
the␈αmachine␈αunderstands.␈α
Part␈αof
␈↓ α←␈↓the␈αprocess␈αinvolves␈αestablishing␈αa␈αlink␈αbetween␈αthe␈αBPS␈αcode␈αand␈αthe␈αLISP
␈↓ α←␈↓evaluator;␈α⊃before␈α⊃the␈α⊃evaluator␈α⊃can␈α⊃call␈α⊃the␈α⊃compiled␈α⊃code,␈α⊃it␈α⊃must␈α⊂know
␈↓ α←␈↓where␈α∂to␈α∂find␈α∂it.␈α∂A␈α∂program␈α∂which␈α∂performs␈α∂these␈α∂operations␈α∂is␈α∂called␈α∂an
␈↓ α←␈↓␈↓↓assembler␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α⊂are␈α⊂two␈α⊂alternatives␈α⊂available␈α⊂to␈α⊂solve␈α⊂the␈α⊂first␈α⊃problem.␈α⊂ We
␈↓ α←␈↓might␈α
add␈α
the␈α
assembly␈α
phase␈α
to␈α
the␈α
end␈α
of␈α
the␈α
compiler.␈α
Then␈α
the␈α
output
␈↓ α←␈↓from␈αthe␈αcompiler␈α
would␈αgo␈αdirectly␈αto␈α
locations␈αin␈αBPS.␈α The␈α
alternative␈αis
␈↓ α←␈↓to␈αcompile␈αthe␈αfunctions␈αonto␈αan␈αexternal␈αmedium,␈αand␈αperform␈αthe␈α
assembly
␈↓ α←␈↓phase␈α∞later;␈α∞this␈α∞has␈α∞some␈α∞advantages.␈α∞ The␈α∞assembly␈α∞phase␈α∞is␈α∞significantly
␈↓ α←␈↓less␈αtime␈αconsuming␈αthan␈αthe␈αearlier␈αphases␈αof␈αa␈αcompiler.␈αTherefore␈αseveral
␈↓ α←␈↓programmers␈α
may␈α
take␈α
advantage␈α
of␈α
assembly␈α
code␈α
without␈α
the␈α
necessity␈α
of
␈↓ α←␈↓recompilation.␈α In␈αeither␈αcase,␈αthe␈αassembler␈αmust␈αcomplete␈αthe␈α
translation␈αto
␈↓ α←␈↓machine␈α⊂code␈α⊂and␈α⊂link␈α⊂that␈α⊂code␈α⊂into␈α∂the␈α⊂object␈α⊂list␈α⊂such␈α⊂that␈α⊂it␈α⊂may␈α∂be
␈↓ α←␈↓accessed.
␈↓"β␈↓ α←␈↓␈↓ β'One␈α⊃of␈α⊃the␈α⊃arguments␈α⊃to␈α⊂the␈α⊃assembler␈α⊃is␈α⊃the␈α⊃representation␈α⊃of␈α⊂the
␈↓ α←␈↓program.␈α
One␈α
of␈α
its␈αarguments␈α
should␈α
describe␈α
where␈αin␈α
BPS␈α
we␈α
wish␈αthe
␈↓ α←␈↓assembled␈α∂code␈α∂to␈α∂begin␈α∂loading;␈α∂this␈α∂second␈α∂argument␈α∂becomes␈α∂the␈α∂initial
␈↓ α←␈↓value␈α∞for␈α∞the␈α∂␈↓↓assembly␈α∞counter␈↓.␈α∞ The␈α∂assembler␈α∞can␈α∞sequence␈α∂through␈α∞the
␈↓ α←␈↓program␈α⊗list,␈α↔looking␈α⊗up␈α⊗each␈α↔definition,␈α⊗manufacturing␈α↔the␈α⊗numerical
␈↓ α←␈↓␈↓6.5␈↓ λsAssemblers 321␈↓
␈↓"β␈↓ α←␈↓equivalent␈α∪of␈α∪each␈α∪instruction,␈α∪and␈α∪then␈α∪depositing␈α∪that␈α∪number␈α∀in␈α∪the
␈↓ α←␈↓location␈α⊗referenced␈α⊗by␈α∃the␈α⊗assembly␈α⊗counter.␈α∃The␈α⊗assembley␈α⊗counter␈α∃is
␈↓ α←␈↓incremented, and the next element of the program list is examined.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α⊃must␈α⊃also␈α⊂have␈α⊃access␈α⊃to␈α⊃an␈α⊂initial␈α⊃symbol␈α⊃table,␈α⊃describing␈α⊂the
␈↓ α←␈↓pre-defined␈α∩symbol␈α⊃names.␈α∩ These␈α⊃pre-defined␈α∩names␈α∩include␈α⊃information
␈↓ α←␈↓about␈α⊂the␈α⊂actual␈α⊂machine␈α⊂locations␈α⊂for␈α⊂the␈α⊂utility␈α⊂functions,␈α⊂the␈α⊂values␈α⊂of
␈↓ α←␈↓special␈α
stacks␈α
or␈α
registers␈α
which␈α∞the␈α
compiler␈α
uses␈α
internally.␈α
We␈α∞must␈α
also
␈↓ α←␈↓have␈αan␈α
instruction␈αlist␈α
which␈αgives␈α
a␈αcorrespondence␈α
between␈αthe␈αnames␈α
like
␈↓ α←␈↓␈↓αALLOC␈↓␈α⊂or␈α⊂␈↓αPUSHJ␈↓␈α⊂and␈α⊂the␈α⊂actual␈α⊂numbers␈α⊂which␈α⊂the␈α⊂hardware␈α⊃uses␈α⊂in
␈↓ α←␈↓interpreting the instruction.␈↓π 13␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Below␈αis␈α
a␈αlow␈αlevel␈α
representation␈αof␈αa␈α
code␈αsequence␈α
for␈α␈↓αf[g[A];h[B]]␈↓
␈↓ α←␈↓assuming that ␈↓αh␈↓ is a primitive routine.
␈↓"∀␈↓ α←␈↓α␈↓ β∂((MOVEI 1 (QUOTE A))␈↓ ¬w␈↓; make argument for call on ␈↓αg
␈↓"β␈↓ α←␈↓α␈↓ β∂ (CALL G)␈↓ ¬w␈↓; call the function␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂ (PUSH P 1)␈↓ ¬w␈↓; save the value␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂ (MOVEI 1 (QUOTE B))
␈↓"β␈↓ α←␈↓α␈↓ β∂ (PUSHJ P H) ␈↓π 14␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂ (MOVE 2 1)
␈↓"β␈↓ α←␈↓α␈↓ β∂ (POP P 1)
␈↓"β␈↓ α←␈↓α␈↓ β∂ (CALL F) )
␈↓"∀␈↓ α←␈↓The␈α∞machine␈α∂representations␈α∞of␈α∞these␈α∂instructions␈α∞are␈α∞encodings␈α∂of␈α∞specific
␈↓ α←␈↓fields␈αof␈α
specific␈αmachine␈α
locations␈αwith␈αspecific␈α
numbers.␈α For␈α
example,␈αthe
␈↓ α←␈↓operation␈α⊃␈↓αPUSH␈↓␈α⊃is␈α⊂represented␈α⊃as␈α⊃a␈α⊂certain␈α⊃number,␈α⊃called␈α⊃its␈α⊂␈↓↓operation
␈↓ α←␈↓↓code␈↓␈αor␈α␈↓↓op␈αcode␈↓,␈αand␈αwhich␈αwill␈αoccupy␈αa␈αcertain␈αarea␈αof␈αa␈αmachine␈αword␈αso
␈↓ α←␈↓that␈α∂the␈α∂CPU␈α∂can␈α∂interpret␈α∂it␈α∂as␈α∂an␈α∂instruction␈α∂to␈α∂push␈α∂something␈α∂onto␈α∞a
␈↓ α←␈↓stack.␈α∞ Other␈α∞fields␈α∞in␈α∞the␈α∞instruction␈α∞are␈α∞to␈α∞be␈α∞interpreted␈α∞as␈α∞references␈α
to
␈↓ α←␈↓stacks,␈α
to␈α
memory␈αlocations,␈α
to␈α
accumulators,␈αconstants␈α
or␈α
external␈αreferences
␈↓ α←␈↓to other routines.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∪must␈α∪exercise␈α∪a␈α∪bit␈α∪of␈α∪care␈α∪in␈α∪handling␈α∪␈↓αQUOTE␈↓d␈α∩expressions.
␈↓ α←␈↓Assembling␈α
a␈α
construct␈α
like␈α
␈↓α(MOVEI␈α
1␈α(QUOTE␈α
(A␈α
B␈α
C)))␈↓␈α
should␈α
have␈αthe
␈↓ α←␈↓effect␈α
of␈αconstructing␈α
the␈αlist␈α
␈↓α(A␈αB␈α
C)␈↓␈α
in␈αfree␈α
space␈αand␈α
placing␈αan␈α
instruction
␈↓ α←␈↓in␈αmemory␈αto␈α
load␈αthe␈αaddress␈α
of␈αthis␈αlist␈α
into␈α␈↓αAC1␈↓.␈α We␈α
must␈αnotice␈αthat␈α
this
␈↓ α←␈↓list␈α⊗is␈α⊗subject␈α↔to␈α⊗garbage␈α⊗collection␈α⊗and,␈α↔if␈α⊗left␈α⊗unprotected,␈α↔could␈α⊗be
␈↓ α←␈↓destroyed.␈α
The␈α
garbage␈α
collector␈α
could␈αlook␈α
through␈α
compiled␈α
code␈α
for␈αany
␈↓ α←␈↓references␈α
to␈α
free-space␈α
or␈α
full-word-space;␈α
or␈α
we␈α
could␈α
make␈α
a␈α
list␈α
of␈α∞all␈α
of
␈↓ α←␈↓these␈αconstants␈αand␈αlet␈αthe␈αgarbage␈αcollector␈αmark␈αthe␈αlist.␈α Looking␈αthrough
␈↓ α←␈↓compiled␈α⊃code␈α⊂is␈α⊃expensive;␈α⊂that␈α⊃expense␈α⊂can␈α⊃be␈α⊂minimized␈α⊃by␈α⊂compiling
␈↓ α←␈↓referneces to constants into an initial block in in the prolog of the code.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 13␈↓A␈α
hardware␈α
machine␈α
is␈α
just␈αanother␈α
interpreter␈α
like␈α
␈↓αeval␈↓.␈α
It␈αis␈α
usually
␈↓ α←␈↓not recursive, but performs more like ␈↓αloop␈↓ in the ␈↓αprog␈↓-evaluator.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 14␈↓Note␈α∞that␈α∞we␈α∞use␈α
␈↓αP␈↓;␈α∞this␈α∞assumes␈α∞we␈α∞use␈α
␈↓αP␈↓␈α∞to␈α∞save␈α∞␈↓¬both␈↓␈α∞value␈α
and
␈↓ α←␈↓control information.
␈↓ α←␈↓␈↓322 Dynamic Structure␈↓
(6.5␈↓
␈↓"∀␈↓ α←␈↓␈↓ ∧[␈↓ ¬␈↓α(JUMP ␈↓loc␈↓α)
␈↓"β␈↓ α←␈↓α␈↓ ∧[␈↓ ¬␈↓ <const1>
␈↓"β␈↓ α←␈↓␈↓ ∧[␈↓ ¬ <const2>
␈↓"β␈↓ α←␈↓␈↓ ∧[␈↓ ¬ . . .
␈↓"β␈↓ α←␈↓␈↓ ∧[␈↓ ¬ <constn>
␈↓"β␈↓ α←␈↓␈↓ ∧[loc␈↓ ¬ <code>
␈↓"β␈↓ α←␈↓␈↓ ∧[␈↓ ¬ . . . ␈↓α)␈↓
␈↓"∀␈↓ α←␈↓Then the garbage collector need only search well-defined positions.
␈↓"β␈↓ α←␈↓␈↓ β'Keeping␈α∞a␈α∞␈↓αQUOTE␈↓-list␈α∞is␈α∞a␈α
compromise;␈α∞it␈α∞is␈α∞a␈α∞compromise␈α∞since␈α
that
␈↓ α←␈↓strategy␈αmight␈αretain␈αunnecessary␈αstructures␈αin␈αcase␈αfunctions␈α
were␈αredefined
␈↓ α←␈↓or recompiled.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α→assembler␈α→also␈α→needs␈α~to␈α→recognize␈α→that␈α→there␈α~are␈α→different
␈↓ α←␈↓instruction␈αformats.␈α That␈αis,␈αsome␈αinstructions␈αuse␈αan␈αopcode␈αand␈αa␈αmemory
␈↓ α←␈↓reference:␈α⊃␈↓α(JUMP L)␈↓;␈α∩some␈α⊃use␈α∩an␈α⊃opcode,␈α∩accumulator,␈α⊃and␈α∩an␈α⊃address:
␈↓ α←␈↓␈↓α(PUSH P 1)␈↓;␈α_and␈α↔some␈α_use␈α↔a␈α_LISP␈α_construct:␈α↔␈↓α(MOVEI 1 (QUOTE A))␈↓.
␈↓ α←␈↓Therefore,␈αthe␈αassembler␈αhas␈αto␈αhave␈αan␈αinitial␈αsymbol␈αtable␈αof␈αopcodes␈αand
␈↓ α←␈↓stack numbers.
␈↓"β␈↓ α←␈↓Here is a sample op-code table with their machine equivalents:
␈↓"∀␈↓ α←␈↓␈↓↓␈↓ ∧Csymbol␈↓ ¬Svalue␈↓α
␈↓"β␈↓ α←␈↓α␈↓ ∧CMOVE␈↓ ¬S200
␈↓"β␈↓ α←␈↓α␈↓ ∧CMOVEI␈↓ ¬S201
␈↓"β␈↓ α←␈↓α␈↓ ∧CSUB␈↓ ¬S274
␈↓"β␈↓ α←␈↓α␈↓ ∧CJUMP␈↓ ¬S254
␈↓"β␈↓ α←␈↓α␈↓ ∧CJUMPE␈↓ ¬S322
␈↓"β␈↓ α←␈↓α␈↓ ∧CJUMPN␈↓ ¬S326
␈↓"β␈↓ α←␈↓α␈↓ ∧CPUSH␈↓ ¬S261
␈↓"β␈↓ α←␈↓α␈↓ ∧CPOP␈↓ ¬S262
␈↓"β␈↓ α←␈↓α␈↓ ∧CPUSHJ␈↓ ¬S260
␈↓"β␈↓ α←␈↓α␈↓ ∧CPOPJ␈↓ ¬S263
␈↓"β␈↓ α←␈↓α␈↓ ∧CRET␈↓ ¬S263
␈↓"β␈↓ α←␈↓α␈↓ ∧CCALL␈↓ ¬S034
␈↓"β␈↓ α←␈↓α␈↓ ∧CP␈↓ ¬S14
␈↓ α←␈↓␈↓6.5␈↓ λsAssemblers 323␈↓
␈↓"∀␈↓ α←␈↓And␈α∩here's␈α∩what␈α∩the␈α∪code␈α∩for␈α∩␈↓αf[g[A];C;h[B]]␈↓␈α∩might␈α∩resemble␈α∪after␈α∩being
␈↓ α←␈↓assembled:
␈↓"⊃␈↓ α←␈↓
⊂αααπααπααααα⊃
␈↓"␈↓ α←␈↓
100 ~201~ 1~ 405~
␈↓"␈↓ α←␈↓
εαααβααβαααααλ
␈↓"␈↓ α←␈↓
~034~ ~ 1107~
␈↓"␈↓ α←␈↓
εαααβααβαααααλ
␈↓"␈↓ α←␈↓
~261~14~ 1~
␈↓"␈↓ α←␈↓
εαααβααβαααααλ
␈↓"␈↓ α←␈↓
~201~ 1~ 406~
␈↓"␈↓ α←␈↓
εαααβααβαααααλ
␈↓"␈↓ α←␈↓
~260~14~11121~
␈↓"␈↓ α←␈↓
εαααβααβαααααλ
␈↓"␈↓ α←␈↓
~200~ 2~ 1~
␈↓"␈↓ α←␈↓
εαααβααβαααααλ
␈↓"␈↓ α←␈↓
~262~14~ 1~
␈↓"␈↓ α←␈↓
εαααβααβαααααλ
␈↓"␈↓ α←␈↓
~034~ ~ 1051~
␈↓"␈↓ α←␈↓
%ααα∀αα∀ααααα$
␈↓"∀␈↓ α←␈↓where␈α∞␈↓αA␈↓␈α∞is␈α∞located␈α∞at␈α∞␈↓
405␈↓;␈α∞the␈α∞atom␈α∞␈↓αF␈↓␈α∞begins␈α∞at␈α∞␈↓
1051␈↓,␈α∞and␈α∂the␈α∞instruction
␈↓ α←␈↓sequence for ␈↓αh␈↓ begins at ␈↓
11121␈↓, etc.
␈↓"β␈↓ α←␈↓␈↓ ∧l␈↓↓6.6 Compilers for Subsets of LISP␈↓
␈↓"β␈↓ α←␈↓We␈α⊃will␈α⊃examine␈α⊃compilers␈α⊃for␈α⊃increasingly␈α⊃complex␈α⊃subsets␈α⊃of␈α⊃LISP;␈α⊃we
␈↓ α←␈↓begin␈α∩with␈α⊃functions,␈α∩composition␈α⊃and␈α∩constant␈α⊃arguments,␈α∩and␈α⊃gradually
␈↓ α←␈↓include␈α∩more␈α∩features.␈α∩ Though␈α∩each␈α∩subset␈α∩is␈α∩a␈α∩simple␈α∩extension␈α∪of␈α∩its
␈↓ α←␈↓predecessor,␈α_each␈α↔subset␈α_introduces␈α↔a␈α_new␈α↔problem␈α_for␈α_the␈α↔compiling
␈↓ α←␈↓algorithm.␈α⊂ If␈α⊃the␈α⊂corresponding␈α⊂evaluator␈α⊃(␈↓αtgmoaf␈↓,␈α⊂␈↓αtgmoafr␈↓,␈α⊂and␈α⊃the␈α⊂most
␈↓ α←␈↓simple␈α∪␈↓αeval␈↓)␈α∩is␈α∪well␈α∪understood,␈α∩then␈α∪the␈α∩corresponding␈α∪additions␈α∪to␈α∩the
␈↓ α←␈↓compilation algorithm are easy to make.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∞first␈α∂compiler␈α∞will␈α∂handle␈α∞representations␈α∞of␈α∂that␈α∞subset␈α∂of␈α∞LISP
␈↓ α←␈↓forms defined by the following BNF equations:
␈↓"∀␈↓ α←␈↓<form>␈↓ βo::= <constant> | <function>[<arg>; ...; <arg>]
␈↓" ␈↓ α←␈↓<arg>␈↓ βo::= <form>
␈↓" ␈↓ α←␈↓<constant>␈↓ βo::= <sexpr>
␈↓" ␈↓ α←␈↓<function>␈↓ βo::= <identifier>
␈↓"∀␈↓ α←␈↓This␈α∀LISP␈α∃subset␈α∀corresponds␈α∃closely␈α∀to␈α∀that␈α∃of␈α∀␈↓αtgmoaf␈↓,␈α∃handling␈α∀only
␈↓ α←␈↓function␈α∩names,␈α∪composition,␈α∩and␈α∪constant␈α∩arguments.␈α∪ In␈α∩the␈α∪interest␈α∩of
␈↓ α←␈↓readability␈α∩and␈α∩generality,␈α∩we␈α∩will␈α∩write␈α∩the␈α∩functions␈α∩using␈α⊃constructors,
␈↓ α←␈↓␈↓324 Dynamic Structure␈↓
(6.6␈↓
␈↓"β␈↓ α←␈↓selectors,␈α⊃and␈α⊃recognizers␈α⊃and␈α⊃supply␈α⊃the␈α⊃necessary␈α⊃bridge␈α⊃to␈α∩our␈α⊃specific
␈↓ α←␈↓representation by simple sub-functions.
␈↓"β␈↓ α←␈↓␈↓ β'All␈α
the␈αcompilers␈α
we␈α
develop␈αwill␈α
be␈α
derived␈αfrom␈α
the␈αsecond␈α
compiling
␈↓ α←␈↓convention,␈α⊂saving␈α∂the␈α⊂results␈α∂on␈α⊂the␈α∂top␈α⊂of␈α∂the␈α⊂stack.␈α∂Our␈α⊂compilers␈α∂will
␈↓ α←␈↓incorporate␈α⊃some␈α⊂knowledge␈α⊃of␈α⊂the␈α⊃␈↓↓SM␈↓␈α⊂machine,␈α⊃and␈α⊂we␈α⊃will␈α⊂try␈α⊃to␈α⊂note
␈↓ α←␈↓places␈α∩where␈α∩substantial␈α∩assumptions␈α∩about␈α∩machine␈α∩structure␈α∪have␈α∩been
␈↓ α←␈↓made.␈α
The␈α
remainder␈α
of␈α
this␈α
section␈α
describes␈α
the␈α
main␈α
components␈α
of␈αthe
␈↓ α←␈↓first compiler.
␈↓"∀␈↓ α←␈↓␈↓αcompexp␈↓␈α
expects␈α∞to␈α
see␈α
either␈α∞a␈α
constant␈α
or␈α∞a␈α
function␈α
followed␈α∞by␈α
a␈α∞list␈α
of
␈↓ α←␈↓␈↓ β'zero␈αor␈αmore␈αarguments.␈αThe␈αappearance␈αof␈αa␈αconstant␈αshould␈αelicit␈αthe
␈↓ α←␈↓␈↓ β'generation␈α⊃of␈α⊂a␈α⊃list␈α⊂containing␈α⊃a␈α⊂single␈α⊃instruction␈α⊂to␈α⊃␈↓αsend␈↓␈α⊃back␈α⊂the
␈↓ α←␈↓␈↓ β'representation␈α⊗of␈α⊗that␈α⊗constant;␈α⊗␈↓αmksend[dest;exp]␈↓␈α⊗is␈α⊗a␈α⊗call␈α⊗on␈α∃the
␈↓ α←␈↓␈↓ β'constructor␈αto␈αgenerate␈αthat␈α
instruction.␈α Since␈αvalues␈αare␈α
always␈αfound
␈↓ α←␈↓␈↓ β'in␈α∪␈↓αAC1␈↓,␈α∩that␈α∪should␈α∩be␈α∪the␈α∩destination␈α∪for␈α∩the␈α∪␈↓αsend␈↓.␈α∩Since␈α∪we␈α∩are
␈↓ α←␈↓␈↓ β'assuming␈α
the␈αexpression␈α
is␈αa␈α
constant,␈αthe␈α
operation␈αcan␈α
be␈α
a␈α␈↓αMOVEI␈↓.
␈↓ α←␈↓␈↓ β'If␈α
the␈α
expression␈α
is␈α
not␈α
a␈α
constant,␈α
we␈α
can␈α
assume␈α
it␈α
is␈α
a␈αcall-by-value
␈↓ α←␈↓␈↓ β'application.␈α∞We␈α∞should␈α∞generate␈α∞code␈α∞to␈α∞evaluate␈α∞each␈α∂argument,␈α∞and
␈↓ α←␈↓␈↓ β'follow that with code for a function call.
␈↓"∀␈↓ α←␈↓αcompexp <=␈↓ ∧∪λ[[exp]␈↓ ∧s[isconst[exp] → list[mksend[1;exp]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧s ␈↓
t␈↓α →␈↓ ¬/λ[[z]compapply[␈↓ ε{func[exp];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧s␈↓ ¬/␈↓ ε{complis[z];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧s␈↓ ¬/␈↓ ε{length[z]]]
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧s␈↓ ¬/ [arglist[exp]] ]]]␈↓
␈↓"∀␈↓ α←␈↓␈↓αcomplis␈↓␈α
gets␈α
the␈α
representation␈α
of␈α
the␈α
argument␈α
list;␈α
it␈α
must␈α
generate␈α
a␈α
code
␈↓ α←␈↓␈↓ β'sequence␈α∪to␈α∪evaluate␈α∪each␈α∩argument␈α∪and␈α∪increment␈α∪the␈α∩destination.
␈↓ α←␈↓␈↓ β'After␈αwe␈αhave␈αcompiled␈αthe␈αlast␈αargument␈αwe␈αshould␈αnot␈αincrement␈αthe
␈↓ α←␈↓␈↓ β'destination.␈α∂ Notice␈α∂that␈α∂we␈α∂have␈α∂used␈α∂␈↓αappend␈↓␈α∂with␈α∂three␈α∞arguments;
␈↓ α←␈↓␈↓ β'this could be justified by defining ␈↓αappend␈↓ as a macro (Section 3.12).
␈↓"∀␈↓ α←␈↓αcomplis <= λ[[u]␈↓ ∧C[null[u] → ( );
␈↓"β␈↓ α←␈↓α␈↓ ∧C null[rest[u]] → compexp[first[u]];
␈↓"β␈↓ α←␈↓α␈↓ ∧C ␈↓
t␈↓α → append[␈↓ ¬kcompexp[first[u]];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬klist[mkalloc[1]];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬kcomplis[rest[u]]]] ]
␈↓"∀␈↓ α←␈↓␈↓αcompapply␈↓␈αhas␈αa␈αsimple␈αtask:␈αit␈αgenerates␈αcode␈αfor␈αallocation␈αof␈αthe␈αvalues;␈αit
␈↓ α←␈↓␈↓ β'takes␈α
the␈α∞list␈α
of␈α
instructions␈α∞made␈α
by␈α
␈↓αcomplis␈↓␈α∞and␈α
adds␈α∞instructions␈α
at
␈↓ α←␈↓␈↓ β'the end of the list to generate a function call on ␈↓αfn␈↓. Here's ␈↓αcompapply␈↓:
␈↓"∀␈↓ α←␈↓αcompapply <= λ[[fn;vals;n] append[␈↓ ε3vals;
␈↓"β␈↓ α←␈↓α␈↓ ε3mklink[n];
␈↓"β␈↓ α←␈↓α␈↓ ε3list[mkcall[fn]]]]
␈↓ α←␈↓␈↓6.6␈↓ π∪Compilers for Subsets of LISP 325␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Finally, here are the constructors, selectors, and recognizers:
␈↓"∀␈↓ α←␈↓␈↓↓Recognizer␈↓α
␈↓" ␈↓ α←␈↓αisconst <= λ[[x] or[␈↓ ∧Onumberp[x];
␈↓"β␈↓ α←␈↓α␈↓ ∧Oeq[x;␈↓
t␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ ∧Oeq[x;␈↓
f␈↓α];
␈↓"β␈↓ α←␈↓α␈↓ ∧Oand[not[atom[x]];eq[first[x];QUOTE]]]]
␈↓"∀␈↓ α←␈↓␈↓↓Selectors␈↓α
␈↓" ␈↓ α←␈↓αfunc <= λ[[x] first[x]]
␈↓" ␈↓ α←␈↓αarglist <= λ[[x] rest[x]]
␈↓"∀␈↓ α←␈↓α␈↓↓Constructors␈↓α
␈↓" ␈↓ α←␈↓αmksend <= λ[[dest;val] list[MOVEI;dest;val]]
␈↓" ␈↓ α←␈↓αmkalloc <= λ[[dest] list[PUSH;P;dest]]
␈↓" ␈↓ α←␈↓αmkcall <= λ[[fn] list[CALL;fn]]
␈↓" ␈↓ α←␈↓αmklink <= λ[[n][eq[n;1] → ( ); ␈↓
t␈↓α → concat[mkmove[n;1];mklink1[sub1[n]]]]
␈↓" ␈↓ α←␈↓αmklink1 <= λ[[n][zerop[n] → ( ); ␈↓
t␈↓α → concat[mkpop[n];mklink1[sub1[n]]]];
␈↓" ␈↓ α←␈↓αmkpop <= λ[[n] list[POP;P;n]]
␈↓" ␈↓ α←␈↓αmkmove <= λ[[dest;val] list[MOVE;dest;val]]
␈↓"∀␈↓ α←␈↓Note␈αthat␈α
␈↓αcompexp␈↓␈αis␈α
just␈αa␈αcomplex␈α
␈↓λr␈↓-mapping␈αwhose␈α
image␈αis␈α
a␈αsequence
␈↓ α←␈↓of machine language instructions.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
code␈α
generated␈α
by␈α
this␈α∞compiler␈α
is␈α
inefficient,␈α
but␈α
that␈α
is␈α∞not␈α
our
␈↓ α←␈↓main␈α∞concern.␈α∞We␈α∞wish␈α∞to␈α∞establish␈α∞an␈α∞intuitive␈α∞and␈α∞correct␈α∞compiler,␈α
then
␈↓ α←␈↓worry␈α
about␈α
efficiency.␈αPremature␈α
concern␈α
for␈α
efficiency␈αis␈α
folly;␈α
we␈αmust␈α
first
␈↓ α←␈↓establish a correct and clean algorithm.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Write␈α∂␈↓αcompexp␈↓␈α∂to␈α∂generate␈α∂code␈α∞for␈α∂␈↓↓option␈α∂1␈↓␈α∂as␈α∂discussed␈α∂on␈α∞page 318.
␈↓ α←␈↓␈↓ β∂Compare␈αthe␈αtwo␈αversions␈αof␈α␈↓αcompexp␈↓;␈αnow␈αwrite␈αa␈αmore␈αabstract␈αversion
␈↓ α←␈↓␈↓ β∂which encompasses both as special cases.
␈↓" ␈↓ α←␈↓2. Write␈α∂␈↓αcompexp␈↓␈α∂and␈α∂associated␈α∂functions␈α∂for␈α∂a␈α∂stack-only␈α∂machine␈α∂using
␈↓ α←␈↓␈↓ β∂the techniques outlined on page 317.
␈↓"β␈↓ α←␈↓␈↓ ∧*␈↓↓6.7 Compilation of Conditional Expressions␈↓
␈↓"β␈↓ α←␈↓Recall␈α
␈↓αtgmoafr␈↓␈α∞of␈α
Section 2.8;␈α∞the␈α
innovation␈α
in␈α∞␈↓αtgmoafr␈↓␈α
was␈α∞the␈α
evaluation
␈↓ α←␈↓of conditional expressions. The BNF equations were augmented by:
␈↓"∀␈↓ α←␈↓␈↓ ∧ε<form> ::= [<form> → <form> ; ... ;<form> → <form>]
␈↓"∀␈↓ α←␈↓␈↓ β'The␈αcompilation␈αof␈αconditional␈αexpressions␈αwill␈αmean␈αan␈αextra␈αpiece␈αof
␈↓ α←␈↓code␈α
in␈α
␈↓αcompexp␈↓␈α
to␈α
recognize␈α
␈↓αCOND␈↓␈α
and␈α
a␈α
new␈α
function␈α
(analogous␈α
to␈α
␈↓αevcond␈↓
␈↓ α←␈↓␈↓326 Dynamic Structure␈↓
(6.7␈↓
␈↓"β␈↓ α←␈↓in␈α∞␈↓αtgmoafr␈↓)␈α∞to␈α∞generate␈α∞the␈α∞code␈α∞for␈α∞the␈α∞␈↓αCOND␈↓-body.␈↓π 15␈↓␈α∞In␈α∞fact,␈α∂the␈α∞major
␈↓ α←␈↓difference␈α
between␈α␈↓αevcond␈↓␈α
and␈α
its␈αcounterpart␈α
in␈α
␈↓αcompexp␈↓,␈αwhich␈α
we␈αshall␈α
call
␈↓ α←␈↓␈↓αcomcond␈↓,␈α∀is␈α∀that␈α∪␈↓αcomcond␈↓␈α∀generates␈α∀code␈α∪for␈α∀each␈α∀of␈α∪the␈α∀branches␈α∀of␈α∪a
␈↓ α←␈↓conditional whereas ␈↓αevcond␈↓ only evaluates one branch.
␈↓"β␈↓ α←␈↓␈↓ β'The effect of ␈↓αcomcond␈↓ on the form:
␈↓"∀␈↓ α←␈↓␈↓↓COND␈↓␈↓ βs␈↓α(COND␈↓ ␈↓α(␈↓
R␈↓∞(␈↓↓ p␈↓β1 ␈↓∞)␈↓α ␈↓
R␈↓∞(␈↓↓ e␈↓β1␈↓∞)␈↓α ) ... (␈↓
R␈↓∞(␈↓↓ p␈↓βn␈↓α ␈↓∞)␈↓α ␈↓
R␈↓∞(␈↓↓ e␈↓βn ␈↓∞)␈↓α))
␈↓"∀␈↓ α←␈↓can␈α∀be␈α∀surmised␈α∀from␈α∀the␈α∪discussion␈α∀of␈α∀␈↓αreceive_test␈↓␈α∀on␈α∀page 311.␈α∪ First
␈↓ α←␈↓generate␈α
code␈α
for␈α
␈↓↓p␈↓β1␈↓;␈α
then␈αgenerate␈α
a␈α
test␈α
for␈α
truth,␈αgoing␈α
to␈α
the␈α
code␈α
for␈α␈↓↓e␈↓β1␈↓␈α
if
␈↓ α←␈↓true,␈α∂and␈α⊂going␈α∂to␈α⊂the␈α∂code␈α⊂for␈α∂␈↓↓p␈↓β2␈↓␈α∂if␈α⊂not␈α∂true.␈α⊂ The␈α∂code␈α⊂for␈α∂␈↓↓e␈↓β1␈↓␈α⊂must␈α∂be
␈↓ α←␈↓followed␈α∀by␈α∪an␈α∀exit␈α∪from␈α∀the␈α∪code␈α∀for␈α∪the␈α∀conditional,␈α∪and␈α∀we␈α∪should
␈↓ α←␈↓generate an error condition to be executed in the case that ␈↓↓p␈↓βn␈↓ is false.
␈↓"β␈↓ α←␈↓␈↓ β'We represent the code as:
␈↓"⊃␈↓ α←␈↓∂ ␈↓<code for ␈↓↓p␈↓β1␈↓>␈↓∂
␈↓"␈↓ α←␈↓∂ /\
␈↓"␈↓ α←␈↓∂ / \
␈↓"␈↓ α←␈↓∂ ␈↓∞G␈↓∂ ␈↓∞H␈↓∂
␈↓"␈↓ α←␈↓∂ T NIL
␈↓"␈↓ α←␈↓∂ / \
␈↓"␈↓ α←␈↓∂ ␈↓∞G␈↓∂ ␈↓∞H␈↓∂
␈↓"␈↓ α←␈↓∂ ␈↓<code for ␈↓↓e␈↓β1␈↓>␈↓∂ ␈↓<code for ␈↓↓p␈↓β2␈↓↓>␈↓∂
␈↓"␈↓ α←␈↓∂ ␈↓∞G␈↓∂ ␈↓∞GH␈↓∂
␈↓"␈↓ α←␈↓∂ / T NIL
␈↓"␈↓ α←␈↓∂ ␈↓∞G␈↓∂ ␈↓∞G␈↓∂ ␈↓∞H␈↓∂
␈↓"␈↓ α←␈↓∂ / ␈↓<code for ␈↓↓e␈↓β2␈↓>␈↓∂ ␈↓<code for ␈↓↓p␈↓β3␈↓↓>␈↓∂
␈↓"␈↓ α←␈↓∂ ␈↓∞G␈↓∂ / ␈↓∞GH␈↓∂
␈↓"␈↓ α←␈↓∂ ␈↓∞H␈↓∂ ␈↓∞G␈↓∂ ... ...
␈↓"␈↓ α←␈↓∂ \ / ␈↓∞G␈↓∂ ␈↓∞H␈↓∂
␈↓"␈↓ α←␈↓∂ ␈↓∞H␈↓∂ ␈↓∞G␈↓∂ T NIL
␈↓"␈↓ α←␈↓∂ \ / / \
␈↓"␈↓ α←␈↓∂ \ / ␈↓∞G␈↓∂ ␈↓∞H␈↓∂
␈↓"␈↓ α←␈↓∂ ␈↓∞H␈↓∂ ␈↓∞G␈↓∂ ␈↓<code for ␈↓↓e␈↓βn␈↓>␈↓∂ ␈↓<code for error␈↓∂>
␈↓"␈↓ α←␈↓∂ \ / ␈↓∞G␈↓∂
␈↓"␈↓ α←␈↓∂ ␈↓∞H␈↓∂ ␈↓∞G␈↓∂
␈↓"␈↓ α←␈↓∂ ... /
␈↓"␈↓ α←␈↓∂ \ /
␈↓"␈↓ α←␈↓∂ ␈↓∞H␈↓∂ ␈↓∞G␈↓∂
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 15␈↓If␈αwe␈αhad␈α
designed␈αthe␈αcompiler␈αlike␈α
the␈αevaluators␈αin␈αSection 5.8␈α
we
␈↓ α←␈↓would␈αneed␈αonly␈α
attach␈αa␈αcompile-property␈α
to␈αthe␈αatom␈α
␈↓αCOND␈↓,␈αand␈αmake␈α
the
␈↓ α←␈↓property-value ␈↓αCOMCOND␈↓.
␈↓ α←␈↓␈↓6.7␈↓ ε∀Compilation of Conditional Expressions 327␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Expressing␈α≥␈↓αcomcond␈↓␈α≥in␈α≥terms␈α≤of␈α≥␈↓↓SM␈↓␈α≥primitives␈α≥requires␈α≤more
␈↓ α←␈↓conventions in our compiler.
␈↓"∀␈↓ α←␈↓␈↓ βWWe␈αmust␈αbe␈αable␈αto␈αtest␈αfor␈α␈↓
t␈↓␈α(or␈α␈↓
f␈↓).␈α Previous␈αconventions
␈↓ α←␈↓␈↓ βWimply␈α
that␈α
the␈α
value␈α
of␈αa␈α
predicate␈α
will␈α
be␈α
found␈α
in␈α␈↓αAC1␈↓.
␈↓ α←␈↓␈↓ βWWe␈α
can␈αtest␈α
for␈αthe␈α
occurrence␈α
of␈α␈↓
t␈↓␈α
or␈α␈↓
f␈↓␈α
using␈αthe␈α
␈↓αJUMPT␈↓
␈↓ α←␈↓␈↓ βWor ␈↓αJUMPF␈↓ instruction (see Section 6.3) respectively.␈↓π 16␈↓
␈↓"β␈↓ α←␈↓␈↓ More Compiling Conventions ␈↓
␈↓"λ␈↓ α←␈↓␈↓ β'Since␈αour␈αcode␈αis␈αto␈αbe␈αa␈α␈↓¬sequence␈↓␈αof␈αinstructions,␈αwe␈αmust␈αlinearize␈αthe
␈↓ α←␈↓graph-representation␈α⊃of␈α⊃the␈α⊃generated␈α⊃code.␈α⊃ We␈α⊃can␈α⊃generate␈α∩a␈α⊃sequence
␈↓ α←␈↓representation␈αby␈αappropriately␈αinterspersing␈αlabels␈αand␈α␈↓αJUMP␈↓s␈αbetween␈α
the
␈↓ α←␈↓blocks of instructions for the ␈↓↓p␈↓βi␈↓'s and ␈↓↓e␈↓βi␈↓'s. We will generate:
␈↓"∀␈↓ α←␈↓␈↓ ∧␈␈↓α(␈↓ ¬G␈↓<code for ␈↓↓p␈↓β1␈↓>␈↓α
␈↓" ␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬G(JUMPF 1 L1)␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧␈␈↓ ¬G<code for ␈↓↓e␈↓β1␈↓>␈↓α
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬G(JUMP L0)
␈↓" ␈↓ α←␈↓α␈↓ ∧␈L1␈↓ ¬G␈↓<code for ␈↓↓p␈↓β2␈↓>␈↓α
␈↓" ␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬G(JUMPF 1 L2)
␈↓"β␈↓ α←␈↓α␈↓ ∧␈ ...
␈↓" ␈↓ α←␈↓α␈↓ ∧␈Ln-1␈↓ ¬G␈↓<code for ␈↓↓p␈↓βn␈↓>␈↓α
␈↓" ␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬G(JUMPF 1 Ln)␈↓
␈↓" ␈↓ α←␈↓␈↓ ∧␈␈↓ ¬G<code for ␈↓↓e␈↓βn␈↓>␈↓α
␈↓" ␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬G(JUMP L0)
␈↓"β␈↓ α←␈↓α␈↓ ∧␈Ln␈↓ ¬G(JUMP ERROR)
␈↓"β␈↓ α←␈↓α␈↓ ∧␈L0␈↓ ¬G )
␈↓"∀␈↓ α←␈↓We␈α⊃need␈α⊃to␈α⊃construct␈α⊃the␈α⊃labels,␈α⊃␈↓αLi␈↓.␈α⊃ These␈α⊃labels␈α⊃should␈α⊃be␈α⊃atomic␈α⊃and
␈↓ α←␈↓should␈αbe␈αdistinct.␈αLISP␈α
has␈αa␈αfunction␈αnamed␈α
␈↓αgensym␈↓␈αwhich␈αis␈αused␈αfor␈α
this
␈↓ α←␈↓task.␈α
␈↓αgensym␈↓␈α
is␈αa␈α
function␈α
of␈αno␈α
arguments␈α
whose␈αvalue␈α
is␈α
an␈αidentifier␈α
called
␈↓ α←␈↓a␈α␈↓↓generated␈α
symbol␈↓,␈αor␈α
"gensym".␈α Gensyms␈αare␈α
not␈αtrue␈α
atoms␈αsince␈αthey␈α
are
␈↓ α←␈↓not␈α⊂placed␈α⊃in␈α⊂the␈α⊃object␈α⊂list;␈α⊃they␈α⊂are␈α⊃usually␈α⊂used␈α⊃only␈α⊂for␈α⊃their␈α⊂unique
␈↓ α←␈↓name.␈α
If␈α
it␈α
is␈α
desired␈α
to␈α
use␈α
them␈α
as␈α
atoms,␈α
they␈α
must␈α
be␈α
placed␈α
on␈α
the␈α
object
␈↓ α←␈↓list␈α⊂using␈α⊂the␈α⊂function␈α⊃␈↓αintern␈↓␈α⊂(page 279).␈α⊂ Gensyms␈α⊂are␈α⊂distinct␈α⊃from␈α⊂each
␈↓ α←␈↓other and will be distinct from all other atoms.␈↓π 17␈↓
␈↓"β␈↓ α←␈↓␈↓ β'We␈α⊂want␈α⊂to␈α∂write␈α⊂a␈α⊂recursive␈α⊂version␈α∂of␈α⊂␈↓αcomcond␈↓;␈α⊂therefore␈α⊂we␈α∂must
␈↓ α←␈↓determine␈α∂what␈α∂code␈α∂gets␈α∂generated␈α∂on␈α∂each␈α∂recursion␈α∂and␈α∂what␈α∂code␈α∞gets
␈↓ α←␈↓generated at the termination case.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 16␈↓In␈α∀this␈α∀implementation␈α∀any␈α∀value␈α∀other␈α∀than␈α∀␈↓
f␈↓␈α∀will␈α∪be
␈↓ α←␈↓considered ␈↓
t␈↓. See Section 5.5.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 17␈↓In␈αmany␈αversions␈αof␈αLISP,␈αgensyms␈αare␈αof␈αthe␈αform␈α␈↓αGn␈↓␈αwhere␈α␈↓αn␈↓␈αis␈αa
␈↓ α←␈↓four␈αdigit␈αnumber␈αbeginning␈αat␈α␈↓α0000␈↓.␈αThus␈αthe␈αfirst␈αcall␈αof␈α␈↓αgensym[␈α]␈↓␈αwould
␈↓ α←␈↓give ␈↓αG0000␈↓; succeeding calls would give ␈↓αG0001, G0002␈↓, etc.
␈↓ α←␈↓␈↓328 Dynamic Structure␈↓
(6.7␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Looking at the example, we see that the block
␈↓"∀␈↓ α←␈↓␈↓ βB␈↓α(␈↓<code for ␈↓↓p␈↓βi␈↓> ␈↓α(JUMPF 1 Li) ␈↓<code for ␈↓↓e␈↓βi␈↓> ␈↓α(JUMP L0) Li)␈↓
␈↓"∀␈↓ α←␈↓is the natural segment for each recursion and that:
␈↓"∀␈↓ α←␈↓␈↓ ¬>␈↓α((JUMP ERROR) L0)␈↓
␈↓"∀␈↓ α←␈↓should␈α
be␈α
generated␈α
for␈α
the␈α
termination␈α
case.␈α
Within␈α
each␈α
block␈α
we␈α
need␈α
a
␈↓ α←␈↓"local"␈αlabel,␈α␈↓αLi␈↓;␈αand␈αwithin␈αeach␈αblock,␈αincluding␈αthe␈αterminal␈αcase,␈αwe␈αrefer
␈↓ α←␈↓to␈αthe␈αlabel␈α␈↓αL0␈↓␈αwhich␈αis␈α"global"␈αto␈αthe␈αwhole␈αconditional.␈α We␈αcan␈αnow␈αadd
␈↓ α←␈↓the recognizer for ␈↓αCOND␈↓ to ␈↓αcompexp␈↓ and construct ␈↓αcomcond␈↓.
␈↓"β␈↓ α←␈↓α␈↓Add the clause:
␈↓" ␈↓ α←␈↓α␈↓ ∧9iscond[exp] → comcond[args␈↓βc␈↓α[exp];gensym[ ]];
␈↓" ␈↓ α←␈↓to␈↓α compexp ␈↓where:
␈↓" ␈↓ α←␈↓αcomcond <= λ[[u;glob]␈↓ ∧␈[null[u] → list[mkerror[ ];glob];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈ ␈↓
t␈↓α → append[␈↓ ε'comclause[␈↓ π+first[u];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ε'␈↓ π+gensym[];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ε'␈↓ π+glob];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ε'comcond[rest[u]; glob] ]
␈↓"∀␈↓ α←␈↓αcomclause <=λ[[p;loc;glob] append[␈↓ ε'compexp[ante[p]];
␈↓"β␈↓ α←␈↓α␈↓ ε' list[mkjumpf[loc]];
␈↓"β␈↓ α←␈↓α␈↓ ε' compexp[conseq[p]];
␈↓"β␈↓ α←␈↓α␈↓ ε' list[mkjump[glob];loc] ]]
␈↓"∀␈↓ α←␈↓α␈↓↓Recognizer␈↓α
␈↓" ␈↓ α←␈↓αiscond <= λ[[x] eq[first[x]; COND]]
␈↓"∀␈↓ α←␈↓α␈↓↓Selectors␈↓α
␈↓" ␈↓ α←␈↓αargs␈↓βc␈↓α <= λ[[x] rest[x]]
␈↓" ␈↓ α←␈↓αante <= λ[[c] first[c]]
␈↓" ␈↓ α←␈↓αconseq <= λ[[c] second[c]]␈↓π 18␈↓α
␈↓"∀␈↓ α←␈↓α␈↓↓Constructors␈↓α
␈↓" ␈↓ α←␈↓αmkerror <= λ[[] (JUMP ERROR)]
␈↓" ␈↓ α←␈↓αmkjumpf <= λ[[l] list[JUMPF;1;l]]
␈↓" ␈↓ α←␈↓αmkjump <= λ[[l] list[JUMP;l]]
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 18␈↓This␈α_definition␈α↔of␈α_␈↓αconseq␈↓␈α_does␈α↔not␈α_allow␈α_extended␈α↔conditional
␈↓ α←␈↓expressions. See problem 2 on page 329.
␈↓ α←␈↓␈↓6.7␈↓ ε∀Compilation of Conditional Expressions 329␈↓
␈↓"∀␈↓ α←␈↓The partially exposed recursive structure of ␈↓αcomcond␈↓ would show:
␈↓"∀␈↓ α←␈↓␈↓αcomcond[((␈↓↓p␈↓β1␈↓ ␈↓↓e␈↓β1␈↓α) ...(␈↓↓p␈↓βn␈↓α ␈↓↓e␈↓βn␈↓α));L0]=
␈↓" ␈↓ α←␈↓α␈↓ ∧␈(␈↓␈↓ ¬k<code for ␈↓↓p␈↓β1␈↓>␈↓α
␈↓" ␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬k(JUMPF 1 L1)␈↓
␈↓" ␈↓ α←␈↓␈↓ ∧␈␈↓ ¬k<code for ␈↓↓e␈↓β1␈↓>␈↓α
␈↓" ␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬k(JUMP L0)
␈↓" ␈↓ α←␈↓α␈↓ ∧␈ L1␈↓ ¬kcomcond[((␈↓↓p␈↓β2␈↓ ␈↓↓e␈↓β2␈↓α) ...(␈↓↓p␈↓βn␈↓ ␈↓↓e␈↓βn␈↓α)); L0])
␈↓"∀␈↓ α←␈↓␈↓ β'We␈α∞need␈α∞to␈α∂extend␈α∞our␈α∞assembler␈α∞to␈α∂handle␈α∞the␈α∞generated␈α∂labels␈α∞and
␈↓ α←␈↓jumps which appear in the conditional expression code.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"∀␈↓ α←␈↓1. Evaluate:␈↓ ∧π␈↓αcompexp[(COND␈↓ ¬k((EQ (QUOTE A)(QUOTE B))(QUOTE C))
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ¬k((NULL (QUOTE A))(QUOTE FOO)))]
␈↓" ␈↓ α←␈↓2. Extend ␈↓αcomcond␈↓ to handle extended conditional expressions.
␈↓"β␈↓ α←␈↓␈↓ ∧↑␈↓↓6.8 One-pass Assemblers and Fixups␈↓
␈↓"β␈↓ α←␈↓Compilation␈α⊂of␈α⊂conditional␈α⊂expressions␈α∂requires␈α⊂that␈α⊂the␈α⊂assembler␈α∂handle
␈↓ α←␈↓the␈α
generated␈α
label␈αconstructs.␈α
On␈α
page 327␈αwe␈α
illustrated␈α
the␈α
general␈αform
␈↓ α←␈↓for␈α∀conditional␈α∪expression␈α∀code.␈α∪ The␈α∀symbols,␈α∪␈↓αL0,␈α∀L1,␈↓␈α∪and␈α∀␈↓αL2␈↓␈α∀in␈α∪that
␈↓ α←␈↓example␈α
are␈αgenerated␈α
symbols,␈αrepresenting␈α
labels.␈α Though␈α
the␈αgensyms␈α
are
␈↓ α←␈↓␈↓¬not␈↓␈αtrue␈αatoms,␈αthey␈α␈↓¬will␈↓␈αsatisfy␈αthe␈αtest␈αfor␈α␈↓αatom␈↓.␈αTherefore␈αwe␈αcan␈αrepresent
␈↓ α←␈↓the recoginzer ␈↓αis_label␈↓ as the predicate ␈↓αatom␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'When␈αthe␈αassembler␈αrecognizes␈αa␈αlabel,␈αit␈αadds␈αinformation␈αto␈αa␈αsymbol
␈↓ α←␈↓table,␈α∞associating␈α∞that␈α
label␈α∞with␈α∞the␈α∞current␈α
value␈α∞of␈α∞the␈α∞assembly␈α
counter.
␈↓ α←␈↓References␈α∂to␈α∂that␈α∂label␈α∂will␈α∂be␈α∂translated␈α∂into␈α∂references␈α∂to␈α∂the␈α∂associated
␈↓ α←␈↓machine␈α∞location.␈α∞ The␈α∞only␈α∞problem␈α∞is␈α∞that␈α∞references␈α∞to␈α∞labels␈α∂may␈α∞occur
␈↓ α←␈↓␈↓¬before␈↓␈α∀we␈α∪have␈α∀come␈α∀across␈α∪the␈α∀label␈α∀definition␈α∪in␈α∀the␈α∀program.␈α∪ Such
␈↓ α←␈↓references␈α
are␈α
called␈α
␈↓↓forward␈α
references␈↓.␈α
For␈α
example,␈α
all␈α
references␈α
in␈α
the
␈↓ α←␈↓␈↓αCOND␈↓-code are forward references.␈↓π 19␈↓
␈↓"β␈↓ α←␈↓␈↓ β'There are two solutions to the forward reference problem:
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 19␈↓If␈α∞we␈α∞scan␈α∞the␈α
instruction␈α∞sequence␈α∞in␈α∞the␈α
order␈α∞in␈α∞which␈α∞the␈α
code
␈↓ α←␈↓would␈α⊃be␈α⊃executed,␈α⊃we␈α⊃always␈α⊃refer␈α⊃to␈α⊃a␈α⊃label␈α⊃before␈α⊃we␈α⊃come␈α⊃across␈α⊂its
␈↓ α←␈↓definition.␈α⊃We␈α∩could␈α⊃skirt␈α∩the␈α⊃forward␈α⊃reference␈α∩problem␈α⊃by␈α∩loading␈α⊃the
␈↓ α←␈↓program␈α∂in␈α∂reverse␈α⊂order:␈α∂rather␈α∂than␈α⊂beginning␈α∂with␈α∂the␈α⊂first␈α∂instruction
␈↓ α←␈↓and␈αloading␈α␈↓¬upward␈↓␈αin␈αmemory,␈αwe␈αcould␈αbegin␈αwith␈αthe␈αlast␈αinstruction␈α
and
␈↓ α←␈↓load␈α⊂downward.␈α⊂ However,␈α⊂this␈α⊃would␈α⊂only␈α⊂be␈α⊂a␈α⊂temporary␈α⊃expedient:␈α⊂an
␈↓ α←␈↓assembler␈α⊂must␈α⊂also␈α⊂handle␈α⊂␈↓αprog␈↓s,␈α⊂and␈α⊂the␈α⊂label␈α⊂structure␈α⊂of␈α⊂␈↓αprog␈↓s␈α⊃is␈α⊂not
␈↓ α←␈↓restricted to such predictable behavior.
␈↓ α←␈↓␈↓330 Dynamic Structure␈↓
(6.8␈↓
␈↓"β␈↓ α←␈↓␈↓↓1.␈↓ Make␈α∞two␈α∞passes␈α∂through␈α∞the␈α∞program␈α∂being␈α∞assembled.␈α∞ The␈α∂first␈α∞pass
␈↓ α←␈↓␈↓ β∂builds␈α∂a␈α∂symbol␈α∂table␈α∞of␈α∂pairs,␈α∂each␈α∂pair␈α∞consisting␈α∂of␈α∂a␈α∂label␈α∂and␈α∞the
␈↓ α←␈↓␈↓ β∂value␈α∂of␈α∞the␈α∂assembly␈α∂counter␈α∞which␈α∂will␈α∞be␈α∂assigned␈α∂to␈α∞it.␈α∂No␈α∂code␈α∞is
␈↓ α←␈↓␈↓ β∂generated; a second pass uses this symbol table to assemble the code.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ The␈αother␈αsolution␈αis␈αto␈αmake␈αone␈αpass␈αthrough␈αthe␈αinput.␈α Whenever␈αwe
␈↓ α←␈↓␈↓ β∂come␈αacross␈αa␈αforward␈αreference␈αwe␈αupdate␈αthe␈αsymbol␈αtable␈αwith␈αa␈αtriple
␈↓ α←␈↓␈↓ β∂consisting␈αof␈αthe␈α
label␈αname,␈αthe␈αassembly␈α
counter,␈αand␈αan␈αindication␈α
that
␈↓ α←␈↓␈↓ β∂the␈αentry␈αis␈αa␈α
forward␈αreference.␈α We␈αassemble␈α
as␈αmuch␈αof␈αthe␈α
instruction
␈↓ α←␈↓␈↓ β∂as␈αwe␈α
can,␈αexpanding␈αthe␈α
other␈αfields.␈α
When␈αa␈αlabel␈α
is␈αdefined,␈αwe␈α
check
␈↓ α←␈↓␈↓ β∂the␈α∩table␈α∩for␈α∩forward␈α∩references␈α∩pending␈α∩on␈α∩that␈α∩label.␈α∩ If␈α∩there␈α∩are
␈↓ α←␈↓␈↓ β∂entries␈αposted,␈αwe␈α␈↓↓fix-up␈↓␈αthose␈αinstructions␈αto␈αreference␈αthe␈αlocation␈αnow
␈↓ α←␈↓␈↓ β∂assigned to the label.
␈↓" ␈↓ α←␈↓␈↓ β'Some␈α≠minor␈α≠programming␈α≤restrictions␈α≠are␈α≠imposed␈α≤by␈α≠one-pass
␈↓ α←␈↓assemblers,␈α≡but␈α≡particularly␈α∨for␈α≡assembling␈α≡compiled␈α∨code,␈α≡one-pass
␈↓ α←␈↓assemblers are usually sufficient and are quite fast.␈↓π 20␈↓
␈↓"β␈↓ α←␈↓␈↓ β'There␈αare␈αat␈αleast␈αtwo␈αways␈αto␈αhandle␈αthe␈αfixup␈αproblem.␈α If␈αthe␈α
fixups
␈↓ α←␈↓are␈αsimple,␈αsay␈αonly␈αrequiring␈αfixups␈α
to␈αthe␈αaddress-part␈αof␈αa␈αword,␈α
then␈αwe
␈↓ α←␈↓may␈αlink␈αthose␈αpending␈αforward␈αreferences␈αtogether,␈αchaining␈αthem␈αon␈αtheir,
␈↓ α←␈↓as yet, un-fixed-up field.
␈↓"⊃␈↓ α←␈↓
⊂αααααπααααα⊃
␈↓"␈↓ α←␈↓
~ ~ ≤' ~←α⊃
␈↓"␈↓ α←␈↓
εαααααβαααααλ ~
␈↓"␈↓ α←␈↓
~ ~ ~ ~
␈↓"␈↓ α←␈↓
εαααααβαααααλ ↑
␈↓"␈↓ α←␈↓
~ ~ ~ ~
␈↓"␈↓ α←␈↓
εαααααβαααααλ ~
␈↓"␈↓ α←␈↓
~ ~ #ααβαα$
␈↓"␈↓ α←␈↓
~ ~ ~←α⊃
␈↓"␈↓ α←␈↓
εαααααβαααααλ ~
␈↓"␈↓ α←␈↓
~ ~ ~ ↑
␈↓"␈↓ α←␈↓
εαααααβαααααλ ~
␈↓"␈↓ α←␈↓
~ ~ ~ ~
␈↓"␈↓ α←␈↓
εαααααβαααααλ ↑
␈↓"␈↓ α←␈↓
~ ~ #ααβαα$
␈↓"␈↓ α←␈↓
~ ~ ~←α⊃
␈↓"␈↓ α←␈↓
εαααααβαααααλ ~
␈↓"␈↓ α←␈↓
~ ~ ~ ↑
␈↓"␈↓ α←␈↓
εαααααβαααααλ ~
␈↓"␈↓ α←␈↓
~ ~ ~ ↑
␈↓"␈↓ α←␈↓
εαααααβαααααλ ~
␈↓"␈↓ α←␈↓
~ ~ ~ ~
␈↓"␈↓ α←␈↓
εαααααβαααααλ ↑
␈↓"␈↓ α←␈↓
pointer from αααααα→ ~ ~ #ααβαα$
␈↓"␈↓ α←␈↓
entry in object list %ααααα∀ααααα$
␈↓" ␈↓ α←␈↓␈↓ A Simple Fixup Scheme ␈↓
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 20␈↓One␈α∃such␈α∃restriction␈α∃requires␈α∃that␈α∃any␈α∃quantities␈α∃which␈α∃specify
␈↓ α←␈↓storage␈αallocation,␈αmust␈α
be␈αresolved␈αbefore␈α
that␈αstorage␈αallocation␈αstatement␈α
is
␈↓ α←␈↓executed.
␈↓ α←␈↓␈↓6.8␈↓ εnOne-pass Assemblers and Fixups 331␈↓
␈↓"β␈↓ α←␈↓Each␈αtime␈αa␈αforward␈αreference␈αis␈αseen␈αit␈αis␈αadded␈αto␈αthe␈αlinked␈αlist;␈αwhen␈αthe
␈↓ α←␈↓label␈α∞is␈α∂finally␈α∞defined␈α∂and␈α∞given␈α∞an␈α∂address␈α∞in␈α∂memory,␈α∞then␈α∂the␈α∞address
␈↓ α←␈↓replaces␈α
each␈α
reference␈αlink.␈α
No␈α
extra␈αstorage␈α
is␈α
used␈αsince␈α
the␈α
linked␈α
list␈αis
␈↓ α←␈↓stored␈αin␈αthe␈α
partially␈αassembled␈αcode.␈α
This␈αassumes␈αa␈α
word␈αsize␈αis␈α
sufficient
␈↓ α←␈↓to␈α
contain␈αfixup␈α
information␈α
as␈αwell␈α
as␈αpartially␈α
assembled␈α
code.␈αIf␈α
this␈αis␈α
not
␈↓ α←␈↓the case, there is an alternative.
␈↓"β␈↓ α←␈↓␈↓ β'Another␈α∪solution,␈α∪which␈α∪is␈α∪potentially␈α∩more␈α∪general␈α∪is␈α∪to␈α∪store␈α∩the
␈↓ α←␈↓information␈α⊂about␈α⊂each␈α⊂fixup␈α⊂in␈α∂the␈α⊂symbol␈α⊂table␈α⊂under␈α⊂each␈α⊂label.␈α∂This
␈↓ α←␈↓solution␈αsolves␈αthe␈αprevious␈αproblem␈α
and␈αwould␈αalso␈αallow␈αfixup␈α
information
␈↓ α←␈↓for arbitrarily small fields in instructions.
␈↓"⊃␈↓ α←␈↓
from object list entry
␈↓"␈↓ α←␈↓
~ ⊂αααααπααα⊃ ⊂αααααπααα⊃ ⊂αααααπααα⊃
␈↓"␈↓ α←␈↓
%αα→~ l# ~ #αβαα→~ r# ~ #αβαα→~ r# ~ ###
␈↓"␈↓ α←␈↓
⊂αααααπααααα⊃ %ααβαα∀ααα$ %ααβαα∀ααα$ %ααβαα∀ααα$
␈↓"␈↓ α←␈↓
~ ~ ~ ↓ ↓ ↓
␈↓"␈↓ α←␈↓
εαααααβαααααλ ~ ~ ~
␈↓"␈↓ α←␈↓
~fixup~ ~←ααααα$ ~ ~
␈↓"␈↓ α←␈↓
εαααααβαααααλ ↓ ↓
␈↓"␈↓ α←␈↓
~ ~ ~ ~ ~
␈↓"␈↓ α←␈↓
εαααααβαααααλ ~ ~
␈↓"␈↓ α←␈↓
~ ~fixup~←ααααααα←ααααααα←ααα$ ~
␈↓"␈↓ α←␈↓
εαααααβαααααλ ↓
␈↓"␈↓ α←␈↓
~ ~fixup~←ααααααα←ααααααα←ααααααααα←ααααααα$
␈↓"␈↓ α←␈↓
εαααααβαααααλ
␈↓"␈↓ α←␈↓
~ ~
␈↓"␈↓ α←␈↓
ε . . . λ
␈↓" ␈↓ α←␈↓␈↓ Another Fixup Scheme ␈↓
␈↓"→␈↓ α←␈↓The␈α∃additional␈α∃information␈α∃tells␈α∃the␈α∃fixup␈α∃routine␈α∃how␈α∃to␈α∃modify␈α∃the
␈↓ α←␈↓referenced location.
␈↓"β␈↓ α←␈↓␈↓ β'Both␈α~methods␈α→are␈α~useful.␈α→Both␈α~have␈α→been␈α~used␈α~extensively␈α→in
␈↓ α←␈↓assemblers␈αand␈αcompilers.␈αWe␈α
now␈αsketch␈αa␈αsimple␈αone-pass␈α
assembler.␈α The
␈↓ α←␈↓assembler will need two functions:
␈↓"∀␈↓ α←␈↓␈↓αdeposit␈↓␈↓α[x;y]␈↓:␈α∩␈↓αx␈↓␈α∩represents␈α∩a␈α∩machine␈α∩address;␈α∩␈↓αy␈↓␈α∩is␈α∩a␈α∩list,␈α∪representing␈α∩the
␈↓ α←␈↓␈↓ β'instruction␈α∪to␈α∪be␈α∪deposited.␈α∪␈↓αy␈↓␈α∪could␈α∪be␈α∪a␈α∪list␈α∪of␈α∪elements:␈α∩␈↓α(␈↓opcode,
␈↓ α←␈↓␈↓ β'accumulator␈α∩number,␈α∪memory␈α∩address␈↓α)␈↓␈α∪The␈α∩value␈α∪of␈α∩␈↓αdeposit␈↓␈α∪is␈α∩the
␈↓ α←␈↓␈↓ β'value of ␈↓αy␈↓.
␈↓"∀␈↓ α←␈↓␈↓αexamine␈↓␈↓α[x]␈↓:␈α⊂␈↓αx␈↓␈α⊂represents␈α⊂a␈α⊂machine␈α⊂address.␈α⊂ The␈α⊂value␈α⊂of␈α⊂␈↓αexamine␈↓␈α⊂is␈α∂the
␈↓ α←␈↓␈↓ β'contents of location ␈↓αx␈↓ in the form of a list as specified above.
␈↓"∀␈↓ α←␈↓We␈αuse␈αour␈αfixup␈αmechanism,␈αcombined␈αwith␈α␈↓αexamine␈↓,␈α␈↓αdeposit␈↓,␈α
and␈α␈↓αputprop␈↓
␈↓ α←␈↓and␈α∞␈↓αremprop␈↓␈α
from␈α∞page 261␈α
to␈α∞write␈α∞the␈α
parts␈α∞of␈α
the␈α∞assembler␈α∞which␈α
deal
␈↓ α←␈↓with␈αforward␈αreferences␈αand␈αlabels.␈α If␈αthe␈αlabel␈αhas␈αbeen␈αassigned␈αa␈αlocation
␈↓ α←␈↓then␈α∂the␈α∂property␈α∂list␈α∂of␈α∂the␈α∂label␈α∂will␈α∂contain␈α∂the␈α∂indicator␈α∂␈↓αSYM␈↓␈α∂and␈α∂an
␈↓ α←␈↓associated␈αvalue␈αrepresenting␈αthe␈αassigned␈αlocation.␈α If␈αthe␈αlabel␈αhas␈α␈↓¬not␈↓␈αbeen
␈↓ α←␈↓previously␈α∩defined␈α∩but␈α∩has␈α∩been␈α∩referenced␈α∩then␈α∩the␈α∩atom␈α∩will␈α∩have␈α⊃an
␈↓ α←␈↓␈↓332 Dynamic Structure␈↓
(6.8␈↓
␈↓"β␈↓ α←␈↓indicator␈α
␈↓αUNDEF␈↓;␈α∞the␈α
value-part␈α
will␈α∞be␈α
a␈α
list␈α∞of␈α
all␈α
those␈α∞locations␈α
which
␈↓ α←␈↓reference␈α∞that␈α
label.␈α∞Since␈α
we␈α∞will␈α
only␈α∞be␈α
doing␈α∞simple␈α
fixups,␈α∞this␈α∞will␈α
be
␈↓ α←␈↓sufficient.␈αThe␈αcontents␈αof␈αany␈αlocation␈αreferenced␈αfrom␈αsuch␈αa␈αfixup␈αlist␈αwill
␈↓ α←␈↓be␈α⊂a␈α⊂partially␈α⊂assembled␈α⊂word␈α⊂with␈α⊂the␈α⊂memory␈α⊂address␈α⊂portion␈α⊂set␈α⊃to␈α⊂␈↓α0␈↓.
␈↓ α←␈↓When␈α⊂the␈α⊂label␈α⊂finally␈α∂is␈α⊂defined␈α⊂we␈α⊂must␈α∂perform␈α⊂the␈α⊂fixups,␈α⊂delete␈α∂the
␈↓ α←␈↓␈↓αUNDEF␈↓ pair, and add a ␈↓αSYM␈↓ pair. There are two main functions.
␈↓"∀␈↓ α←␈↓␈↓αdefloc␈↓␈α∩is␈α⊃called␈α∩when␈α∩a␈α⊃label␈α∩has␈α⊃been␈α∩defined;␈α∩if␈α⊃there␈α∩are␈α∩no␈α⊃pending
␈↓ α←␈↓␈↓ β'forward␈α∞references␈α∞then␈α∞the␈α∞␈↓αSYM␈↓␈α∞pair␈α∞is␈α∞simply␈α∞added,␈α∞otherwise␈α∞the
␈↓ α←␈↓␈↓ β'fixup mechanism is exercised.
␈↓"∀␈↓ α←␈↓αdefloc <= λ[[lab;loc] prog[[z]␈↓ ¬S[null[z ← get[lab;UNDEF]] → go[a]];
␈↓" ␈↓ α←␈↓α␈↓ ∧␈fixup␈↓ ¬Sdeposit[␈↓ ε3car[z];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬S␈↓ ε3fixit[examine[car[z]];loc]];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬S[z ← cdr[z] → go[fixup]];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬Sremprop[lab;UNDEF];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈a␈↓ ¬Sreturn[putprop[lab;loc;SYM]]]]
␈↓"∀␈↓ α←␈↓␈↓αfixit <= λ[[x;l] mkinstr[op[x];ac[x];add[x];l]]␈↓
␈↓"∀␈↓ α←␈↓␈↓αgval␈↓␈αis␈αcalled␈αwhen␈αa␈αlabel␈αis␈αreferenced.␈αIf␈αthe␈αlabel␈αis␈αalready␈αdefined␈αthen
␈↓ α←␈↓␈↓ β'it␈αsimply␈αreturns␈αthe␈α␈↓αSYM␈↓␈αvalue;␈αotherwise␈αit␈αadds␈αa␈αforward␈αreference
␈↓ α←␈↓␈↓ β'to the list.
␈↓"∀␈↓ α←␈↓αgval <= λ[[lab]␈↓ ∧∨[get[lab;SYM];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨ ␈↓
t␈↓α → putprop[lab;cons[loc;get[lab;UNDEF]];UNDEF];0]]
␈↓"∀␈↓ α←␈↓Notes: these functions use lots of tricks.
␈↓"β␈↓ α←␈↓␈↓ βW␈↓↓1.␈↓ In␈α∂␈↓αdefloc␈↓␈α∂we␈α∂use␈α∂␈↓αget␈↓␈α⊂as␈α∂a␈α∂predicate,␈α∂relying␈α∂on␈α⊂our␈α∂convention
␈↓ α←␈↓␈↓ ∧πthat a non-␈↓αNIL␈↓ value represents truth (Section 5.5).
␈↓" ␈↓ α←␈↓␈↓ βW␈↓↓2.␈↓ In␈αthat␈αsame␈αconditional,␈αwe␈α
also␈αrely␈αon␈αthe␈αfact␈αthat␈α
the␈αvalue
␈↓ α←␈↓␈↓ ∧πof␈α∞an␈α
assignment␈α∞statement␈α∞is␈α
the␈α∞value␈α∞of␈α
its␈α∞right␈α∞hand␈α
side.
␈↓ α←␈↓␈↓ ∧πWe appeal to points ␈↓↓1␈↓ and ␈↓↓2␈↓ in the second conditional of ␈↓αdefloc␈↓.
␈↓" ␈↓ α←␈↓␈↓ βW␈↓↓3.␈↓ In␈α␈↓αgval␈↓,␈αthere␈αis␈αno␈αe␈↓β1␈↓;␈αrecalling␈α(Section 5.5)␈αthat␈αif␈αp␈↓β1␈↓␈αevaluates
␈↓ α←␈↓␈↓ ∧πto␈α∪something␈α∪non-␈↓αNIL␈↓,␈α∪then␈α∪that␈α∩value␈α∪is␈α∪the␈α∪value␈α∪of␈α∩the
␈↓ α←␈↓␈↓ ∧πconditional expression.
␈↓" ␈↓ α←␈↓␈↓ βW␈↓↓4.␈↓ We␈α∪also␈α∪use␈α∪an␈α∪extended␈α∪conditional␈α∪in␈α∪␈↓αgval␈↓,␈α∪executing␈α∪the
␈↓ α←␈↓␈↓ ∧π␈↓αputprop␈↓ and then returning ␈↓α0␈↓.
␈↓" ␈↓ α←␈↓␈↓ βW␈↓↓5.␈↓ Note also that ␈↓αloc␈↓ is a non-local variable in ␈↓αgval␈↓.
␈↓"β␈↓ α←␈↓␈↓ ∧
␈↓↓6.9 A compiler for Simple ␈↓αeval␈↓↓: The Value Stack␈↓α
␈↓"β␈↓ α←␈↓The␈α⊂major␈α∂failing␈α⊂of␈α⊂the␈α∂previous␈α⊂␈↓αcompexp␈↓␈α⊂(Section 6.5)␈α∂is␈α⊂its␈α⊂inability␈α∂to
␈↓ α←␈↓handle␈α∀variables.␈α∀ A␈α∀related␈α∀failing␈α∀is␈α∀its␈α∀inability␈α∀to␈α∀compile␈α∀code␈α∪for
␈↓ α←␈↓λ-definitions. This section addresses both problems.
␈↓"β␈↓ α←␈↓From page 321, we know what ␈↓αcompexp␈↓ will do with:
␈↓ α←␈↓␈↓6.9␈↓ ¬eA compiler for Simple ␈↓αeval␈↓: The Value Stack 333␈↓α
␈↓"β␈↓ α←␈↓␈↓ ε␈↓αf[g[A];h[B]]␈↓
␈↓" ␈↓ α←␈↓α␈↓ β∂(MOVE 1 (QUOTE A))␈↓ ¬w␈↓; get ␈↓αA␈↓ into ␈↓α1.
␈↓"β␈↓ α←␈↓α␈↓ β∂(CALL G)␈↓ ¬w␈↓; call the function named ␈↓αg
␈↓"β␈↓ α←␈↓α␈↓ β∂(PUSH P 1)␈↓ ¬w␈↓; save the value␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂(MOVE 1 (QUOTE B))␈↓ ¬w␈↓; get ␈↓αB␈↓ into ␈↓α1
␈↓"β␈↓ α←␈↓α␈↓ β∂(PUSHJ P H)␈↓ ¬w␈↓; call ␈↓αh
␈↓"β␈↓ α←␈↓α␈↓ β∂(MOVE 2 1)␈↓ ¬w␈↓; restore the arguments in␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂(POP P 1)␈↓ ¬w␈↓; preparation for␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂(CALL F)␈↓ ¬w␈↓; calling ␈↓αf
␈↓"∀␈↓ α←␈↓No suprises yet. What would we expect to see for a compiled version of:
␈↓" ␈↓ α←␈↓␈↓ εε␈↓αf[g[x];h[y]]␈↓ ?
␈↓" ␈↓ α←␈↓We␈α␈↓¬should␈↓␈αexpect␈αto␈αsee␈αthe␈αsame␈αcode␈αexcept␈αwe␈αwould␈αhave␈αinstructions␈αto
␈↓ α←␈↓send␈αthe␈αvalues␈α
of␈α␈↓αx␈↓␈αand␈α
␈↓αy␈↓␈αinto␈αaccumulator␈α
␈↓α1␈↓␈αat␈αthe␈α
appropriate␈αtime.␈αSo␈α
the
␈↓ α←␈↓first␈α∞problem␈α∞is␈α∞how␈α∞to␈α∞find␈α∂the␈α∞values␈α∞of␈α∞variables.␈α∞Assume␈α∞we␈α∂are␈α∞really
␈↓ α←␈↓interested in compiling:
␈↓" ␈↓ α←␈↓␈↓ ¬?␈↓αj <= λ[[x;y] f[g[x];h[y]]]␈↓
␈↓" ␈↓ α←␈↓This␈αadded␈α
problem␈αmakes␈α
our␈αquestion␈αeasier.␈α
Consider␈αa␈α
call␈αon␈α
␈↓αj␈↓:␈α␈↓αj[A;B]␈↓,
␈↓ α←␈↓for␈α
example.␈α
We␈α
know␈α
that␈α
the␈αexecution␈α
of␈α
the␈α
call␈α
occurs␈α
after␈α
the␈αvalues␈α
␈↓αA␈↓
␈↓ α←␈↓and␈α
␈↓αB␈↓␈αhave␈α
been␈αset␈α
up␈αin␈α
␈↓αAC1␈↓␈α
and␈α␈↓αAC2␈↓.␈α
Thus␈αat␈α
␈↓¬that␈↓␈αtime␈α
we␈α
do␈αindeed
␈↓ α←␈↓know␈αwhat␈αthe␈αvalues␈αof␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓␈αare␈αsupposed␈αto␈αbe.␈αFor␈αsake␈αof␈αsimplicity,
␈↓ α←␈↓assume␈α
that␈α
the␈α
variables␈α␈↓αx␈↓␈α
and␈α
␈↓αy␈↓␈α
are␈α
strictly␈αlocal.␈α
That␈α
is,␈α
no␈α
one␈αwithin␈α
the
␈↓ α←␈↓bodies␈α
of␈α
either␈α␈↓αg␈↓␈α
or␈α
␈↓αh␈↓␈αuses␈α
␈↓αx␈↓␈α
or␈α␈↓αy␈↓␈α
free;␈α
we␈αwill␈α
worry␈α
about␈α
compilation␈αof
␈↓ α←␈↓free␈αreferences␈αlater.␈α
Since␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓␈α
are␈αlocal,␈αonly␈α␈↓αj␈↓␈α
needs␈αto␈αfind␈αtheir␈α
values.
␈↓ α←␈↓We␈α
cannot␈α∞leave␈α
the␈α∞values␈α
in␈α∞the␈α
␈↓αAC␈↓s␈α∞since␈α
those␈α∞registers␈α
are␈α∞needed␈α
for
␈↓ α←␈↓other computations. Rather, we will save ␈↓αx␈↓ and ␈↓αy␈↓ in the top of the stack ␈↓αP␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Since␈α∩␈↓αP␈↓␈α∩contains␈α∩the␈α∪values␈α∩of␈α∩partial␈α∩computations,␈α∩and␈α∪now␈α∩also
␈↓ α←␈↓contains␈αthe␈α
values␈αof␈α
the␈αlocal␈α
λ-variables,␈α␈↓αP␈↓␈α
is␈αalso␈α
called␈αthe␈α
␈↓↓value␈αstack␈↓.
␈↓ α←␈↓This␈αis␈αa␈αvalue␈αstack␈αsimilar␈αto␈αthat␈αdescribed␈αin␈αdeep-binding␈α(Section 5.18);
␈↓ α←␈↓however␈αwe␈αdo␈αnot␈αneed␈αthe␈αname␈αstack␈αhere.␈αThe␈αcompiler␈αwill␈α␈↓¬know␈↓␈αwhere
␈↓ α←␈↓on␈αthe␈αstack␈αvalues␈αof␈αlocal␈αvariables␈αcan␈αbe␈αfound;␈αit␈αwill␈αput␈αthem␈αthere␈αso
␈↓ α←␈↓it␈α
␈↓¬should␈↓␈α
know.␈α
This␈αlack␈α
of␈α
a␈α
name␈α
stack␈αis␈α
a␈α
mixed␈α
blessing;␈α
we␈αsave␈α
space,
␈↓ α←␈↓but␈α∞we␈α∞have␈α∞lost␈α∞the␈α∞names;␈α∂the␈α∞names␈α∞are␈α∞useful␈α∞when␈α∞we␈α∂are␈α∞debugging
␈↓ α←␈↓code,␈α
and␈α
necessary␈αfor␈α
a␈α
full␈αLISP␈α
implementation.␈α
Note␈αthat␈α
␈↓αP␈↓␈α
is␈αnot␈α
solely
␈↓ α←␈↓a␈α∂value␈α∂stack;␈α∞it␈α∂also␈α∂contains␈α∞the␈α∂control␈α∂information.␈α∞ We␈α∂are␈α∂not␈α∞always
␈↓ α←␈↓able␈α∂to␈α∞mix␈α∂access␈α∞and␈α∂control␈α∞information␈α∂on␈α∞one␈α∂stack;␈α∞in␈α∂fact,␈α∂we␈α∞know
␈↓ α←␈↓that␈α⊂a␈α⊂stack␈α⊂is␈α⊂not␈α⊂always␈α∂a␈α⊂sufficent␈α⊂vehicle␈α⊂for␈α⊂describing␈α⊂LISP's␈α∂access
␈↓ α←␈↓requirements.␈α However,␈αa␈α
very␈αlarge␈αsubset␈αof␈α
LISP␈α␈↓¬does␈↓␈αallow␈αa␈α
single-stack
␈↓ α←␈↓implementation,␈αand␈αwe␈αwill␈αbe␈αcompiling␈αwithin␈αthat␈αsubset␈αfor␈αmost␈αof␈αthis
␈↓ α←␈↓chapter.
␈↓"β␈↓ α←␈↓␈↓ β'Addressing␈α
the␈αtask␈α
at␈αhand,␈α
the␈αinstructions␈α
for␈αthe␈α
body␈αof␈α
␈↓αj␈↓␈α
will␈αbe
␈↓ α←␈↓very␈α
similar␈α
to␈α
those␈αdisplayed␈α
for␈α
␈↓αf[g[A];h[B]]␈↓.␈α
We␈α
will␈αgenerate␈α
instructions
␈↓ α←␈↓to␈α∞save␈α∞the␈α∞values␈α∞on␈α∞the␈α∞actual␈α∞parameters␈α∞by␈α∞prefixing␈α∞the␈α
␈↓αcompexp␈↓-code
␈↓ α←␈↓with two ␈↓αPUSH␈↓ operations:
␈↓ α←␈↓␈↓334 Dynamic Structure␈↓
(6.9␈↓
␈↓"β␈↓ α←␈↓α␈↓ ¬/(PUSH P 1)
␈↓"β␈↓ α←␈↓α␈↓ ¬/(PUSH P 2)
␈↓"∀␈↓ α←␈↓After␈αexecution␈αof␈αthis␈αpair␈αof␈αinstructions,␈αcalled␈αthe␈α␈↓↓prolog␈↓,␈αthe␈αvalue␈α
of␈α␈↓αy␈↓
␈↓ α←␈↓is on the top of the stack, and the value of ␈↓αx␈↓ is the next element down.␈↓π 21␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Now␈αthat␈αwe␈αhave␈αsaved␈αthe␈αvalues,␈αwe␈αneed␈αinstructions␈αto␈α␈↓αsend␈↓␈αthem
␈↓ α←␈↓to␈α␈↓αAC1␈↓␈αwhen␈α
the␈αvalue␈αis␈α
needed.␈αWe␈αwill␈α
implement␈α␈↓αsend@␈↓␈αusing␈αthe␈α
␈↓αMOVE␈↓
␈↓ α←␈↓instruction␈α(Section 6.3).␈αIn␈αthis␈αcase␈αour␈αmemory␈αreference␈αwill␈αbe␈αrelative␈αto
␈↓ α←␈↓the␈α
top␈α
of␈α
the␈α
␈↓αP␈↓␈α
stack.␈α
Relative␈α
addressing␈α
is␈α
described␈α
in␈α
our␈α
machine␈α
as␈α
an
␈↓ α←␈↓address␈α∂field␈α∂of␈α∂the␈α∂form␈α∂"␈↓αn P␈↓",␈α∂where␈α∂␈↓αn␈↓␈α∂designates␈α∂the␈α∂offset␈α∂into␈α∂␈↓αP␈↓␈α∞and
␈↓ α←␈↓references␈α∀the␈α∪␈↓αn␈↓πth␈↓␈α∀element,␈α∪counting␈α∀backwards␈α∪from␈α∀zero.␈α∪Thus␈α∀in␈α∪our
␈↓ α←␈↓current␈αexample␈α"␈↓α0 P␈↓"␈αrefers␈αto␈αthe␈αvalue␈α
for␈α␈↓αy␈↓␈αand␈α"␈↓α-1 P␈↓"␈αrefers␈αto␈α␈↓αx␈↓␈α
at␈αthe
␈↓ α←␈↓time␈α⊃␈↓αj␈↓␈α⊃is␈α⊂entered.␈α⊃ Be␈α⊃sure␈α⊂to␈α⊃realize␈α⊃also␈α⊂that␈α⊃our␈α⊃addressing␈α⊃is␈α⊂relative;
␈↓ α←␈↓though␈α␈↓α"0 P"␈↓␈αrefers␈αto␈α␈↓αy␈↓␈αat␈αentry␈αtime,␈α␈↓α0 P␈↓␈αwill␈α␈↓¬not␈↓␈αrefer␈αto␈α␈↓αy␈↓␈αwhen␈αwe␈αhave
␈↓ α←␈↓pushed␈α
something␈α
onto␈α
the␈α
stack.␈α
Be␈α
sure␈α
to␈α
realize␈α
that␈α
we␈α∞␈↓¬cannot␈↓␈α
change
␈↓ α←␈↓our␈α∩relative␈α⊃addressing␈α∩to␈α∩hard␈α⊃machine␈α∩locations␈α⊃in␈α∩the␈α∩assembler.␈α⊃The
␈↓ α←␈↓addressing␈αmust␈α
always␈αbe␈α
relative.␈αWe␈α
will␈αbe␈α
compiling␈αcode␈α
for␈αrecursive
␈↓ α←␈↓functions.␈αEach␈αrecursive␈αcall␈αmust␈αget␈αa␈αfresh␈αsegment␈αof␈αthe␈αvalue␈αstack␈αin
␈↓ α←␈↓which␈α
to␈αstore␈α
its␈αresults.␈α
A␈α
similar␈αproblem␈α
appeared␈αwhen␈α
we␈αexamined␈α
the
␈↓ α←␈↓␈↓αCALL-RET␈↓␈α⊂mechanism␈α⊃on␈α⊂page 314.␈α⊃There␈α⊂we␈α⊃were␈α⊂dealing␈α⊃with␈α⊂control
␈↓ α←␈↓information stored on a stack.
␈↓"β␈↓ α←␈↓␈↓ β'Finally,␈αwe␈αcannot␈αleave␈αthe␈αcode␈αfor␈α
␈↓αj␈↓␈αas␈αit␈αstands.␈αIf␈αthe␈αprolog␈α
pushes
␈↓ α←␈↓two␈α
entries␈α
onto␈αthe␈α
stack␈α
then␈αwe␈α
had␈α
better␈αconstruct␈α
an␈α
epilog␈α
to␈αremove
␈↓ α←␈↓them;␈α⊂otherwise␈α⊂the␈α⊂stack␈α⊂will␈α⊂not␈α⊂be␈α⊂in␈α⊂the␈α⊂state␈α⊂expected␈α⊂by␈α⊂the␈α⊂calling
␈↓ α←␈↓program.␈αAs␈αwe␈αleave␈α␈↓αj␈↓␈αwe␈αsubtract␈α␈↓α2␈↓␈αfrom␈αthe␈αpointer␈α␈↓αP␈↓␈αto␈αsynchronize␈αthe
␈↓ α←␈↓stack. The constant ␈↓α2␈↓ is designated as ␈↓α(C 2)␈↓. Finally we exit via ␈↓αRET␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'One␈αfurther␈αembellishment␈αis␈α
needed:␈αsince␈αwe␈αare␈αdefining␈α
a␈αfunction
␈↓ α←␈↓and␈α∞turning␈α
it␈α∞into␈α
compiled␈α∞code,␈α
we␈α∞must␈α
preface␈α∞the␈α
code␈α∞sequence␈α
with
␈↓ α←␈↓information␈αto␈αour␈αassembler␈αto␈αdesignate␈α␈↓αj␈↓␈αas␈αa␈αmachine-coded␈αcall-by-value
␈↓ α←␈↓function.␈α∂The␈α∞assembler␈α∂will␈α∞make␈α∂a␈α∞new␈α∂property-value␈α∞pair␈α∂consisting␈α∞of
␈↓ α←␈↓the␈αproperty␈αname␈α␈↓αSUBR␈↓␈αand␈αan␈αassociated␈αvalue␈αpart␈αwhich␈αis␈αthe␈αvalue␈α
of
␈↓ α←␈↓the␈αassembly␈αcounter␈αwhen␈αthe␈αassembly␈αwas␈αbegun.␈α That␈αpair␈αis␈αplaced␈αon
␈↓ α←␈↓the p-list of the atom representing the function name.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 21␈↓The␈αobservant␈αreader␈αwill␈αnote␈αthat␈αthe␈α␈↓αPUSH␈↓␈αfor␈α␈↓αx␈↓␈αis␈αunnecessary.
␈↓ α←␈↓Since␈αwe␈α
have␈αassumed␈α
that␈α␈↓αx␈↓␈αand␈α
␈↓αy␈↓␈αare␈α
strictly␈αlocal,␈αand␈α
since␈αno␈α
one␈αelse
␈↓ α←␈↓needs␈α∂the␈α∂value␈α∞of␈α∂␈↓αx␈↓␈α∂except␈α∞for␈α∂␈↓αg[x]␈↓,␈α∂we␈α∞can␈α∂simply␈α∂compute␈α∂␈↓αg[x]␈↓␈α∞directly.
␈↓ α←␈↓One␈αmight␈αalso␈αthink␈α
that␈αwe␈αcould␈αleave␈α␈↓αB␈↓␈α
in␈α␈↓αAC2␈↓␈αwhile␈αwe␈αcalculated␈α
␈↓αg[x]␈↓;
␈↓ α←␈↓we cannot do that, as ␈↓αg[x]␈↓ might use ␈↓αAC2␈↓. We must ␈↓αPUSH␈↓ ␈↓αy␈↓.
␈↓ α←␈↓␈↓6.9␈↓ ¬eA compiler for Simple ␈↓αeval␈↓: The Value Stack 335␈↓α
␈↓"∀␈↓ α←␈↓α␈↓ β∂(LAP J SUBR)␈↓ ¬;␈↓; says ␈↓αj␈↓ is a function␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂(PUSH P 1)␈↓ ¬;␈↓; save the input args␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂(PUSH P 2)
␈↓"β␈↓ α←␈↓α␈↓ β∂(MOVE 1 -1 P)␈↓ ¬;␈↓; get ␈↓αx
␈↓"β␈↓ α←␈↓α␈↓ β∂(CALL G)␈↓ ¬;␈↓; call the function named ␈↓αg
␈↓"β␈↓ α←␈↓α␈↓ β∂(PUSH P 1)␈↓ ¬;␈↓; save the value␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂(MOVE 1 -1 P)␈↓ ¬;␈↓; get ␈↓αy
␈↓"β␈↓ α←␈↓α␈↓ β∂(PUSHJ P H)␈↓ ¬;␈↓; call ␈↓αh
␈↓"β␈↓ α←␈↓α␈↓ β∂(MOVE 2 1)␈↓ ¬;␈↓; set up the arguments in␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂(POP P 1)␈↓ ¬;␈↓; preparation for␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂(CALL F)␈↓ ¬;␈↓; calling ␈↓αf
␈↓"β␈↓ α←␈↓α␈↓ β∂(SUB P (C 2))␈↓ ¬;␈↓; synchronize the stack by removing
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ ¬;; the two saved args␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂(RET)␈↓ ¬;␈↓; exit with ␈↓αAC1␈↓ containing the value of ␈↓αj[x;y]
␈↓"∀␈↓ α←␈↓As␈α
you␈α
read␈α∞the␈α
code␈α
and␈α∞as␈α
you␈α
study␈α∞its␈α
execution␈α
you␈α∞should␈α
remember
␈↓ α←␈↓that␈α↔the␈α↔addressing␈α↔in␈α↔the␈α↔code␈α_is␈α↔relative␈α↔to␈α↔the␈α↔top␈α↔of␈α_the␈α↔stack:
␈↓ α←␈↓␈↓α(MOVE 1 -1 P)␈↓␈αgets␈α
␈↓αx␈↓␈αin␈αone␈α
instance␈αand␈α
finds␈α␈↓αy␈↓␈αin␈α
another,␈αbecause␈αthe␈α
top
␈↓ α←␈↓of the stack changes. Here is a picture of the execution of the code:
␈↓"∀␈↓ α←␈↓αAC1: x ; AC2: y␈↓ ¬←␈↓ ε≠␈↓ λAC1: x ; AC2: y
␈↓"β␈↓ α←␈↓α␈↓ β∂|␈↓ βK| (PUSH P 1)␈↓ ¬←| x␈↓ ε≠| (PUSH P 2)␈↓ λ| y␈↓ λG| (MOVE 1 -1 P)
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βK =>␈↓ ¬←␈↓ ε≠ =>␈↓ λ| x␈↓ λG| =>
␈↓"∀␈↓ α←␈↓αAC1: x ; AC2: y␈↓ ¬←AC1: g[x] ; AC2: ?
␈↓"β␈↓ α←␈↓α␈↓ β∂| y␈↓ βK| (CALL G)␈↓ ¬←| y␈↓ ε≠| (PUSH P 1)␈↓ λ|g[x]␈↓ λG| (MOVE 1 -1 P)
␈↓"β␈↓ α←␈↓α␈↓ β∂| x␈↓ βK| =>␈↓ ¬←| x␈↓ ε≠| =>␈↓ λ| y␈↓ λG| =>
␈↓"β␈↓ α←␈↓α␈↓ β∂␈↓ βK␈↓ ¬←| x␈↓ ε≠|
␈↓"∀␈↓ α←␈↓αAC1: y ; AC2: ?␈↓ ¬←AC1: h[y] ; AC2: ?␈↓ λAC1: h[y] ; AC2: h[y]
␈↓"β␈↓ α←␈↓α␈↓ β∂|g[x]␈↓ βK| (PUSHJ P H)␈↓ ¬←|g[x]␈↓ ε≠| (MOVE 2 1)␈↓ λ|g[x]␈↓ λG| (POP P AC1)
␈↓"β␈↓ α←␈↓α␈↓ β∂| y␈↓ βK| =>␈↓ ¬←| y␈↓ ε≠| =>␈↓ λ| y␈↓ λG| =>
␈↓"β␈↓ α←␈↓α␈↓ β∂| x␈↓ βK|␈↓ ¬←| x␈↓ ε≠|␈↓ λ| x␈↓ λG|
␈↓"∀␈↓ α←␈↓αAC1: g[x] ; AC2: h[y]␈↓ ¬←AC1: f[g[x];h[y]]
␈↓"β␈↓ α←␈↓α␈↓ β∂| y␈↓ βK| (CALL 2 F)␈↓ ¬←| y␈↓ ε≠| (SUB P (C 2)) => (RET) ␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂| x␈↓ βK| =>␈↓ ¬←| x␈↓ ε≠|
␈↓"∀␈↓ α←␈↓␈↓ ∧{␈↓↓6.10 A Compiler for Simple ␈↓αeval␈↓↓␈↓α
␈↓"β␈↓ α←␈↓Now␈αthat␈αwe␈αknow␈αwhat␈αthe␈αruntime␈αcode␈αfor␈αlocal␈αvariable␈αreferences␈α␈↓¬could␈↓
␈↓ α←␈↓be,␈α
we␈α
must␈α
describe␈α
an␈α
algorithm␈α
which␈α
will␈α
generate␈α
similar␈α
code.␈α
We␈α
shall
␈↓ α←␈↓␈↓336 Dynamic Structure␈↓
_6.10␈↓
␈↓"β␈↓ α←␈↓simulate␈αthe␈αbehavior␈αof␈αthe␈αruntime␈αstack␈αwhile␈αwe␈αare␈αcompiling␈αthe␈αcode.
␈↓ α←␈↓The␈α∞compiler␈α∞cannot␈α∞know␈α
what␈α∞the␈α∞␈↓¬values␈↓␈α∞of␈α
the␈α∞variables␈α∞will␈α∞be␈α∞at␈α
run
␈↓ α←␈↓time␈α⊗but␈α↔it␈α⊗can␈α⊗know␈α↔␈↓¬where␈↓␈α⊗to␈α↔find␈α⊗the␈α⊗values.␈α↔ We␈α⊗will␈α↔carry␈α⊗this
␈↓ α←␈↓information␈αthrough␈α
the␈αcompiler␈α
in␈αa␈αmanner␈α
reminiscent␈αof␈α
the␈α␈↓αassoc␈↓-style
␈↓ α←␈↓symbol␈αtable␈αof␈αthe␈α␈↓αeval␈↓␈αin␈αSection 3.5.␈α Instead␈αof␈αposting␈αthe␈αcurrent␈αvalues
␈↓ α←␈↓in␈α∂the␈α∂stack,␈α∂the␈α∂compiler␈α∂will␈α∂post␈α∂information␈α∂about␈α∂the␈α∂positions␈α∂of␈α∂the
␈↓ α←␈↓variables␈α∞relative␈α
to␈α∞the␈α∞top␈α
of␈α∞the␈α∞stack␈α
at␈α∞the␈α∞time␈α
we␈α∞enter␈α∞the␈α
function.
␈↓ α←␈↓The␈α∃variable-position␈α∀list,␈α∃␈↓αvpl␈↓,␈α∃contains␈α∀this␈α∃information.␈α∀ If␈α∃we␈α∃are␈α∀to
␈↓ α←␈↓compile a function with λ-variables, ␈↓α[x;y;z]␈↓ then ␈↓αvpl␈↓ will begin with:
␈↓" ␈↓ α←␈↓␈↓ ¬5␈↓α((X . 1), (Y . 2), (Z . 3) ...␈↓
␈↓" ␈↓ α←␈↓When␈α
we␈α
set␈α
up␈α
␈↓αvpl␈↓␈αwe␈α
also␈α
set␈α
the␈α
␈↓↓offset␈↓,␈αcalled␈α
␈↓αoff␈↓,␈α
to␈α
minus␈α
the␈αnumber␈α
of
␈↓ α←␈↓arguments␈αadded␈αto␈α␈↓αvpl␈↓,␈αin␈αthis␈α
case␈α-3.␈α Now␈αif␈αwe␈αcome␈αacross␈α
a␈αreference,
␈↓ α←␈↓say␈α∞to␈α∞␈↓αY␈↓,␈α∞while␈α∞compiling␈α∞code,␈α∞we␈α∞use␈α∞␈↓αcdr[assoc[Y;vpl]]␈↓␈α∞to␈α∞retrieve␈α∞␈↓α2␈↓.␈α
The
␈↓ α←␈↓offset␈α⊂plus␈α⊂this␈α⊂retrieved␈α⊃value␈α⊂gives␈α⊂us␈α⊂the␈α⊃relative␈α⊂position␈α⊂of␈α⊂␈↓αY␈↓␈α⊃in␈α⊂the
␈↓ α←␈↓stack: -3 + 2 = -1. Thus to refer to the location of ␈↓αY␈↓ we use ␈↓α(... -1 P)␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'What␈αhappens␈αas␈αwe␈αadd␈αelements␈α
to␈αthe␈αstack?␈α Or␈αto␈αbe␈αmore␈α
precise,
␈↓ α←␈↓what␈αhappens␈αas␈α
we␈αgenerate␈αcode␈α
which␈αwhen␈αexecuted␈α
will␈αadd␈αelements␈α
to
␈↓ α←␈↓the␈α∂stack?␈α∂ Clearly␈α∂we␈α∂must␈α∂modify␈α∂the␈α∂offset.␈α∂ If␈α∂we␈α∂add␈α∂one␈α∂element,␈α∞we
␈↓ α←␈↓would␈α⊂set␈α⊂␈↓αoff␈↓␈α∂to␈α⊂-4.␈α⊂ Then␈α⊂to␈α∂reference␈α⊂␈↓αY␈↓␈α⊂now␈α∂use␈α⊂-4␈α⊂+␈α⊂2␈α∂=␈α⊂-2;␈α⊂that␈α⊂is,␈α∂a
␈↓ α←␈↓reference to ␈↓αY␈↓ is now of the form:
␈↓" ␈↓ α←␈↓␈↓ ε
␈↓α( . . . -2 P) ␈↓
␈↓" ␈↓ α←␈↓But␈α∂that's␈α∞right.␈α∂ ␈↓αY␈↓␈α∞is␈α∂now␈α∂further␈α∞down␈α∂in␈α∞the␈α∂run␈α∞time␈α∂stack.␈α∂ Thus␈α∞the
␈↓ α←␈↓`symbol␈αtable'␈αis␈αreally␈αdefined␈αby␈α␈↓αoff␈↓␈αplus␈αthe␈αcurrent␈α␈↓αvpl␈↓.␈αHere's␈αa␈αsketch␈αof
␈↓ α←␈↓the proposed ␈↓αcompexp␈↓ in its performance of local variable recognition.
␈↓"∀␈↓ α←␈↓α␈↓ ∧.islocalvar[exp] → list[mkvar[1;loc[exp;off;vpl]]]
␈↓" ␈↓ α←␈↓α␈↓where:␈↓α␈↓ ∧,loc <= λ[[x;off;vpl] plus[off;cdr[assoc[x;vpl]]]]
␈↓" ␈↓ α←␈↓α␈↓and,␈↓α␈↓ ∧:mkvar <= λ[[ac;mem] list[MOVE;ac;mem;P]]
␈↓"∀␈↓ α←␈↓Next,␈αwhen␈αwill␈αthe␈αcompiler␈αmake␈αmodifications␈αto␈αthe␈αtop␈αof␈αthe␈αstack?␈αWe
␈↓ α←␈↓push␈αnew␈α
elements␈αwhen␈α
we␈αare␈αcompiling␈α
the␈αarguments␈α
to␈αa␈α
function␈αcall.
␈↓ α←␈↓We␈α⊃know␈α∩that␈α⊃␈↓αcomplis␈↓␈α∩is␈α⊃the␈α∩function␈α⊃which␈α∩compiles␈α⊃the␈α∩argument␈α⊃list.
␈↓ α←␈↓Thus␈α∩our␈α∩new␈α⊃␈↓αcomplis␈↓␈α∩must␈α∩know␈α⊃about␈α∩␈↓αoff␈↓␈α∩and␈α⊃␈↓αvpl␈↓,␈α∩and␈α∩since␈α⊃␈↓αcomplis␈↓
␈↓ α←␈↓changes the state of the stack, then it must change ␈↓αoff␈↓ appropriately:
␈↓"∀␈↓ α←␈↓αcomplis <= λ[[u;off;vpl]␈↓ ¬↔[null [u] → ( );
␈↓"β␈↓ α←␈↓α␈↓ ¬↔ null[rest[u]] → compexp[first[u];off; vpl];
␈↓"β␈↓ α←␈↓α␈↓ ¬↔ ␈↓
t␈↓α → append␈↓ ε3[compexp [first[u]; off; vpl];
␈↓"β␈↓ α←␈↓α␈↓ ¬↔␈↓ ε3 list[mkalloc[1]];
␈↓"β␈↓ α←␈↓α␈↓ ¬↔␈↓ ε3 complis [rest[u]; sub1[off]; vpl]]]]
␈↓"∀␈↓ α←␈↓Notice␈αthat␈α␈↓αcomplis␈↓␈αcompiles␈αthe␈αarguments␈αfrom␈αleft␈αto␈αright,␈αfollowing␈αeach
␈↓ α←␈↓with␈α
␈↓α(PUSH P 1)␈↓␈α
and␈αrecurring␈α
with␈α
a␈α
new␈αoffset␈α
which␈α
reflects␈α
the␈αeffect␈α
of
␈↓ α←␈↓the ␈↓αPUSH␈↓. This function is analogous to ␈↓αevlis␈↓.
␈↓ α←␈↓␈↓6.10␈↓ π5A Compiler for Simple ␈↓αeval␈↓ 337␈↓α
␈↓"β␈↓ α←␈↓␈↓ β'Here's a brief description of the rest of the new compiler.␈↓π 22␈↓
␈↓"∀␈↓ α←␈↓␈↓αcompile[fn;vars;exp]:␈α∞fn␈↓␈α∞is␈α
the␈α∞name␈α∞of␈α
the␈α∞function␈α∞to␈α
be␈α∞compiled.␈α∞␈↓αvars␈↓␈α
is
␈↓ α←␈↓␈↓ β'the list of lambda variables. ␈↓αexp␈↓ is the lambda-body.
␈↓"∀␈↓ α←␈↓αcompile <= λ[␈↓ ∧∪[fn;vars;exp]
␈↓"β␈↓ α←␈↓α␈↓ ∧∪λ[[n] append[␈↓ ¬Smkprolog[fn;n];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬Scompexp[exp; -n; prup[vars;1]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬Smkepilog[n]]]
␈↓"β␈↓ α←␈↓α␈↓ ∧∪ [length[vars]] ]
␈↓"∀␈↓ α←␈↓αmkprolog <= λ[[f;n] concat[list[LAP;f;SUBR];mkpushs[n;1]]]
␈↓"∞␈↓ α←␈↓αmkpushs <= λ[[n;m][␈↓ ∧slessp[n;m] → ( );
␈↓"β␈↓ α←␈↓α␈↓ ∧s␈↓
t␈↓α → concat[mkalloc[m]; mkpushs[n;add1[m]]]]]
␈↓"∀␈↓ α←␈↓αmkepilog <= λ[[n] list[mksync[n];mkret[]]]
␈↓"∞␈↓ α←␈↓αmksync <=λ[[n] list[SUB;P;list[C;n]]]
␈↓"∞␈↓ α←␈↓αmkret <=λ[[] (RET)]
␈↓"∀␈↓ α←␈↓␈↓αprup[vars;n]:␈α↔vars␈↓␈α↔is␈α↔a␈α↔lambda␈α_list,␈α↔␈↓αn␈↓␈α↔is␈α↔an␈α↔integer.␈α↔ ␈↓αprup␈↓␈α_builds␈α↔a
␈↓ α←␈↓␈↓ β'variable-position list.
␈↓"∀␈↓ α←␈↓αprup <= λ[[vars;n][␈↓ ∧[null[vars] → ( );
␈↓"β␈↓ α←␈↓α␈↓ ∧[␈↓
t␈↓α → concat[␈↓ ¬←cons[first[vars]; n];
␈↓"β␈↓ α←␈↓α␈↓ ∧[␈↓ ¬←prup[rest[vars];add1[n]]]]]
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 22␈↓This␈α_compiler␈α_was␈α_adapted␈α_from␈α_one␈α_written␈α_by␈α↔J. McCarthy
␈↓ α←␈↓([McC 76]),␈α∃and␈α∃proved␈α∃correct␈α∃by␈α∃R. London ([Lon 71])␈α∃and␈α∃M. Newey
␈↓ α←␈↓([New 75]).
␈↓ α←␈↓␈↓338 Dynamic Structure␈↓
_6.10␈↓
␈↓"∀␈↓ α←␈↓␈↓αcompexp[exp;off;vpl]␈↓:␈αThis␈αfunction␈αgenerates␈α
the␈αcode␈αfor␈αconstants␈α
and␈αfor
␈↓ α←␈↓␈↓ β'references␈α∃to␈α∀variables.␈α∃ If␈α∀the␈α∃variable␈α∀is␈α∃local,␈α∀a␈α∃simple␈α∃␈↓αsend␈↓␈α∀is
␈↓ α←␈↓␈↓ β'generated,␈α~otherwise␈α~a␈α~call␈α~on␈α~␈↓αlookup␈↓␈α~results.␈α~ If␈α~a␈α~conditional
␈↓ α←␈↓␈↓ β'expression␈α
is␈αrecognized,␈α
␈↓αcomcond␈↓␈αis␈α
called␈αto␈α
produce␈αthe␈α
code.␈α
If␈α␈↓αexp␈↓
␈↓ α←␈↓␈↓ β'does␈α
not␈α
fit␈αone␈α
of␈α
these␈αcategories,␈α
it␈α
is␈α
assumed␈αto␈α
be␈α
an␈αapplication␈α
of
␈↓ α←␈↓␈↓ β'a␈α⊂call-by-value␈α∂function.␈α⊂ In␈α∂this␈α⊂case,␈α∂␈↓αcomplis␈↓␈α⊂compiles␈α⊂the␈α∂argument
␈↓ α←␈↓␈↓ β'list,␈α⊂leaving␈α∂the␈α⊂arguments␈α⊂in␈α∂the␈α⊂stack;␈α∂␈↓αloadac␈↓␈α⊂loads␈α⊂the␈α∂appropriate
␈↓ α←␈↓␈↓ β'␈↓αAC␈↓'s.␈α and␈αthen␈αwe␈αgenerate␈αa␈αcall␈αon␈αthe␈αfunction,␈αand␈αfinally␈αgenerate
␈↓ α←␈↓␈↓ β'the ␈↓αSUB␈↓ to synchronize the stack.
␈↓"∀␈↓ α←␈↓αcompexp <= λ[␈↓ ∧∨[exp;off;vpl]
␈↓"β␈↓ α←␈↓α␈↓ βW␈↓ ∧∨[isconst[exp] → list[mkconst[1;exp]];
␈↓"β␈↓ α←␈↓α␈↓ βW␈↓ ∧∨ islocalvar[exp] → list [mkvar[1;loc[exp;off;vpl]]];
␈↓"β␈↓ α←␈↓α␈↓ βW␈↓ ∧∨ isnonlocal[exp] → list[mklookup[exp]];
␈↓"β␈↓ α←␈↓α␈↓ βW␈↓ ∧∨ iscond[exp] → comcond[␈↓ εKargs␈↓βc␈↓α[exp];
␈↓"β␈↓ α←␈↓α␈↓ βW␈↓ ∧∨␈↓ εβ␈↓ εKgensym[ ];
␈↓"β␈↓ α←␈↓α␈↓ βW␈↓ ∧∨␈↓ εβ␈↓ εKoff;
␈↓"β␈↓ α←␈↓α␈↓ βW␈↓ ∧∨␈↓ εβ␈↓ εKvpl];
␈↓"β␈↓ α←␈↓α␈↓ βW␈↓ ∧∨ isfun+args[exp] → λ[[z] compapply[␈↓ πsfunc[exp];
␈↓"β␈↓ α←␈↓α␈↓ βW␈↓ ∧∨␈↓ εβ␈↓ εK␈↓ πscomplis[z;off;vpl];
␈↓"β␈↓ α←␈↓α␈↓ βW␈↓ ∧∨␈↓ εβ␈↓ εK␈↓ πslength[z]]
␈↓"β␈↓ α←␈↓α␈↓ βW␈↓ ∧∨␈↓ εβ [arglist[exp]] ]]
␈↓"∀␈↓ α←␈↓␈↓αcompapply␈↓ is found on page 324.
␈↓"∀␈↓ α←␈↓␈↓αcomcond[u;glob;off;vpl]␈↓:␈α
this␈α
compiles␈α
the␈α
body␈α
of␈α
conditional␈α
expressions.␈α
␈↓αu␈↓
␈↓ α←␈↓␈↓ β'is␈α
the␈α
p␈↓βi␈↓ - e␈↓βi␈↓␈αlist;␈α
␈↓αglob␈↓␈α
will␈αbe␈α
bound␈α
to␈α
a␈αgenerated␈α
symbol␈α
name;␈α␈↓αoff␈↓␈α
and
␈↓ α←␈↓␈↓ β'␈↓αvpl␈↓ will always be the offset and the variable-position list.
␈↓"∀␈↓ α←␈↓αcomcond <= λ[[u;glob;off;vpl]␈↓ ¬←[null[u] → list[mkerror[ ];glob];
␈↓"β␈↓ α←␈↓α␈↓ ¬←␈↓ ππ␈↓
t␈↓α → append[␈↓ λ#comclause[␈↓ 'first[u];
␈↓"β␈↓ α←␈↓α␈↓ ¬←␈↓ ππ␈↓ λ#gensym[];
␈↓"β␈↓ α←␈↓α␈↓ ¬←␈↓ ππ␈↓ λ#glob;
␈↓"β␈↓ α←␈↓α␈↓ ¬←␈↓ ππ␈↓ λ#off;
␈↓"β␈↓ α←␈↓α␈↓ ¬←␈↓ ππ␈↓ λ#vpl];
␈↓"β␈↓ α←␈↓α␈↓ ¬←␈↓ ππcomcond[rest[u]; glob;off;vpl] ]]
␈↓"∀␈↓ α←␈↓αcomclause <=λ[[p;loc;glob;off;vpl]append[␈↓ εocompexp[␈↓ πgante[p];
␈↓"β␈↓ α←␈↓α␈↓ εo␈↓ πgoff;
␈↓"β␈↓ α←␈↓α␈↓ εo␈↓ πgvpl];
␈↓"β␈↓ α←␈↓α␈↓ εolist[mkjumpf[loc]];
␈↓"β␈↓ α←␈↓α␈↓ εocompexp[␈↓ πgconseq[p];
␈↓"β␈↓ α←␈↓α␈↓ εo␈↓ πgoff;
␈↓"β␈↓ α←␈↓α␈↓ εo␈↓ πgvpl];
␈↓"β␈↓ α←␈↓α␈↓ εolist[mkjump[glob];loc]]]
␈↓"∀␈↓ α←␈↓␈↓ β'Here is a partial sketch of ␈↓αcompile␈↓ operating on
␈↓"∀␈↓ α←␈↓␈↓ ¬D␈↓αj <= λ[[x;y]f[g[x];h[y]]]␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Compare the code it generates with the code we saw on page 335.
␈↓ α←␈↓␈↓6.10␈↓ π5A Compiler for Simple ␈↓αeval␈↓ 339␈↓α
␈↓"β␈↓ α←␈↓αcompile[J;(X Y);(F (G X) (H Y))]
␈↓" ␈↓ α←␈↓α␈↓gives:␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β{append␈↓ ∧s[((LAP J SUBR));
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (PUSH P 1)
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (PUSH P 2)
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s compexp[(F (G X) (H Y));-2;prup[(X Y);1]];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s ((SUB P (C 2))
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (RET))] ␈↓
␈↓"∀␈↓ α←␈↓where:␈↓ ∧.␈↓αprup[(X Y);1]␈↓ gives ␈↓α((X . 1) (Y . 2))␈↓
␈↓"∀␈↓ α←␈↓␈↓αcompexp[(F (G X) (H Y));-2;((X . 1) (Y . 2))]␈↓
␈↓" ␈↓ α←␈↓results in:␈↓α␈↓ β{append␈↓ ∧s[complis[((G X) (H Y));-2;((X . 1) (Y . 2))];
␈↓"β␈↓ α←␈↓␈↓ β{␈↓ ∧s␈↓α mklink[2];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s ((CALL F))]
␈↓"∞␈↓ α←␈↓α␈↓and ␈↓αmklink[2]␈↓ evaluates to: ␈↓α((MOVE 2 1) (POP P 1))␈↓.
␈↓" ␈↓ α←␈↓Thus the code we're getting looks like:
␈↓"∀␈↓ α←␈↓α␈↓ β{␈↓ ∧s((LAP J SUBR)
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (PUSH P 1)
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (PUSH P 2)
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s complis[((G X) (H Y)); -2; ((X . 1) (Y . 2))]
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (MOVE 2 1)
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (POP P 1)
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (CALL F)
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (SUB P (C 2))
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (RET) )
␈↓"β␈↓ α←␈↓αcomplis␈↓␈α⊂is␈α∂interesting␈α⊂since␈α∂it␈α⊂actually␈α⊂uses␈α∂the␈α⊂␈↓αvpl␈↓␈α∂we␈α⊂have␈α⊂been␈α∂carrying
␈↓ α←␈↓along. It gives rise to:
␈↓"∀␈↓ α←␈↓α␈↓ β{ append␈↓ ∧s[compexp[(G X);-2;((X . 1) (Y . 2))];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s ((PUSH P 1));
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s complis[((H Y));-3;((X . 1) (Y . 2))]]
␈↓" ␈↓ α←␈↓α␈↓and the ␈↓αcompexp␈↓ computation involves, in part:
␈↓"∞␈↓ α←␈↓α␈↓ β{ append[complis[(X);-2;((X . 1) (Y . 2))];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s ((CALL G))]
␈↓"∀␈↓ α←␈↓α␈↓Finally this ␈↓αcomplis␈↓ generates the long awaited variable reference using:
␈↓"∞␈↓ α←␈↓αcompexp[X;-2;((X . 1) (Y . 2))] ␈↓giving, ␈↓α((MOVE 1 -1 P))␈↓
␈↓ α←␈↓␈↓340 Dynamic Structure␈↓
_6.10␈↓
␈↓"∀␈↓ α←␈↓So our code is:
␈↓" ␈↓ α←␈↓α␈↓ β{␈↓ ∧s((LAP J SUBR)
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (PUSH P 1)
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (PUSH P 2)
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (MOVE 1 -1 P)
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (CALL G)
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (PUSH P 1)
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s complis[((H Y)); -3; ((X . 1) (Y . 2))]
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (MOVE 2 1)
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (POP P 1)
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (CALL F)
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (SUB P (C 2))
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧s (RET) )␈↓
␈↓"∀␈↓ α←␈↓Notice that the offset is different within the call:
␈↓" ␈↓ α←␈↓␈↓ ∧m␈↓α complis[((H Y));-3;((X . 1) (Y . 2))]␈↓
␈↓" ␈↓ α←␈↓But that is as it should be: now there is an extra value on the stack.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓␈↓↓1.␈↓ Complete the code generation for the above example.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ Extend the compiling algorithm to recognize anonymous λ-expressions.
␈↓"β␈↓ α←␈↓␈↓ ¬∨␈↓↓6.11 Efficient Compilation␈↓
␈↓"β␈↓ α←␈↓¬␈↓ β'"At␈α⊃the␈α⊃risk␈α⊂of␈α⊃slight␈α⊃exaggeration,␈α⊂we␈α⊃must␈α⊃chastize,␈α⊃in␈α⊂today's
␈↓ α←␈↓¬␈↓ β'programming␈α
environment,␈α
the␈α
process␈α
of␈α
compilation␈α
and␈α
the␈αvery
␈↓ α←␈↓¬␈↓ β'word␈α∩itself,␈α∩which␈α∩is␈α∩the␈α∩grand␈α∩euphemism␈α∩of␈α∪computer␈α∩science.
␈↓ α←␈↓¬␈↓ β'Compilation␈α⊃is␈α⊃like␈α⊂an␈α⊃alchemistic␈α⊃art␈α⊂of␈α⊃transforming␈α⊃gold␈α⊂into
␈↓ α←␈↓¬␈↓ β'lead,␈α∞to␈α∞obtain␈α∞pencils␈α∞and␈α∞hence␈α∞to␈α∞communicate␈α∞information.␈α
The
␈↓ α←␈↓¬␈↓ β'task␈α⊂of␈α∂a␈α⊂compiler␈α⊂is␈α∂to␈α⊂take␈α⊂programs␈α∂of␈α⊂a␈α⊂high-level,␈α∂relatively
␈↓ α←␈↓¬␈↓ β'problem-oriented␈α⊃language,␈α⊂and␈α⊃mutilate␈α⊂them␈α⊃beyond␈α⊂recognition,
␈↓ α←␈↓¬␈↓ β'distorting␈α↔them␈α↔finally␈α↔into␈α↔sequences␈α↔of␈α↔unrecognizable␈α↔codes
␈↓ α←␈↓¬␈↓ β'comprehensible␈α∩only␈α⊃to␈α∩elements␈α∩of␈α⊃electronic␈α∩circuitry␈α∩capable␈α⊃of
␈↓ α←␈↓¬␈↓ β'actual␈α
execution.␈α
Incredibly,␈α
the␈α
high-level␈α
intent␈α
is␈α
simulated␈α
in␈α
this
␈↓ α←␈↓¬␈↓ β'world␈α∃of␈α∃wires,␈α∃and␈α⊗the␈α∃program␈α∃is␈α∃executed␈α∃as␈α⊗desired.␈α∃The
␈↓ α←␈↓¬␈↓ β'compilation␈α
process␈α
is␈α
magic.␈α
A␈α
translation␈α
has␈α
been␈α∞made␈α
between
␈↓ α←␈↓¬␈↓ β'two␈αrepresentations␈αwith␈αno␈αapparent␈αsimilarity␈αof␈αform␈α
or␈αcontent,
␈↓ α←␈↓¬␈↓ β'yet␈α∂those␈α∞representations␈α∂are␈α∂in␈α∞a␈α∂global␈α∞sense␈α∂equivalent␈α∂in␈α∞their
␈↓ α←␈↓¬␈↓ β'execution."
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ ∧ Mark Elson, ␈↓αConcepts of Programming Languages␈↓ [Els 73]
␈↓ α←␈↓␈↓6.11␈↓ πrEfficient Compilation 341␈↓
␈↓"β␈↓ α←␈↓We␈αhave␈αdiscussed␈αcompilation␈αat␈αtwo␈αdifferent␈αlevels:␈αwe␈αcan␈αtranslate␈αLISP
␈↓ α←␈↓expressions␈α∞into␈α∞sequences␈α∞of␈α∞the␈α∞LISP␈α∞control␈α∞primitives␈α∞of␈α∞Section 6.2;␈α
or
␈↓ α←␈↓we␈α
can␈αtranslate␈α
into␈αthe␈α
instructions␈α
of␈αthe␈α
␈↓↓SM␈↓␈αmachine␈α
of␈α
Section 6.3.␈αWe
␈↓ α←␈↓conceptualized␈αthe␈αcompilers␈αin␈αterms␈αof␈αhigher␈αlevel␈αabstractions,␈αbut␈αbiased
␈↓ α←␈↓many␈αof␈αour␈αchoices␈αtowards␈αimplementation␈αon␈αthe␈α␈↓↓SM␈↓␈αinstruction␈αset.␈αOur
␈↓ α←␈↓choices influenced the efficiency of the resulting compiler.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α
should␈α
first␈αclarify␈α
what␈α
we␈αmean␈α
by␈α
efficiency␈αin␈α
this␈α
context.␈α If
␈↓ α←␈↓the␈αcompiler␈αproduces␈αcode␈αfor␈αthe␈αLISP␈αprimitives␈αand␈αthen␈αwe␈αencode␈αthe
␈↓ α←␈↓LISP␈α⊂primitives␈α⊂in␈α∂terms␈α⊂of␈α⊂the␈α∂␈↓↓SM␈↓␈α⊂instruction␈α⊂set,␈α∂then␈α⊂we␈α⊂get␈α⊂a␈α∂simple
␈↓ α←␈↓compiler␈α
which␈α
tends␈α
to␈α
produce␈α∞inefficient␈α
code;␈α
inefficent,␈α
in␈α
terms␈α∞of␈α
the
␈↓ α←␈↓␈↓↓SM␈↓␈αmachine,␈αnot␈αin␈αterms␈αof␈αthe␈αLISP␈αprimitives.␈αSuch␈αa␈αcompiler␈αwould␈αbe
␈↓ α←␈↓efficient␈α
in␈α
terms␈αof␈α
compilation␈α
time␈α
and␈αmight␈α
suffice␈α
for␈α
debugging␈αruns
␈↓ α←␈↓or student projects.
␈↓"β␈↓ α←␈↓␈↓ β'More␈α
likely,␈α
efficient␈α∞compilation␈α
is␈α
taken␈α∞to␈α
mean␈α
production␈α∞of␈α
code
␈↓ α←␈↓which␈α≤we␈α≠could␈α≤expect␈α≠from␈α≤a␈α≠reasonably␈α≤bright␈α≠machine-language
␈↓ α←␈↓programmer.␈α∂ It␈α∞should␈α∂run␈α∞reasonably␈α∂fast,␈α∞not␈α∂have␈α∂obviously␈α∞redundant
␈↓ α←␈↓instructions,␈α∞and␈α∞not␈α∞take␈α∞too␈α∞much␈α∞space␈α∞in␈α∞the␈α∞machine.␈α∞It␈α∞is␈α∞this␈α
second
␈↓ α←␈↓interpretation␈αof␈α
efficiency␈αwhich␈αwe␈α
shall␈αuse.␈αIn␈α
this␈αinterpretation␈αwe␈α
don't
␈↓ α←␈↓simply␈α∞implement␈α∞the␈α∞LISP␈α∞primitives,␈α∞but␈α∞take␈α∞a␈α∞more␈α∞global␈α∞view␈α∞of␈α∞the
␈↓ α←␈↓underlying␈α∞machine.␈α∞We␈α∞take␈α∞advantage␈α∞of␈α∞more␈α∞of␈α∞the␈α∞hardware␈α
features,
␈↓ α←␈↓incorporating␈αthem␈αdeeper␈αinto␈αthe␈α
structure␈αof␈αthe␈αcompiler.␈α This␈αprocess␈α
is
␈↓ α←␈↓called␈α≡optimization.␈α≥ Optimization␈α≡defies␈α≥the␈α≡mismatch␈α≡between␈α≥the
␈↓ α←␈↓programming␈αlanguage␈αand␈αthe␈αhardware␈αmachine.␈α The␈αresult␈αis␈αa␈αcompiler
␈↓ α←␈↓which␈α
is␈αmuch␈α
more␈αmachine␈α
dependent,␈αmay␈α
require␈αmore␈α
processing␈αtime,
␈↓ α←␈↓but produces much better code for that specific machine.
␈↓"β␈↓ α←␈↓␈↓ β'Our␈α≠current␈α≤compilation␈α≠algorithm␈α≤has␈α≠many␈α≤opportunities␈α≠for
␈↓ α←␈↓improvement.␈α A␈αmajor␈αinefficiency␈α
occurs␈αin␈αsaving␈αand␈αrestoring␈α
quantities
␈↓ α←␈↓on␈αthe␈αstack.␈αThis␈αis␈αa␈αsymptom␈αof␈αa␈αmore␈αserious␈αdisease:␈αthe␈αcompiler␈αdoes
␈↓ α←␈↓not␈α∞remember␈α
what␈α∞will␈α∞be␈α
in␈α∞the␈α
␈↓αAC␈↓'s␈α∞at␈α∞run-time.␈α
Since␈α∞we␈α∞are␈α
assuming
␈↓ α←␈↓that␈αthe␈αarguments␈αto␈αa␈αfunction␈αcall␈αare␈αto␈αbe␈αpassed␈αthrough␈αthe␈α␈↓αAC␈↓'s,␈αand
␈↓ α←␈↓since␈α∂it␈α∞is␈α∂expensive␈α∞to␈α∂save␈α∞and␈α∂restore␈α∞these␈α∂registers,␈α∞we␈α∂should␈α∂make␈α∞a
␈↓ α←␈↓concerted␈α⊃effort␈α⊃to␈α⊃remember␈α⊃which␈α⊃quantities␈α⊃are␈α⊃in␈α⊃which␈α⊃␈↓αAC␈↓␈α∩and␈α⊃not
␈↓ α←␈↓reload␈α
them␈α
unnecessarily.␈α
This␈α∞optimization␈α
is␈α
dependent␈α
on␈α∞the␈α
hardware
␈↓ α←␈↓of our machine; if we had only one ␈↓αAC␈↓, the trick would not be applicable.
␈↓"β␈↓ α←␈↓␈↓ ∧W␈↓↓6.12 Efficiency: Primitive Operations␈↓
␈↓"β␈↓ α←␈↓We␈α
should␈α∞be␈α
able␈α∞to␈α
generate␈α∞references␈α
into␈α∞␈↓αAC␈↓'s␈α
other␈α∞that␈α
␈↓αAC1␈↓.␈α∞This␈α
is
␈↓ α←␈↓particularly useful for compiling constant arguments.
␈↓ α←␈↓␈↓342 Dynamic Structure␈↓
_6.12␈↓
␈↓"β␈↓ α←␈↓␈↓ β'For example, the call on ␈↓αf[1;A]␈↓ should be generated as:
␈↓"∀␈↓ α←␈↓α␈↓ ∧g(MOVEI 1 1)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(MOVEI 2 (QUOTE A))
␈↓"β␈↓ α←␈↓α␈↓ ∧g(CALL F)
␈↓"∀␈↓ α←␈↓There is no reason to save constants in the stack.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∂also␈α∂expect␈α∂that␈α∂the␈α⊂LISP␈α∂primitive␈α∂operations,␈α∂␈↓αcar,␈α∂cdr,␈α⊂cons,␈α∂eq,␈↓
␈↓ α←␈↓and␈α␈↓αatom␈↓␈αshould␈αoccur␈αrather␈αfrequently␈αin␈αcompiled␈αcode;␈↓π 23␈↓␈αand␈αwe␈αshould
␈↓ α←␈↓expect␈α
that␈αa␈α
reasonable␈αcompiler␈α
be␈αcognizant␈α
of␈αtheir␈α
existence␈αand␈α
compile
␈↓ α←␈↓more␈α∞efficient␈α∂code␈α∞for␈α∞their␈α∂execution.␈α∞ In␈α∞this␈α∂section␈α∞we␈α∞will␈α∂enlarge␈α∞the
␈↓ α←␈↓instruction␈αset␈αof␈αour␈αmachine,␈αadding␈αplausible␈αoperations␈αfor␈αsome␈αof␈αthese
␈↓ α←␈↓primitives.␈↓π 24␈↓
␈↓"β␈↓ α←␈↓␈↓ β'␈↓αCAR␈↓␈α_is␈α↔an␈α_instruction,␈α_taking␈α↔two␈α_arguments:␈α_an␈α↔␈↓αac␈↓␈α_and␈α_a␈α↔␈↓αloc␈↓
␈↓ α←␈↓respectively.␈α⊂The␈α⊂␈↓αcar␈↓␈α⊂operation␈α⊂is␈α∂performed␈α⊂from␈α⊂␈↓αloc␈↓␈α⊂to␈α⊂␈↓αac␈↓.␈α⊂ For␈α∂example
␈↓ α←␈↓when␈α∞compiling␈α∂the␈α∞call,␈α∂␈↓αf[1;car[x]]␈↓,␈α∞we␈α∂want␈α∞the␈α∂value␈α∞␈↓αcar[x]␈↓␈α∂in␈α∞␈↓αAC2␈↓.␈α∂If␈α∞␈↓αx␈↓
␈↓ α←␈↓were␈α⊃in␈α⊂-␈↓α5 P␈↓␈α⊃then␈α⊃we␈α⊂could␈α⊃accomplish␈α⊂the␈α⊃loading␈α⊃with:␈α⊂␈↓α(CAR␈α⊃2␈α⊃-5␈α⊂P)␈↓
␈↓ α←␈↓instead of:
␈↓"∀␈↓ α←␈↓α␈↓ ∧g(MOVE 1 -5 P)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(CALL CAR)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(MOVE 2 1)
␈↓"∀␈↓ α←␈↓Since␈α∪the␈α∀second␈α∪argument␈α∀to␈α∪␈↓αCAR␈↓␈α∀can␈α∪be␈α∀an␈α∪accumulator,␈α∀the␈α∪second
␈↓ α←␈↓argument to ␈↓αf[1;car[car[x]]]␈↓ can be compiled as:
␈↓"∀␈↓ α←␈↓α␈↓ ∧g(CAR 2 -5 P)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(CAR 2 2)
␈↓"∀␈↓ α←␈↓We␈α
will␈α∞assume␈α
the␈α∞existence␈α
of␈α∞an␈α
analogous␈α∞␈↓αCDR␈↓␈α
instruction.␈α∞With␈α
these
␈↓ α←␈↓two instructions we can significantly improve the code for ␈↓αcar-cdr␈↓-chains.
␈↓"β␈↓ α←␈↓␈↓ β'Another source of efficiency is available to us. Consider the clause:
␈↓"∀␈↓ α←␈↓α␈↓ ¬p[eq[x;A] → B; ...]
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 23␈↓Though␈α⊃we␈α⊃program␈α⊃at␈α⊃an␈α⊃abstract␈α⊃level,␈α⊃the␈α⊃code␈α⊃passed␈α⊃to␈α⊂the
␈↓ α←␈↓compiler may have many ␈↓αcar-cdr␈↓ references; see Section 6.18.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 24␈↓Some␈α∂of␈α∂these␈α∞instuctions␈α∂exist␈α∂on␈α∞the␈α∂PDP-10.␈α∂HLRZ␈α∂and␈α∞HRRZ
␈↓ α←␈↓are␈αused␈αfor␈α␈↓αcar␈↓␈αand␈α␈↓αcdr␈↓,␈αrespectively;␈αand␈αthe␈αPDP-6␈αwhich␈αwas␈αdelivered␈α
to
␈↓ α←␈↓Stanford had a hardware ␈↓αcons␈↓ operation.
␈↓ α←␈↓␈↓6.12␈↓ εnEfficiency: Primitive Operations 343␈↓
␈↓"∀␈↓ α←␈↓Assuming␈α∞that␈α∞␈↓αx␈↓␈α∞were␈α∞on␈α∞the␈α∞top␈α∞of␈α∞the␈α∞stack,␈α∞our␈α∞current␈α∂compiler␈α∞would
␈↓ α←␈↓generate:
␈↓"∀␈↓ α←␈↓α␈↓ ∧g (MOVE 1 0 P)
␈↓"β␈↓ α←␈↓α␈↓ ∧g (MOVEI 2 (QUOTE A))
␈↓"β␈↓ α←␈↓α␈↓ ∧g (CALL EQ)
␈↓"β␈↓ α←␈↓α␈↓ ∧g (JUMPF 1 L1)
␈↓"β␈↓ α←␈↓α␈↓ ∧g (MOVEI 1 (QUOTE B))
␈↓"β␈↓ α←␈↓α␈↓ ∧g (JUMP LOUT)
␈↓"β␈↓ α←␈↓α␈↓ ∧gL1 ...
␈↓"∀␈↓ α←␈↓The␈α∂use␈α∂of␈α⊂predicates␈α∂in␈α∂this␈α∂context␈α⊂does␈α∂not␈α∂require␈α∂construction␈α⊂of␈α∂the
␈↓ α←␈↓constants␈α
␈↓
t␈↓␈α
and␈α␈↓
f␈↓.␈α
All␈α
we␈α
need␈αto␈α
do␈α
is␈αimplement␈α
the␈α
␈↓αeq␈↓␈α
test␈αas␈α
a␈α
jump␈αto␈α
one
␈↓ α←␈↓of two locations.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αwill␈αintroduce␈αan␈αinstruction␈α␈↓αCAME␈↓␈αtaking␈αtwo␈αarguments;␈αfirst,␈αan
␈↓ α←␈↓␈↓αac␈↓␈αand␈αthe␈αsecond,␈αa␈α␈↓αloc␈↓.␈α␈↓αCAME␈↓␈αcompares␈αthe␈αcontents␈αof␈αthe␈αtwo␈αarguments,
␈↓ α←␈↓and if they are equal, it skips the next instruction.
␈↓"β␈↓ α←␈↓Thus the above example could be compiled as:
␈↓"∀␈↓ α←␈↓α␈↓ ∧g (MOVEI 1 (QUOTE A))
␈↓"β␈↓ α←␈↓α␈↓ ∧g (CAME 1 0 P)
␈↓"β␈↓ α←␈↓α␈↓ ∧g (JUMP L1)
␈↓"β␈↓ α←␈↓α␈↓ ∧g (MOVEI 1 (QUOTE B))
␈↓"β␈↓ α←␈↓α␈↓ ∧g (JUMP LOUT)
␈↓"β␈↓ α←␈↓α␈↓ ∧gL1 ...
␈↓"∀␈↓ α←␈↓Notice␈α∞that␈α∞we␈α
have␈α∞added␈α∞an␈α
extra␈α∞piece␈α∞of␈α
knowledge␈α∞to␈α∞the␈α∞compiler;␈α
it
␈↓ α←␈↓knows␈α∪that␈α∪␈↓αeq␈↓␈α∪is␈α∪commutative␈α∩in␈α∪this␈α∪instance.␈↓π 25␈↓␈α∪We␈α∪still␈α∪require␈α∩some
␈↓ α←␈↓artifacts␈α∃in␈α∃the␈α∃compiler␈α⊗to␈α∃generate␈α∃full␈α∃procedure␈α∃calls␈α⊗on␈α∃predicates
␈↓ α←␈↓particularly␈α∂since␈α∂predicates␈α∂may␈α∂return␈α∂values␈α∂other␈α∂than␈α∂␈↓
t␈↓␈α∂and␈α∂␈↓
f␈↓.␈α∂But␈α∞in
␈↓ α←␈↓many instances, particularly within ␈↓αcomcond␈↓, we can generate tighter code.
␈↓"β␈↓ α←␈↓␈↓ ∧l␈↓↓6.13 Efficiency: Calling Sequences␈↓
␈↓"β␈↓ α←␈↓We␈αwant␈αto␈α
integrate␈αthe␈αnew␈αcompiling␈α
techniques␈αinto␈αour␈α
compiler.␈αSince
␈↓ α←␈↓LISP␈αdepends␈αheavily␈αon␈αprocedure␈αcalls,␈αthe␈αcomputation␈αof␈αparameter␈αlists
␈↓ α←␈↓and␈α∞procedure␈α∂calls␈α∞is␈α∂an␈α∞area␈α∂of␈α∞great␈α∞concern␈α∂to␈α∞the␈α∂designer␈α∞of␈α∂a␈α∞LISP
␈↓ α←␈↓compiler.
␈↓"β␈↓ α←␈↓␈↓ β'Here␈α∪is␈α∪the␈α∪code␈α∪which␈α∪the␈α∪current␈α∪compiler␈α∪will␈α∪produce␈α∪for␈α∪the
␈↓ α←␈↓expression ␈↓αf[1;g[3]; car[x]]␈↓:
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 25␈↓If␈α⊃there␈α⊂are␈α⊃side-effects␈α⊃in␈α⊂the␈α⊃computation␈α⊂of␈α⊃the␈α⊃arguments,␈α⊂the
␈↓ α←␈↓order␈α
can␈α
make␈α
a␈α
difference.␈α
However␈α
unless␈α
explicitly␈α
stated␈α
our␈α
compilers
␈↓ α←␈↓do not have to consider side-effects.
␈↓ α←␈↓␈↓344 Dynamic Structure␈↓
_6.13␈↓
␈↓"∀␈↓ α←␈↓α␈↓ ∧g(MOVEI 1 1)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(PUSH P 1)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(MOVEI 1 3)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(CALL G)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(PUSH P 1)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(MOVE 1 -2 P)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(CALL CAR)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(MOVE 3 1)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(POP P 2)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(POP P 1)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(CALL F)
␈↓"∀␈↓ α←␈↓By␈αway␈αof␈αmotivation␈αand␈αintroduction,␈αhere␈αis␈αwhat␈αour␈αnext␈αcompiler␈αdoes
␈↓ α←␈↓for the same call:
␈↓"∀␈↓ α←␈↓α␈↓ ∧g(MOVEI 1 3)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(CALL G)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(MOVE 2 1)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(CAR 3 0 P)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(MOVEI 1 1)
␈↓"β␈↓ α←␈↓α␈↓ ∧g(CALL F)
␈↓"∀␈↓ α←␈↓Examination␈α
of␈α
the␈αcode␈α
shows␈α
the␈αresults␈α
of␈α
several␈αoptimization␈α
techniques.
␈↓ α←␈↓We␈α∂are␈α∂using␈α⊂the␈α∂␈↓αCAR␈↓␈α∂instruction␈α⊂of␈α∂the␈α∂last␈α⊂section.␈α∂ We␈α∂are␈α⊂also␈α∂doing
␈↓ α←␈↓operations␈α
into␈α
␈↓αAC␈↓'s␈α
other␈α∞than␈α
␈↓αAC1␈↓.␈α
This␈α
allows␈α∞us␈α
to␈α
remove␈α
some␈α∞of␈α
the
␈↓ α←␈↓obnoxious ␈↓αPUSH-POP␈↓ sequences.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∂major␈α∂modification␈α∂involves␈α∂an␈α∂analysis␈α∂of␈α∂the␈α∂arguments␈α∞being
␈↓ α←␈↓compiled␈α⊂for␈α⊂a␈α⊂function␈α⊂call.␈α⊃The␈α⊂function␈α⊂␈↓αcomplis␈↓␈α⊂is␈α⊂responsible␈α⊃for␈α⊂that
␈↓ α←␈↓analysis.␈α⊂ Within␈α⊃our␈α⊂new␈α⊃␈↓αcomplis␈↓␈α⊂we␈α⊃will␈α⊂divide␈α⊃the␈α⊂arguments␈α⊃into␈α⊂two
␈↓ α←␈↓classes: trivial␈α~and␈α~complex.␈α→ Since␈α~most␈α~of␈α→our␈α~worry␈α~is␈α~about␈α→the
␈↓ α←␈↓optimization␈α∀of␈α∀the␈α∀␈↓αAC␈↓'s,␈α∀we␈α∀will␈α∪make␈α∀␈↓αcomplis␈↓␈α∀the␈α∀major␈α∀state␈α∀of␈α∪the
␈↓ α←␈↓compiler. We can define ␈↓αcompexp␈↓ as:
␈↓"∀␈↓ α←␈↓α␈↓ ∧βcompexp <= λ[[exp;vpl;off] complis[list[exp];vpl;off]]
␈↓"∀␈↓ α←␈↓␈↓αcomplis␈↓␈α∃is␈α∀the␈α∃natural␈α∀place␈α∃to␈α∀deal␈α∃with␈α∀register␈α∃allocation␈α∀since␈α∃it␈α∀is
␈↓ α←␈↓responsible␈α⊃for␈α∩the␈α⊃compilation␈α∩of␈α⊃the␈α∩actual␈α⊃parameters.␈α∩The␈α⊃alternative
␈↓ α←␈↓would␈αbe␈αto␈αpass␈αthe␈α␈↓αAC␈↓␈αdestination␈αto␈α␈↓αcompexp␈↓.␈αThat␈αscheme␈αbecomes␈αquite
␈↓ α←␈↓complex␈α⊂if␈α∂dealt␈α⊂with␈α⊂consistently.␈α∂So␈α⊂␈↓αcomplis␈↓␈α∂becomes␈α⊂the␈α⊂kernel␈α∂function
␈↓ α←␈↓and must examine each argument to a function call.
␈↓"β␈↓ α←␈↓␈↓ β'Trivial␈α∩arguments␈α∩are␈α∩those␈α∩which␈α∩need␈α∩make␈α∩no␈α∩demands␈α∩on␈α∩the
␈↓ α←␈↓runtime␈α
stack;␈α
the␈α
computation␈α
they␈α
entail␈α
can␈α
all␈α
be␈α
done␈α
in␈α
the␈α␈↓αAC␈↓␈α
registers.
␈↓ α←␈↓Thus␈α⊂the␈α⊃code␈α⊂that␈α⊃the␈α⊂compiler␈α⊂generates␈α⊃need␈α⊂not␈α⊃involve␈α⊂␈↓αPUSH-POP␈↓
␈↓ α←␈↓sequences.␈α∞ For␈α∞example,␈α∞references␈α∞to␈α∞constants␈α∞need␈α∞not␈α∞be␈α∞generated␈α
and
␈↓ α←␈↓then␈α∂pushed␈α∂onto␈α∞the␈α∂stack;␈α∂we␈α∞can␈α∂compile␈α∂the␈α∞other␈α∂arguments␈α∂first␈α∞and
␈↓ α←␈↓then,␈α⊂just␈α⊂before␈α⊂we␈α⊂call␈α⊂the␈α⊂function,␈α⊂load␈α⊂the␈α⊂appropriate␈α⊂␈↓αAC␈↓␈α⊃with␈α⊂that
␈↓ α←␈↓constant.␈α⊃A␈α⊃similar␈α⊃argument␈α⊃can␈α∩be␈α⊃used␈α⊃for␈α⊃postponing␈α⊃the␈α∩loading␈α⊃of
␈↓ α←␈↓␈↓6.13␈↓ π∞Efficiency: Calling Sequences 345␈↓
␈↓"β␈↓ α←␈↓variable␈α∪references.␈↓π 26␈↓␈α∩The␈α∪third␈α∩trivial␈α∪construct␈α∩for␈α∪this␈α∩␈↓αcomplis␈↓␈α∪is␈α∩the
␈↓ α←␈↓handling␈α⊃of␈α⊂␈↓αcar-cdr␈↓␈α⊃chains.␈α⊃We␈α⊂will␈α⊃use␈α⊃our␈α⊂augmented␈α⊃instruction␈α⊃set␈α⊂to
␈↓ α←␈↓perform␈α∞computation␈α∞of␈α∞␈↓αcar␈↓s␈α∞and␈α∞␈↓αcdr␈↓s␈α∞directly␈α∞to␈α∞a␈α∞specified␈α∂␈↓αAC␈↓.␈α∞ Complex
␈↓ α←␈↓arguments␈α⊗are␈α⊗those␈α⊗which␈α⊗require␈α⊗some␈α⊗non-trivial␈α⊗computation;␈α∃each
␈↓ α←␈↓non-trivial␈α∞computation␈α∞will␈α∞be␈α∞prefaced␈α∞with␈α∞a␈α∞␈↓αPUSH␈↓␈α∞to␈α∞save␈α∞the␈α
current
␈↓ α←␈↓contents of ␈↓αAC1␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Besides␈α
the␈αcompilation␈α
of␈α
efficient␈αcode␈α
we␈α
would␈αalso␈α
like␈α
to␈αmake␈α
the
␈↓ α←␈↓compiler␈αefficient.␈α
We␈αwould␈α
like␈αto␈α
make␈αthe␈α
compiling␈αprocess␈α
as␈αone-pass
␈↓ α←␈↓as␈α∩possible.␈α∩Our␈α∩basic␈α∩tasks␈α∩in␈α∩the␈α∩new␈α∩␈↓αcomplis␈↓␈α∩are␈α∩classification␈α∩of␈α⊃the
␈↓ α←␈↓arguments␈αand␈αcompilation␈αof␈αthe␈αcode.␈αWith␈αa␈αlittle␈αcare␈αwe␈αcan␈αdo␈αboth␈αat
␈↓ α←␈↓the␈α∂same␈α∂time.␈α∂There␈α∂is␈α∂nothing␈α∂problematic␈α∂about␈α∂the␈α∂compilation␈α∂of␈α∂the
␈↓ α←␈↓trivial code.␈↓π 27␈↓ We thus turn to the complex code.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∂old␈α∂␈↓αcomplis␈↓␈α∂generated␈α⊂a␈α∂block␈α∂<code␈α∂␈↓αarg␈↓βi␈↓>-␈↓αPUSH␈↓␈α∂on␈α⊂each␈α∂cycle.
␈↓ α←␈↓That␈α
code␈α
was␈α
followed␈α
by␈α
a␈α
␈↓αMOVE␈↓␈α
to␈α
move␈α
the␈α
last␈α
value␈α
from␈α
␈↓αAC1␈↓␈αto␈α
␈↓αACn␈↓.
␈↓ α←␈↓In␈α⊂the␈α∂previous␈α⊂compiler␈α∂␈↓αcompexp␈↓␈α⊂was␈α∂the␈α⊂major␈α∂function;␈α⊂it␈α⊂handled␈α∂the
␈↓ α←␈↓bulk␈α
of␈α
the␈α
code␈α
generation.␈α
Here␈α
␈↓αcomplis␈↓␈α
will␈α
be␈α
the␈α
major␈α
function.␈α
The␈α
old
␈↓ α←␈↓␈↓αcomplis␈↓␈α⊂had␈α∂three␈α⊂states:␈α⊂empty␈α∂argument␈α⊂list,␈α∂singleton␈α⊂argument␈α⊂list,␈α∂and
␈↓ α←␈↓otherwise␈α∂condition.␈α∂The␈α∂new␈α∂␈↓αcomplis␈↓␈α∂has␈α∂two␈α∂states;␈α∂this␈α∂is␈α∂done␈α∂to␈α∞make
␈↓ α←␈↓␈↓αcomplis␈↓␈α!shorter.␈α! On␈α!each␈α!cycle␈α!through␈α!␈↓αcomplis␈↓␈α!we␈α!generate␈α a
␈↓ α←␈↓␈↓αPUSH␈↓-<code ␈↓αarg␈↓βi␈↓>␈α
sequence.␈α
Now␈αwe␈α
have␈α
a␈α
spurious␈α␈↓αPUSH␈↓␈α
on␈α
the␈α␈↓¬front␈↓␈α
of
␈↓ α←␈↓the sequence; one ␈↓αrest␈↓ will take care of ␈↓¬that␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αmust␈αalso␈αgenerate␈α
a␈αlist␈αof␈α␈↓αPOP␈↓s␈αto␈α
suffix␈αto␈αthe␈αcomplex␈α
code␈αto
␈↓ α←␈↓get␈αthe␈α
saved␈αvalues␈αback␈α
into␈αthe␈α
proper␈α␈↓αAC␈↓'s:␈αone␈α
pop␈αfor␈α
each␈αargument.
␈↓ α←␈↓The␈α∀␈↓¬last␈↓␈α∪␈↓αPOP␈↓␈α∀should␈α∀be␈α∪modified␈α∀to␈α∪be␈α∀a␈α∀␈↓αMOVE␈↓␈α∪since␈α∀we␈α∀have␈α∪not
␈↓ α←␈↓generated␈αthe␈α
corresponding␈α␈↓αPUSH␈↓.␈αThe␈α
memory␈αfield␈αof␈α
the␈αlast␈α
␈↓αPOP␈↓␈αhas
␈↓ α←␈↓the␈αneeded␈αinformation;␈αit␈αtells␈αus␈αwhere␈αthe␈α␈↓αMOVE␈↓␈αwe␈αwant␈αto␈αmake␈αshould
␈↓ α←␈↓go:
␈↓"∀␈↓ α←␈↓α␈↓ ¬∃(POP P N) => (MOVE N 1)
␈↓"∀␈↓ α←␈↓This␈α
modified␈α
list␈α
of␈α
␈↓αPOP␈↓s␈α
is␈α
added␈α
to␈α
the␈α
code␈α
sequence,␈α
followed␈α∞by␈α
any
␈↓ α←␈↓trivial␈α∩code␈α∩which␈α∩we␈α∩may␈α∪have␈α∩generated.␈α∩Note␈α∩that␈α∩this␈α∪reordering␈α∩is
␈↓ α←␈↓strictly␈α∂an␈α∂efficiency␈α∂consideration␈α⊂under␈α∂the␈α∂assumption␈α∂that␈α∂the␈α⊂␈↓αAC␈↓'s␈α∂are
␈↓ α←␈↓being␈α∀used␈α∀to␈α∀simulate␈α∀a␈α∀temporary␈α∀␈↓αdest␈↓␈α∀block,␈α∀which␈α∀will␈α∀immediately
␈↓ α←␈↓become␈α∂a␈α∞block␈α∂of␈α∞local␈α∂bindings,␈α∞and␈α∂which␈α∞are␈α∂subject␈α∞to␈α∂local␈α∂use␈α∞only.
␈↓ α←␈↓With this introduction, here is ␈↓αcomplis␈↓ and friends:
␈↓"∀␈↓ α←␈↓␈↓αcomplis <= λ[[u;off;vpl] complis␈↓λ'␈↓α[u;off;off;vpl;();();();1]␈↓
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 26␈↓But␈α∞note␈α∂that␈α∞the␈α∞argument␈α∂for␈α∞variables␈α∞is␈α∂shaky;␈α∞if␈α∂our␈α∞compiler
␈↓ α←␈↓handled␈α∪programs␈α∀with␈α∪side-effects␈α∪then␈α∀we␈α∪could␈α∪␈↓¬not␈↓␈α∀be␈α∪sure␈α∀that␈α∪the
␈↓ α←␈↓postponed value would be the same as that fetched at the "proper" time.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 27␈↓That's why it's trivial!
␈↓ α←␈↓␈↓346 Dynamic Structure␈↓
_6.13␈↓
␈↓"∀␈↓ α←␈↓αcomplis␈↓λ'␈↓α <= λ[[u;org;off;vpl;triv;cmplx;pop;ac]
␈↓"β␈↓ α←␈↓α␈↓ ∧∪[null[u] →␈↓ ¬↔[null[cmplx] → triv;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔ ␈↓
t␈↓α → append␈↓ ε3[rest[cmplx];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ε3 list[mkmove[mem[first[pop]];1]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ε3 rest[pop];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ε3 triv]];
␈↓" ␈↓ α←␈↓α␈↓ ∧∪ isconst[first[u]] → complis␈↓λ'␈↓α[␈↓ ε{rest[u];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ε{org;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ε{off;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ε{vpl;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ε{concat[mkconst[ac;first[u]];triv];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ε{cmplx;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ε{pop;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ε{add1[ac]];
␈↓" ␈↓ α←␈↓α␈↓ ∧∪ isvar[first[u]] → complis␈↓λ'␈↓α[␈↓ εcrest[u];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ εcorg;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ εcoff;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ εcvpl;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ εcconcat[␈↓ π7mkvar[␈↓ λac;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ εc␈↓ π7␈↓ λloc[first[u];off;vpl]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ εc␈↓ π7triv];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ εccmplx;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ εcpop;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ εcadd1[ac]];
␈↓" ␈↓ α←␈↓α␈↓ ∧∪ iscarcdr[first[u]] → complis␈↓λ'␈↓α[␈↓ ππrest[u];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ππorg;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ππoff;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ππvpl;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ππappend[␈↓ πsreverse[compcarcdr[␈↓ cac;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ππ␈↓ πs␈↓ c first[u]
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ππ␈↓ πs␈↓ c off;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ππ␈↓ πs␈↓ c vpl]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ππ␈↓ πstriv];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ππcmplx;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ππpop;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬↔␈↓ ππadd1[ac]];
␈↓ α←␈↓␈↓6.13␈↓ π∞Efficiency: Calling Sequences 347␈↓
␈↓" ␈↓ α←␈↓α␈↓ ∧∪ iscond[first[u] → complis␈↓λ'␈↓α[␈↓ εorest[u];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬#␈↓ εoorg;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬#␈↓ εosub1[off];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬#␈↓ εovpl;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬#␈↓ εotriv;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬#␈↓ εoappend[␈↓ π[cmplx;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬#␈↓ εo␈↓ π[concat[␈↓ λ/mkpush[1];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬#␈↓ εo␈↓ π[␈↓ λ/comcond[␈↓ ≠args␈↓βc␈↓α[␈↓ cfirst[u]]
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬#␈↓ εo␈↓ π[␈↓ λ/␈↓ ≠gensym[];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬#␈↓ εo␈↓ π[␈↓ λ/␈↓ ≠off;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬#␈↓ εo␈↓ π[␈↓ λ/␈↓ ≠vpl]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬#␈↓ εoconcat[mkpop[ac];pop];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬#␈↓ εoadd1[ac]];
␈↓" ␈↓ α←␈↓α␈↓ ∧∪ ␈↓
t␈↓α → complis␈↓λ'␈↓α[␈↓ ¬Grest[u];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬Gorg;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬Gsub1[off];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬Gvpl;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬Gtriv;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬Gappend[␈↓ ε3cmplx;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬G␈↓ ε3concat[␈↓ ππmkpush[1];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬G␈↓ ε3␈↓ ππλ[[z] compapply[␈↓ λ←func[first[u]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬G␈↓ ε3␈↓ ππ␈↓ λ←complis[␈↓ Kz;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬G␈↓ ε3␈↓ ππ␈↓ λ←␈↓ Koff;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬G␈↓ ε3␈↓ ππ␈↓ λ←␈↓ Kvpl];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬G␈↓ ε3␈↓ ππ␈↓ λ←length[z]]]
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬G␈↓ ε3␈↓ ππ [arglist[first[u]] ]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬Gconcat[mkpop[ac];pop];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ¬Gadd1[ac]]]
␈↓"→␈↓ α←␈↓αmkmove <= λ[[ac;loc][eq[ac;loc] → (); ␈↓
t␈↓α → list[MOVE;ac;loc]]]
␈↓"∀␈↓ α←␈↓αcompcarcdr <= λ[[ac;exp;off;vpl]
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7[isvar[arg[exp]] → list[mkcarcdr[␈↓ π[func[exp];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7␈↓ ¬G␈↓ π[ac;
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7␈↓ ¬G␈↓ π[loc[arg[exp];off;vpl]]]
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7␈↓
t␈↓α → concat[␈↓ ¬Gmkcarcdr_ac[func[exp];ac;ac];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7␈↓ ¬Gcompcarcdr[ac;arg[exp];off;vpl]]]]
␈↓"∀␈↓ α←␈↓αiscarcdr <=λ[[u]␈↓ ∧7[iscar[u] →iscarcdr[arg[u]]
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7 iscdr[u] →iscarcdr[arg[u]]
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7 atom[u] → or[isvar[u];isconst[u]];
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧7 ␈↓
t␈↓α → ␈↓
f␈↓α ]]
␈↓"∀␈↓ α←␈↓αiscar <= λ[[x] eq[func[x];CAR]]
␈↓" ␈↓ α←␈↓αiscdr <= λ[[x] eq[func[x];CDR]]
␈↓" ␈↓ α←␈↓αmkcarcdr <=λ[[carcdr;ac;loc] list[carcdr;ac;loc]]
␈↓ α←␈↓␈↓348 Dynamic Structure␈↓
_6.14␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬ ␈↓↓6.14 Efficiency: Predicates␈↓
␈↓"β␈↓ α←␈↓We␈α⊂have␈α⊂already␈α⊂noted␈α⊃in␈α⊂Section 6.12␈α⊂that␈α⊂some␈α⊂efficiencies␈α⊃are␈α⊂possible
␈↓ α←␈↓when␈α
predicates␈α
occur␈αwithin␈α
conditional␈α
expressions.␈αHere␈α
we␈α
will␈αexamine
␈↓ α←␈↓more␈αpossibilities.␈αThe␈αfirst␈αpoint␈α
of␈αcontention␈αis␈αthat␈αthe␈α
current␈α␈↓αcomclause␈↓
␈↓ α←␈↓is␈α∞␈↓¬not␈↓␈α∞good␈α∞enough.␈α∂We␈α∞want␈α∞to␈α∞be␈α∞able␈α∂to␈α∞use␈α∞the␈α∞Boolean␈α∂special␈α∞forms:
␈↓ α←␈↓␈↓αand[u␈↓β1␈↓α; ...;u␈↓βn␈↓α]␈↓␈α⊃and␈α⊃␈↓αor[u␈↓β1␈↓α; ...;u␈↓βn␈↓α]␈↓.␈α⊃The␈α⊂definition␈α⊃of␈α⊃these␈α⊃constructs␈α⊂requires
␈↓ α←␈↓they␈α
not␈α∞evaluate␈α
any␈α
more␈α∞arguments␈α
than␈α
necessary.␈α∞ We␈α
can␈α∞exploit␈α
this
␈↓ α←␈↓property␈α∂when␈α∞␈↓αand␈↓␈α∂and␈α∞␈↓αor␈↓␈α∂appear␈α∞as␈α∂predicates␈α∞in␈α∂conditional␈α∞expressions.
␈↓ α←␈↓We␈αwill␈αadd␈αrecognizers␈αfor␈α␈↓αand␈↓␈αand␈α␈↓αor␈↓␈αinside␈α␈↓αcomclause␈↓␈αand␈αwill␈αadd␈αa␈αnew
␈↓ α←␈↓section to the compiler to deal with their compilation.
␈↓"β␈↓ α←␈↓␈↓ β'First, here is the structure of typical code sequences:
␈↓"∀␈↓ α←␈↓α␈↓ βKand[u␈↓β1␈↓α; ... u␈↓βn␈↓α] → e;␈↓ ε3or[u␈↓β1␈↓α; ... u␈↓βn␈↓α] → e;
␈↓"β␈↓ α←␈↓α␈↓ βK ␈↓gives:␈↓ ε3gives:␈↓α
␈↓"β␈↓ α←␈↓α␈↓ βK <code for u␈↓β1␈↓α>␈↓ ε3 <code for u␈↓β1␈↓α>
␈↓"β␈↓ α←␈↓α␈↓ βK (JUMPF 1 lint)␈↓ ε3 (JUMPT 1 loc)
␈↓"β␈↓ α←␈↓α␈↓ βK <code for u␈↓β2␈↓α>␈↓ ε3 <code for u␈↓β2␈↓α>
␈↓"β␈↓ α←␈↓α␈↓ βK (JUMPF 1 lint)␈↓ ε3 (JUMPT 1 loc)
␈↓"β␈↓ α←␈↓α␈↓ βK . . .␈↓ ε3 . . .
␈↓"β␈↓ α←␈↓α␈↓ βK <code for u␈↓βn␈↓α>␈↓ ε3 <code for u␈↓βn␈↓α>
␈↓"β␈↓ α←␈↓α␈↓ βK (JUMPF 1 lint)␈↓ ε3 (JUMPT 1 loc)
␈↓"β␈↓ α←␈↓α␈↓ βK (JUMP loc)␈↓ ε3 (JUMP lint)
␈↓"β␈↓ α←␈↓α␈↓ βKloc␈↓ ε3loc
␈↓"β␈↓ α←␈↓α␈↓ βK ␈↓<code for ␈↓αe␈↓>␈↓ ε3 <code for ␈↓αe␈↓>␈↓α
␈↓"β␈↓ α←␈↓α␈↓ βK (JUMP lout)␈↓ ε3 (JUMP lout)
␈↓"β␈↓ α←␈↓α␈↓ βKlint␈↓ ε3lint
␈↓"∀␈↓ α←␈↓The␈α∞label␈α∞␈↓αlint␈↓␈α∞indicates␈α∞the␈α∞next␈α∞clause␈α∞in␈α∞the␈α∞conditional␈α∞expression.␈α
Note
␈↓ α←␈↓the␈αsymmetry␈αbetween␈αthe␈αcode␈αfor␈α␈↓αand␈↓␈α
and␈αthe␈αcode␈αfor␈α␈↓αor␈↓.␈αThere␈αis␈αa␈α
slight
␈↓ α←␈↓inefficiency␈αin␈α␈↓αand␈↓␈αwith␈α␈↓α(JUMP loc)␈↓␈αimmedately␈αfollowed␈αby␈α␈↓αloc␈↓,␈αbut␈αwe␈αcan
␈↓ α←␈↓easily remove that.
␈↓"β␈↓ α←␈↓Here is a ␈↓αcompclause␈↓ which will generate it:
␈↓"∀␈↓ α←␈↓αcompclause <=λ[[p;loc;glob;off;vpl] append[␈↓ ππcompred[␈↓ π␈ante[p];
␈↓"β␈↓ α←␈↓α␈↓ ππ␈↓ π␈loc;
␈↓"β␈↓ α←␈↓α␈↓ ππ␈↓ π␈off;
␈↓"β␈↓ α←␈↓α␈↓ ππ␈↓ π␈vpl];
␈↓"β␈↓ α←␈↓α␈↓ ππcompexp[␈↓ π␈conseq[p];
␈↓"β␈↓ α←␈↓α␈↓ ππ␈↓ π␈off;
␈↓"β␈↓ α←␈↓α␈↓ ππ␈↓ π␈vpl];
␈↓"β␈↓ α←␈↓α␈↓ ππlist[mkjump[glob];loc]]]
␈↓ α←␈↓␈↓6.14␈↓ πnEfficiency: Predicates 349␈↓
␈↓"∀␈↓ α←␈↓αcompred <= λ[[p;lint;off;vpl]␈↓ ¬S[isand[p] → compandor[␈↓ π␈args[p];
␈↓"β␈↓ α←␈↓α␈↓ ¬S␈↓ ε{␈↓ π␈off;
␈↓"β␈↓ α←␈↓α␈↓ ¬S␈↓ ε{␈↓ π␈vpl;
␈↓"β␈↓ α←␈↓α␈↓ ¬S␈↓ ε{␈↓ π␈list[mkjumpnil[lint]];
␈↓"β␈↓ α←␈↓α␈↓ ¬S␈↓ ε{␈↓ π␈()];
␈↓"β␈↓ α←␈↓α␈↓ ¬S isor[p] → λ␈↓ ε{[[loc] compandor[␈↓ λSargs[p];
␈↓"β␈↓ α←␈↓α␈↓ ¬S␈↓ ε{␈↓ π␈␈↓ λSoff;
␈↓"β␈↓ α←␈↓α␈↓ ¬S␈↓ ε{␈↓ π␈␈↓ λSvpl;
␈↓"β␈↓ α←␈↓α␈↓ ¬S␈↓ ε{␈↓ π␈␈↓ λSlist[mkjumpt[loc]];
␈↓"β␈↓ α←␈↓α␈↓ ¬S␈↓ ε{␈↓ π␈␈↓ λSlist[␈↓ βmkjmp[lint];
␈↓"β␈↓ α←␈↓α␈↓ ¬S␈↓ ε{␈↓ π␈␈↓ λS␈↓ βloc]]]
␈↓"β␈↓ α←␈↓α␈↓ ¬S␈↓ ε{[gensym[]];
␈↓"β␈↓ α←␈↓α␈↓ ¬S ␈↓
t␈↓α → append[␈↓ ε{compexp[p;off;vpl];
␈↓"β␈↓ α←␈↓α␈↓ ¬S␈↓ ε{list[mkjumpf[lint]]] ]]
␈↓"∀␈↓ α←␈↓αcompandor <=λ[[u;off;vpl;inst;fini]␈↓ ε'[null[u] → fini;
␈↓"β␈↓ α←␈↓α␈↓ ε' ␈↓
t␈↓α → append[␈↓ πOcompexp[first[u];off;vpl];
␈↓"β␈↓ α←␈↓α␈↓ ε'␈↓ πOinst;
␈↓"β␈↓ α←␈↓α␈↓ ε'␈↓ πOcompandor[␈↓ λ←rest[u];
␈↓"β␈↓ α←␈↓α␈↓ ε'␈↓ πO␈↓ λ←off;
␈↓"β␈↓ α←␈↓α␈↓ ε'␈↓ πO␈↓ λ←vpl;
␈↓"β␈↓ α←␈↓α␈↓ ε'␈↓ πO␈↓ λ←inst;
␈↓"β␈↓ α←␈↓α␈↓ ε'␈↓ πO␈↓ λ←fini]] ]]]
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. We␈α⊂should␈α⊂recognize␈α⊂the␈α⊂construct␈α⊂␈↓
t␈↓ →␈↓↓e␈↓βi␈↓␈α⊂in␈α⊂conditional␈α⊂expressions␈α∂and
␈↓ α←␈↓␈↓ β∂compile special code for it. We should also realize that in the construct:
␈↓"β␈↓ α←␈↓α␈↓ ¬'[␈↓↓p␈↓β1␈↓α → ␈↓↓e␈↓β1␈↓α ... ␈↓
t␈↓α → ␈↓↓e␈↓βi␈↓α; ...␈↓↓p␈↓βn␈↓α → ␈↓↓e␈↓βn␈↓α]
␈↓"β␈↓ α←␈↓␈↓ β∂we␈α∩can␈α⊃␈↓¬never␈↓␈α∩reach␈α∩any␈α⊃part␈α∩of␈α∩the␈α⊃conditional␈α∩after␈α∩the␈α⊃␈↓
t␈↓-predicate;
␈↓ α←␈↓␈↓ β∂therefore␈α∂no␈α∂code␈α∂should␈α∂be␈α∞generated.␈α∂Rewrite␈α∂the␈α∂compiler␈α∂to␈α∞handle
␈↓ α←␈↓␈↓ β∂these additional observations about conditionals.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∞second␈α∂point,␈α∞above,␈α∞is␈α∂a␈α∞special␈α∞instance␈α∂of␈α∞a␈α∂general␈α∞compiling
␈↓ α←␈↓␈↓ β∂question.␈α
How␈α
clever␈α
should␈α
the␈α
compiler␈αbe?␈α
If␈α
it␈α
can␈α
recognize␈α
that␈αa
␈↓ α←␈↓␈↓ β∂piece␈α
of␈α
program␈α
can␈α
never␈α
be␈αreached,␈α
should␈α
it␈α
tell␈α
the␈α
user␈α
or␈αshould␈α
it
␈↓ α←␈↓␈↓ β∂compile minimal code?
␈↓" ␈↓ α←␈↓2. Write␈α
a␈α∞new␈α
␈↓αcompile␈↓␈α∞including␈α
all␈α∞the␈α
efficiency␈α∞considerations␈α
discussed
␈↓ α←␈↓␈↓ β∂so far.
␈↓" ␈↓ α←␈↓3. When␈α
we␈α
apply␈αthe␈α
convention␈α
that␈αanything␈α
non-␈↓αNIL␈↓␈α
is␈αa␈α
representation
␈↓ α←␈↓␈↓ β∂of␈αtruth,␈αit␈αis␈αoften␈αconvenient␈α
to␈αevaluate␈α␈↓αand␈↓␈αand␈α␈↓αor␈↓␈αfor␈α
"value".␈α That
␈↓ α←␈↓␈↓ β∂is␈αtheir␈αvalue␈α
is␈αeither␈α␈↓αNIL␈↓␈αor␈α
non-␈↓αNIL␈↓.␈αExtend␈αour␈αcompiler␈α
to␈αhandle
␈↓ α←␈↓␈↓ β∂such uses of these functions.
␈↓" ␈↓ α←␈↓4. Extend␈α⊃the␈α⊃compiler␈α∩to␈α⊃compile␈α⊃efficient␈α∩code␈α⊃for␈α⊃compositions␈α∩of␈α⊃the
␈↓ α←␈↓␈↓ β∂predicates ␈↓αand␈↓, ␈↓αor␈↓, and ␈↓αnot␈↓.
␈↓ α←␈↓␈↓350 Dynamic Structure␈↓
_6.15␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬%␈↓↓6.15 A Compiler for ␈↓αprogs␈↓↓␈↓α
␈↓"β␈↓ α←␈↓The␈αcompiler␈αof␈αthis␈αsection␈αwill␈αnot␈αcompile␈αall␈α␈↓αprog␈↓s;␈αit␈αis␈αonly␈αintended␈αto
␈↓ α←␈↓demonstrate some of the salient features of a ␈↓αprog␈↓ compiler. They are:
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ Handling␈α∩of␈α∩assignments.␈α∩Since␈α∩we␈α∩are␈α∩assuming␈α∩local␈α∪variables,␈α∩then
␈↓ α←␈↓␈↓ β∂storage to the value stack is sufficient.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ The␈α⊃␈↓αgo␈↓-label␈α⊃pair.␈α⊃We␈α⊂will␈α⊃assume␈α⊃that␈α⊃this␈α⊂can␈α⊃be␈α⊃passed␈α⊃off␈α⊃to␈α⊂the
␈↓ α←␈↓␈↓ β∂assembler.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ On␈α
leaving␈α
a␈α
␈↓αprog␈↓-body␈α
we␈α
have␈α
to␈α
remove␈α
the␈α
␈↓αprog␈↓-variables␈α∞from␈α
the
␈↓ α←␈↓␈↓ β∂top of the stack. This is done by comparing the current ␈↓αoff␈↓ with ␈↓αvpl␈↓.
␈↓"∀␈↓ α←␈↓αcompprog <=λ[[locals;body;off;vpl]
␈↓"β␈↓ α←␈↓α␈↓ ∧7λ[[n]append[␈↓ ¬←mkpushlistnil[n];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ¬←compbody[␈↓ εcbody;
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ¬←␈↓ εclabels[body];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ¬←␈↓ εcdifference[off;n];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ¬←␈↓ εcpruploc[locals;-off;vpl];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ¬←␈↓ εcn;
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ¬←␈↓ εcgensym[]]]
␈↓"β␈↓ α←␈↓α␈↓ ∧7 [length[locals]]
␈↓"∀␈↓ α←␈↓αpruploc <= λ[[locals;off;vpl]␈↓ ¬G[null[locals] → vpl;
␈↓"β␈↓ α←␈↓α␈↓ ¬G ␈↓
t␈↓α → pruploc[␈↓ εorest[locals];
␈↓"β␈↓ α←␈↓α␈↓ ¬G␈↓ εoadd1[off];
␈↓"β␈↓ α←␈↓α␈↓ ¬G␈↓ εoconcat[cons[first[locals];off];vpl]]]]
␈↓"∀␈↓ α←␈↓αlabels <= λ[[body]␈↓ ∧C[null[body] → ();
␈↓"β␈↓ α←␈↓α␈↓ ∧C islabel[first[body]] → concat[first[body];labels[rest[body]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧C ␈↓
t␈↓α → labels[rest[body]]]]
␈↓"∀␈↓ α←␈↓αcompbody <= λ[[body;labels;off;vpl;n;exit]
␈↓"β␈↓ α←␈↓α␈↓ ∧7[null[body] →list[mkconst[1;NIL];exit;mksync[n]];
␈↓" ␈↓ α←␈↓α␈↓ ∧7 islabel[first[body]] → concat[first[body];compbody[␈↓ 'rest[body];
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ¬;␈↓ π+␈↓ λ/␈↓ λS␈↓ 'labels;
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ¬;␈↓ π+␈↓ λ/␈↓ λS␈↓ 'off;
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ¬;␈↓ π+␈↓ λ/␈↓ λS␈↓ 'vpl;
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ¬;␈↓ π+␈↓ λ/␈↓ λS␈↓ 'n;
␈↓"β␈↓ α←␈↓α␈↓ ∧7␈↓ ¬;␈↓ π+␈↓ λ/␈↓ λS␈↓ 'exit]];
␈↓" ␈↓ α←␈↓α␈↓ β{ isgo[first[body]] → append[␈↓ εolist[compgo[␈↓ λarg[first[body]];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧␈␈↓ εo␈↓ πs␈↓ λlabels]];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧␈␈↓ εocompbody[␈↓ πsrest[body];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧␈␈↓ εo␈↓ πslabels;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧␈␈↓ εo␈↓ πsoff;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧␈␈↓ εo␈↓ πsvpl;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧␈␈↓ εo␈↓ πsn;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧␈␈↓ εo␈↓ πsexit]];
␈↓ α←␈↓␈↓6.15␈↓ πzA Compiler for ␈↓αprogs␈↓ 351␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β{ isret[first[body]] → append[␈↓ εocompexp[␈↓ πgarglist[first[body]];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εo␈↓ πgoff;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εo␈↓ πgvpl];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εosync[off;vpl];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εolist[mkjump[exit]];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εocompbody[␈↓ πg␈↓ πsrest[body];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εo␈↓ πg␈↓ πslabels;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εo␈↓ πg␈↓ πsoff;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εo␈↓ πg␈↓ πsvpl;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εo␈↓ πg␈↓ πsn;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εo␈↓ πg␈↓ πsexit]];
␈↓" ␈↓ α←␈↓α␈↓ β{ issetq[first[body]] → append[␈↓ εocompexp[rhs[first[body]];off;vpl];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εolist[mkmovem[␈↓ λ#1;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εo␈↓ πs␈↓ λ#loc[␈↓ λSlhs[first[body]];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εo␈↓ πs␈↓ λ#␈↓ λSoff;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εo␈↓ πs␈↓ λ#␈↓ λSvpl]]];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εocompbody[␈↓ πsrest[body];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εo␈↓ πslabels;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εo␈↓ πsoff;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εo␈↓ πsvpl;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εo␈↓ πsn;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ εo␈↓ πsexit]];
␈↓" ␈↓ α←␈↓α␈↓ β{ iscond[first[body]] → append[␈↓ ε{compcondprog[arg[first[body]]];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ε{compbody[␈↓ π␈rest[body];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ε{␈↓ π␈labels;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ε{␈↓ π␈off;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ε{␈↓ π␈vpl;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ε{␈↓ π␈n;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ε{␈↓ π␈exit]];
␈↓" ␈↓ α←␈↓α␈↓ β{␈↓
t␈↓α → append[␈↓ ¬#compexp[first[body];off;vpl];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ¬#compbody[rest[body];labels;off;vpl;n;exit]]]]
␈↓"∀␈↓ α←␈↓αcompgo <= λ[[x;l][member[x;l] → mkjump[x]; ␈↓
t␈↓α → err[UNDEFINED_TAG]]];
␈↓"∀␈↓ α←␈↓This␈α∞␈↓αcompprog␈↓␈α
only␈α∞handles␈α
a␈α∞subset␈α
of␈α∞the␈α
semantics␈α∞of␈α
␈↓αprog␈↓.␈α∞ We␈α∞do␈α
not
␈↓ α←␈↓handle␈α∞any␈α∞non-local␈α∞jumps;␈α∞a␈α∞new␈α∞list␈α∂of␈α∞labels␈α∞is␈α∞made␈α∞up␈α∞on␈α∞entry␈α∂to␈α∞a
␈↓ α←␈↓␈↓αprog␈↓␈αand␈αonly␈αthat␈αset␈αof␈αlabels␈αis␈αaccessible␈αfor␈α␈↓αgo␈↓s.␈α As␈αa␈αfurther␈αrestriction,
␈↓ α←␈↓we also assume that the ␈↓αprog␈↓ variables are used in a strictly local fashion.
␈↓"β␈↓ α←␈↓␈↓ ε≡␈↓↓Problem␈↓
␈↓"→␈↓ α←␈↓1. Write ␈↓αcompcondprog␈↓.
␈↓ α←␈↓␈↓352 Dynamic Structure␈↓
_6.16␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬≡␈↓↓6.16 Further Optimizations␈↓
␈↓"β␈↓ α←␈↓This␈α
section␈α
is␈α
in␈α
the␈α
nature␈α
of␈α
hints␈α
and␈α
possibilities␈α
for␈α
expansion␈α∞of␈α
the
␈↓ α←␈↓basic compiling algorithms.
␈↓"β␈↓ α←␈↓␈↓ β'One␈αof␈αthe␈αfirst␈αthings␈αto␈α
note␈αabout␈αthe␈αcompiling␈αalgorithm␈αis␈αits␈α
lack
␈↓ α←␈↓of␈αknowledge␈αabout␈αwhat␈αit␈αhas␈αin␈αthe␈αvarious␈α␈↓αAC␈↓'s.␈αFrequently␈αthe␈αcompiled
␈↓ α←␈↓code␈α
will␈α
load␈α
up␈α∞one␈α
of␈α
the␈α
registers␈α
with␈α∞a␈α
quantity␈α
that␈α
is␈α∞already␈α
there.
␈↓ α←␈↓Thus␈αthe␈α
first␈αsuggestion:␈α
build␈αa␈α
list␈αof␈α
what's␈αin␈α
various␈αregisters.␈αWe␈α
know
␈↓ α←␈↓what's␈αthere␈αwhen␈αwe␈αenter␈αthe␈αfunction;␈αwhenever␈αwe␈αperform␈αan␈αoperation
␈↓ α←␈↓which␈α⊂destroys␈α⊂a␈α⊂register␈α⊂then␈α⊃we␈α⊂have␈α⊂to␈α⊂update␈α⊂the␈α⊃compiler's␈α⊂memory.
␈↓ α←␈↓Whenever␈αwe␈αneed␈αa␈αquantity,␈αwe␈αcheck␈αthe␈αmemory.␈αIf␈αthe␈αobject␈αis␈αalready
␈↓ α←␈↓in␈α
the␈α
␈↓αAC␈↓'s␈αthen␈α
we␈α
use␈αit.␈α
Clearly␈α
there␈αis␈α
a␈α
point␈αat␈α
which␈α
the␈αcomplexity␈α
of
␈↓ α←␈↓the␈α
object␈αstored␈α
is␈αtoo␈α
complicated␈αto␈α
be␈αworth␈α
remembering.␈α
However,␈αthe
␈↓ α←␈↓idea␈α↔can␈α↔be␈α↔used␈α↔quite␈α⊗profitably␈α↔for␈α↔variable␈α↔references␈α↔and␈α⊗simple
␈↓ α←␈↓computations.␈α∃This␈α∃idea␈α∃is␈α∃a␈α∀simple␈α∃form␈α∃of␈α∃␈↓↓common␈α∃sub␈α∀expression
␈↓ α←␈↓↓elimination␈↓.␈α∂For␈α⊂example,␈α∂assuming␈α⊂that␈α∂the␈α⊂compiler␈α∂knows␈α⊂␈↓αx␈↓␈α∂is␈α⊂in␈α∂␈↓αAC1␈↓,
␈↓ α←␈↓here's code for:
␈↓"∀␈↓ α←␈↓α␈↓ ¬\f[car[x];cdr[car[x]]]
␈↓" ␈↓ α←␈↓α␈↓ ¬←(CAR 1 1)
␈↓"β␈↓ α←␈↓α␈↓ ¬←(CDR 2 1)
␈↓"β␈↓ α←␈↓α␈↓ ¬←(CALL F)
␈↓"∀␈↓ α←␈↓This␈αidea␈αcan␈αbe␈αextended.␈αThere␈αis␈αnothing␈αsacred␈αabout␈αknowing␈αonly␈αthe
␈↓ α←␈↓contents␈α∩of␈α∩the␈α∩special␈α∩registers.␈α∩We␈α∩could␈α∩keep␈α∩a␈α∩history␈α∩of␈α∪the␈α∩partial
␈↓ α←␈↓computations␈αin␈αthe␈αstack.␈αThen␈αif␈αwe␈αneed␈αa␈αpartial␈αresult␈αwe␈αmight␈αfind␈αit
␈↓ α←␈↓already␈α⊂computed␈α∂in␈α⊂the␈α∂␈↓αAC␈↓s␈α⊂or␈α∂stored␈α⊂on␈α∂the␈α⊂stack.␈α∂We␈α⊂might␈α⊂also␈α∂keep
␈↓ α←␈↓track␈α
of␈αwhether␈α
stack␈αor␈α
␈↓αAC␈↓␈αcontents␈α
are␈αstill␈α
needed.␈α For␈α
example,␈α
in␈αour
␈↓ α←␈↓compiled␈αfunction␈α␈↓αj␈↓␈αon␈αpage 335␈αwe␈α
might␈αhave␈αnoticed␈αthat␈αafter␈αthe␈αcall␈α
on
␈↓ α←␈↓␈↓αg␈↓,␈α∩the␈α∩value␈α∩of␈α∩␈↓αx␈↓␈α∩was␈α∩no␈α∩longer␈α∩needed;␈α∩therefore␈α∩we␈α∩need␈α∩not␈α∩save␈α∩␈↓αx␈↓.
␈↓ α←␈↓Similarly␈α∞we␈α
don't␈α∞need␈α∞the␈α
value␈α∞of␈α∞␈↓αy␈↓␈α
after␈α∞the␈α
call␈α∞on␈α∞␈↓αh␈↓.␈α
If␈α∞we␈α∞build␈α
this
␈↓ α←␈↓kind␈α⊂of␈α⊂information␈α∂into␈α⊂a␈α⊂compiler,␈α⊂we␈α∂can␈α⊂generate␈α⊂more␈α⊂efficient␈α∂code.
␈↓ α←␈↓However,␈αmany␈αof␈αthese␈αideas␈αmust␈αbe␈αused␈αwith␈αsome␈αcare.␈α Side-effects␈αcan
␈↓ α←␈↓destroy the validity of partial results.
␈↓"β␈↓ α←␈↓␈↓ β'Notice␈α
that␈α
we␈α
are␈α
comparing␈α
the␈α␈↓¬symbolic␈↓␈α
values␈α
in␈α
the␈α
␈↓αAC␈↓'s␈α
or␈αstack;
␈↓ α←␈↓we␈α∂cannot␈α∂look␈α∂for␈α∞actual␈α∂values.␈α∂This␈α∂idea␈α∞of␈α∂symbolic␈α∂processing␈α∂can␈α∞be
␈↓ α←␈↓exploited␈α↔at␈α↔a␈α_much␈α↔more␈α↔sophisticated␈α_level␈α↔in␈α↔LISP␈α_compilers.␈α↔ In
␈↓ α←␈↓particular,␈α∃we␈α∃can␈α∃perform␈α∃program␈α∃transformations.␈α∃ For␈α∃example,␈α∃the
␈↓ α←␈↓compiler␈α
can␈αrewrite␈α
program␈αsegments␈α
taking␈αadvantage␈α
of␈αtransformations
␈↓ α←␈↓it␈α∪knows.␈α∪These␈α∀transformations␈α∪typically␈α∪involve␈α∀equivalence␈α∪preserving
␈↓ α←␈↓operations which might lead to more efficient compiled code.
␈↓"β␈↓ α←␈↓␈↓ β'For␈α⊗example␈α∃several␈α⊗LISP␈α∃compilers␈α⊗have␈α∃the␈α⊗ability␈α⊗to␈α∃perform
␈↓ α←␈↓recursion␈α∩removal,␈α∩replacing␈α∩recursive␈α∩programs␈α∩with␈α∪equivalent␈α∩iterative
␈↓ α←␈↓versions.␈↓π 28␈↓ Here's a case:
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 28␈↓All these transformations should be invisible to the user.
␈↓ α←␈↓␈↓6.16␈↓ πkFurther Optimizations 353␈↓
␈↓"β␈↓ α←␈↓α␈↓ βWrev <= λ[[x;y][null[x] → y; ␈↓
t␈↓α → rev[rest[x];concat[first[x];y]]]]
␈↓"∀␈↓ α←␈↓This program is automatically rewritten as:
␈↓" ␈↓ α←␈↓αrev <= λ[[x;y] prog[[]␈↓ ∧␈l␈↓ ¬↔[null[x] → return[y]];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬↔y ← concat[first[x];y];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬↔x ← rest[x];
␈↓"β␈↓ α←␈↓α␈↓ ∧␈␈↓ ¬↔go[l] ]]
␈↓"∀␈↓ α←␈↓This␈α∂second␈α∂version␈α∂makes␈α∞no␈α∂demands␈α∂on␈α∂the␈α∞run-time␈α∂stack;␈α∂it␈α∂does␈α∞not
␈↓ α←␈↓stack␈α∂its␈α∞partial␈α∂computation␈α∂like␈α∞the␈α∂recursive␈α∞version.␈α∂Each␈α∂recursive␈α∞call
␈↓ α←␈↓pushes␈αthe␈αvalues␈αfor␈α␈↓αx␈↓␈αand␈α
␈↓αy␈↓;␈αthe␈αiterative␈αversion␈αuses␈αtwo␈α
fixed␈αlocations.
␈↓ α←␈↓Typically the second version on ␈↓αrev␈↓ will execute faster.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α
major␈α
obstacle␈α
to␈α
most␈α
kinds␈α
of␈α
optimization␈α
is␈α
the␈α
unrestricted␈α
use
␈↓ α←␈↓of␈α∩labels␈α∩and␈α⊃␈↓αgo␈↓s.␈α∩Consider␈α∩a␈α⊃piece␈α∩of␈α∩compiler␈α⊃code␈α∩which␈α∩has␈α∩a␈α⊃label
␈↓ α←␈↓attached␈αto␈α
it.␈α Before␈α
we␈αcan␈α
be␈αassured␈α
of␈αthe␈α
integrity␈αof␈α
an␈α␈↓αAC␈↓␈α
we␈αmust
␈↓ α←␈↓ascertain␈αthat␈αevery␈αpossible␈αpath␈αto␈αthat␈αlabel␈αmaintains␈αthat␈α␈↓αAC␈↓.␈α This␈αis␈αa
␈↓ α←␈↓very␈αdifficult␈αtask.␈αThe␈αlabel␈αand␈αgoto␈αstructure␈αrequired␈αby␈α␈↓αcompile␈↓␈αis␈αquite
␈↓ α←␈↓simple.␈αHowever␈α
if␈αwe␈α
wished␈αto␈αbuild␈α
an␈αoptimizing␈α
compiler␈αfor␈αLISP␈α
with
␈↓ α←␈↓␈↓αprog␈↓s we would have to confront this problem.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Extend␈α∪the␈α∪compiling␈α∀algorithm␈α∪to␈α∪remember␈α∪what␈α∀it␈α∪has␈α∪in␈α∀its␈α∪␈↓αAC␈↓
␈↓ α←␈↓␈↓ β∂registers. How much of the scheme is dependent on lack of side-effects?
␈↓" ␈↓ α←␈↓2. Titled:␈α∂"␈α∂If␈α∂we␈α∂only␈α∂had␈α∂an␈α∂instruction...␈α∂"␈α∂We␈α∂advocate␈α⊂an␈α∂instruction,
␈↓ α←␈↓␈↓ β∂␈↓αEXCH␈α∂ac␈α⊂loc␈↓,␈α∂which␈α∂will␈α⊂exchange␈α∂the␈α⊂contents␈α∂of␈α∂the␈α⊂␈↓αac␈↓␈α∂and␈α⊂the␈α∂␈↓αloc␈↓.
␈↓ α←␈↓␈↓ β∂This␈α
instruction␈α
could␈α
be␈αused␈α
effectively␈α
on␈α
the␈αcode␈α
for␈α
␈↓αj␈↓␈α
on␈αpage 334
␈↓ α←␈↓␈↓ β∂to save a ␈↓αPUSH-POP␈↓ pair.
␈↓"β␈↓ α←␈↓␈↓ β∂Here's ␈↓αEXCH␈↓ in action, using the results of the previous exercise:
␈↓"∀␈↓ α←␈↓α␈↓ β∂((LAP J SUBR)␈↓ ¬w␈↓; says ␈↓αj␈↓ is a function␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂ (PUSH P 2)
␈↓"β␈↓ α←␈↓α␈↓ β∂ (CALL G)␈↓ ¬w␈↓; call the function named ␈↓αg
␈↓"β␈↓ α←␈↓α␈↓ β∂ (EXCH 1 0 P)␈↓ ¬w␈↓; save the value and dredge up ␈↓αy
␈↓"β␈↓ α←␈↓α␈↓ β∂ (CALL H)␈↓ ¬w␈↓; call ␈↓αh
␈↓"β␈↓ α←␈↓α␈↓ β∂ (MOVE 2 1)
␈↓"β␈↓ α←␈↓α␈↓ β∂ (POP P 1)␈↓ ¬w␈↓; preparation for␈↓α
␈↓"β␈↓ α←␈↓α␈↓ β∂ (CALL F)␈↓ ¬w␈↓; calling ␈↓αf
␈↓"β␈↓ α←␈↓α␈↓ β∂ (RET))␈↓ ¬w␈↓; exit.␈↓α AC1␈↓ still has the value from ␈↓αf.
␈↓"∀␈↓ α←␈↓␈↓ β∂Look␈α
for␈α
general␈α
situations␈α
where␈α
␈↓αEXCH␈↓␈α
can␈α
be␈α
used.␈α
Try␈α
to␈αnotice␈α
other
␈↓ α←␈↓␈↓ β∂areas of the compiler which would benefit form new instructions.
␈↓" ␈↓ α←␈↓3. Write code for the factorial function, and simulate the execution on ␈↓α2!␈↓.
␈↓" ␈↓ α←␈↓4. Write␈αa␈αLISP␈αfunction␈αto␈αtake␈αrecursive␈αschemes␈αinto␈αequivalent␈αiterative
␈↓ α←␈↓␈↓ β∂ones␈αin␈αthe␈αstyle␈αof␈αthe␈α␈↓αrev␈↓␈αexample␈αon␈αpage 353.␈αYour␈αfirst␈αversion␈αneed
␈↓ α←␈↓␈↓ β∂not␈α
be␈αas␈α
efficient␈α
as␈αthe␈α
one␈α
advertized␈αthere,␈α
but␈α
try␈αto␈α
make␈α
it␈αbetter␈α
as
␈↓ α←␈↓␈↓ β∂you proceed. See [Dar 73] for this and related transformations.
␈↓ α←␈↓␈↓354 Dynamic Structure␈↓
_6.17␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬≥␈↓↓6.17 Functional Arguments␈↓
␈↓"β␈↓ α←␈↓Function␈α
variables␈α
add␈α
more␈α
complication␈α
to␈α
the␈α
compiling␈α
algorithms.␈α We
␈↓ α←␈↓will␈αaddress␈αthe␈α
simpler␈αcases␈αof␈αfunctional␈α
arguments.␈α There␈αare␈αtwo␈α
issuses
␈↓ α←␈↓involved:␈αwhat␈αto␈αcompile␈αwhen␈αa␈α␈↓αfunction␈↓␈αconstruct␈αis␈αrecognized;␈αand␈αwhat
␈↓ α←␈↓to do when the function position of an application is a variable.
␈↓"β␈↓ α←␈↓Consider an example:
␈↓"∀␈↓ α←␈↓α␈↓ ¬Rfoo[ ...;function[␈↓εf␈↓α];...]␈↓
␈↓"∀␈↓ α←␈↓We␈αgenerate␈α
␈↓α(MOVEI␈α1␈α
␈↓εf␈↓α)␈↓␈αand␈αcompile␈α
␈↓εf␈↓␈αif␈α
it␈αis␈α
a␈αλ-definition;␈αotherwise␈α
we
␈↓ α←␈↓essentially generate ␈↓α(MOVEI 1 (QUOTE ␈↓εf␈↓α))␈↓.
␈↓"β␈↓ α←␈↓Assume ␈↓αfoo␈↓ is defined as:
␈↓"β␈↓ α←␈↓α␈↓ ¬⊂foo <= λ[[ ...;g; ...] ....g[␈↓↓t␈↓β1␈↓α; ...;␈↓↓t␈↓βn␈↓α]]
␈↓"∀␈↓ α←␈↓The␈α∂instance␈α∂of␈α∂␈↓αg␈↓␈α∂in␈α∂␈↓αg[␈↓↓t␈↓β1␈↓α;␈α⊂...;␈↓↓t␈↓βn␈↓α]]␈↓␈α∂is␈α∂a␈α∂special␈α∂case␈α∂of␈α∂a␈α⊂computed␈α∂function
␈↓ α←␈↓(page 158);␈α
in␈α∞this␈α
case,␈α∞the␈α
computation␈α
is␈α∞only␈α
a␈α∞variable␈α
lookup.␈α∞We␈α
will
␈↓ α←␈↓display the more general code for a computed function call of the form:
␈↓"∀␈↓ α←␈↓α␈↓ ελexp[␈↓↓t␈↓β1␈↓α; ...;␈↓↓t␈↓βn␈↓α]
␈↓"∀␈↓ α←␈↓α␈↓We get:␈↓α␈↓ ∧Cappend[␈↓ ¬;<compexp[exp;off;vpl];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬;list[mkalloc[1]];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬;<complis[(␈↓↓t␈↓β1␈↓α; ...;␈↓↓t␈↓βn␈↓α);off-1;vpl]>
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬;list[mkalloc[1]];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬;((CALLF n 0 P))
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬;((SUB P (C 1))]
␈↓"∀␈↓ α←␈↓The␈α⊃calling␈α⊃structure␈α⊃for␈α⊃a␈α⊃functional␈α⊃argument␈α⊃is␈α⊃slightly␈α⊃different.␈α⊂The
␈↓ α←␈↓arguments␈α∂are␈α∂on␈α⊂the␈α∂stack␈α∂but,␈α∂more␈α⊂importantly,␈α∂note␈α∂that␈α∂the␈α⊂call␈α∂must
␈↓ α←␈↓always␈αbe␈αtrapped␈αand␈αdecoded.␈α We␈α
cannot␈αreplace␈αthat␈αcall␈αwith␈αa␈α
␈↓αPUSHJ␈↓
␈↓ α←␈↓to␈α∃some␈α∃machine␈α∃language␈α∃code␈α∃for␈α∃the␈α∃function␈α∃because␈α⊗the␈α∃function
␈↓ α←␈↓referred␈α
to␈αcan␈α
change.␈α
We␈αuse␈α
a␈α
␈↓αCALLF␈↓␈αinstruction␈α
to␈α
designate␈αa␈α
call␈αon␈α
a
␈↓ α←␈↓functional␈α
argument.␈α
Since␈α
the␈α
value␈αof␈α
the␈α
expression␈α
may␈α
very␈αwell␈α
change
␈↓ α←␈↓during execution we can ␈↓¬never␈↓ replace the ␈↓αCALLF␈↓ with a ␈↓αPUSHJ␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Often,␈α∞unneeded␈α∞generality␈α∞allowed␈α∞by␈α∞the␈α∞functional␈α∞notation␈α∂can␈α∞be
␈↓ α←␈↓removed␈α∂by␈α∂the␈α∂compiler.␈α∞ Production␈α∂LISP␈α∂compilers,␈α∂like␈α∂the␈α∞MACLISP
␈↓ α←␈↓compiler,␈α↔produce␈α↔very␈α↔efficient␈α_code␈α↔for␈α↔many␈α↔uses␈α↔of␈α_the␈α↔mapping
␈↓ α←␈↓functions, like ␈↓αmaplist␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α~problems␈α~of␈α~compiling␈α~efficient␈α~code␈α~become␈α~magnified␈α~if
␈↓ α←␈↓generalized␈αcontrol␈αstructures␈αare␈αanticipated.␈αThe␈αproblem␈αis␈αsimilar␈α
to␈αthat
␈↓ α←␈↓of␈α
recognizing␈αan␈α
implied␈αloop␈α
construct␈α
in␈αa␈α
program␈αusing␈α
labels␈α
and␈α␈↓αgo␈↓'s
␈↓ α←␈↓to␈α∞control␈α∞the␈α∂algorithm.␈α∞Control␈α∞constructs␈α∂like␈α∞␈↓αcatch␈↓␈α∞and␈α∂␈↓αthrow␈↓␈α∞(page 198)
␈↓ α←␈↓have␈α_some␈α_advantages␈α_here;␈α↔rather␈α_than␈α_using␈α_evaluation␈α_relative␈α↔to
␈↓ α←␈↓arbitrary␈α⊃access␈α⊃and␈α⊃control␈α⊂environments␈α⊃([Bob 73a]),␈α⊃these␈α⊃constructs␈α⊂do
␈↓ α←␈↓impose some regularity which the compiler and the programmer can exploit.
␈↓ α←␈↓␈↓6.18␈↓ π>Macros and Special Forms 355␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬∧␈↓↓6.18 Macros and Special Forms␈↓
␈↓"β␈↓ α←␈↓We␈α⊃wish␈α⊃to␈α⊂extend␈α⊃our␈α⊃compiling␈α⊂algorithm␈α⊃to␈α⊃handle␈α⊃macro␈α⊂definitions.
␈↓ α←␈↓Consider␈α
the␈αexample␈α
of␈α
defining␈α␈↓αplus␈↓␈α
of␈α
an␈αindefinite␈α
number␈αof␈α
arguments
␈↓ α←␈↓given␈α∞on␈α∞page 156.␈α∞ A␈α∞compiler␈α∂can␈α∞make␈α∞execution␈α∞of␈α∞macros␈α∂much␈α∞more
␈↓ α←␈↓efficient␈α→than␈α~their␈α→special␈α→form␈α~counterpart.␈α→Macros␈α~usually␈α→involve
␈↓ α←␈↓transformations␈α⊂which␈α⊂can␈α∂be␈α⊂executed␈α⊂at␈α∂compile␈α⊂time,␈α⊂whereas␈α⊂a␈α∂special
␈↓ α←␈↓form␈α
may␈α
involve␈α∞run␈α
time␈α
information.␈α
For␈α∞example,␈α
consider␈α
the␈α∞case␈α
of
␈↓ α←␈↓the␈α∞macro␈α∞definiton␈α
of␈α∞␈↓αplus␈↓.␈α∞ When␈α
␈↓αplus␈↓␈α∞is␈α∞called␈α
we␈α∞know␈α∞the␈α∞number␈α
of
␈↓ α←␈↓arguments,␈αand␈αcan␈αsimply␈αexpand␈αthe␈αmacro␈αto␈αa␈αnest␈αof␈αcalls␈αon␈α␈↓α*plus␈↓.␈αFor
␈↓ α←␈↓example:
␈↓"∀␈↓ α←␈↓α␈↓ βTplus[x;add1[y];z] ␈↓expands to␈↓α *plus[x;*plus[add1[y];z]] ␈↓
␈↓"∀␈↓ α←␈↓The␈α∞second␈α∞expression␈α
may␈α∞be␈α∞compiled␈α∞into␈α
machine␈α∞code␈α∞which␈α∞uses␈α
the
␈↓ α←␈↓hardware arithmetic unit.
␈↓"β␈↓ α←␈↓␈↓ β'Macros␈α∪can␈α∀also␈α∪be␈α∪used␈α∀effectively␈α∪in␈α∪implementing␈α∀abstract␈α∪data
␈↓ α←␈↓structures␈αand␈αcontrol␈αstructures.␈αFor␈αexample,␈αthe␈αconstructors,␈αselectors,␈αand
␈↓ α←␈↓recognizers␈α↔which␈α↔help␈α↔characterize␈α⊗an␈α↔abstract␈α↔data␈α↔structure␈α↔can␈α⊗be
␈↓ α←␈↓expressed␈αas␈αvery␈αsimple␈αS-expr␈αoperations.␈α These␈αoperations␈αare␈αperformed
␈↓ α←␈↓quite␈αfrequently␈αso␈αany␈αimprovement␈αin␈αtheir␈αrunning␈αefficiency␈αwould␈αhave
␈↓ α←␈↓dramatic␈αimpact.␈α Recall␈αthat␈αon␈αpage 77␈αwe␈αdefined␈α␈↓αcoef␈↓␈αas␈α␈↓αcar␈↓.␈αFor␈αspeed␈α
of
␈↓ α←␈↓execution␈α
it␈αwould␈α
be␈αbetter␈α
to␈αuse␈α
␈↓αcar␈↓␈α
instead␈αof␈α
␈↓αcoef␈↓.␈α Compiled␈α
calls␈αon␈α
␈↓αcoef␈↓
␈↓ α←␈↓would␈α∂invoke␈α∞the␈α∂function-calling␈α∞mechanism,␈α∂whereas␈α∞many␈α∂compilers␈α∞can
␈↓ α←␈↓substitute␈α⊃actual␈α⊃hardware␈α⊃instructions␈α⊃for␈α⊃calls␈α⊃on␈α⊃␈↓αcar␈↓;␈α⊃the␈α⊃code␈α⊂executes
␈↓ α←␈↓faster␈α⊂and␈α⊃requires␈α⊂less␈α⊃space.␈α⊂ However,␈α⊃good␈α⊂programming␈α⊃style␈α⊂dictates
␈↓ α←␈↓that␈αwe␈αstay␈αabstract␈αand␈αuse␈α␈↓αcoef␈↓.␈αCompiled␈αmacros␈αcan␈αresolve␈αthis␈αtension,
␈↓ α←␈↓giving both abstraction and fast compiled code. Define:
␈↓"∀␈↓ α←␈↓␈↓ ¬∞␈↓αcoef <␈↓βm␈↓α= λ[[l] cons[CAR;cdr[l]]]␈↓
␈↓"∀␈↓ α←␈↓The␈α∩user␈α∩writes␈α∩␈↓α(COEF␈α∩...)␈↓;␈α∪the␈α∩evaluator␈α∩sees␈α∩␈↓α(COEF␈α∩...)␈↓␈α∪and␈α∩evaluates
␈↓ α←␈↓␈↓α(CAR ...)␈↓;␈αthe␈αcompiler␈αsees␈α
␈↓α(COEF ...)␈↓␈αand␈αcompiles␈αcode␈αfor␈α
␈↓α(CAR ...)␈↓.␈αWith
␈↓ α←␈↓macros,␈α∪we␈α∀can␈α∪get␈α∀the␈α∪efficient␈α∀code,␈α∪the␈α∀readibility,␈α∪and␈α∀flexibility␈α∪of
␈↓ α←␈↓representation.
␈↓"β␈↓ α←␈↓␈↓ β'Macros␈α∩can␈α∩also␈α∩be␈α∩used␈α∩to␈α∩perform␈α∩most␈α∩of␈α∩the␈α∩operations␈α⊃which
␈↓ α←␈↓special␈αforms␈αare␈αmeant␈α
to␈αdo.␈α Since␈α␈↓αeval␈↓␈α
handles␈αcalls␈αon␈αspecial␈α
forms,␈αwe
␈↓ α←␈↓should␈α∂examine␈α∂the␈α∂extensions␈α∂to␈α∂␈↓αcompile␈↓␈α∂to␈α∂generate␈α∂such␈α∂code.␈α⊂We␈α∂have
␈↓ α←␈↓seen␈αthat␈αin␈αcompiling␈αarguments␈αto␈α(normal)␈αfunctions,␈αwe␈αgenerate␈αthe␈αcode
␈↓ α←␈↓for␈α
each,␈α
followed␈α
by␈α
code␈α
to␈α
save␈α
the␈α
result␈α
in␈α
the␈α
run-time␈α
stack,␈α∞␈↓αP␈↓.␈α
The
␈↓ α←␈↓argument␈αto␈αa␈αspecial␈αform␈αis␈α␈↓¬unevaluated␈↓,␈αby␈αdefinition.␈αAll␈αwe␈αcan␈αthus␈αdo
␈↓ α←␈↓for␈α∞a␈α
call␈α∞of␈α∞the␈α
form␈α∞␈↓αf[l]␈↓,␈α∞where␈α
␈↓αf␈↓␈α∞is␈α
a␈α∞special␈α∞form,␈α
is␈α∞pass␈α∞the␈α
argument,
␈↓ α←␈↓compiling something like:
␈↓"∀␈↓ α←␈↓α␈↓ ¬8(MOVEI AC1 (␈↓
R␈↓∞(␈↓α l ␈↓∞)␈↓α))
␈↓"β␈↓ α←␈↓α␈↓ ¬o(CALL 1 (E F))
␈↓ α←␈↓␈↓356 Dynamic Structure␈↓
_6.18␈↓
␈↓"β␈↓ α←␈↓␈↓ β'We␈α
have␈α
already␈αmentioned␈α
some␈α
of␈α
the␈αdangers␈α
in␈α
using␈αspecial␈α
forms;
␈↓ α←␈↓the␈αfact␈αthat␈αa␈αcompiler␈αcannot␈αdo␈αmuch␈αwith␈αthem␈αeither,␈αmakes␈αthem␈αeven
␈↓ α←␈↓less attractive.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Extend the last ␈↓αcompile␈↓ function to handle macros.
␈↓" ␈↓ α←␈↓2. Define␈α
␈↓αand␈↓␈α
as␈α
a␈α
macro␈α
in␈α
terms␈α
of␈α
␈↓αcond␈↓.␈α
Compare␈α
the␈α
code␈α∞produced␈α
in
␈↓ α←␈↓␈↓ β∂the␈αtwo␈αcases.␈αHow␈αcould␈αyou␈αimprove␈αthe␈αcompiler␈αto␈αmake␈αthe␈αtwo␈αsets
␈↓ α←␈↓␈↓ β∂of code more nearly alike?
␈↓"β␈↓ α←␈↓␈↓ ∧z␈↓↓6.19 Compilation and Variables␈↓
␈↓"β␈↓ α←␈↓Our␈α⊃compilers␈α⊂translate␈α⊃the␈α⊂formal␈α⊃parameter␈α⊂list␈α⊃into␈α⊂a␈α⊃block␈α⊃of␈α⊂storage
␈↓ α←␈↓allocated␈α⊂in␈α⊃the␈α⊂stack,␈α⊂␈↓αP␈↓.␈α⊃ The␈α⊂body␈α⊂of␈α⊃the␈α⊂function␈α⊃definition␈α⊂references
␈↓ α←␈↓those␈αlocal␈αvariables␈αas␈αthe␈αcorresponding␈αstack␈αentries.␈α This␈αscheme␈α
suffices
␈↓ α←␈↓only␈αfor␈αlambda␈αor␈α␈↓αprog␈↓␈αvariables␈αwhich␈αare␈αused␈αin␈αa␈αstrictly␈α
local␈αfashion.
␈↓ α←␈↓We␈αhave␈α
said␈αthat␈αλ-expressions␈α
may␈αrefer␈αto␈α
global␈αor␈αfree␈α
variables.␈α The
␈↓ α←␈↓lookup␈α⊃mechanism␈α⊂finds␈α⊃the␈α⊂latest␈α⊃active␈α⊂binding␈α⊃of␈α⊂that␈α⊃variable␈α⊃in␈α⊂the
␈↓ α←␈↓current␈αsymbol␈α
table;␈αthis␈α
is␈αthe␈α
dynamic␈αbinding␈α
strategy.␈α Care␈α
is␈αrequired
␈↓ α←␈↓in␈α∩extending␈α∩the␈α∩compiling␈α∩algorithms␈α∩to␈α∩handle␈α∩dynamic␈α∩binding.␈α∩ The
␈↓ α←␈↓problem␈αinvolves␈αreference␈αto␈αvariables␈αwhich␈αare␈αcurrently␈αλ-bound␈αbut␈αare
␈↓ α←␈↓non-local. Such variables are called ␈↓↓special variables␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Assume␈α
that␈α
we␈α
are␈α
implementing␈α
a␈α
deep␈α
binding␈α
algorithm.␈α
If␈α
all␈αwe
␈↓ α←␈↓store␈α∞on␈α∞the␈α∂stack␈α∞is␈α∞the␈α∂value␈α∞of␈α∞a␈α∂variable,␈α∞then␈α∞another␈α∂program␈α∞which
␈↓ α←␈↓expects␈αto␈α
use␈αthat␈α
value␈αwill␈αhave␈α
no␈αway␈α
of␈αfinding␈α
that␈αstored␈αvalue.␈α
One
␈↓ α←␈↓scheme␈αis␈α
to␈αstore␈α
pairs␈αon␈α
the␈αstack:␈α
name␈αand␈α
value;␈αthen␈α
we␈αcan␈αsearch␈α
the
␈↓ α←␈↓stack␈α∪for␈α∀the␈α∪latest␈α∀binding.␈α∪ This␈α∀scheme␈α∪is␈α∀compatible␈α∪with␈α∀the␈α∪stack
␈↓ α←␈↓implementation␈α
of␈α
deep␈αbinding␈α
given␈α
in␈αSection 5.18.␈α
The␈α
compiler␈αcan␈α
still
␈↓ α←␈↓"know"␈αwhere␈αall␈αthe␈αlocal␈αvariables␈αare␈αon␈αthe␈αstack␈αand␈αcan␈αbe␈αa␈αbit␈αclever
␈↓ α←␈↓about searching for the globals or special variables.
␈↓"β␈↓ α←␈↓␈↓ β'Shallow␈α
binding␈αimplementations␈α
offer␈α
an␈αalternative.␈α
We␈α
can␈αstill␈α
store
␈↓ α←␈↓variables␈αon␈αthe␈αstack␈↓π 29␈↓␈αif␈αwe␈αare␈αsure␈αthat␈αthe␈αvariable␈αis␈αused␈αin␈αa␈αstrictly
␈↓ α←␈↓local␈α⊃fashion.␈α∩If␈α⊃a␈α∩variable␈α⊃is␈α∩to␈α⊃be␈α⊃used␈α∩as␈α⊃a␈α∩special␈α⊃variable␈α∩then␈α⊃the
␈↓ α←␈↓compiled␈α∂code␈α∂should␈α∞access␈α∂the␈α∂value␈α∂cell␈α∞of␈α∂that␈α∂variable.␈α∂ The␈α∞compiler
␈↓ α←␈↓recognizes␈αa␈αvariable␈αas␈αspecial␈αby␈αlooking␈αfor␈αthe␈αproperty␈αname␈α␈↓αSPECIAL␈↓
␈↓ α←␈↓on␈αthe␈αproperty␈αlist␈αof␈αthe␈αatom;␈αif␈αthe␈αproperty␈αexists␈αand␈αits␈αvalue␈αis␈α␈↓
t␈↓␈αthen
␈↓ α←␈↓the␈α
variable␈α
is␈α
a␈α
special␈α
variable␈α
and␈α
the␈α
compiler␈α
generates␈α∞different␈α
code.
␈↓ α←␈↓When␈αa␈αvariable,␈αsay␈α␈↓αx␈↓,␈αis␈αdeclared␈αspecial␈αthe␈αcompiler␈αwill␈αemit␈αa␈αreference
␈↓ α←␈↓to␈α∪␈↓αx␈↓␈α∀as␈α∪␈↓α(GETV AC␈↓βi␈↓α X)␈↓␈α∪or␈α∀␈↓α(PUTV AC␈↓βi␈↓α X)␈↓␈α∪rather␈α∪than␈α∀the␈α∪corresponding
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 29␈↓We␈α
assume␈α
throughout␈α
this␈α
discussion␈α
that␈α
we␈α
are␈α
compiling␈α
code␈α
for
␈↓ α←␈↓the stack implementation of shallow binding as given in Section 5.19.
␈↓ α←␈↓␈↓6.19␈↓ π/Compilation and Variables 357␈↓
␈↓"β␈↓ α←␈↓reference␈α∂to␈α∂a␈α∂location␈α∞on␈α∂the␈α∂stack.␈α∂ ␈↓αGETV␈↓␈α∞and␈α∂␈↓αPUTV␈↓␈α∂are␈α∂instructions␈α∞to
␈↓ α←␈↓access or modify the contents of the value cell.
␈↓"β␈↓ α←␈↓␈↓ β'When␈α∞the␈α∞LISP␈α∞assembler␈α∞sees␈α
one␈α∞of␈α∞these␈α∞instructions,␈α∞it␈α∞will␈α
locate
␈↓ α←␈↓the␈α∪value cell␈α∪of␈α∪atom␈α∪and␈α∪assemble␈α∩a␈α∪reference␈α∪to␈α∪that␈α∪cell.␈α∪ Since␈α∩the
␈↓ α←␈↓location␈α∂of␈α∂the␈α∂value cell␈α∂does␈α∂not␈α∂change,␈α∂we␈α∂can␈α∂always␈α∂find␈α∂the␈α∂current
␈↓ α←␈↓binding.␈αAny␈αinterpreted␈αfunction␈αcan␈αalso␈αsample␈αthe␈αvalue cell␈αso␈αnon-local
␈↓ α←␈↓values can be passed between compiled and interpreted functions.
␈↓"β␈↓ α←␈↓␈↓ β'Assume␈αa␈αfunction␈α␈↓αf␈↓␈αcalls␈αa␈αfunction␈α␈↓αg␈↓,␈αand␈αassume␈αthat␈α␈↓αg␈↓␈αuses␈αsome␈αof
␈↓ α←␈↓␈↓αf␈↓'s␈α
λ-variables.␈α∞ The␈α
usual␈α∞compilation␈α
for␈α∞␈↓αf␈↓␈α
would␈α∞place␈α
the␈α∞λ-variables␈α
in
␈↓ α←␈↓the␈αstack␈αand␈αthey␈αwould␈αnot␈αbe␈αaccessible␈αto␈α␈↓αg␈↓.␈αOur␈αcompiler␈αmust␈αtherefore
␈↓ α←␈↓be␈α↔modified␈α↔to␈α↔generate␈α_different␈α↔prolog␈α↔and␈α↔epilog␈α↔code␈α_for␈α↔special
␈↓ α←␈↓variables.␈α The␈αcode␈αmust␈αsave␈αthe␈αold␈αcontents␈αof␈αeach␈αspecial␈αvalue cell␈αon
␈↓ α←␈↓entry␈α∞to␈α∞the␈α∞function,␈α∞and␈α∞the␈α∞compiler␈α∞must␈α∞generate␈α∞code␈α∞to␈α∞restore␈α
those
␈↓ α←␈↓cells␈α∪at␈α∪function␈α∪exit.␈α∪ Any␈α∪references␈α∀in␈α∪either␈α∪␈↓αf␈↓␈α∪or␈α∪␈↓αg␈↓␈α∪to␈α∀those␈α∪special
␈↓ α←␈↓variables␈α
will␈α
involve␈α
␈↓αGETV-PUTV␈↓␈α
rather␈α
than␈α
references␈α
into␈α
the␈α
stack␈α
␈↓αP␈↓.
␈↓ α←␈↓In this scheme, ␈↓αlookup[x;env]␈↓ is given by ␈↓αgetv[x]␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Non-local␈α↔variables␈α↔cause␈α_several␈α↔problems␈α↔in␈α↔LISP.␈α_The␈α↔simple
␈↓ α←␈↓mechanism␈α∂we␈α∂used␈α∞for␈α∂referencing␈α∂local␈α∞variables␈α∂is␈α∂no␈α∂longer␈α∞applicable.
␈↓ α←␈↓Other␈α
programming␈α
languages␈α
allow␈α
the␈α
use␈α
of␈α
non-local␈α
variables,␈αsome,␈α
like
␈↓ α←␈↓APL␈α∪([Ive 62]),␈α∪only␈α∪allow␈α∪global␈α∪variables;␈α∪others,␈α∪like␈α∪Algol60␈α∪and␈α∩its
␈↓ α←␈↓successors␈α∩([Alg 63]␈α∩and␈α⊃[Alg 75]),␈α∩allow␈α∩free␈α⊃as␈α∩well␈α∩as␈α∩global␈α⊃variables.
␈↓ α←␈↓However,␈α→Algol␈α→compilers␈α→are␈α→much␈α→simpler␈α→to␈α→construct␈α→than␈α→LISP
␈↓ α←␈↓compilers,␈α∞and␈α
we␈α∞should␈α∞explore␈α
some␈α∞of␈α∞the␈α
reasons.␈↓π 30␈↓␈α∞One␈α∞simplicity␈α
of
␈↓ α←␈↓Algol␈α
is␈α∞its␈α
treatment␈α∞of␈α
procedure␈α
valued␈α∞variables.␈α
Algol␈α∞dialects␈α
typically
␈↓ α←␈↓restrict␈αthemselves␈αto␈αwhat␈αLISP␈αcalls␈αfunctional␈αarguments.␈αAlgol␈αdialects␈αdo
␈↓ α←␈↓not␈α∂allow␈α∞arbitrary␈α∂procedures␈α∞to␈α∂be␈α∞returned␈α∂as␈α∞values.␈α∂ Their␈α∞restrictions
␈↓ α←␈↓allow␈α∞the␈α∞run␈α∞time␈α∂environment␈α∞to␈α∞be␈α∞modelled␈α∂in␈α∞a␈α∞stack,␈α∞as␈α∂described␈α∞in
␈↓ α←␈↓Section 5.18.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∂difference␈α∂between␈α∂LISP␈α∂and␈α∂Algol,␈α∂which␈α∂is␈α∂more␈α∂to␈α∂the␈α∂point
␈↓ α←␈↓here,␈α∞is␈α∞their␈α∞binding␈α
strategies (Section 3.11).␈α∞A␈α∞typical␈α∞LISP␈α∞uses␈α
dynamic
␈↓ α←␈↓binding␈α∪whereas␈α∪Algol␈α∩uses␈α∪static␈α∪binding.␈α∩The␈α∪difference␈α∪is␈α∪that␈α∩Algol
␈↓ α←␈↓translators␈α
determine␈α∞the␈α
bindings␈α
of␈α∞variables␈α
at␈α
the␈α∞time␈α
the␈α∞definition␈α
is
␈↓ α←␈↓made␈αwhereas␈αLISP␈αdetermines␈αbindings␈αat␈αthe␈αtime␈αthe␈αfunction␈αis␈αapplied.
␈↓ α←␈↓That␈α
is,␈αdefinitions␈α
in␈αAlgol␈α
always␈αimply␈α
an␈αapplication␈α
of␈α␈↓αfunction␈↓,␈α
binding
␈↓ α←␈↓up␈α∂all␈α∂non-local␈α∂variables.␈α∞The␈α∂net␈α∂effect␈α∂is␈α∞that␈α∂Algol␈α∂does␈α∂not␈α∂have␈α∞free
␈↓ α←␈↓variables␈α
in␈α
the␈α
sense␈α
that␈α
there␈α∞is␈α
any␈α
choice␈α
of␈α
bindings;␈α
all␈α∞choices␈α
have
␈↓ α←␈↓been␈α⊗made␈α⊗when␈α⊗a␈α⊗procedure␈α⊗is␈α⊗declared.␈α⊗That␈α⊗binding␈α↔decision␈α⊗has
␈↓ α←␈↓dramatic␈α∩results␈α∪when␈α∩we␈α∪come␈α∩to␈α∩implement␈α∪language␈α∩translators.␈α∪As␈α∩a
␈↓ α←␈↓result,␈α∂Algol␈α∞can␈α∂effectively␈α∞compile␈α∂all␈α∞variable␈α∂references␈α∞to␈α∂be␈α∞references
␈↓ α←␈↓into␈αthe␈αrun␈αtime␈αstack,␈αand␈αneed␈αnot␈αretain␈αthe␈αname␈αstack␈αfor␈αvariable␈α
look
␈↓ α←␈↓up␈α
at␈α
run␈α
time.␈α It␈α
is␈α
not␈α
at␈α
all␈αclear␈α
yet␈α
which␈α
binding␈α
strategy␈αwill␈α
dominate.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 30␈↓For␈αreasons␈αother␈αthan␈αthose␈αwe␈αare␈αaddressing␈αhere,␈αAPL␈αcompilers
␈↓ α←␈↓are difficult to construct.
␈↓ α←␈↓␈↓358 Dynamic Structure␈↓
_6.19␈↓
␈↓"β␈↓ α←␈↓Counterexamples␈α∃to␈α∃exclusive␈α∃use␈α∃of␈α∃either␈α∃strategy␈α∃exist.␈α⊗Recent␈α∃work
␈↓ α←␈↓([Ste 76b],␈α
[Sus 75],␈α[Hew 76])␈α
points␈α
to␈αthe␈α
use␈α
of␈αstatic␈α
binding␈αin␈α
LISP-like
␈↓ α←␈↓languages.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α≤typical␈α≤preserve␈α≤of␈α≤dynamic␈α≤binding␈α≤is␈α≤that␈α≥of␈α≤interactive
␈↓ α←␈↓programming,␈α→where␈α→programs␈α→are␈α_created␈α→as␈α→separate␈α→entities␈α→to␈α_be
␈↓ α←␈↓connected␈αinto␈αa␈αcohesive␈αwhole␈αat␈αsome␈αlater␈αtime.␈αFrequently␈αone␈αwants␈αthe
␈↓ α←␈↓bindings of the free variables to be postponed until such a grouping is made.
␈↓"β␈↓ α←␈↓␈↓ ∧v␈↓↓6.20 Compiling and Interpreting␈↓
␈↓"β␈↓ α←␈↓We␈αhave␈αdiscussed␈αthe␈αsimilarities␈αbetween␈αcompilers␈αand␈αinterpreters.␈α Now
␈↓ α←␈↓that␈α∃we␈α∃have␈α∃seen␈α∃compilers␈α∃in␈α∃some␈α∃detail␈α∃we␈α∃should␈α⊗reexamine␈α∃the
␈↓ α←␈↓relationships.␈α∃ The␈α∃compilation␈α∀of␈α∃conditional␈α∃expressions␈α∃introduces␈α∀an
␈↓ α←␈↓interesting␈α⊃dichotomy␈α⊃between␈α⊃the␈α⊃action␈α⊃of␈α⊃an␈α⊃interpreter␈α⊃and␈α⊃that␈α⊃of␈α⊂a
␈↓ α←␈↓typical compiler.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α→will␈α→restrict␈α→ourselves␈α→to␈α→a␈α→simple␈α→form␈α→of␈α→the␈α→conditional
␈↓ α←␈↓expression: ␈α␈↓αif[p;then;otherwise]␈↓, ␈αwhere␈α␈↓αp␈↓␈αis␈αa␈αan␈αexpression␈αgiving␈α␈↓
t␈↓␈αor␈α␈↓
f␈↓;
␈↓ α←␈↓␈↓αthen␈↓␈αis␈αthe␈αexpression␈α
to␈αbe␈αevaluated␈αif␈α
␈↓αp␈↓␈αgives␈α␈↓
t␈↓;␈αotherwise␈α
the␈αexpression,
␈↓ α←␈↓␈↓αotherwise␈↓,␈α∀is␈α∀to␈α∀be␈α∀evaluated.␈α∀It␈α∪is␈α∀an␈α∀easy␈α∀exercise␈α∀to␈α∀express␈α∀a␈α∪LISP
␈↓ α←␈↓conditional in terms of ␈↓αif␈↓ expressions.
␈↓"β␈↓ α←␈↓␈↓ β'When␈αan␈αinterpreter␈αevaluates␈αa␈αconditional␈αexpression␈αor␈αan␈α␈↓αif␈↓,␈αit␈αwill
␈↓ α←␈↓evaluate␈α∞either␈α
␈↓αthen␈↓␈α∞or␈α∞␈↓αotherwise␈↓;␈α
not␈α∞both.␈α∞When␈α
a␈α∞compiler␈α∞compiles␈α
code
␈↓ α←␈↓for␈α⊂an␈α∂␈↓αif␈↓␈α⊂expression,␈α∂it␈α⊂compiles␈α∂␈↓¬both␈↓␈α⊂branches.␈α∂ Certainly,␈α⊂we␈α⊂cannot␈α∂only
␈↓ α←␈↓compile␈α
␈↓¬one␈↓␈α
branch␈α
of␈α
the␈α
␈↓αif␈↓;␈α
we␈α
expect␈α
different␈α
input␈α
values␈α
to␈αuse␈α
different
␈↓ α←␈↓branches,␈α⊃otherwise␈α⊂the␈α⊃conditional␈α⊃expression␈α⊂should␈α⊃not␈α⊃have␈α⊂appeared.
␈↓ α←␈↓For␈αexample,␈αif␈αa␈αparticular␈αevaluation␈α␈↓¬never␈↓␈αtakes␈αthe␈α␈↓αotherwise␈↓␈αbranch␈αof␈αa
␈↓ α←␈↓conditional,␈↓π 31␈↓␈α⊃then␈α⊂we␈α⊃need␈α⊃not␈α⊂compile␈α⊃code␈α⊂for␈α⊃that␈α⊃branch;␈α⊂compiling
␈↓ α←␈↓code␈α
for␈α
program␈α
segments␈α
which␈α
are␈α
not␈α
executed␈α
is␈α
disconcerting.␈α
At␈αa␈α
later
␈↓ α←␈↓date,␈α
a␈αdifferent␈α
evaluation␈α
might␈αtake␈α
the␈αother␈α
branch,␈α
and␈αat␈α
that␈αtime,␈α
we
␈↓ α←␈↓should be able to compile the branch.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∞will␈α∞show␈α∞that␈α∞it␈α∞is␈α∞possible␈α∞to␈α∞interpret␈α∞␈↓¬and␈↓␈α∞compile␈α∞at␈α∂the␈α∞same
␈↓ α←␈↓time.␈↓π 32␈↓␈α∃The␈α∀relevant␈α∃observation␈α∃is␈α∀that␈α∃large␈α∀parts␈α∃of␈α∃compiling␈α∀and
␈↓ α←␈↓interpreting␈α∀algorithms␈α∀are␈α∪identical;␈α∀they␈α∀deal␈α∪with␈α∀decoding␈α∀the␈α∪input
␈↓ α←␈↓expression␈α∞and␈α∞understanding␈α∂which␈α∞constructs␈α∞are␈α∂present.␈α∞It␈α∞is␈α∂only␈α∞after
␈↓ α←␈↓the␈α
interpreter␈α
or␈αcompiler␈α
has␈α
discovered␈α
the␈αnature␈α
of␈α
the␈α
expression␈αthat
␈↓ α←␈↓the specifics of compilation or interpretation come into play.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αwill␈αbuild␈αan␈αevaluator/compiler␈αnamed␈α␈↓αevcom␈↓␈αbased␈αon␈αthe␈α
explicit
␈↓ α←␈↓access␈αevaluator␈αof␈αSection 4.6.␈α It␈αwill␈αhandle␈αcompilation␈αand␈αinterpretation
␈↓ α←␈↓of␈α≤applicative␈α≠forms␈α≤involving␈α≠either␈α≤primitive␈α≠functions␈α≤or␈α≠named
␈↓ α←␈↓λ-definitions;␈α∩it␈α∩will␈α∩recognize␈α∩the␈α∩difference␈α∩between␈α∩local␈α∪and␈α∩non-local
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 31␈↓That does ␈↓¬not␈↓ imply that the ␈↓αotherwise␈↓-branch will ␈↓¬never␈↓ be visited.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 32␈↓See [Mit 70] for a similar idea applied to a different language.
␈↓ α←␈↓␈↓6.20␈↓ π*Compiling and Interpreting 359␈↓
␈↓"β␈↓ α←␈↓variable␈α∞references,␈α∂compiling␈α∞(and␈α∂executing)␈α∞calls␈α∂on␈α∞␈↓αlookup␈↓␈α∂for␈α∞non-local
␈↓ α←␈↓references,␈αand␈αuse␈αa␈α
faster␈αrelative␈αaddressing␈αtechnique␈αfor␈α
local␈αvariables.
␈↓ α←␈↓Finally␈α~it␈α≠will␈α~"incrementally␈α~compile"␈α≠␈↓αif␈↓␈α~expressions,␈α≠executing␈α~(and
␈↓ α←␈↓generating␈α
code␈α∞for)␈α
the␈α
branch␈α∞designated␈α
by␈α
the␈α∞predicate;␈α
and␈α∞will␈α
leave
␈↓ α←␈↓sufficient␈α⊗information␈α⊗available␈α∃such␈α⊗that␈α⊗if␈α∃the␈α⊗other␈α⊗branch␈α⊗is␈α∃ever
␈↓ α←␈↓executed, then code is compiled for it.
␈↓"β␈↓ α←␈↓␈↓ β'Before␈α⊗sketching␈α⊗␈↓αevcom␈↓,␈α⊗one␈α⊗other␈α⊗implementation␈α⊗detail␈α⊗is␈α⊗worth
␈↓ α←␈↓mentioning.␈α⊃ We␈α⊃cannot␈α⊃simply␈α∩replace␈α⊃a␈α⊃LISP␈α⊃expression␈α∩with␈α⊃compiled
␈↓ α←␈↓code;␈α
LISP␈αexpressions␈α
are␈α
data␈αstructures␈α
and␈αwe␈α
must␈α
be␈αable␈α
to␈αoperate␈α
on
␈↓ α←␈↓that␈α
data␈α
structure␈α
representation␈αwithout␈α
being␈α
aware␈α
that␈α
a␈αcompilation␈α
has
␈↓ α←␈↓occurred.␈α∃ For␈α∃example␈α∃the␈α∃LISP␈α∀editor␈α∃(Section 6.22)␈α∃must␈α∃be␈α∃able␈α∀to
␈↓ α←␈↓manipulate␈α⊃the␈α⊃S-expr␈α⊃representation.␈α⊃ So␈α⊃rather␈α⊃than␈α⊃replace␈α⊂expressions
␈↓ α←␈↓with␈αcode␈αwe␈α
␈↓¬associate␈↓␈αthe␈αcode␈αwith␈α
the␈αexpression␈αusing␈αan␈α
association␈αlist
␈↓ α←␈↓whose␈α↔name-entries␈α↔are␈α↔LISP␈α↔expressions␈α↔and␈α↔whose␈α↔value-entries␈α↔are
␈↓ α←␈↓sequences␈αof␈αinstructions.␈↓π 33␈↓␈αThe␈αvariable␈α␈↓αcode␈↓␈αis␈αused␈αto␈αhold␈αthe␈αassociation
␈↓ α←␈↓list or code buffer.
␈↓"β␈↓ α←␈↓␈↓ β'Finally␈α⊂here␈α⊂is␈α⊂the␈α∂sketch.␈α⊂We␈α⊂have␈α⊂left␈α∂out␈α⊂many␈α⊂of␈α⊂the␈α∂subsidiary
␈↓ α←␈↓functions␈α
and␈α
have␈α∞left␈α
out␈α
all␈α
of␈α∞the␈α
execution␈α
mechanism␈α
involved␈α∞in␈α
␈↓αxct␈↓
␈↓ α←␈↓and␈α⊂␈↓αexecute␈↓;␈α⊂␈↓αxct␈↓␈α⊂executes␈α⊂a␈α⊂single␈α⊂instruction␈α⊂and␈α⊂␈↓αexecute␈↓␈α⊂is␈α⊂the␈α⊂combined
␈↓ α←␈↓assembler and execution device.
␈↓"∀␈↓ α←␈↓αevcom <=
␈↓"β␈↓ α←␈↓α␈↓ αwλ[␈↓ β∂[exp]
␈↓"β␈↓ α←␈↓α␈↓ αw␈↓ β∂prog[␈↓ βW[z]
␈↓"β␈↓ α←␈↓α␈↓ αw␈↓ β∂␈↓ βWreturn[␈↓ ∧7[z ← hascode[exp] → execute[z];
␈↓"β␈↓ α←␈↓α␈↓ αw␈↓ β∂␈↓ βW␈↓ ∧7 isconst[exp] → xct1[list[mksend[exp]]];
␈↓"β␈↓ α←␈↓α␈↓ αw␈↓ β∂␈↓ βW␈↓ ∧7 z ← isvar[exp] →␈↓ ε≠[islocal[z] → xct1[send_code[list[mklocal[z]]];
␈↓"β␈↓ α←␈↓α␈↓ αw␈↓ β∂␈↓ βW␈↓ ∧7␈↓ ε≠ isfun[z] → send_code[evcom1[def[z];()]];
␈↓"β␈↓ α←␈↓α␈↓ αw␈↓ β∂␈↓ βW␈↓ ∧7␈↓ ε≠ issubr[z] → send_code[list[mkpushj[z]]];
␈↓"β␈↓ α←␈↓α␈↓ αw␈↓ β∂␈↓ βW␈↓ ∧7␈↓ ε≠ ␈↓
t␈↓α → xct1[send_code[list[mkglob[z]]]]];
␈↓"β␈↓ α←␈↓α␈↓ αw␈↓ β∂␈↓ βW␈↓ ∧7 isif[exp] → send_code[evif[exp]];
␈↓"β␈↓ α←␈↓α␈↓ αw␈↓ β∂␈↓ βW␈↓ ∧7 ␈↓
t␈↓α → send_code[mkcode[␈↓ εcxct1[list[mkalloc[vars[fun[exp]]]]];
␈↓"β␈↓ α←␈↓α␈↓ αw␈↓ β∂␈↓ βW␈↓ ∧7␈↓ ε≠␈↓ εcevcomlist[args[exp]];
␈↓"β␈↓ α←␈↓α␈↓ αw␈↓ β∂␈↓ βW␈↓ ∧7␈↓ ε≠␈↓ εcxct1[list[mkcall[fun[exp]]]]] ]]] ]]
␈↓" ␈↓ α←␈↓αevcom1 <= λ[[exp;code] evcom[exp]]
␈↓" ␈↓ α←␈↓αxct1 <= λ[[x] xct[x]; x]
␈↓"∀␈↓ α←␈↓Here's␈α∞the␈α∞essence␈α∞of␈α∞␈↓αevcom␈↓:␈α∞if␈α∂the␈α∞expression␈α∞has␈α∞code␈α∞in␈α∞the␈α∂current␈α∞code
␈↓ α←␈↓buffer,␈α∞then␈α∞we␈α
execute␈α∞it.␈α∞A␈α
constant␈α∞is␈α∞executed␈α
and␈α∞produces␈α∞code,␈α
since
␈↓ α←␈↓that␈αconstant␈αmay␈αbe␈αa␈αsubexpression␈αof␈αa␈αlarger␈αexpression␈αbeing␈αcompiled;
␈↓ α←␈↓we␈αdo␈αnot␈αsave␈αthe␈αconstant␈αcode␈αin␈αthe␈αcode␈αbuffer.␈αTwo␈αtypes␈αof␈αvariables
␈↓ α←␈↓are␈α
recognized:␈α
a␈α
local␈α
variable␈α
is␈α
recognized␈α
by␈α
its␈α
presence␈α
in␈α
the␈αlocal␈α
table;
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 33␈↓In␈α∀actual␈α∃practice,␈α∀such␈α∀a␈α∃representation␈α∀would␈α∃be␈α∀prohibitively
␈↓ α←␈↓expensive and we would substitute a hash array technique; see Section 7.14.
␈↓ α←␈↓␈↓360 Dynamic Structure␈↓
_6.20␈↓
␈↓"β␈↓ α←␈↓a␈α⊃relative␈α⊃address␈α⊃reference␈α⊃can␈α⊃be␈α⊂given␈α⊃for␈α⊃that␈α⊃entry.␈α⊃ If␈α⊃the␈α⊂variable
␈↓ α←␈↓reference␈α
is␈α
non-local,␈α
then␈α
we␈α
compile␈αa␈α
version␈α
of␈α
␈↓αlookup␈↓;␈α
the␈α
actual␈αform␈α
of
␈↓ α←␈↓that␈α∩code␈α∩will␈α∩depend␈α∩on␈α∩the␈α∩binding␈α∩implementation␈α∩(shallow␈α∪or␈α∩deep).
␈↓ α←␈↓Either type of variable reference saves the code using ␈↓αsend_code␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'If␈α
the␈α
variable␈α
references␈α
a␈α∞function␈α
name,␈α
then␈α
we␈α
must␈α∞compile␈α
and
␈↓ α←␈↓execute␈αcode␈αfor␈α
that␈αfunction␈αdefinition.␈↓π 34␈↓␈αWe␈α
use␈αthe␈αfunction␈α␈↓αevcom1␈↓␈α
since
␈↓ α←␈↓the␈α⊂code␈α⊂buffer,␈α⊂␈↓αcode␈↓,␈α⊂must␈α⊂be␈α⊂re-initialized␈α⊂within␈α⊂the␈α⊂compilation␈α⊂of␈α∂the
␈↓ α←␈↓function␈αbody␈αsince␈αcode␈αin␈αthe␈αouter␈αenvironment␈αwon't␈αbe␈αvalid␈αwithin␈αthe
␈↓ α←␈↓function␈α⊃body.␈α⊃ Finally,␈α⊃the␈α⊃variable␈α⊃might␈α⊃be␈α⊃a␈α⊃reference␈α⊃to␈α⊃a␈α⊃primitive
␈↓ α←␈↓function,␈α⊗in␈α↔which␈α⊗case␈α↔we␈α⊗just␈α⊗return␈α↔the␈α⊗call␈α↔and␈α⊗let␈α↔the␈α⊗function
␈↓ α←␈↓application execute it.
␈↓"β␈↓ α←␈↓␈↓ β'If␈α⊂the␈α⊂expression␈α∂is␈α⊂an␈α⊂application,␈α⊂we␈α∂generate␈α⊂and␈α⊂execute␈α⊂code␈α∂to
␈↓ α←␈↓allocate␈α∃space,␈α∀compile␈α∃and␈α∃execute␈α∀the␈α∃argument␈α∀list,␈α∃and␈α∃if␈α∀necessary
␈↓ α←␈↓compile, but always execute, the function call.
␈↓"∀␈↓ α←␈↓αhascode <= λ[[exp] prog[[z]
␈↓"β␈↓ α←␈↓α␈↓ ¬↔return[␈↓ ¬w[z ← findcode[exp] → cdr[z];
␈↓"β␈↓ α←␈↓α␈↓ ¬↔␈↓ ¬w ␈↓
t␈↓α → ␈↓
f␈↓]]]]
␈↓" ␈↓ α←␈↓αevcomlist <= λ[[l]␈↓ ∧C[null[l] → ();
␈↓"β␈↓ α←␈↓α␈↓ ∧C null[rest[l]] → evcom[first[l]];
␈↓"β␈↓ α←␈↓α␈↓ ∧C ␈↓
t␈↓α → mkcode[␈↓ ¬←evcom[first[l]];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬←xct1[((NEXT))];
␈↓"β␈↓ α←␈↓α␈↓ ∧C␈↓ ¬←evcomlist[rest[l]]]]]
␈↓" ␈↓ α←␈↓The␈α∃compilation␈α∃and␈α∃execution␈α⊗of␈α∃␈↓αif␈↓␈α∃expressions␈α∃is␈α⊗interesting.␈α∃ When
␈↓ α←␈↓compiling␈α
the␈αfirst␈α
reference␈αto␈α
an␈α␈↓αif␈↓␈α
instance,␈αwe␈α
compile␈αthe␈α
predicate␈αand
␈↓ α←␈↓one␈αof␈αthe␈αbranches;␈αwe␈αassociate␈αa␈αstructure␈αwith␈αthe␈αinstance;␈αthat␈αstructure
␈↓ α←␈↓has␈αeither␈αthe␈αname␈α
␈↓αifa␈↓␈αor␈α␈↓αifb␈↓␈αdepending␈α
on␈αwhich␈αbranch␈αwas␈α
compiled.␈αIf
␈↓ α←␈↓we␈α
come␈α
across␈α
this␈α
instance␈αof␈α
␈↓αif␈↓␈α
again␈α
(either␈α
in␈αa␈α
loop␈α
or␈α
in␈α
a␈αrecursion)
␈↓ α←␈↓then␈α
we␈αfind␈α
the␈α␈↓αifa␈↓␈α
or␈α␈↓αifb␈↓␈α
entry␈αin␈α
␈↓αcode␈↓.␈α
If␈αwe␈α
pick␈αthe␈α
same␈αbranch␈α
of␈αthe␈α
␈↓αif␈↓
␈↓ α←␈↓then␈α
nothing␈α
new␈α
happens;␈α
but␈α∞if␈α
the␈α
(compiled)␈α
predicate␈α
evaluated␈α∞to␈α
the
␈↓ α←␈↓other␈α⊗truth␈α⊗value,␈α∃then␈α⊗we␈α⊗compile␈α⊗the␈α∃other␈α⊗branch␈α⊗and␈α⊗associate␈α∃a
␈↓ α←␈↓completely compiled program with the original ␈↓αif␈↓ expression.
␈↓"∀␈↓ α←␈↓αevif <= λ[[exp] prog[␈↓ ∧s[l p a b]
␈↓"β␈↓ α←␈↓α␈↓ ∧sl ← body[exp];
␈↓"β␈↓ α←␈↓α␈↓ ∧sp ← pred[l];
␈↓"β␈↓ α←␈↓α␈↓ ∧sa ← ante[l];
␈↓"β␈↓ α←␈↓α␈↓ ∧sb ← ow[l];
␈↓"β␈↓ α←␈↓α␈↓ ∧sp ← evcom[p];
␈↓"β␈↓ α←␈↓α␈↓ ∧sreturn[list[␈↓ εβ[receive[] → mkifa[exp;p;evcom[a];b];
␈↓"β␈↓ α←␈↓α␈↓ ∧s␈↓ εβ ␈↓
t␈↓α → mkifb[exp;p;a;evcom[b]]]]] ]]
␈↓"∀␈↓ α←␈↓The␈α⊂construction␈α⊂of␈α∂the␈α⊂completed␈α⊂conditional␈α⊂code␈α∂is␈α⊂the␈α⊂business␈α⊂of␈α∂the
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 34␈↓We␈α∂assume␈α⊂the␈α∂variable␈α∂is␈α⊂being␈α∂used␈α∂as␈α⊂a␈α∂function;␈α∂we␈α⊂make␈α∂no
␈↓ α←␈↓attempt to handle funtional objects referenced as data.
␈↓ α←␈↓␈↓6.20␈↓ π*Compiling and Interpreting 361␈↓
␈↓"β␈↓ α←␈↓next␈αfunction,␈α␈↓αmkifcode␈↓.␈αA␈αcall␈αto␈αthis␈αfunction␈αis␈αmanufactured␈αwithin␈α␈↓αmkifa␈↓
␈↓ α←␈↓and ␈↓αmfifb␈↓.
␈↓"∀␈↓ α←␈↓αmkifcode <= λ[[p;a;b]
␈↓"β␈↓ α←␈↓α␈↓ ∧Oλ[[l;l1] mkcode[␈↓ ε≠p;
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ε≠list[mkjumpf[l]];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ε≠a;
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ε≠list[mkjump[l1]];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ε≠list[l];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ε≠b;
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ε≠list[l1]]]
␈↓"β␈↓ α←␈↓α␈↓ ∧O [gensym[];gensym[]] ]
␈↓"∀␈↓ α←␈↓␈↓↓Recognizers␈↓α
␈↓" ␈↓ α←␈↓αislocal <= λ[[x]in[x;local[env]]]
␈↓" ␈↓ α←␈↓αisif <= λ[[x] eq[first[x] IF]]
␈↓" ␈↓ α←␈↓αisprim <= λ[[ins] get[ins;INST]]
␈↓" ␈↓ α←␈↓αisfun <= λ[[x] get[x; EXPR]]
␈↓"∀␈↓ α←␈↓α␈↓↓Constructors␈↓α
␈↓" ␈↓ α←␈↓αmklocal <= λ[[var] list[SEND@;var]]
␈↓" ␈↓ α←␈↓αmkglob <= λ[[x] list[LOOKUP;x]]
␈↓" ␈↓ α←␈↓αmkalloc <= λ[[vars] list[ALLOC;vars]]
␈↓" ␈↓ α←␈↓αmkcall <= λ[[fn] list[CALL;fn]]
␈↓"∀␈↓ α←␈↓We␈αhave␈αleft␈αout␈αa␈αsignificant␈αamount␈αof␈αdetail␈αand␈αwe␈αhave␈αonly␈αcovered␈αa
␈↓ α←␈↓subset␈α∪of␈α∩LISP,␈α∪but␈α∩the␈α∪result␈α∩should␈α∪be␈α∩understandable;␈α∪and␈α∪it␈α∩should
␈↓ α←␈↓further␈α∃clarify␈α∃the␈α∃relationships␈α∃between␈α∃compilation␈α∃and␈α∃interpretation.
␈↓ α←␈↓Typical␈α⊂discussions␈α⊃of␈α⊂compilers␈α⊃and␈α⊂interpreters␈α⊂lead␈α⊃one␈α⊂to␈α⊃believe␈α⊂that
␈↓ α←␈↓there␈α∃is␈α∃a␈α∃severe␈α∃flexibility/efficiency␈α∃tradeoff␈α∃imposed␈α∃in␈α∃dealing␈α∀with
␈↓ α←␈↓compilers.␈α∞If␈α
you␈α∞compile␈α
programs␈α∞you␈α
must␈α∞give␈α
up␈α∞a␈α
lot␈α∞of␈α∞flexibility␈α
in
␈↓ α←␈↓editing␈α⊃and␈α∩debugging.␈α⊃ With␈α∩a␈α⊃properly␈α∩designed␈α⊃language␈α∩and␈α⊃flexible
␈↓ α←␈↓machine architecture, that is not true.
␈↓ α←␈↓␈↓362 Dynamic Structure␈↓
_6.21␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬ ␈↓↓6.21 Interactive Programming␈↓
␈↓"β␈↓ α←␈↓¬␈↓ β'"...␈αWhat␈α
is␈αactually␈αhappening,␈α
I␈αam␈α
afraid,␈αis␈αthat␈α
we␈αall␈αtell␈α
each
␈↓ α←␈↓¬␈↓ β'other␈α
and␈α
ourselves␈α∞that␈α
software␈α
engineering␈α
techniques␈α∞should␈α
be
␈↓ α←␈↓¬␈↓ β'improved␈αconsiderably,␈αbecause␈αthere␈αis␈αa␈αcrisis.␈αBut␈αthere␈αare␈αa␈αfew
␈↓ α←␈↓¬␈↓ β'boundary␈α
conditions␈α
which␈α
apparently␈αhave␈α
to␈α
be␈α
satisfied.␈α
I␈αwill␈α
list
␈↓ α←␈↓¬␈↓ β'them for you:
␈↓"∀␈↓ α←␈↓¬␈↓ β'␈↓↓1.␈↓¬ We may not change our thinking habits.
␈↓" ␈↓ α←␈↓¬␈↓ β'␈↓↓2.␈↓¬ We may not change our programming tools.
␈↓" ␈↓ α←␈↓¬␈↓ β'␈↓↓3.␈↓¬ We may not change our hardware.
␈↓" ␈↓ α←␈↓¬␈↓ β'␈↓↓4.␈↓¬ We may not change our tasks.
␈↓" ␈↓ α←␈↓¬␈↓ β'␈↓↓5.␈↓¬ We␈αmay␈αnot␈αchange␈αthe␈αorganizational␈αset-up␈αin␈αwhich␈αthe␈αwork
␈↓ α←␈↓¬␈↓ βWhas to be done.
␈↓"∀␈↓ α←␈↓¬␈↓ β'Now␈αunder␈α
these␈αfive␈α
immutable␈αboundary␈α
conditions,␈αwe␈α
have␈αto␈α
try
␈↓ α←␈↓¬␈↓ β'to␈α≤improve␈α≠matters.␈α≤This␈α≤is␈α≠utterly␈α≤ridiculous.␈α≤Thank␈α≠you.
␈↓ α←␈↓¬␈↓ β'(␈↓αApplause␈↓).
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ ∧BE. Dijkstra, ␈↓αConference of Software Engineering, 1968␈↓.
␈↓"λ␈↓ α←␈↓We␈α⊗have␈α∃talked␈α⊗about␈α∃the␈α⊗constructs␈α∃of␈α⊗LISP;␈α∃we␈α⊗have␈α⊗talked␈α∃about
␈↓ α←␈↓interpreters␈αand␈αcompilers␈αfor␈αLISP;␈αand␈αwe␈αhave␈αtalked␈αa␈αlittle␈αabout␈αinput
␈↓ α←␈↓and␈α→output␈α_conventions␈α→for␈α_the␈α→language.␈α_The␈α→combination␈α→of␈α_these
␈↓ α←␈↓properties␈α
leads␈α
us␈α
to␈α
a␈α
most␈αinteresting␈α
practical␈α
aspect␈α
of␈α
LISP:␈α
its␈α
use␈αas␈α
an
␈↓ α←␈↓interactive␈α⊂programming␈α⊂language.␈α⊂A␈α⊂programming␈α⊂language␈α⊂is␈α⊂a␈α⊂tool␈α∂for
␈↓ α←␈↓building␈α∪programs.␈α∪LISP's␈α∪representation␈α∪of␈α∪programs␈α∪as␈α∀data␈α∪structures
␈↓ α←␈↓coupled␈α≤with␈α≤the␈α≤availablilty␈α≤of␈α≤display␈α≤terminals␈α≤offers␈α≤the␈α≤LISP
␈↓ α←␈↓programmer␈α≤unique␈α≤opportunities␈α≤for␈α≤the␈α≤interactive␈α≤construction␈α≠of
␈↓ α←␈↓programs.␈α∀ Historically,␈α∀machines␈α∀have␈α∀been␈α∀oriented␈α∀towards␈α∃the␈α∀rapid
␈↓ α←␈↓execution␈α∞of␈α∞well-defined␈α∂numerical␈α∞algorithms.␈α∞This␈α∂perspective␈α∞over-looks
␈↓ α←␈↓two important points.
␈↓"β␈↓ α←␈↓␈↓ β'First,␈α↔the␈α↔actual␈α_process␈α↔of␈α↔discovering,␈α↔debugging,␈α_refining,␈α↔and
␈↓ α←␈↓encoding␈α→the␈α→algorithm␈α→is␈α→a␈α~complex␈α→process.␈α→In␈α→the␈α→early␈α~days␈α→of
␈↓ α←␈↓computation,␈α⊂the␈α⊂programmer␈α∂performed␈α⊂the␈α⊂analysis␈α∂on␈α⊂the␈α⊂algorithm␈α∂on
␈↓ α←␈↓paper,␈α∞transcribed␈α∂the␈α∞algorithm␈α∞to␈α∂a␈α∞programming␈α∞language,␈α∂encoded␈α∞that
␈↓ α←␈↓program␈α∩on␈α∩coding␈α∩sheets␈α∩and␈α∩keypunched␈α∩a␈α∩card␈α∩deck.␈α∩That␈α∩deck␈α∩was
␈↓ α←␈↓supplied␈α∞with␈α∞data␈α∞cards␈α∞and␈α
presented␈α∞to␈α∞the␈α∞machine.␈α∞ If␈α∞some␈α
abnormal
␈↓ α←␈↓behavior␈α⊂was␈α⊂detected␈α⊂in␈α⊂the␈α⊃program,␈α⊂an␈α⊂uninspiring␈α⊂octal␈α⊂dump␈α⊃of␈α⊂the
␈↓ α←␈↓contents␈α
of␈αmemory␈α
was␈αpresented.␈α
Often␈αthe␈α
state␈αof␈α
the␈αmachine␈α
at␈αthe␈α
time
␈↓ α←␈↓the␈α⊃dump␈α⊂was␈α⊃taken␈α⊂had␈α⊃only␈α⊂a␈α⊃casual␈α⊂relationship␈α⊃with␈α⊂the␈α⊃actual␈α⊂bug.
␈↓ α←␈↓␈↓6.21␈↓ πIInteractive Programming 363␈↓
␈↓"β␈↓ α←␈↓Memory␈α∪dumps␈α∪were␈α∪an␈α∩appalling␈α∪debugging␈α∪technique,␈α∪even␈α∪then.␈α∩ As
␈↓ α←␈↓higher␈α∂level␈α∞languages␈α∂became␈α∞more␈α∂popular,␈α∞memory␈α∂dumps␈α∂became␈α∞even
␈↓ α←␈↓less␈α∩attractive;␈α∩the␈α⊃dump␈α∩gave␈α∩little␈α⊃insight␈α∩unless␈α∩the␈α∩programmer␈α⊃knew
␈↓ α←␈↓where␈α
the␈αprogram␈α
and␈αdata␈α
resided␈αin␈α
memory,␈αand␈α
understood␈α
what␈αcode
␈↓ α←␈↓was produced by the compiler.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∃programmer␈α∃had␈α∃a␈α∀slightly␈α∃more␈α∃appealing␈α∃alternative␈α∀called
␈↓ α←␈↓␈↓↓tracing␈↓.␈α∃A␈α∃program␈α∃could␈α⊗be␈α∃embellished␈α∃with␈α∃print␈α⊗statements␈α∃whose
␈↓ α←␈↓purpose␈α∂was␈α∂to␈α∂determine␈α⊂access␈α∂behavior␈α∂by␈α∂printing␈α∂values␈α⊂of␈α∂variables,
␈↓ α←␈↓and␈αto␈αdiscover␈α
control␈αbehavior␈αby␈α
printing␈αmessages␈αat␈α
entry␈αand␈αexit␈α
from
␈↓ α←␈↓procedures.␈α This␈αtracing␈αtechnique␈αwas␈αfrequently␈αavailable␈αas␈αan␈αoperating
␈↓ α←␈↓system␈α∪option.␈α∪Then␈α∪the␈α∀programmer␈α∪would␈α∪supply␈α∪control␈α∀cards␈α∪which
␈↓ α←␈↓expressed␈α∀what␈α∃kind␈α∀of␈α∀output␈α∃was␈α∀desired.␈α∀In␈α∃either␈α∀case,␈α∃unless␈α∀this
␈↓ α←␈↓technique␈α∀was␈α∪used␈α∀with␈α∀resolute␈α∪precision,␈α∀the␈α∪output␈α∀would␈α∀either␈α∪be
␈↓ α←␈↓voluminous or uninformative, or both.
␈↓"β␈↓ α←␈↓␈↓ β'When␈α⊃the␈α⊃cause␈α⊃of␈α⊃a␈α⊂bug␈α⊃was␈α⊃discovered␈α⊃the␈α⊃offending␈α⊃cards␈α⊂were
␈↓ α←␈↓replaced␈α
and␈αthe␈α
deck␈α
was␈αresubmitted␈α
for␈α
execution.␈αThis␈α
cycle␈αwas␈α
repeated
␈↓ α←␈↓until␈α∞an␈α∞acceptable␈α
program␈α∞was␈α∞developed.␈α
This␈α∞approach␈α∞is␈α∞still␈α
followed
␈↓ α←␈↓by␈α
a␈αmajority␈α
of␈α
programmers.␈αWhat␈α
is␈α
missed␈αis␈α
that␈α
much␈αof␈α
the␈αdetail␈α
and
␈↓ α←␈↓tedium␈α⊂of␈α⊂these␈α⊂early␈α⊂phases␈α⊂of␈α⊂program␈α⊂development␈α⊂can␈α⊂be␈α⊂aided␈α⊂by␈α⊂a
␈↓ α←␈↓well-constructed␈αinteractive␈αprogramming␈αsystem.␈α The␈αmajor␈αdifficulty␈αis␈αthe
␈↓ α←␈↓emphasis␈α_on␈α_program␈α→execution␈α_rather␈α_than␈α_program␈α→debugging␈α_and
␈↓ α←␈↓development.␈αMost␈αarchitectures␈αand␈αlanguages␈αassume␈αthat␈αa␈αprogram␈αruns.
␈↓ α←␈↓If␈α⊗one␈α⊗assumes␈α⊗that␈α⊗"programs␈α∃never␈α⊗run",␈α⊗and␈α⊗designs␈α⊗a␈α∃"debugging
␈↓ α←␈↓architecture" then a LISP-like machine appears.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
second␈αpoint␈α
which␈αis␈α
overlooked␈α
is␈αthat␈α
a␈αlarge␈α
class␈αof␈α
interesting
␈↓ α←␈↓problems␈α≠are␈α≠not␈α≠included␈α≠in␈α≠the␈α≠range␈α≠of␈α≠"well-defined␈α~numerical
␈↓ α←␈↓algorithms".␈α In␈α
fact␈αmost␈α
of␈αthe␈α
problems␈αwhich␈α
are␈αtraditionally␈αattacked␈α
by
␈↓ α←␈↓LISP␈α⊗programs␈α↔fall␈α⊗into␈α↔this␈α⊗class:␈α↔language␈α⊗design,␈α↔theorem␈α⊗proving,
␈↓ α←␈↓compiler␈α∂writing,␈α∂and␈α∂of␈α⊂course,␈α∂artificial␈α∂intelligence.␈α∂In␈α⊂such␈α∂"exploratory
␈↓ α←␈↓programming"␈α∞it␈α∞is␈α∞often␈α∞the␈α
case␈α∞that␈α∞no␈α∞well-defined␈α∞algorithm␈α∞is␈α
known,
␈↓ α←␈↓and␈α∂it␈α∞will␈α∂be␈α∞the␈α∂final␈α∞program␈α∂which␈α∞␈↓¬is␈↓␈α∂the␈α∞algorithm.␈α∂Such␈α∞exploratory
␈↓ α←␈↓programming␈α⊗requires␈α⊗that␈α⊗the␈α∃programming␈α⊗language␈α⊗be␈α⊗usable␈α⊗as␈α∃a
␈↓ α←␈↓sophisticated␈α≠"desk␈α≠calculator".␈α≠It␈α≠requires␈α≠experimentation␈α≤with,␈α≠and
␈↓ α←␈↓execution␈αof,␈α
partially␈αspecified␈α
programs;␈αthat␈α
is,␈αthe␈α
ability␈αto␈α
develop␈αand
␈↓ α←␈↓run␈αpieces␈αof␈αprograms;␈αto␈αbuild␈αup␈αa␈αlarger␈αprogram␈αfrom␈αpieces;␈αto␈αquickly
␈↓ α←␈↓modify␈α∪either␈α∪the␈α∪program␈α∀text␈α∪or␈α∪the␈α∪computational␈α∀results␈α∪themselves,
␈↓ α←␈↓before the programmers have lost their train of thought.
␈↓"β␈↓ α←␈↓␈↓ β'An␈α∞important␈α∞outgrowth␈α∞of␈α∂such␈α∞exploratory␈α∞programming␈α∞is␈α∂a␈α∞LISP
␈↓ α←␈↓technique␈α≠called␈α≠"throw-away␈α~implementation".␈α≠In␈α≠developing␈α≠a␈α~large
␈↓ α←␈↓programming␈α∞system␈α∞one␈α∞begins␈α∞with␈α∞a␈α∞few␈α∞simple␈α∞ideas␈α∞and␈α∞incrementally
␈↓ α←␈↓develops␈α⊂the␈α⊂larger␈α⊂system.␈α⊂ If␈α⊂some␈α⊂of␈α⊂the␈α⊂ideas␈α⊂are␈α⊂inadequate␈α⊂they␈α⊂are
␈↓ α←␈↓replaced.␈α
At␈αsome␈α
stage␈αan␈α
adequate␈αmodel␈α
is␈αdeveloped;␈α
it␈αmay␈α
be␈αlacking␈α
in
␈↓ α←␈↓some␈α
aspects,␈α
but␈α
it␈α
does␈α
model␈α
the␈α
desired␈α
phenomenon.␈α
At␈α
that␈α
point,␈α
the
␈↓ α←␈↓programmer's␈α~understanding␈α~has␈α→been␈α~sufficiently␈α~improved,␈α~that␈α→the
␈↓ α←␈↓␈↓364 Dynamic Structure␈↓
_6.21␈↓
␈↓"β␈↓ α←␈↓implementation␈α
should␈α
be␈α
thrown␈αaway␈α
and␈α
a␈α
new,␈α
more␈αenlightened␈α
version,
␈↓ α←␈↓created.␈α∪ Certainly␈α∪this␈α∪kind␈α∀of␈α∪programming␈α∪can␈α∪be␈α∀accomplished␈α∪with
␈↓ α←␈↓languages␈α≠other␈α≠than␈α≠LISP;␈α≠the␈α≠point␈α≠is␈α≠that␈α≠an␈α≠interactive␈α~LISP
␈↓ α←␈↓environment␈α∞is␈α∞sufficiently␈α
responsive␈α∞that␈α∞an␈α
implementation␈α∞cycle␈α∞is␈α
quite
␈↓ α←␈↓short␈α⊂and␈α⊂relatively␈α⊃painless.␈α⊂The␈α⊂effect␈α⊂is␈α⊃that␈α⊂the␈α⊂programmer␈α⊃does␈α⊂not
␈↓ α←␈↓invest␈α∂so␈α⊂much␈α∂effort␈α⊂in␈α∂an␈α∂implementation␈α⊂that␈α∂he␈α⊂feels␈α∂a␈α⊂compulsion␈α∂to
␈↓ α←␈↓patch an inferior implementation, rather than start afresh.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊃development␈α⊃of␈α⊃an␈α⊃interactive␈α⊃programming␈α⊃system␈α⊃has␈α⊃several
␈↓ α←␈↓important␈α∞implications.␈α
The␈α∞usual␈α
partitioning␈α∞of␈α
program␈α∞preparation␈α
into
␈↓ α←␈↓editing,␈α
running,␈αand␈α
debugging␈α
is␈αno␈α
longer␈α
adequate.␈αThe␈α
text␈α
editor␈αand
␈↓ α←␈↓the␈αdebugger␈αare␈αintegral␈αparts␈αof␈αthe␈αsystem.␈αA␈αprogramming␈α
"environment"
␈↓ α←␈↓is␈α∞established␈α
in␈α∞which␈α
all␈α∞facets␈α
of␈α∞programming␈α
are␈α∞integrated.␈α∞The␈α
tasks
␈↓ α←␈↓which␈α∂are␈α∞usually␈α∂performed␈α∂by␈α∞an␈α∂operating␈α∂system␈α∞are␈α∂subsumed␈α∂by␈α∞the
␈↓ α←␈↓programming␈αlanguage.␈α The␈αidea␈αof␈αa␈αseparate␈αfile␈αsystem␈αbecomes␈αobsolete,
␈↓ α←␈↓and␈α⊃all␈α⊂programs␈α⊃and␈α⊂data␈α⊃are␈α⊂accessible␈α⊃from␈α⊂within␈α⊃the␈α⊂"environment".
␈↓ α←␈↓This␈α
has␈α
an␈α
important␈α
advantage␈α
in␈α
LISP-like␈α
representations␈α
of␈αprograms:
␈↓ α←␈↓the␈αconversion␈α
from␈αinternal␈α
representation␈αto␈α
"text␈αfile"␈α
format␈αis␈α
eliminated.
␈↓ α←␈↓The␈α≤technique␈α≠puts␈α≤added␈α≤burden␈α≠on␈α≤naming␈α≠facilities␈α≤so␈α≤that␈α≠a
␈↓ α←␈↓programmer's␈α∂definitions␈α⊂are␈α∂accessible,␈α⊂but␈α∂are␈α⊂unambigiously␈α∂addressible.
␈↓ α←␈↓The␈α∂effect␈α∞is␈α∂to␈α∞structure␈α∂the␈α∂interactive␈α∞environment␈α∂as␈α∞a␈α∂very␈α∂large␈α∞data
␈↓ α←␈↓base␈α
containing␈α
programs␈α
and␈α
data␈α
structures.␈α
The␈α
programmer␈α
has␈α
accessing
␈↓ α←␈↓procedures␈αto␈αmanipulate␈αthe␈αelements␈αin␈αthe␈αbase.␈αAll␈αthe␈αitems␈αin␈α
the␈αbase
␈↓ α←␈↓are␈α⊂accessible␈α∂as␈α⊂data␈α∂structures;␈α⊂the␈α⊂editor␈α∂can␈α⊂modify␈α∂any␈α⊂objects␈α⊂in␈α∂the
␈↓ α←␈↓base.␈α∂ Some␈α∂of␈α∞the␈α∂objects␈α∂can␈α∞be␈α∂executed␈α∂as␈α∞procedures;␈α∂the␈α∂evaluator␈α∞is
␈↓ α←␈↓responsible for this.
␈↓"β␈↓ α←␈↓␈↓ β'A␈αprocedure␈αobject␈αcan␈αbe␈αfurther␈αexpanded␈αinto␈α
machine␈αinstructions
␈↓ α←␈↓for␈αfaster␈α
execution;␈αthis␈α
may␈αeither␈αbe␈α
done␈αby␈α
an␈αexplicit␈α
call␈αon␈αa␈α
compiler
␈↓ α←␈↓or␈α∀be␈α∀done␈α∀invisibly␈α∀by␈α∀a␈α∀compiler/interpreter.␈α∀If␈α∀an␈α∀evaluation␈α∀is␈α∀not
␈↓ α←␈↓performing␈α
as␈α
expected,␈α
yet␈αanother␈α
data␈α
structure␈α
manipulating␈α
program␈αis
␈↓ α←␈↓available.␈α∞ The␈α∞debugger␈α∞is␈α∞able␈α∞to␈α∞manipulate␈α∞both␈α∞the␈α∞program␈α∞structure
␈↓ α←␈↓and␈α∂the␈α∂run time␈α⊂data␈α∂structures␈α∂which␈α∂the␈α⊂evaluator␈α∂has␈α∂created.␈α⊂Any␈α∂of
␈↓ α←␈↓these␈α
data␈αstructure␈α
manipulating␈α
programs␈αis␈α
able␈α
to␈αcall␈α
any␈αother␈α
program.
␈↓ α←␈↓The␈α~effect␈α~is␈α~a␈α~programming␈α~philosophy␈α~sometimes␈α~characterized␈α~as
␈↓ α←␈↓"middle-out"␈αrather␈αthan␈α"top-down"␈αor␈α"bottom-up".␈αThe␈αemphasis␈αis␈αon␈αthe
␈↓ α←␈↓programming␈α∪process␈α∩rather␈α∪than␈α∪on␈α∩the␈α∪final␈α∩product.␈↓π 35␈↓␈α∪This␈α∪view␈α∩of
␈↓ α←␈↓program␈α∪development␈α∪is␈α∪in␈α∪direct␈α∪conflict␈α∪with␈α∪the␈α∪traditional␈α∪approach
␈↓ α←␈↓which␈αgrew␈α
from␈αthe␈α
card deck␈αphilosophy,␈αand␈α
assumed␈αthat␈α
machine␈αtime
␈↓ α←␈↓was␈αmore␈αvaluable␈αthan␈αprogrammer␈αtime.␈α Several␈αcurrent␈αresearch␈αprojects
␈↓ α←␈↓are␈α⊂developing␈α⊂along␈α⊂these␈α⊃lines;␈α⊂among␈α⊂them␈α⊂are␈α⊂[Hew 75],␈α⊃[Int 75],␈α⊂and
␈↓ α←␈↓[Win 75]. All of these projects are based on LISP.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 35␈↓Compare␈α∞the␈α∞term␈α∞"structured␈α∞programming".␈α∞The␈α∞emphasis␈α∂is␈α∞also
␈↓ α←␈↓on␈α_the␈α↔action.␈α_The␈α↔application␈α_of␈α↔a␈α_methodology␈α↔should␈α_aid␈α_in␈α↔the
␈↓ α←␈↓development␈α∞of␈α∞an␈α∞object␈α∞with␈α∞the␈α∞desired␈α∞characteristics.␈α∂In␈α∞programming,
␈↓ α←␈↓that implies the existence of a programming environment.
␈↓ α←␈↓␈↓6.21␈↓ πIInteractive Programming 365␈↓
␈↓"β␈↓ α←␈↓␈↓ β'It␈αis␈αnot␈αaccidental␈α
that␈αLISP␈αis␈αthe␈α
major␈αlanguage␈αfor␈αthese␈α
kinds␈αof
␈↓ α←␈↓programming␈α⊗tasks;␈α⊗it␈α⊗is␈α⊗the␈α⊗features␈α⊗of␈α⊗the␈α⊗language␈α⊗which␈α⊗make␈α∃it
␈↓ α←␈↓amenable␈αto␈α
such␈αprogramming␈α
tasks.␈αIn␈αthe␈α
next␈αtwo␈α
sections␈αwe␈αwill␈α
discuss
␈↓ α←␈↓two␈α→of␈α→the␈α→ingredients␈α~of␈α→interactive␈α→programming␈α→systems;␈α~this␈α→will
␈↓ α←␈↓illuminate␈α∩the␈α∩features␈α∩of␈α∩LISP␈α∩which␈α∩make␈α∩it␈α∩important␈α∩for␈α∩interactive
␈↓ α←␈↓programming.
␈↓"β␈↓ α←␈↓␈↓ β'First␈αwe␈α
will␈αsketch␈α
some␈αbasic␈αfeatures␈α
of␈αLISP␈α
text␈αeditors.␈α
This␈αwill
␈↓ α←␈↓show␈α∂some␈α∂of␈α∂the␈α∞benefits␈α∂of␈α∂having␈α∂the␈α∞program␈α∂structure␈α∂available␈α∂as␈α∞a
␈↓ α←␈↓data␈αstructure.␈αThe␈αsucceeding␈αsection␈αwill␈αdiscuss␈αa␈αfew␈αfeatures␈αof␈αa␈αtypical
␈↓ α←␈↓LISP␈α∪debugging␈α∩system;␈α∪this␈α∩will␈α∪further␈α∩demonstrate␈α∪the␈α∪advantages␈α∩of
␈↓ α←␈↓having a natural program representation available at run time.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α∞is␈α∞no␈α
standard␈α∞LISP␈α∞editor␈α
or␈α∞debugger.␈↓π 36␈↓␈α∞Therefore␈α∞the␈α
next
␈↓ α←␈↓sections␈α∃will␈α∃contain␈α∃general␈α∃information␈α∃rather␈α∃than␈α∃an␈α∃abundance␈α∃of
␈↓ α←␈↓concrete␈α
examples.␈α
The␈α
design␈α
of␈α
such␈αdevices␈α
is␈α
a␈α
subject␈α
of␈α
very␈αpersonal
␈↓ α←␈↓preferences␈α∂and␈α∂prejudices.␈α∂Some␈α∞characteristics␈α∂are␈α∂common␈α∂and␈α∂those␈α∞we
␈↓ α←␈↓will␈αstress.␈αA␈αrelated␈αdifficulty␈αis␈αthat␈αediting␈αand␈αdebugging␈αdevices␈αare␈αbest
␈↓ α←␈↓done␈α∂as␈α∂interactive␈α∞display␈α∂programs,␈α∂rather␈α∞than␈α∂as␈α∂key-punch␈α∂or␈α∞teletype
␈↓ α←␈↓programs.␈↓π 37␈↓␈α~Interactive␈α~programming␈α≠is␈α~a␈α~very␈α~visual␈α≠and␈α~dynamic
␈↓ α←␈↓enterprise;␈α↔teletype-oriented␈α⊗interaction␈α↔is␈α⊗not␈α↔sufficient;␈α⊗it␈α↔results␈α↔in␈α⊗a
␈↓ α←␈↓presentation more like a comic strip than a movie.
␈↓"β␈↓ α←␈↓␈↓ ¬]␈↓↓6.22 LISP Editors␈↓
␈↓"β␈↓ α←␈↓A␈αLISP␈αeditor␈αis␈αjust␈αanother␈αLISP␈αprogram;␈αit␈αoperates␈αon␈αa␈αdata␈αstructure.
␈↓ α←␈↓In␈α
this␈αcase␈α
the␈α
data␈αstructure␈α
represents␈αa␈α
program.␈α
A␈αsimple␈α
editor␈αcould␈α
be
␈↓ α←␈↓constructed along the lines of the ␈↓αsubst␈↓ function:
␈↓"∀␈↓ α←␈↓αsubst␈↓λ'␈↓α <= λ[[x;y;z]␈↓ ∧O[atom[z] → [equal[y;z] → x; ␈↓
t␈↓α → z];
␈↓"β␈↓ α←␈↓α␈↓ ∧O ␈↓
t␈↓α → cons[␈↓ ¬Gsubst␈↓λ'␈↓α[x;y;car[z]];
␈↓"β␈↓ α←␈↓α␈↓ ∧O␈↓ ¬Gsubst␈↓λ'␈↓α[x;y;cdr[z]]]]]
␈↓"∀␈↓ α←␈↓That␈α
is,␈α
we␈α∞would␈α
let␈α
␈↓αz␈↓␈α
be␈α∞the␈α
program;␈α
␈↓αy␈↓,␈α
the␈α∞piece␈α
of␈α
text␈α
to␈α∞be␈α
replaced;
␈↓ α←␈↓and␈α␈↓αx␈↓,␈αthe␈αnew␈α
text.␈α Such␈αglobal␈αediting␈α
is␈αuseful␈αsometimes,␈αbut␈αtext␈α
editing
␈↓ α←␈↓is␈α⊂a␈α⊂more␈α∂local␈α⊂and␈α⊂controlled␈α⊂action,␈α∂better␈α⊂accomplished␈α⊂as␈α⊂a␈α∂interactive
␈↓ α←␈↓process.
␈↓"β␈↓ α←␈↓␈↓ β'A␈αtypical␈α
editor␈αwill␈αtake␈α
an␈αexpression␈α
as␈αinput␈αand␈α
will␈αthen␈α
enter␈αa
␈↓ α←␈↓"listen-loop",␈α⊃waiting␈α⊃for␈α⊃commands␈α⊃from␈α⊃the␈α⊃user.␈α⊃ The␈α⊃input␈α⊂expression
␈↓ α←␈↓may␈α⊗either␈α↔be␈α⊗a␈α⊗list␈α↔representing␈α⊗a␈α⊗constant␈α↔data␈α⊗structure,␈α⊗or␈α↔a␈α⊗list
␈↓ α←␈↓representing␈αa␈α
(constant)␈αfunction.␈α
There␈αare␈αcommands␈α
for␈αthe␈α
selection␈αof
␈↓ α←␈↓a␈α∂subexpression␈α⊂of␈α∂the␈α⊂input␈α∂expression;␈α⊂and␈α∂there␈α⊂are␈α∂commands␈α⊂for␈α∂the
␈↓ α←␈↓replacement of expressions with other expressions.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 36␈↓Indeed,␈αther␈αis␈αno␈αstandard␈αLISP.␈αThe␈αlanguage␈αis␈αdynamic,␈αflexible,
␈↓ α←␈↓and still developing after twenty years.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 37␈↓That is one of the author's many preferences and prejudices.
␈↓ α←␈↓␈↓366 Dynamic Structure␈↓
_6.22␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∂mechanics␈α∞of␈α∂presenting␈α∂list␈α∞structure␈α∂to␈α∞the␈α∂user␈α∂are␈α∞interesting.
␈↓ α←␈↓Since␈α
a␈α
list␈α
may␈αbe␈α
deeply␈α
nested,␈α
we␈αneed␈α
a␈α
convenient␈α
way␈α
of␈αdesignating
␈↓ α←␈↓subexpressions.␈α∩A␈α⊃display␈α∩device␈α⊃might␈α∩illuminate␈α⊃the␈α∩selected␈α⊃expression
␈↓ α←␈↓more␈α&brightly␈α'than␈α&the␈α'containing␈α&expression.␈α'More␈α&structured
␈↓ α←␈↓representations␈α_of␈α_text␈α_are␈α_required,␈α_however.␈α_ A␈α_"pretty-printed"␈α↔(see
␈↓ α←␈↓Section 9.2␈α∂or␈α∂page 274)␈α∂version␈α∂of␈α∂the␈α∂text␈α∂may␈α∂be␈α∂presented.␈α∂ If␈α∂the␈α∂text
␈↓ α←␈↓is␈α∂too␈α∂extensive␈α∂to␈α∂fit␈α∂on␈α∂the␈α∂display␈α∂face,␈α∂then␈α∂abbreviational␈α∂devices␈α∂are
␈↓ α←␈↓available.
␈↓"β␈↓ α←␈↓␈↓ β'If␈αthe␈αtext␈αis␈αdeeply␈αnested␈αit␈αis␈αoften␈αdifficult␈αto␈αperceive␈αthe␈αtop␈αlevel
␈↓ α←␈↓structure␈αeven␈αin␈αpretty-printed␈αform.␈α Consider␈αthe␈αS-expr␈αrepresentation␈αof
␈↓ α←␈↓the definition of ␈↓αmember␈↓:
␈↓"∀␈↓ α←␈↓α(MEMBER
␈↓"β␈↓ α←␈↓α (LAMBDA (X L)
␈↓"β␈↓ α←␈↓α (COND␈↓ βo((NULL L) NIL)
␈↓"β␈↓ α←␈↓α␈↓ βo((EQ X (FIRST L)) T)
␈↓"β␈↓ α←␈↓α␈↓ βo(T (MEMBER X (REST L))))))
␈↓"∀␈↓ α←␈↓In␈αthis␈αcase␈αthe␈αstructure␈αof␈αthe␈α␈↓αCOND␈↓␈αis␈αclear;␈αbut␈αit␈αis␈αclearer␈αif␈αwe␈α
express
␈↓ α←␈↓it as:
␈↓"∀␈↓ α←␈↓α␈↓ βK(LAMBDA (X L) (COND & & &))
␈↓"∀␈↓ α←␈↓Or given a linear list:␈↓ ¬2␈↓α(␈↓εa b c d e f g h i j k l␈↓α)␈↓
␈↓"β␈↓ α←␈↓it may be more instructive to display it as:
␈↓"∀␈↓ α←␈↓␈↓ ¬G␈↓α(␈↓εa b c d e f g h i ␈↓α... )␈↓
␈↓" ␈↓ α←␈↓or ␈↓ ¬a␈↓α( ...␈↓εd e f g h i ␈↓α... )␈↓.
␈↓"∀␈↓ α←␈↓where the focus of attention is controlled by the user.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α
should␈α
be␈α
commands␈α
to␈α
move␈α
selected␈α
subexpressions␈α
to␈α
different
␈↓ α←␈↓locations␈αwithin␈αthe␈αsame␈αstructure␈αand␈αmove␈αexpressions␈αto␈αother␈αstructures.
␈↓ α←␈↓Since␈α∞a␈α∞common␈α∞text␈α∞error␈α∞in␈α∞LISP␈α∞is␈α∞the␈α∞misplacing␈α∞of␈α∂parentheses,␈α∞there
␈↓ α←␈↓are commands to move parentheses.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α∩are␈α∩at␈α∩least␈α∪two␈α∩reasons␈α∩for␈α∩text␈α∩editors:␈α∪programmers␈α∩make
␈↓ α←␈↓errors,␈αand␈αprograms␈αwhich␈αare␈αcorrect␈α
need␈αto␈αbe␈αmodified␈αto␈αperform␈α
other
␈↓ α←␈↓tasks.␈α⊂Particularly␈α∂in␈α⊂exploratory␈α∂programming,␈α⊂the␈α⊂"try-it-and-see"␈α∂attitude
␈↓ α←␈↓must␈α⊂be␈α⊃supported.␈α⊂Thus␈α⊃we␈α⊂demand␈α⊃a␈α⊂flexible␈α⊃editor␈α⊂which␈α⊃can␈α⊂"undo"
␈↓ α←␈↓changes␈α
to␈α
functions␈α
or␈α
constant␈α
data␈α
structure.␈α
LISP␈α
editors␈α
have␈αthe␈α
ability
␈↓ α←␈↓to␈α
save␈αthe␈α
current␈αedit␈α
structure␈αsuch␈α
that␈α
an␈α"undo"␈α
can␈αrestore␈α
to␈αthat␈α
state
␈↓ α←␈↓if needed.
␈↓"β␈↓ α←␈↓␈↓ β'Regardless␈α∪of␈α∪the␈α∪idiosyncrasies␈α∩of␈α∪a␈α∪particular␈α∪editor␈α∪the␈α∩common
␈↓ α←␈↓feature␈α∞is␈α∞that␈α∞LISP␈α∞editors␈α∞are␈α∞structure-oriented␈α∞editors.␈α∞They␈α∂operate␈α∞on
␈↓ α←␈↓S-expressions,␈α⊂not␈α∂text␈α⊂strings␈α∂or␈α⊂card␈α⊂images.␈α∂ A␈α⊂program␈α∂is␈α⊂not␈α⊂a␈α∂linear
␈↓ α←␈↓string␈α
of␈αcharacters;␈α
it␈αis␈α
a␈αstructured␈α
entity,␈αwhose␈α
parts␈α
are␈αdistinguishable
␈↓ α←␈↓as␈α⊃representations␈α⊃of␈α∩instances␈α⊃of␈α⊃programming␈α⊃language␈α∩constructs.␈α⊃ The
␈↓ α←␈↓␈↓6.22␈↓ λfLISP Editors 367␈↓
␈↓"β␈↓ α←␈↓editing process should take cognizance of that structure.␈↓π 38␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬/␈↓↓6.23 Debugging in LISP␈↓
␈↓"β␈↓ α←␈↓Few␈αareas␈αof␈αcomputer␈αscience␈αfield␈αare␈αas␈αprimitive␈αas␈αthe␈αart␈αof␈αdebugging;
␈↓ α←␈↓few␈α∪areas␈α∪of␈α∀the␈α∪field␈α∪are␈α∪as␈α∀important.␈α∪The␈α∪development␈α∪of␈α∀a␈α∪correct
␈↓ α←␈↓program␈α∪is␈α∀the␈α∪point␈α∀of␈α∪our␈α∀programming␈α∪activity.␈α∀ The␈α∪power␈α∀of␈α∪our
␈↓ α←␈↓debugging␈α∂techniques␈α∞has␈α∂been␈α∂directly␈α∞related␈α∂to␈α∞the␈α∂sophistication␈α∂of␈α∞the
␈↓ α←␈↓hardware/software␈α∩interface␈α∩which␈α∩is␈α∪available.␈α∩ Not␈α∩until␈α∩the␈α∪advent␈α∩of
␈↓ α←␈↓sophisticated␈α⊃on-line␈α⊃systems␈α⊂has␈α⊃there␈α⊃really␈α⊂been␈α⊃any␈α⊃hope␈α⊃for␈α⊂practical
␈↓ α←␈↓"correct-program" construction.
␈↓"β␈↓ α←␈↓␈↓ β'Several␈αpieces␈αof␈αinformation␈αare␈αrequired␈αto␈αdo␈αinteractive␈αdebugging.
␈↓ α←␈↓We␈α∞need␈α∞an␈α
indication␈α∞of␈α∞the␈α
error␈α∞condition;␈α∞we␈α
need␈α∞the␈α∞current␈α∞state␈α
of
␈↓ α←␈↓the␈α∞computation;␈α∞we␈α
need␈α∞to␈α∞have␈α∞some␈α
indication␈α∞of␈α∞how␈α∞the␈α
computation
␈↓ α←␈↓arrived␈α⊗at␈α⊗the␈α⊗error␈α⊗condition;␈α∃and,␈α⊗if␈α⊗interactive␈α⊗debugging␈α⊗is␈α⊗to␈α∃be
␈↓ α←␈↓meaningful,␈α∪we␈α∪need␈α∪the␈α∪ability␈α∪to␈α∪modify␈α∪the␈α∪computation␈α∪and␈α∩resume
␈↓ α←␈↓execution␈α
in␈αthat␈α
modified␈α
environment.␈α This␈α
last␈αpoint␈α
is␈α
quite␈αimportant;
␈↓ α←␈↓it␈α∞has␈α
implications␈α∞for␈α∞programming␈α
style.␈α∞ First,␈α∞we␈α
␈↓¬should␈↓␈α∞hope␈α∞to␈α
modify
␈↓ α←␈↓an␈α
errant␈α
calculation␈αrather␈α
than␈α
restart␈α
the␈αentire␈α
computation.␈α
To␈αstart␈α
over
␈↓ α←␈↓is␈α∩like␈α∩repunching␈α∩a␈α∩whole␈α∩card␈α∩deck␈α∩because␈α∩one␈α∩card␈α∩was␈α∩wrong.␈α⊃We
␈↓ α←␈↓repunch␈αthe␈αoffending␈αcards␈αand␈αretain␈αthe␈αrest.␈αSimilarly,␈αwe␈αshould␈αexpect
␈↓ α←␈↓to␈α~throw␈α~away␈α~offending␈α→computations␈α~and␈α~retaining␈α~the␈α→remainder.
␈↓ α←␈↓Typically,␈α∂computation␈α∂is␈α∂not␈α∂as␈α∂local␈α∂and␈α∂exciseable␈α∂as␈α∂removing␈α∂a␈α∂single
␈↓ α←␈↓card;␈α
a␈αprimary␈α
purpose␈α
of␈αmost␈α
computation␈α
is␈αto␈α
pass␈α
a␈αresult␈α
to␈αsome␈α
other
␈↓ α←␈↓procedure.␈α∂ However,␈α∂if␈α∂we␈α∂try␈α∂to␈α∂localize␈α∂the␈α∂effects␈α∂of␈α∂each␈α∂procedure␈α∂to
␈↓ α←␈↓simple␈αparameter␈αpassing␈αand␈αvalue␈αreturning␈αthen␈αwe␈αhave␈αa␈αbetter␈αchance
␈↓ α←␈↓of␈αdiscovering␈αa␈αpoint␈αin␈αthe␈αcomputation␈αhistory␈αwhich␈αis␈αprior␈αto␈αthe␈αerror;
␈↓ α←␈↓return␈α
the␈α
control␈αstack␈α
to␈α
that␈α
point;␈αmodify␈α
the␈α
erring␈α
procedure␈αand␈α
restart
␈↓ α←␈↓the␈α⊗computation␈α⊗from␈α∃that␈α⊗point.␈α⊗This␈α∃implies␈α⊗that␈α⊗procedures␈α∃should
␈↓ α←␈↓minimize␈α∞their␈α∂use␈α∞of␈α∂side-effects;␈α∞for␈α∞it␈α∂is␈α∞side-effects␈α∂which␈α∞spoil␈α∂the␈α∞nice
␈↓ α←␈↓applicative␈α∀behavior␈α∀and␈α∀will␈α∪require␈α∀the␈α∀programmer␈α∀to␈α∀make␈α∪explicit
␈↓ α←␈↓modifications␈αin␈α
the␈αcomputational␈α
environment␈αbefore␈α
a␈αcomputation␈αcan␈α
be
␈↓ α←␈↓restarted.␈α
This␈α
attention␈α
to␈α
program␈α
interaction␈α
is␈α
another␈α
manifestation␈α
of
␈↓ α←␈↓the␈α␈↓↓modular␈α
programming␈↓␈αstyle;␈α
each␈αprocedure␈α
is␈αa␈α
module,␈αor␈α
black␈αbox,
␈↓ α←␈↓dependent␈α
on␈α∞other␈α
procedures␈α∞only␈α
through␈α∞well-defined␈α
input␈α∞and␈α
output
␈↓ α←␈↓considerations.␈α
It␈α
is␈α
this␈α
style␈α
of␈α
modular␈α
programming␈α
which␈α
will␈α
enhance
␈↓ α←␈↓the use of interactive debugging tools.
␈↓"β␈↓ α←␈↓␈↓ β'This␈α∃section␈α∃will␈α∃deal␈α∃only␈α∃with␈α∃the␈α∃primitive␈α∃mechanisms␈α∀which
␈↓ α←␈↓underlie␈αLISP␈αdebugging␈αtechniques.␈αThe␈αdiscussions␈αof␈αmore␈α
complex␈αtools
␈↓ α←␈↓which␈α∀are␈α∃available␈α∀or␈α∃are␈α∀comtemplated␈α∃are␈α∀well-documented␈α∃in␈α∀other
␈↓ α←␈↓sources; [Int 75], [Moo 74].
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 38␈↓␈α⊂A␈α⊂case␈α⊂can␈α⊂be␈α⊂made␈α⊂for␈α⊂believing␈α⊂that␈α⊂the␈α⊃program␈α⊂␈↓¬construction␈↓
␈↓ α←␈↓process should also be driven by that structure [Han 71].
␈↓ α←␈↓␈↓368 Dynamic Structure␈↓
_6.23␈↓
␈↓"β␈↓ α←␈↓␈↓ β'A␈αdebugging␈αsystem␈αfor␈αan␈αapplicative␈αlanguage␈αlike␈αLISP␈αis␈αbased␈αon
␈↓ α←␈↓the␈αfollowing␈α
kinds␈αof␈α
information:␈αwhich␈αfunctions␈α
are␈αbeing␈α
entered;␈αwhat
␈↓ α←␈↓are␈αthe␈αactual␈αparameters;␈αand␈αwhat␈αare␈αthe␈αvalues␈αbeing␈αreturned.␈α With␈αan
␈↓ α←␈↓open␈α
implementation,␈α
like␈αthat␈α
provided␈α
by␈αLISP,␈α
such␈α
information␈αis␈α
readily
␈↓ α←␈↓available.
␈↓"β␈↓ α←␈↓␈↓ β'Assume␈αthat␈αwe␈αwish␈αto␈α
monitor␈αthe␈αbehavior␈αof␈αthe␈αfunction,␈α
␈↓αfoo␈↓.␈αWe
␈↓ α←␈↓will␈α⊂place␈α⊃the␈α⊂real␈α⊂definition␈α⊃of␈α⊂␈↓αfoo␈↓␈α⊂on␈α⊃another␈α⊂symbol␈α⊂table␈α⊃entry␈α⊂(using
␈↓ α←␈↓␈↓αgensym[]␈↓) and redefine ␈↓αfoo␈↓ such that, when it is called, it will:
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ Print the values of the current actual parameters.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ Use ␈↓αapply␈↓ to call the real defintion of ␈↓αfoo␈↓ with the actual parameters.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ Print the value of the call on ␈↓αfoo␈↓.
␈↓" ␈↓ α←␈↓␈↓↓4.␈↓ Return control and the value to the calling program.
␈↓" ␈↓ α←␈↓Since␈α␈↓αfoo␈↓␈αmay␈αbe␈αrecursive␈αwe␈αshould␈αalso␈αgive␈αsome␈αindication␈αof␈αthe␈αdepth
␈↓ α←␈↓of recursion being executed.
␈↓"β␈↓ α←␈↓␈↓ β'Now␈α∃every␈α∃call␈α∃on␈α∃␈↓αfoo␈↓␈α∃will␈α∃give␈α∃us␈α∃the␈α∃pertinent␈α∃statistics.␈α∀ This
␈↓ α←␈↓technique␈α∀is␈α∀called␈α∀␈↓↓tracing␈↓.␈α∀The␈α∪current␈α∀description␈α∀is␈α∀similar␈α∀to␈α∪many
␈↓ α←␈↓implementations␈α
on␈α
teletype-like␈α
devices.␈αGiven␈α
an␈α
interactive␈α
display␈α
and␈αa
␈↓ α←␈↓well-defined␈α⊃"LISP␈α⊃machine"␈α⊃description␈α⊃like␈α⊃that␈α⊃in␈α⊃␈↓αpeval␈↓ (Section 4.8),␈α⊂a
␈↓ α←␈↓much more satisfactory trace can be given.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∞trace␈α∂mechanism␈α∞can␈α∂be␈α∞used␈α∞for␈α∂both␈α∞interpreted␈α∂and␈α∞compiled
␈↓ α←␈↓function␈αcalls,␈αbut␈αsome␈αcare␈αneeds␈αbe␈αtaken.␈α Interpreted␈αcalls␈αon␈α␈↓αfoo␈↓␈αwill␈αgo
␈↓ α←␈↓through␈α␈↓αeval␈↓,␈αand␈αif␈α
␈↓α(CALL␈α...␈αFOO)␈↓␈αis␈αbeing␈α
used␈αin␈αthe␈αcompiled␈α
code␈αthe
␈↓ α←␈↓␈↓αCALL␈↓␈α⊂decoder␈α∂can␈α⊂pass␈α∂control␈α⊂to␈α∂the␈α⊂tracing␈α∂mechanism.␈α⊂If␈α∂the␈α⊂call␈α⊂is␈α∂a
␈↓ α←␈↓␈↓αPUSHJ␈↓,␈α∞control␈α∂passes␈α∞directly␈α∂to␈α∞the␈α∂machine␈α∞language␈α∂code␈α∞and␈α∂we␈α∞will
␈↓ α←␈↓not intercept the call.
␈↓"β␈↓ α←␈↓␈↓ β'In␈α∃most␈α∃implementations␈α∃of␈α∀LISP␈α∃the␈α∃programmer␈α∃may␈α∀selectively
␈↓ α←␈↓replace␈α
a␈α∞␈↓αCALL␈↓␈α
by␈α∞a␈α
␈↓αPUSHJ␈↓.␈α∞␈↓π 39␈↓␈α
A␈α∞␈↓αPUSHJ␈↓␈α
will␈α∞be␈α
executed␈α∞at␈α
machine
␈↓ α←␈↓speed,␈α⊗transfering␈α↔to␈α⊗known␈α↔location;␈α⊗whereas␈α⊗the␈α↔␈↓αCALL␈↓␈α⊗is␈α↔passed␈α⊗to
␈↓ α←␈↓␈↓αdecode␈↓ (page 316)␈α
and␈α
the␈α
function␈α
definition␈α
is␈α
looked␈α
up;␈α
therefore␈α
after␈αa
␈↓ α←␈↓program␈α∪is␈α∪debugged,␈α∪the␈α∪programmer␈α∪may␈α∪replace␈α∪the␈α∪␈↓αCALL␈↓␈α∪with␈α∪the
␈↓ α←␈↓␈↓αPUSHJ␈↓␈αand␈αthe␈αprograms␈αwill␈α
execute␈αfaster.␈αOn␈αsome␈αimplementations␈α
this
␈↓ α←␈↓action␈αis␈αreversible ([Ste pc]);␈αa␈αtable,␈αrelating␈αthe␈α␈↓αCALL␈↓s␈αto␈αthe␈α␈↓αPUSHJ␈↓s,␈αis
␈↓ α←␈↓built; when tracing is desired, the ␈↓αCALL␈↓ version is made available.␈↓π 40␈↓
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 39␈↓As we have seen, ␈↓αCALL␈↓s to functional variables won't be replaced.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 40␈↓Actually,␈α∞the␈α∞scheme␈α∞is␈α∞as␈α∞follows:␈α∞instead␈α∞of␈α∞assembling␈α∂the␈α∞␈↓αCALL␈↓
␈↓ α←␈↓into␈αa␈αmemory␈αlocation,␈αan␈αXCT␈αis␈αassembled;␈αthe␈αXCT␈αreferences␈αa␈αcopy␈α
of
␈↓ α←␈↓a␈α
table␈αwhich␈α
contains␈αthe␈α
actual␈α␈↓αCALL␈↓.␈α
The␈αuser␈α
may␈αreplace␈α
the␈α␈↓αCALL␈↓s␈α
by
␈↓ α←␈↓␈↓αPUSHJ␈↓,␈α∂but␈α∂also␈α⊂has␈α∂the␈α∂original␈α∂table␈α⊂available␈α∂to␈α∂replace␈α⊂the␈α∂modified
␈↓ α←␈↓version␈α
when␈α
tracing␈α
is␈α
desired.␈α∞This␈α
XCT␈α
trick␈α
has␈α
the␈α∞additional␈α
benefit
␈↓ α←␈↓of␈α⊂allowing␈α⊂several␈α⊂users␈α⊂to␈α⊃share␈α⊂a␈α⊂"pure"␈α⊂piece␈α⊂of␈α⊂program,␈α⊃while␈α⊂some
␈↓ α←␈↓people␈αare␈αtracing␈αand␈αsome␈αpeople␈αare␈αnot.␈αThe␈αadded␈αflexibility␈αmore␈αthan
␈↓ α←␈↓compensates for the slight decrease in speed.
␈↓ α←␈↓␈↓6.23␈↓ λ≠Debugging in LISP 369␈↓
␈↓"β␈↓ α←␈↓␈↓ β'A␈α⊂variant␈α⊂of␈α∂this␈α⊂tracing␈α⊂scheme␈α∂can␈α⊂be␈α⊂used␈α∂to␈α⊂monitor␈α⊂␈↓αSET␈↓s␈α∂and
␈↓ α←␈↓␈↓αSETQ␈↓s.␈α
We␈α
can␈α
modify␈α
their␈α
definitions␈α
to␈α
print␈α
the␈α
name␈α
of␈α∞the␈α
variable
␈↓ α←␈↓and␈αthe␈αnew␈αvalue,␈αperform␈αthe␈αassignment,␈αand␈αreturn.␈α This␈αtechnique␈αcan
␈↓ α←␈↓be␈α
lost␈αin␈α
some␈αcompiled␈α
code.␈αIf␈α
we␈αcompile␈α
local␈αvariables␈α
as␈αreferences␈α
into
␈↓ α←␈↓the␈α∞value␈α∞stack,␈α∞we␈α∞have␈α∞lost␈α∞both␈α
the␈α∞names␈α∞and␈α∞the␈α∞ability␈α∞to␈α∞trace␈α
their
␈↓ α←␈↓behavior.␈α∂ Variable␈α∂references␈α∂which␈α∂use␈α∂␈↓αPUTV␈↓␈α∂and␈α∂␈↓αGETV␈↓␈α∂can␈α⊂be␈α∂traced
␈↓ α←␈↓like␈α
␈↓αCALL␈↓.␈α
In␈α
fact,␈α
on␈α
␈↓↓SM␈↓,␈α
we␈α
have␈α
an␈α
operation␈α
analogous␈α
to␈α
␈↓αPUSHJ␈↓,␈α
so
␈↓ α←␈↓the␈α∪␈↓αCALL-PUSHJ␈↓␈α∪technique␈α∪is␈α∪open␈α∪to␈α∪us.␈α∪ ␈↓αPUTV␈↓␈α∪and␈α∪␈↓αGETV␈↓␈α∪can␈α∩be
␈↓ α←␈↓implemented as hardware ␈↓αMOVEM␈↓ and ␈↓αMOVE␈↓ instructions.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αtrace␈αfacility␈αis␈αa␈αdebugging␈αfeature␈αwhich␈αhas␈αbeen␈αadapted␈αfrom
␈↓ α←␈↓the␈α↔batch-processsing␈α⊗versions␈α↔of␈α↔LISP.␈α⊗There␈α↔is␈α⊗a␈α↔related,␈α↔but␈α⊗more
␈↓ α←␈↓interactive,␈αversion␈αof␈αthis␈αtechnique␈αcalled␈αthe␈α␈↓↓break␈↓␈αpackage.␈α In␈αthis␈αmode
␈↓ α←␈↓of␈α
tracing,␈αthe␈α
user␈αcan␈α
specify␈α
that␈αthe␈α
program␈αshould␈α
halt␈α
on␈αrecognition
␈↓ α←␈↓of␈α
certain␈α
conditions.␈α
If␈α
that␈α
halt␈α
occurs,␈α
the␈α
break␈α
package␈α
is␈α
entered␈αand␈α
the
␈↓ α←␈↓user␈α∂may␈α∂then␈α∂type␈α∂commands␈α⊂which␈α∂survey␈α∂the␈α∂state␈α∂of␈α⊂the␈α∂computation.
␈↓ α←␈↓Expressions␈α⊗may␈α⊗be␈α⊗evaluated,␈α⊗which␈α⊗may␈α⊗themselves␈α⊗enter␈α⊗the␈α∃break
␈↓ α←␈↓package␈α
recursively.␈α
If␈α
desired,␈α
the␈α
LISP␈α
editor␈α
may␈α
be␈α
called␈α
either␈α
to␈α
edit
␈↓ α←␈↓function␈α
definitions␈α
or␈α
to␈αedit␈α
an␈α
expression␈α
on␈αthe␈α
actual␈α
control␈α
stack␈αof␈α
the
␈↓ α←␈↓current computation.
␈↓"β␈↓ α←␈↓␈↓ β'Since␈α∂it␈α∞is␈α∂difficult␈α∞to␈α∂predetermine␈α∞when␈α∂a␈α∞computation␈α∂may␈α∞require
␈↓ α←␈↓debugging,␈α∪several␈α∪systems␈α∪supply␈α∩an␈α∪interrupt␈α∪system␈α∪analogous␈α∪to␈α∩that
␈↓ α←␈↓found␈αin␈αhardware␈αmachines.␈αStriking␈α
certain␈αkeys␈αmay␈αthen␈αcause␈α
interrupts
␈↓ α←␈↓to␈α⊂the␈α⊃break␈α⊂package,␈α⊂just␈α⊃as␈α⊂if␈α⊂a␈α⊃break␈α⊂condition␈α⊃were␈α⊂pre-programmed.
␈↓ α←␈↓Such␈α∂a␈α⊂feature␈α∂is␈α∂useful␈α⊂in␈α∂conjunction␈α⊂with␈α∂the␈α∂trace␈α⊂package.␈α∂If␈α⊂a␈α∂trace
␈↓ α←␈↓indicates␈α⊂to␈α⊂the␈α⊂user␈α⊂that␈α⊂the␈α⊂computation␈α⊂is␈α⊂not␈α⊂performing␈α⊂according␈α∂to
␈↓ α←␈↓expectation␈αthen␈αan␈αinterrupt␈αkey␈αcan␈αbe␈αstruck␈αand␈αthe␈αcomputation␈αwill␈αbe
␈↓ α←␈↓suspended.
␈↓"β␈↓ α←␈↓␈↓ β'User-definable␈α⊂interrupt␈α⊂systems␈α⊂apply␈α∂to␈α⊂other␈α⊂areas␈α⊂of␈α∂computation
␈↓ α←␈↓than␈αthat␈αof␈αdebugging.␈αThe␈αmost␈αwell-developed␈αsystem␈αis␈αthat␈αof␈α
MacLISP.
␈↓ α←␈↓The␈α⊃ability␈α∩to␈α⊃selectively␈α∩trace␈α⊃the␈α∩execution,␈α⊃coupled␈α∩with␈α⊃the␈α∩ability␈α⊃to
␈↓ α←␈↓interrupt␈αa␈αcomputation,␈αallows␈α
the␈αuser␈αto␈αexamine␈αcomputations␈α
which␈αare
␈↓ α←␈↓suspected of divergence.
␈↓ α←␈↓␈↓370 Storage Structures and Efficiency␈↓
87.␈↓
␈↓"β␈↓ α←␈↓␈↓ ␈↓↓CHAPTER 7␈↓
␈↓"β␈↓ α←␈↓␈↓ Storage Structures and Efficiency␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬g␈↓↓7.1 Introduction␈↓
␈↓"β␈↓ α←␈↓This␈α
chapter␈α
reconciles␈α∞some␈α
of␈α
the␈α
generality␈α∞of␈α
LISP␈α
with␈α
the␈α∞realities␈α
of
␈↓ α←␈↓contemporary␈α_data␈α_structures␈α_and␈α_machine␈α_organization.␈α_ Though␈α_any
␈↓ α←␈↓algorithm␈αcan␈α
be␈αcoded␈α
in␈αterms␈αof␈α
manipulations␈αof␈α
binary␈αtrees,␈αoften␈α
there
␈↓ α←␈↓are␈α⊗more␈α↔efficient␈α⊗organizations␈α⊗of␈α↔data.␈α⊗ For␈α⊗example,␈α↔our␈α⊗numerical
␈↓ α←␈↓algorithms␈αcould␈αbe␈αexpressed␈αas␈αlist␈αalgorithms␈αusing␈α␈↓α( )␈↓,␈α␈↓α(( ))␈↓,␈α␈↓α(( ) ( ))␈↓,␈αand
␈↓ α←␈↓so␈α⊃on,␈α∩as␈α⊃representations␈α∩for␈α⊃␈↓α0␈↓,␈α∩␈↓α1␈↓,␈α⊃␈↓α2␈↓,␈α∩respectively.␈α⊃ Most␈α∩machines␈α⊃supply
␈↓ α←␈↓hardware␈α~arithmetic␈α~representations␈α~and␈α~operations,␈α~making␈α≠such␈α~list
␈↓ α←␈↓representations unnecessary.
␈↓"β␈↓ α←␈↓␈↓ β'At␈α∀the␈α∀next␈α∀level␈α∀of␈α∃data␈α∀organization␈α∀are␈α∀vectors␈α∀and␈α∃arrays␈α∀of
␈↓ α←␈↓numerals.␈α∪ These␈α∀data␈α∪structures␈α∀could␈α∪also␈α∪be␈α∀stored␈α∪in␈α∀a␈α∪list-structure
␈↓ α←␈↓format␈α∀and␈α∀individual␈α∀components␈α∀could␈α∀be␈α∀accessed␈α∀by␈α∀␈↓αcar-cdr␈↓␈α∀chains.
␈↓ α←␈↓However,␈α⊗most␈α∃machines␈α⊗have␈α∃a␈α⊗hardware␈α∃organization␈α⊗which␈α⊗can␈α∃be
␈↓ α←␈↓exploited␈α~to␈α~increase␈α~accessing␈α→efficiency␈α~over␈α~the␈α~list␈α→representation.
␈↓ α←␈↓Sequential␈αstorage␈αfor␈αelements,␈αoften␈αcoupled␈αwith␈αhardware␈α
index␈αregisters
␈↓ α←␈↓for fast access to elements, makes a more effective representation.
␈↓"β␈↓ α←␈↓␈↓ β'Similarly,␈α∞strings␈α∞can␈α
be␈α∞represented␈α∞as␈α
lists␈α∞of␈α∞characters.␈α∞ The␈α
string
␈↓ α←␈↓processing␈α∀operations␈α∀are␈α∀expressible␈α∃as␈α∀LISP␈α∀algorithms;␈α∀again,␈α∃this␈α∀is
␈↓ α←␈↓usually␈αnot␈αthe␈αmost␈α
reasonable␈αrepresentation.␈αSome␈αmachines␈αsupply␈α
special
␈↓ α←␈↓hardware aids for string operations.
␈↓ α←␈↓␈↓7.1␈↓ λ↑Introduction 371␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Even␈αat␈α
the␈αlevel␈α
of␈αlist-structure␈αoperations,␈α
simple␈αbinary␈α
trees␈αmight
␈↓ α←␈↓not␈αbe␈αthe␈αmost␈αexpeditious␈αrepresentation.␈α Also␈αmany␈αof␈αthe␈αalgorithms␈αwe
␈↓ α←␈↓have presented in LISP are overly wasteful of computation time.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α∃are␈α∃no␈α∃general␈α∃rules␈α∃for␈α∃selecting␈α∃data␈α∃representations␈α∀and
␈↓ α←␈↓chosing␈α∂programming␈α∂style.␈α∂Efficiency␈α∂must␈α∂be␈α∂balanced␈α∂against␈α∂generality.
␈↓ α←␈↓The␈αchosen␈αrepresentation␈α
must␈αmatch␈αthe␈α
machine␈αand␈αthe␈αproblem␈α
domain
␈↓ α←␈↓being␈α∪studied.␈α∪ If␈α∪the␈α∪problem␈α∩is␈α∪strictly␈α∪numerical,␈α∪then␈α∪list-structure␈α∩is
␈↓ α←␈↓overly␈α
general.␈α
If␈α
simple␈α
string␈α
manipulation␈α
is␈α
sufficient,␈α
then␈α
list␈α
processing
␈↓ α←␈↓also␈α
may␈α
be␈α
too␈α
general.␈αThere␈α
are␈α
many␈α
applications␈α
of␈α
list␈αprocessing␈α
which
␈↓ α←␈↓are␈α
so␈α
sufficiently␈α
well behaved␈α∞that␈α
complex␈α
devices␈α
like␈α∞garbage␈α
collectors
␈↓ α←␈↓are␈α⊃unnecessary.␈α⊂ However,␈α⊃understanding␈α⊂the␈α⊃programming␈α⊂art␈α⊃in␈α⊃a␈α⊂rich
␈↓ α←␈↓environment␈α_such␈α→as␈α_LISP,␈α_prepares␈α→the␈α_programmer␈α_to␈α→apply␈α_these
␈↓ α←␈↓techniques␈αin␈αa␈αmeaningful␈αway.␈α Many␈αtimes␈αa␈αrepresentation␈αin␈αLISP␈αis␈αall
␈↓ α←␈↓that␈α
is␈αneeded;␈α
a␈α
"throw-away␈αimplementation"␈α
may␈αanswer␈α
the␈α
question.␈α A
␈↓ α←␈↓clean␈α∂representation␈α∂with␈α∂comprehensible␈α∂algorithms␈α∂is␈α∂developed.␈α∂ Once␈α∂␈↓¬a␈↓
␈↓ α←␈↓representation is developed, it is easy to get better ones.
␈↓"β␈↓ α←␈↓␈↓ ¬:␈↓↓7.2 Vectors and Arrays␈↓
␈↓"β␈↓ α←␈↓␈↓↓Vectors␈↓. Vectors,␈α∂also␈α∞known␈α∂as␈α∂one-dimensional␈α∞arrays,␈α∂are␈α∂usually␈α∞stored
␈↓ α←␈↓sequentially␈α
in␈α∞memory.␈α
Simple␈α∞vectors␈α
are␈α∞usually␈α
stored␈α∞one␈α
element␈α∞to␈α
a
␈↓ α←␈↓memory␈α∃location␈α∃though␈α∃this␈α∃is␈α∃not␈α∃a␈α∃necessity;␈α∃for␈α∃example,␈α⊗a␈α∃vector
␈↓ α←␈↓representation␈α∩of␈α∩a␈α∩complex␈α⊃number␈α∩may␈α∩be␈α∩stored␈α⊃as␈α∩pairs␈α∩of␈α∩cells.␈α⊃ If
␈↓ α←␈↓vectors␈α∃of␈α∀nonhomogeneous␈α∃data␈α∃modes␈α∀are␈α∃contemplated,␈α∃each␈α∀element
␈↓ α←␈↓would␈α∂include␈α∂type␈α∞information.␈α∂ Also,␈α∂we␈α∂have␈α∞seen␈α∂a␈α∂representation␈α∂of␈α∞a
␈↓ α←␈↓stack␈α∂as␈α∞a␈α∂(sequential)␈α∞vector␈α∂with␈α∞access␈α∂made␈α∞via␈α∂a␈α∞global␈α∂pointer␈α∂to␈α∞the
␈↓ α←␈↓vector.␈α
In␈α
any␈αcase,␈α
most␈α
languages␈α
make␈αsome␈α
restrictions␈α
on␈α
the␈αbehavior
␈↓ α←␈↓of␈αvectors␈αsuch␈αthat␈αefficient␈αaccessing␈αof␈αelements␈αcan␈αbe␈αmade.␈α Vectors␈αare
␈↓ α←␈↓an␈α∩attractive␈α∩representation␈α∩when␈α∩the␈α∩size␈α∩of␈α∩data␈α∩objects␈α∩will␈α∩not␈α∩vary.
␈↓ α←␈↓Given␈αsuch␈α
a␈αstatic␈αbehavior,␈α
machines␈αcan␈αperform␈α
access␈αand␈α
updating␈αof
␈↓ α←␈↓the elements rapidly.
␈↓"β␈↓ α←␈↓␈↓↓Arrays␈↓. Arrays␈αare␈αvectors␈αwhich␈αallow␈αvectors␈αas␈αelements.␈α For␈αexample,␈αa
␈↓ α←␈↓two-dimensional␈αarray␈αis␈αa␈αvector,␈αwhose␈αelements␈αare␈αvectors␈αof␈αindividuals.
␈↓ α←␈↓We␈α∀will␈α∀restrict␈α∀attention␈α∀to␈α∀array␈α∀whose␈α∀elements␈α∀are␈α∀all␈α∀of␈α∀the␈α∀same
␈↓ α←␈↓dimensions;␈α∩efficient␈α∩representation␈α∩of␈α⊃more␈α∩general␈α∩arrays,␈α∩called␈α⊃␈↓↓ragged
␈↓ α←␈↓↓arrays␈↓,␈α∪will␈α∪be␈α∪examined␈α∪in␈α∪Section 7.13.␈α∪ We␈α∪will␈α∪restrict␈α∪our␈α∩attention
␈↓ α←␈↓further␈α
to␈αtwo-dimensional␈α
arrays,␈αthough␈α
most␈αof␈α
the␈α
discussion␈αgeneralizes
␈↓ α←␈↓very␈α
naturally.␈α
Since␈α
most␈α
machine␈α
memories␈α
are␈α
organized␈α
as␈αlinear␈α
devices,
␈↓ α←␈↓we␈α∩map␈α∪arrays␈α∩onto␈α∪a␈α∩linear␈α∪representation.␈α∩ A␈α∪common␈α∩implementation
␈↓ α←␈↓stores␈αthe␈αarray␈αby␈αrows;␈αthat␈αis,␈αeach␈αrow␈αis␈αstored␈αsequentially␈α-␈αfirst,␈αrow␈α1;
␈↓ α←␈↓then␈α⊃row␈α⊂2, ...␈α⊃ and␈α⊂so␈α⊃on.␈α⊂ A␈α⊃simple␈α⊂calculation␈α⊃finds␈α⊂the␈α⊃location␈α⊃of␈α⊂an
␈↓ α←␈↓arbitrary␈α∞element,␈α∂A[i;j],␈α∞given␈α∂the␈α∞location␈α∂of␈α∞the␈α∂first␈α∞element␈α∂A[1;1]␈α∞and
␈↓ α←␈↓the length of each row of the array. For an array A[1:M; 1:N],
␈↓ α←␈↓␈↓372 Storage Structures and Efficiency␈↓
(7.2␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧Wloc[A[i;j]] = loc [A[1;1]] + (i-1)*N + (j-1)
␈↓"∀␈↓ α←␈↓In␈αlanguages␈αlike␈αFortran␈αwhich␈αrequire␈αthat␈αthe␈αsize␈αof␈αthe␈αarray␈αbe␈αknown
␈↓ α←␈↓at␈αcompile-time␈αthe␈αcompiler␈αcan␈αgenerate␈αthe␈αaccessing␈αcode␈αas␈αreferences␈αto
␈↓ α←␈↓specific␈α∂memory␈α∂locations.␈α∂ Languages,␈α⊂like␈α∂Algol␈α∂60␈α∂and␈α∂some␈α⊂versions␈α∂of
␈↓ α←␈↓LISP,␈αallow␈αthe␈αsize␈αof␈αan␈αarray␈αto␈αbe␈αdetermined␈αat␈αrun-time.␈α Algol␈α60,␈αfor
␈↓ α←␈↓example,␈αrequires␈αthe␈αdeclaration␈αof␈αthe␈αtype␈α(real,␈αboolean,␈αetc.)␈αof␈αthe␈αarray
␈↓ α←␈↓and␈α⊂specification␈α⊃of␈α⊂the␈α⊂number␈α⊃of␈α⊂dimensions␈α⊂in␈α⊃the␈α⊂array,␈α⊂but␈α⊃the␈α⊂size
␈↓ α←␈↓specification␈α↔of␈α↔each␈α⊗dimension␈α↔can␈α↔be␈α⊗postponed␈α↔until␈α↔run-time.␈α⊗ To
␈↓ α←␈↓implement␈α∂this␈α∂flexibility,␈α∂a␈α∞␈↓↓dope␈α∂vector␈↓␈α∂is␈α∂introduced.␈α∞A␈α∂dope␈α∂vector␈α∂is␈α∞a
␈↓ α←␈↓header␈α⊂or␈α⊂␈↓↓descriptor␈↓␈α⊂associated␈α⊃with␈α⊂the␈α⊂area␈α⊂containing␈α⊂the␈α⊃actual␈α⊂array
␈↓ α←␈↓elements.␈αThe␈αinformation␈αin␈αthe␈αdope␈αvector␈αtells␈αthe␈αfunctions␈αwhich␈αaccess
␈↓ α←␈↓the␈αarray␈αhow␈αto␈αtreat␈αthe␈αdata.␈α Type␈αand␈αdimensionality␈αare␈αtypical␈αentries
␈↓ α←␈↓in dope vectors.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊂compiler␈α⊂can␈α⊂determine␈α⊂the␈α⊂size␈α⊂of␈α⊂the␈α⊂dope␈α⊂vector,␈α⊂but␈α∂cannot
␈↓ α←␈↓determine␈α↔its␈α↔contents.␈α⊗ The␈α↔dope␈α↔vector␈α↔is␈α⊗filled␈α↔in␈α↔when␈α↔the␈α⊗array
␈↓ α←␈↓declaration␈α∩is␈α∩executed;␈α∪at␈α∩that␈α∩time␈α∩the␈α∪array␈α∩bounds␈α∩are␈α∪known.␈α∩ The
␈↓ α←␈↓compiler␈αcannot␈α
allocate␈αspace␈αfor␈α
the␈αarray␈αelements;␈α
the␈αallocation␈α
must␈αbe
␈↓ α←␈↓done␈α⊃at␈α⊂run-time.␈α⊃ At␈α⊂that␈α⊃time␈α⊂we␈α⊃allocate␈α⊂space␈α⊃and␈α⊂complete␈α⊃the␈α⊂dope
␈↓ α←␈↓vector. All references to array elements must use the dope vector.
␈↓"β␈↓ α←␈↓␈↓ β'Assume␈α∀that␈α∪the␈α∀array␈α∀elements␈α∪are␈α∀stored␈α∀by␈α∪rows.␈α∀ Look␈α∀at␈α∪the
␈↓ α←␈↓calculation␈α⊂of␈α⊂the␈α⊂location␈α⊂of␈α⊂element␈α⊂A[i;j].␈α⊂ For␈α⊂specific␈α⊂execution␈α⊃of␈α⊂an
␈↓ α←␈↓array␈α∂declaration␈α∂much␈α∂of␈α∂this␈α∂information␈α∂is␈α∂constant;␈α∂the␈α∂location␈α∂of␈α∞the
␈↓ α←␈↓array elements, in particular, A[1;1] and the number of columns N are fixed.
␈↓"β␈↓ α←␈↓␈↓ β'Thus we rewrite the calculation as:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ βK constant part␈↓ ε3variable part
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ βK[loc [A[1;1]]-N-1] +␈↓ ε3 (i*N+j)
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α⊃constant␈α∩part␈α⊃is␈α∩stored␈α⊃in␈α⊃the␈α∩dope␈α⊃vector.␈α∩ When␈α⊃we␈α∩wish␈α⊃to
␈↓ α←␈↓reference␈αan␈αelement␈αA[i;j]␈αwe␈αneed␈αonly␈αcompute␈αthe␈αvariable␈αpart␈αand␈αadd
␈↓ α←␈↓it to the constant part.
␈↓"β␈↓ α←␈↓␈↓ β'The dope vector for A [1:M; 1:N] perhaps might contain
␈↓"⊃␈↓ α←␈↓
⊂ααααααααααααααα⊃
␈↓"␈↓ α←␈↓
~ 2 ~
␈↓"␈↓ α←␈↓
εααααααπααααααααλ
␈↓"␈↓ α←␈↓
~ 1 ~ M ~
␈↓"␈↓ α←␈↓
εααααααβααααααααλ
␈↓"␈↓ α←␈↓
~ 1 ~ N ~
␈↓"␈↓ α←␈↓
εαααααα∀ααααααααλ
␈↓"␈↓ α←␈↓
~ constant part ~
␈↓"␈↓ α←␈↓
εαααααααααααααααλ
␈↓"␈↓ α←␈↓
~ . . . ~
␈↓"␈↓ α←␈↓
array elements
␈↓"␈↓ α←␈↓
~ . . . ~
␈↓"␈↓ α←␈↓
%ααααααααααααααα$
␈↓"∀␈↓ α←␈↓There␈α⊂is␈α∂another␈α⊂scheme␈α⊂for␈α∂storing␈α⊂arrays␈α∂which␈α⊂is␈α⊂used␈α∂in␈α⊂some␈α⊂of␈α∂the
␈↓ α←␈↓Burroughs␈α∪machines ([Org 71], [Dor 76]).␈α∀Each␈α∪row␈α∪is␈α∀stored␈α∪sequentially
␈↓ α←␈↓␈↓7.2␈↓ λ∂Vectors and Arrays 373␈↓
␈↓"β␈↓ α←␈↓and␈αaccess␈αto␈αseparate␈αrows␈αis␈α
made␈αthrough␈αa␈αdevice␈αcalled␈αa␈α
␈↓↓mother-vector␈↓.
␈↓ α←␈↓The mother-vector is a vector of pointers to the rows.
␈↓"β␈↓ α←␈↓␈↓ β'Thus,
␈↓"⊃␈↓ α←␈↓
⊂ααααααα⊃ ⊂αααααααααααααααααα␈↓ λkαααα␈↓ '⊃
␈↓"␈↓ α←␈↓
~ #ααβααα→~ A␈↓β11␈↓
A␈↓β12␈↓
...␈↓ π␈A␈↓β1N␈↓
␈↓ λk~
␈↓"␈↓ α←␈↓
εαααααααλ %αααααααααααααααααα␈↓ λkαααα␈↓ '$
␈↓"␈↓ α←␈↓
~ #ααβαα→ . . .
␈↓"␈↓ α←␈↓
εαααααααλ
␈↓"␈↓ α←␈↓
. . .
␈↓"␈↓ α←␈↓
εαααααααλ ⊂αααααααααααααααααα␈↓ λkαααα␈↓ '⊃
␈↓"␈↓ α←␈↓
~ #ααβααα→~ A␈↓βM1␈↓
A␈↓βM2␈↓
...␈↓ π␈A␈↓βMN␈↓
␈↓ λk~
␈↓"␈↓ α←␈↓
%ααααααα$ %αααααααααααααααααα␈↓ λkαααα␈↓ '$
␈↓"∀␈↓ α←␈↓␈↓ β'Notice␈α∪that␈α∀the␈α∪accessing␈α∀computation␈α∪is␈α∀very␈α∪inexpensive.␈↓π 1␈↓␈α∀On␈α∪a
␈↓ α←␈↓virtual␈α
memory␈α
machine␈αthis␈α
array␈α
organization␈α
can␈αbe␈α
helpful;␈α
all␈αrows␈α
need
␈↓ α←␈↓not␈α⊂be␈α⊂in␈α⊂memory␈α∂at␈α⊂once.␈α⊂ If␈α⊂an␈α∂access␈α⊂to␈α⊂a␈α⊂row␈α∂not␈α⊂in␈α⊂core␈α⊂is␈α⊂made,␈α∂a
␈↓ α←␈↓"page fault"␈α∩is␈α∪raised;␈α∩the␈α∩monitor␈α∪brings␈α∩the␈α∩row␈α∪into␈α∩memory␈α∪and␈α∩the
␈↓ α←␈↓computation␈α&continues.␈α& The␈α&mother-vector␈α&scheme␈α&generalizes␈α%to
␈↓ α←␈↓multidimensional␈α∂arrays,␈α∞and␈α∂can␈α∞also␈α∂be␈α∞used␈α∂in␈α∞conjunction␈α∂with␈α∂a␈α∞dope
␈↓ α←␈↓vector.
␈↓"β␈↓ α←␈↓␈↓ β'An␈α∃implementation␈α∀of␈α∃an␈α∃array␈α∀facility␈α∃in␈α∀LISP␈α∃might␈α∃include␈α∀a
␈↓ α←␈↓declaration:
␈↓"∀␈↓ α←␈↓␈↓αarray␈↓␈↓α[␈↓<identifier>;<type>;<bounds>;␈α...␈α;<bounds>],␈αwhere␈αthe␈αidentifier␈αnames
␈↓ α←␈↓␈↓ β'the␈α
array;␈α
the␈α
type␈α
could␈α
be␈α
numeric␈α
or␈α
S-expr;␈α
and␈α
finally␈α
a␈α
declaration
␈↓ α←␈↓␈↓ β'of␈α
upper␈α
and␈αlower␈α
bounds␈α
for␈α
each␈αdimension␈α
would␈α
be␈αneeded.␈α
␈↓αarray␈↓
␈↓ α←␈↓␈↓ β'is␈α⊂a␈α⊃special␈α⊂form␈α⊃whose␈α⊂effect␈α⊃is␈α⊂to␈α⊃make␈α⊂the␈α⊃array␈α⊂name␈α⊃a␈α⊂␈↓αSUBR␈↓,
␈↓ α←␈↓␈↓ β'whose code is the calculation of the dope vector. Thus,
␈↓"⊃␈↓ α←␈↓
⊂ααααααπαααα⊃ ⊂ααααααααααααα⊃
␈↓"␈↓ α←␈↓
~ SUBR ~ #αβ→αα→~ dope vector ~
␈↓"␈↓ α←␈↓
%αααααα∀αααα$ ~ calculation ~
␈↓"␈↓ α←␈↓
εαααααααααααααλ
␈↓"␈↓ α←␈↓
~ array ~
␈↓"␈↓ α←␈↓
~ elements ~
␈↓"␈↓ α←␈↓
. . .
␈↓"␈↓ α←␈↓
%ααααααααααααα$
␈↓"∀␈↓ α←␈↓If␈α∞we␈α∞are␈α∞to␈α∞store␈α∞S-exprs␈α∂in␈α∞the␈α∞array,␈α∞then␈α∞the␈α∞garbage␈α∞collector␈α∂must␈α∞be
␈↓ α←␈↓able to mark the entries. This is the reason for including type information.
␈↓"β␈↓ α←␈↓␈↓ β'When␈αan␈α
array␈αelement␈αis␈α
to␈αbe␈αreferenced,␈α
the␈αsubscripts␈αare␈α
evaluated
␈↓ α←␈↓(since␈αthe␈αarray␈αname␈αwas␈αdeclared␈αas␈αa␈α␈↓αSUBR␈↓)␈αand␈αthe␈αdope␈αvector␈αcode␈αis
␈↓ α←␈↓executed. That computation results in a reference to the appropriate cell.
␈↓"β␈↓ α←␈↓␈↓ β'We also must be able to store information in the array.
␈↓"∀␈↓ α←␈↓␈↓αstore[␈↓<name>[<subscr>;␈α∞...␈α∞;<subscr>];<value>]␈α
:␈α∞␈↓αstore␈↓␈α∞is␈α
a␈α∞special␈α∞form␈α
whose
␈↓ α←␈↓␈↓ β'effect is to store the value of <value> in the designated array element.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 1␈↓However␈αaccess␈αby␈αarray␈αcolumns␈αcan␈αbe␈αexpensive.␈αIf␈αeach␈αrow␈αis␈αon
␈↓ α←␈↓a separate page in the machine, the access overhead can be substantial.
␈↓ α←␈↓␈↓374 Storage Structures and Efficiency␈↓
(7.2␈↓
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∞have␈α∞discussed␈α∞storage␈α∞allocation␈α∞and␈α∞accessing␈α∞of␈α∞array␈α∞elements.
␈↓ α←␈↓Important␈α∞distinctions␈α∞in␈α∂language␈α∞design␈α∞appear␈α∞in␈α∂discussing␈α∞deallocation
␈↓ α←␈↓of␈α⊂array␈α⊂space.␈α⊂ Typical␈α⊂Algol-like␈α⊂languages␈α⊂impose␈α⊂a␈α⊂stack␈α⊂discipline␈α⊂on
␈↓ α←␈↓storage␈α
management.␈α
This␈α∞imples␈α
that␈α
the␈α∞arrray␈α
elements␈α
may␈α∞be␈α
allocated
␈↓ α←␈↓in␈α∂the␈α∂run-time␈α∂stack.␈α∂It␈α∞also␈α∂implies␈α∂that␈α∂the␈α∂elements␈α∂become␈α∞inaccessible
␈↓ α←␈↓once␈α↔the␈α↔block␈α↔which␈α↔allocated␈α_that␈α↔array␈α↔has␈α↔been␈α↔exited.␈α_ This␈α↔is
␈↓ α←␈↓implemented␈αby␈αpopping␈αthe␈αarray␈αfrom␈αthe␈αstack.␈αThere␈αare␈αtwo␈αways␈αfor␈αa
␈↓ α←␈↓language␈α⊃to␈α⊃assure␈α⊂that␈α⊃no␈α⊃references␈α⊂to␈α⊃"inaccessible"␈α⊃elements␈α⊃can␈α⊂occur
␈↓ α←␈↓(such␈αreferences␈αare␈αcalled␈α␈↓↓dangling␈αreferences␈↓.)␈αEither␈αrestrict␈αthe␈αsemantics
␈↓ α←␈↓of␈α
the␈α
language␈α
such␈α
that␈α
no␈α∞such␈α
references␈α
can␈α
occur␈α
(Algol␈α
60),␈α∞or␈α
allow
␈↓ α←␈↓constructs␈α
which␈α
␈↓¬may␈↓␈αcause␈α
dangling␈α
references,␈αbut␈α
declare␈α
any␈αoccurrence␈α
to
␈↓ α←␈↓be an error (Algol 68).
␈↓"β␈↓ α←␈↓␈↓ β'LISP-like␈α
languages␈α
suppose␈α
that␈α
data␈α
structures␈α
are␈α
to␈α
be␈α∞retained␈α
as
␈↓ α←␈↓long␈α⊃as␈α⊃they␈α⊃are␈α⊃accessible;␈α⊃that␈α⊃treatement␈α⊃is␈α⊃also␈α⊃given␈α⊃to␈α⊃LISP␈α⊃arrays.
␈↓ α←␈↓Therefore␈α∂arrays␈α∂are␈α∂allocated␈α∂and␈α∂deallocated␈α∂in␈α∂a␈α∂manner␈α∂similar␈α∂to␈α∂the
␈↓ α←␈↓␈↓αcons␈↓␈αoperation␈αfor␈αS-exprs;␈αsequential␈αblocks␈αare␈αmaintained␈αin␈αa␈αfree␈αlist;␈αwe
␈↓ α←␈↓will say more about this in Section 7.13.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∂two␈α∂management␈α∂philosophies␈α∞for␈α∂deallocation␈α∂of␈α∂data␈α∞structures
␈↓ α←␈↓are␈α⊂characterized␈α⊂as␈α⊂the␈α⊂␈↓↓deletion␈α∂strategy␈↓␈α⊂and␈α⊂the␈α⊂␈↓↓retention␈α⊂strategy␈↓;␈α∂see
␈↓ α←␈↓[Ber 71].
␈↓"β␈↓ α←␈↓␈↓ ε≡␈↓↓Problem␈↓
␈↓"→␈↓ α←␈↓1. Implement␈αa␈αstack␈αin␈αLISP␈αfirst␈αusing␈αlists␈αor␈αdotted␈αpairs,␈αthen␈αusing␈αan
␈↓ α←␈↓␈↓ β∂array. Include implementations of the stack operations.
␈↓"β␈↓ α←␈↓␈↓ ¬∃␈↓↓7.3 Strings and Linear LISP␈↓
␈↓"β␈↓ α←␈↓On␈αpage 268␈αwe␈αdiscussed␈αone␈αrepresentation␈αfor␈αLISP␈αprint␈αnames:␈αa␈α
linked
␈↓ α←␈↓list␈α
of␈α
full␈α
words;␈α
each␈α
full␈α
word␈α
contained␈α
a␈α
segment␈α
of␈α
the␈α
atom␈α
name.␈α
Print
␈↓ α←␈↓names␈α∂are␈α∞a␈α∂special␈α∞instance␈α∂of␈α∞a␈α∂data␈α∞structure␈α∂named␈α∞strings;␈α∂our␈α∂use␈α∞of
␈↓ α←␈↓strings␈α
in␈α
LISP␈α
has␈α
been␈α
restricted␈α
to␈α
manipulating␈α
string␈α
constants.␈α
In␈α
this
␈↓ α←␈↓section␈α∩we␈α∩will␈α⊃discuss␈α∩alternative␈α∩representations␈α⊃for␈α∩strings,␈α∩and␈α⊃discuss
␈↓ α←␈↓further␈α
operations␈α
on␈α
string␈α
objects.␈α
Most␈α
production␈α
LISP␈α
systems␈α∞have␈α
a
␈↓ α←␈↓comprehensive␈αset␈αof␈αstring␈αoperations.␈α As␈αwith␈αnumbers␈αand␈αvectors,␈αstring
␈↓ α←␈↓operations␈αcould␈αeasily␈αbe␈αrepresented␈α
as␈αoperations␈αon␈αS-exprs;␈αhowever␈αit␈α
is
␈↓ α←␈↓frequently␈α∩more␈α∩efficient␈α∩to␈α∪represent␈α∩strings␈α∩as␈α∩a␈α∩separate␈α∪abstract␈α∩data
␈↓ α←␈↓structure.
␈↓"β␈↓ α←␈↓␈↓ β'Each␈αstring␈αobject␈αis␈αa␈αsequence␈αof␈αcharacters.␈αThe␈αelements␈αof␈αa␈αstring
␈↓ α←␈↓may␈α⊂not␈α⊂be␈α⊂strings;␈α⊂this␈α⊂is␈α⊂the␈α⊂essential␈α⊂difference␈α⊂between␈α⊃sequences␈α⊂and
␈↓ α←␈↓strings.␈α∀ That␈α∪simplification␈α∀of␈α∪data␈α∀structures␈α∪introduces␈α∀some␈α∪different
␈↓ α←␈↓aspects␈αof␈αstorage␈αmanagement.␈αIt␈αis␈αthese␈αissues␈αwhich␈αwe␈αwill␈αemphasize␈αin
␈↓ α←␈↓this section.
␈↓ α←␈↓␈↓7.3␈↓ π[Strings and Linear LISP 375␈↓
␈↓"β␈↓ α←␈↓␈↓ β'The␈αprimitive␈αstring␈αmanipulations␈α-- constructors␈αselectors,␈αrecognizers,
␈↓ α←␈↓and␈α∂equality --␈α∞are␈α∂similar␈α∞to␈α∂those␈α∞for␈α∂sequences.␈α∞ Therefore␈α∂we␈α∂use␈α∞LISP
␈↓ α←␈↓M-expression␈αsyntax␈αwhen␈α
describing␈αthe␈αoperations;␈α
for␈αthat␈αreason␈α
we␈αcall
␈↓ α←␈↓the␈αstring␈αlanguage,␈α␈↓↓linear␈αLISP␈↓.␈α The␈αimplementation␈αallows␈αthe␈αcreation␈αof
␈↓ α←␈↓strings␈α∂of␈α∂arbitrary␈α∞length;␈α∂it␈α∂allows␈α∞the␈α∂generation␈α∂of␈α∞new␈α∂strings␈α∂and␈α∞the
␈↓ α←␈↓decomposition␈α⊂of␈α⊂existing␈α⊂strings.␈α⊂ Since␈α∂arbitrary␈α⊂length␈α⊂strings␈α⊂are␈α⊂to␈α∂be
␈↓ α←␈↓created,␈α
an␈αorganization␈α
similar␈αto␈α
free␈αspace␈α
will␈αbe␈α
used.␈αThe␈α
storage␈αarea
␈↓ α←␈↓for strings will be called ␈↓↓string space␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'String␈α⊃space␈α⊃is␈α⊃a␈α⊂linear␈α⊃sequence␈α⊃of␈α⊃cells;␈α⊂each␈α⊃cell␈α⊃can␈α⊃contain␈α⊂one
␈↓ α←␈↓character.␈α A␈αstring␈αwill␈αbe␈α
represented␈αas␈αa␈αsequence␈αof␈αcontiguous␈α
character
␈↓ α←␈↓cells.␈α The␈α
value␈αof␈αa␈α
string␈αvariable␈α
will␈αbe␈αrepresented␈α
as␈αa␈αpair,␈α
containing
␈↓ α←␈↓character␈αcount␈α
and␈αa␈αpointer␈α
to␈αthe␈αbeginning␈α
of␈αthe␈αcharacter␈α
sequence␈αin
␈↓ α←␈↓string space.
␈↓"β␈↓ α←␈↓␈↓ β'Thus,
␈↓"⊃␈↓ α←␈↓
⊂αααπααα⊃
␈↓"␈↓ α←␈↓
~ 3 ~ # ~
␈↓"␈↓ α←␈↓
%ααα∀αβα$
␈↓"␈↓ α←␈↓
~
␈↓"␈↓ α←␈↓
%α→ααα→⊃
␈↓"␈↓ α←␈↓
↓
␈↓"␈↓ α←␈↓
. . .ααααπαααπαααπαααπαααπαααπααα . . .
␈↓"␈↓ α←␈↓
A B B 1 D . . . string space
␈↓"␈↓ α←␈↓
. . .αααα∀ααα∀ααα∀ααα∀ααα∀ααα∀ααα . . .
␈↓"∀␈↓ α←␈↓encodes the string ␈↓αABB␈↓.
␈↓"β␈↓ α←␈↓There are two primitive selector functions: ␈↓αfirst␈↓ and ␈↓αrest␈↓.
␈↓"∀␈↓ α←␈↓␈↓αfirst[x]␈↓␈α
is␈α
the␈αfirst␈α
character␈α
of␈αthe␈α
string␈α
represented␈αby␈α
␈↓αx␈↓.␈α
␈↓αfirst␈↓␈αis␈α
undefined
␈↓ α←␈↓␈↓ β'for the empty string, ␈↓εe␈↓. For example,
␈↓"∀␈↓ α←␈↓␈↓ ¬#␈↓αfirst[ABC]␈↓ is ␈↓αA; first[␈↓εe␈↓α]␈↓ = ␈↓λB␈↓
␈↓"∀␈↓ α←␈↓␈↓αrest[x]␈↓␈α
is␈α
the␈α
string␈α
of␈α
characters␈α
which␈α
remains␈α
when␈α
the␈α
first␈α
character␈αof
␈↓ α←␈↓␈↓ β'the␈α∞string␈α∂is␈α∞deleted.␈α∞ ␈↓αrest␈↓␈α∂is␈α∞also␈α∞undefined␈α∂for␈α∞the␈α∞empty␈α∂string.␈α∞For
␈↓ α←␈↓␈↓ β'example,
␈↓"∀␈↓ α←␈↓␈↓ ¬q␈↓αrest[ABC]␈↓ is ␈↓αBC␈↓
␈↓"∀␈↓ α←␈↓There is one constructor primitive.
␈↓"∀␈↓ α←␈↓␈↓αconcat[x;y]␈↓␈αcreates␈αa␈αnew␈αstring.␈α ␈↓αx␈↓␈αis␈αa␈αcharacter;␈α␈↓αy␈↓␈αis␈αa␈αstring.␈α␈↓αconcat␈↓␈αforms␈αa
␈↓ α←␈↓␈↓ β'string consisting of the concatenation of ␈↓αx␈↓ with ␈↓αy␈↓. For example,
␈↓"∀␈↓ α←␈↓␈↓ ¬S␈↓αconcat[A;BC]␈↓ is ␈↓αABC␈↓
␈↓"∀␈↓ α←␈↓There are two primitive recognizers:
␈↓"∀␈↓ α←␈↓␈↓αchar␈↓␈↓α[x]␈↓: is ␈↓αx␈↓ a single character?
␈↓" ␈↓ α←␈↓␈↓αnull␈↓␈↓α[x]␈↓: is ␈↓αx␈↓ the empty string?
␈↓"β␈↓ α←␈↓For example:␈↓ ¬↔␈↓αchar[A] ␈↓is ␈↓
t␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬↔␈↓αchar[AB] ␈↓is ␈↓
f␈↓
␈↓ α←␈↓␈↓376 Storage Structures and Efficiency␈↓
(7.3␈↓
␈↓"β␈↓ α←␈↓Finally,␈α
we␈α
include␈α
a␈α
version␈α
of␈α
the␈α
equality␈α
predicate␈α
which␈α
will␈αdetermine␈α
if
␈↓ α←␈↓two characters are identical.
␈↓"β␈↓ α←␈↓␈↓ ∧[␈↓αx = y␈↓: are ␈↓αx␈↓ and ␈↓αy␈↓ the same character?
␈↓"∞␈↓ α←␈↓␈↓ ∧[␈↓αAB = AB ␈↓is ␈↓λB␈↓
␈↓"∀␈↓ α←␈↓The␈α∞implementation␈α∞of␈α∂these␈α∞string␈α∞primitives␈α∞is␈α∂less␈α∞complex␈α∞than␈α∂that␈α∞of
␈↓ α←␈↓LISP␈α∞primitives.␈α∞ ␈↓αfirst␈↓␈α∞generates␈α∞a␈α∞character␈α∞count␈α∞of␈α∞one␈α∞and␈α∞a␈α∞pointer␈α
to
␈↓ α←␈↓the␈α∞first␈α∞character␈α∞of␈α∞the␈α∞parent␈α∞string.␈α∞ ␈↓αrest␈↓␈α∞generates␈α∞a␈α∞character␈α∞count␈α
of
␈↓ α←␈↓one␈αless␈αthan␈αthat␈αof␈αthe␈αparent␈αand␈αa␈αpointer␈αto␈αthe␈αsecond␈αcharacter␈αof␈αthe
␈↓ α←␈↓parent␈α∞string.␈α∞ Therefore,␈α∞this␈α∞implementation␈α∞shares␈α∞substrings,␈α∞just␈α∂as␈α∞␈↓αcar␈↓
␈↓ α←␈↓and ␈↓αcdr␈↓ share substructure.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊂implementation␈α⊃of␈α⊂the␈α⊂recognizers␈α⊃and␈α⊂the␈α⊂equality␈α⊃predicate␈α⊂is
␈↓ α←␈↓straightforward.␈α We␈αwill␈αblur␈αthe␈αdistinction␈αbetween␈αcharacters␈αand␈αstrings
␈↓ α←␈↓of␈α
length␈α
one.␈α
Thus␈α
␈↓αchar␈↓␈α
need␈αonly␈α
check␈α
the␈α
character␈α
count.␈α
␈↓αnull␈↓␈α
gives␈α␈↓
t␈↓␈α
if
␈↓ α←␈↓the␈α∂count␈α∂is␈α∂zero.␈α∂ To␈α∂implement␈α∂equality,␈α∂we␈α∂note␈α∂that␈α∂characters␈α∂are␈α∂not
␈↓ α←␈↓stored uniquely, so we must make an actual character comparison.
␈↓"β␈↓ α←␈↓␈↓ β'As␈αwith␈αfull␈αLISP,␈αthe␈αimplementation␈αof␈αthe␈αconstructor␈αrequires␈αmore
␈↓ α←␈↓care.␈α≥Since␈α≥our␈α≥implementation␈α≥requires␈α≥that␈α≥string␈α≥components␈α≤be
␈↓ α←␈↓contiguous,␈α∞we␈α∞must␈α∞copy␈α∞the␈α∞arguments␈α∞to␈α∞␈↓αconcat␈↓.␈α∞ To␈α∞evaluate␈α
␈↓αconcat[x;y]␈↓,
␈↓ α←␈↓we␈αcopy␈α␈↓αx␈↓,␈αthen␈αcopy␈α␈↓αy␈↓␈αso␈αthat␈α␈↓αy␈↓␈αfollows␈α␈↓αx␈↓␈αin␈αfree␈αstring␈αspace;␈αwe␈αgenerate␈αa
␈↓ α←␈↓character␈α
count␈αof␈α
one␈αplus␈α
the␈α
count␈αof␈α
␈↓αy␈↓,␈αand␈α
generate␈α
a␈αpointer␈α
to␈αthe␈α
copy
␈↓ α←␈↓of␈α∞␈↓αx␈↓.␈α
The␈α∞copies␈α∞are␈α
made␈α∞in␈α
the␈α∞free␈α∞string␈α
space␈α∞in␈α
a␈α∞manner␈α∞similar␈α
to
␈↓ α←␈↓that used in ␈↓αcons␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∞storage␈α
management␈α∞is␈α∞somewhat␈α
different␈α∞from␈α
that␈α∞of␈α∞a␈α
simple
␈↓ α←␈↓LISP␈α∩implementation.␈α∩ Since␈α∩the␈α⊃copying␈α∩operation␈α∩within␈α∩␈↓αconcat␈↓␈α⊃allocate
␈↓ α←␈↓space,␈αwe␈αmust␈αinclude␈α
some␈αmethod␈αfor␈αdeallocating␈αspace.␈α
Though␈αsimpler
␈↓ α←␈↓methods␈αmay␈α
suffice␈αwe␈α
us␈αa␈αgarbage␈α
collector.␈↓π 2␈↓␈αThe␈α
marking␈αphase␈αis␈α
much
␈↓ α←␈↓simpler␈αthan␈α
that␈αfor␈αLISP;␈α
it␈αis␈αnot␈α
recursive.␈αWe␈αuse␈α
the␈αdescriptor␈α
in␈αthe
␈↓ α←␈↓symbol␈α∃table␈α∃to␈α∃mark␈α∃each␈α∀character␈α∃string.␈α∃ However,␈α∃we␈α∃cannot␈α∀stop
␈↓ α←␈↓marking␈αsimply␈αbecause␈αwe␈αhave␈αencountered␈αa␈αpreviously␈αmarked␈α
character.
␈↓ α←␈↓Since␈α∞we␈α∞are␈α∞sharing␈α∞substrings,␈α∞we␈α∞must␈α∞visit␈α∞each␈α∞character␈α∞in␈α∂the␈α∞string
␈↓ α←␈↓item.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αsweep␈αphase␈αneeds␈αto␈αbe␈αmore␈αcomprehensive␈αfor␈αstring␈αcollection.
␈↓ α←␈↓Since␈αstrings␈αare␈αstored␈αsequentially,␈αa␈αfragmented␈αstring␈αspace␈αis␈αof␈αlittle␈α
use.
␈↓ α←␈↓We␈α
must␈α∞compact␈α
all␈α
the␈α∞referenceable␈α
strings␈α
into␈α∞one␈α
end␈α
of␈α∞string␈α
space,
␈↓ α←␈↓and␈α
free␈α
a␈α
linear␈αblock␈α
for␈α
the␈α
new␈α
free␈αstring␈α
space.␈α
Since␈α
we␈α
are␈αsharing
␈↓ α←␈↓substrings,␈α∩a␈α⊃little␈α∩care␈α⊃must␈α∩be␈α⊃exercised.␈α∩ When␈α⊃we␈α∩move␈α⊃a␈α∩string,␈α⊃the
␈↓ α←␈↓descriptor␈αof␈αany␈αvariable␈αreferencing␈αany␈αpart␈αof␈αthat␈αparent␈αstring␈αmust␈αbe
␈↓ α←␈↓changed␈α∂to␈α∂reflect␈α⊂the␈α∂new␈α∂location.␈α∂ So␈α⊂before␈α∂we␈α∂begin␈α∂the␈α⊂relocation␈α∂of
␈↓ α←␈↓strings,␈αwe␈α
sort␈αthe␈αstring␈α
descriptors␈αon␈α
the␈αbasis␈αof␈α
their␈αpointers␈αinto␈α
string
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 2␈↓Since␈α⊃string␈α⊃operations␈α⊃are␈α⊃quite␈α⊃well-behaved,␈α⊃a␈α⊃reference␈α⊂counter
␈↓ α←␈↓could␈αbe␈α
used.␈αWe␈α
use␈αa␈α
garbage␈αcollector␈α
for␈αits␈α
elegance␈αand␈αits␈α
pedagogical
␈↓ α←␈↓value for the next section.
␈↓ α←␈↓␈↓7.3␈↓ π[Strings and Linear LISP 377␈↓
␈↓"β␈↓ α←␈↓space.␈α⊃ We␈α⊂then␈α⊃recognize␈α⊃each␈α⊂parent␈α⊃string,␈α⊃moving␈α⊂it␈α⊃down␈α⊃into␈α⊂freed
␈↓ α←␈↓locations␈α
and␈α
update␈α
the␈α
address␈α
pointers␈α
in␈α
the␈α
descriptors␈α
of␈α
any␈α
substrings.
␈↓ α←␈↓Eventually␈α
all␈α
strings␈α∞will␈α
be␈α
compacted;␈α
the␈α∞string␈α
space␈α
pointer␈α
can␈α∞be␈α
set
␈↓ α←␈↓and␈α⊃the␈α⊃computation␈α⊃continued.␈α∩ Next,␈α⊃we␈α⊃adapt␈α⊃the␈α∩compacting␈α⊃garbage
␈↓ α←␈↓collectors for use in LISP.
␈↓"β␈↓ α←␈↓␈↓ ∧U␈↓↓7.4 A Compacting Collector for LISP␈↓
␈↓"β␈↓ α←␈↓We␈α
can␈α
combine␈α
the␈α
simplicity␈αof␈α
the␈α
original␈α
mark-sweep␈α
garbage␈αcollector
␈↓ α←␈↓with␈αthe␈αsophistication␈αof␈αthe␈αcollection␈αphase␈αof␈αstring␈αgarbage␈αcollector␈αand
␈↓ α←␈↓produce a compacting garbage collector for LISP.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α∩are␈α∩several␈α∩motivations␈α∩for␈α∩compacting␈α∩storage.␈α∩First,␈α⊃besides
␈↓ α←␈↓making␈α⊗the␈α⊗␈↓¬active␈↓␈α⊗storage␈α⊗contiguous,␈α∃we␈α⊗also␈α⊗make␈α⊗the␈α⊗␈↓¬free␈↓␈α∃locations
␈↓ α←␈↓contiguous.␈α Thus␈α
the␈αfree␈α
lists␈αcan␈α
be␈αhandled␈α
as␈αvectors␈α
rather␈αthan␈αas␈α
lists.
␈↓ α←␈↓This␈α
simplifies␈α
storage␈α∞allocation:␈α
to␈α
allocate␈α
the␈α∞next␈α
free␈α
element,␈α∞take␈α
the
␈↓ α←␈↓next element in the free space vector.
␈↓"β␈↓ α←␈↓␈↓ β'Another␈α
reason␈αfor␈α
concern␈αwith␈α
compacting␈αis␈α
related␈αto␈α
hardware.␈αIf
␈↓ α←␈↓the␈α∀underlying␈α∀machine␈α∀is␈α∀using␈α∀a␈α∪paging␈α∀scheme,␈α∀then␈α∀we␈α∀can␈α∀try␈α∪to
␈↓ α←␈↓minimize␈α∞page-faults␈α
by␈α∞keeping␈α∞the␈α
LISP␈α∞structures␈α
localized.␈α∞In␈α∞the␈α
worst
␈↓ α←␈↓case,␈α∂we␈α∂could␈α∂have␈α∞every␈α∂element␈α∂of␈α∂a␈α∂list␈α∞on␈α∂a␈α∂separate␈α∂page;␈α∂this␈α∞could
␈↓ α←␈↓require␈αthat␈α
the␈αmemory␈α
manager␈αretrieve␈α
a␈αnew␈α
page␈αfor␈α
every␈αreference.␈↓π 3␈↓
␈↓ α←␈↓However,␈α∂we␈α⊂cannot␈α∂restrict␈α⊂the␈α∂operations␈α∂of␈α⊂the␈α∂LISP␈α⊂programmer.␈α∂The
␈↓ α←␈↓underlying␈αhardware␈α␈↓↓must␈↓␈αbe␈αinvisible␈αto␈αthe␈αuser.␈αThe␈αnext␈αbest␈αthing␈αis␈αto
␈↓ α←␈↓try␈αto␈α
keep␈αthe␈αstructures␈α
as␈αlocal␈αas␈α
possible;␈αcompaction␈αof␈α
spaces␈αis␈α
a␈αfirst
␈↓ α←␈↓attempt at this. We will discuss other lower-level tricks later.
␈↓"β␈↓ α←␈↓␈↓ β'Compaction␈α↔is␈α_important␈α↔in␈α↔languages␈α_other␈α↔than␈α↔LISP.␈α_ If␈α↔the
␈↓ α←␈↓language␈α∂allocates␈α⊂storage␈α∂in␈α⊂a␈α∂manner␈α⊂similar␈α∂to␈α⊂LISP␈α∂but␈α⊂the␈α∂constructs
␈↓ α←␈↓allow␈α⊂␈↓¬different␈↓-sized␈α⊃blocks␈α⊂to␈α⊂be␈α⊃specified␈α⊂(a␈α⊂string␈α⊃processor␈α⊂is␈α⊃a␈α⊂simple
␈↓ α←␈↓example), then compaction may be necessary.␈↓π 4␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Granted␈α∂that␈α∂compaction␈α∂is␈α∂a␈α∂worthwhile␈α∂endeavor,␈α∂we␈α⊂proceed.␈α∂ We
␈↓ α←␈↓can't␈α∂simply␈α∂mark␈α∂all␈α∂the␈α∂active␈α∂cells␈α∂and␈α∂then␈α∂move␈α∂them␈α⊂into␈α∂unmarked
␈↓ α←␈↓cells␈α∞to␈α∞compact␈α∂the␈α∞space.␈α∞ We␈α∞must␈α∂also␈α∞maintain␈α∞the␈α∂original␈α∞topological
␈↓ α←␈↓relationships between the elements.
␈↓"⊃␈↓ α←␈↓
⊂αααπααα⊃␈↓ εo ⊂αααπααα⊃
␈↓"␈↓ α←␈↓
204 ~204~204~␈↓ is not the same as ␈↓
␈↓ εo100 ~204~204~
␈↓"␈↓ α←␈↓
%ααα∀ααα$␈↓ εo %ααα∀ααα$
␈↓"∀␈↓ α←␈↓␈↓ β' Besides␈α⊃moving␈α⊃the␈α∩cells,␈α⊃we␈α⊃must␈α⊃also␈α∩update␈α⊃each␈α⊃reference␈α∩to␈α⊃a
␈↓ α←␈↓moved location:
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 3␈↓Very␈α∃little␈α∃empirical␈α∃work␈α∃has␈α∀been␈α∃done␈α∃on␈α∃the␈α∃actual␈α∀storage
␈↓ α←␈↓requirements␈αand␈αrunning␈αenvironment␈αof␈αLISP.␈α A␈αstart␈αis␈αmade␈αin␈α[Cl 76];
␈↓ α←␈↓much more should be done.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 4␈↓As we shall soon see, the rationale is applicable in LISP as well.
␈↓ α←␈↓␈↓378 Storage#Structures and#Efficiency␈↓
(7.4␈↓
␈↓"⊃␈↓ α←␈↓
⊂αααπααα⊃␈↓ εo ⊂αααπααα⊃
␈↓"␈↓ α←␈↓
204 ~204~204~␈↓¬ is␈↓ the same as ␈↓
␈↓ εo100 ~100~100~
␈↓"␈↓ α←␈↓
%ααα∀ααα$␈↓ εo %ααα∀ααα$
␈↓"∀␈↓ α←␈↓To␈α
handle␈αthese␈α
problems,␈αwe␈α
expand␈αthe␈α
sweep␈αphase␈α
into␈αtwo␈α
phases:␈αthe
␈↓ α←␈↓␈↓↓relocating␈↓ phase and the ␈↓↓updating␈↓ phase.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊗relocating␈α⊗phase␈α⊗begins␈α↔after␈α⊗all␈α⊗active␈α⊗structure␈α↔is␈α⊗marked.
␈↓ α←␈↓Assume␈α⊂we␈α⊂are␈α∂to␈α⊂compact␈α⊂all␈α∂active␈α⊂structure␈α⊂␈↓¬down␈↓␈α∂to␈α⊂the␈α⊂bottom␈α⊂of␈α∂the
␈↓ α←␈↓space.␈αFirst␈αwe␈αinitialize␈αtwo␈αpointers:␈αa␈α␈↓↓free␈↓␈αpointer␈αto␈αthe␈αlowest␈αcell␈α
in␈αthe
␈↓ α←␈↓space;␈α∞and␈α∞an␈α∂␈↓↓active␈↓␈α∞pointer␈α∞to␈α∂the␈α∞top␈α∞of␈α∞the␈α∂space.␈α∞ We␈α∞move␈α∂the␈α∞active
␈↓ α←␈↓pointer␈α⊃down␈α⊃until␈α⊃we␈α⊃come␈α∩across␈α⊃a␈α⊃␈↓¬marked␈↓␈α⊃location;␈α⊃we␈α⊃move␈α∩the␈α⊃free
␈↓ α←␈↓pointer␈α
up␈αuntil␈α
we␈αlocate␈α
an␈α␈↓¬unmarked␈↓␈α
cell.␈αWe␈α
want␈αto␈α
move␈α
that␈αmarked
␈↓ α←␈↓cell␈αdown␈αinto␈αthe␈αfree␈αlocation;␈αbut␈αwe␈αmust␈αalso␈αsupply␈αenough␈αinformation
␈↓ α←␈↓to␈α
maintain␈α
the␈α
original␈α
relationships␈α
in␈α
the␈α
transformed␈α
structure.␈α
The␈α
cell
␈↓ α←␈↓we move may reference other cells which will be moved.
␈↓"β␈↓ α←␈↓␈↓ β'Here's a picture:
␈↓"⊃␈↓ α←␈↓
. . .
␈↓"␈↓ α←␈↓
⊂αααπααα⊃
␈↓"␈↓ α←␈↓
77 ~ 65~402~
␈↓"␈↓ α←␈↓
%ααα∀ααα$
␈↓"␈↓ α←␈↓
. . .
␈↓"␈↓ α←␈↓
⊂αααπααα⊃
␈↓"␈↓ α←␈↓
100 ~ ~ ~ ←␈↓free pointer␈↓
␈↓"␈↓ α←␈↓
%ααα∀ααα$
␈↓"␈↓ α←␈↓
. . .
␈↓"␈↓ α←␈↓
⊂αααπααα⊃
␈↓"␈↓ α←␈↓
155 ~ ~ ~
␈↓"␈↓ α←␈↓
%ααα∀ααα$
␈↓"␈↓ α←␈↓
. . .
␈↓"␈↓ α←␈↓
⊂αααπααα⊃
␈↓"␈↓ α←␈↓
204 ~402~ 77~
␈↓"␈↓ α←␈↓
%ααα∀ααα$
␈↓"␈↓ α←␈↓
. . .
␈↓"␈↓ α←␈↓
⊂αααπααα⊃
␈↓"␈↓ α←␈↓
402 ~204~402~ ←␈↓active pointer␈↓
␈↓"␈↓ α←␈↓
%ααα∀ααα$
␈↓"␈↓ α←␈↓
. . .
␈↓"∀␈↓ α←␈↓Cell␈α␈↓
77␈↓␈αwas␈αactive␈αso␈αwe␈αleft␈αit␈αalone;␈αit␈αreferences␈αcell␈α␈↓
65␈↓,␈αwhich␈αhas␈αalready
␈↓ α←␈↓been␈αvisited;␈αand␈α
also␈αreferences␈αcell␈α
␈↓
402␈↓␈αwhich␈αis␈α
about␈αto␈αmove.␈α
We␈αmove
␈↓ α←␈↓the␈α∞contents␈α∂of␈α∞cell␈α∂␈↓
402␈↓␈α∞into␈α∂cell␈α∞␈↓
100␈↓,␈α∞and␈α∂to␈α∞let␈α∂everyone␈α∞know␈α∂where␈α∞the
␈↓ α←␈↓contents has gone, we leave a forwarding address of ␈↓
100␈↓ in location ␈↓
402␈↓.
␈↓"β␈↓ α←␈↓Thus,
␈↓"⊃␈↓ α←␈↓
⊂αααπααα⊃
␈↓"␈↓ α←␈↓
100 ~204~402~ ←␈↓free pointer␈↓
␈↓"␈↓ α←␈↓
%ααα∀ααα$
␈↓"␈↓ α←␈↓
. . .
␈↓"␈↓ α←␈↓
⊂αααπααα⊃
␈↓"␈↓ α←␈↓
402 ~ ~100~ ←␈↓active pointer␈↓
␈↓"␈↓ α←␈↓
%ααα∀ααα$
␈↓"∀␈↓ α←␈↓The␈αactive␈αpointer,␈αhaving␈αwrit,␈αmoves␈αon;␈αit␈αskips␈αover␈αany␈αunmarked␈αcells,
␈↓ α←␈↓looking␈α∂for␈α∂the␈α∂next␈α∂marked␈α∞location.␈α∂Assume␈α∂the␈α∂next␈α∂marked␈α∂location␈α∞is
␈↓ α←␈↓␈↓7.4␈↓ εuA Compacting Collector for LISP 379␈↓
␈↓"β␈↓ α←␈↓␈↓
204␈↓.␈α
It␈α
stops␈αthere␈α
and␈α
waits␈αfor␈α
the␈α
free␈α
pointer␈αto␈α
discover␈α
that␈αlocation␈α
␈↓
155␈↓
␈↓ α←␈↓is␈α∂the␈α∂next␈α∞free␈α∂location.␈α∂ In␈α∂its␈α∞search␈α∂the␈α∂free␈α∞pointer␈α∂will␈α∂skip␈α∂over␈α∞any
␈↓ α←␈↓marked␈α∞cells.␈α∞ The␈α∂same␈α∞relocation␈α∞operation␈α∞occurs:␈α∂the␈α∞contents␈α∞of␈α∂␈↓
204␈↓␈α∞is
␈↓ α←␈↓moved␈α∩to␈α∩location␈α∩␈↓
155␈↓,␈α⊃and␈α∩the␈α∩forwarding␈α∩address␈α⊃of␈α∩␈↓
155␈↓␈α∩is␈α∩placed␈α⊃in
␈↓ α←␈↓location␈α
␈↓
204␈↓.␈αThe␈α
process␈α
continues␈αuntil␈α
the␈α
two␈αpointers␈α
collide.␈α
Call␈αthat
␈↓ α←␈↓collision␈α∂point␈α∂␈↓↓col␈↓.␈α∂ When␈α∂they␈α∂meet,␈α∞all␈α∂locations␈α∂above␈α∂␈↓↓col␈↓␈α∂either␈α∂will␈α∞be
␈↓ α←␈↓free␈α
or␈α
will␈α
contain␈α
forwarding␈α
addresses.␈α
All␈α
addresses,␈α
␈↓↓col␈↓␈α
and␈α
below,␈α
will
␈↓ α←␈↓contain␈α⊂marked␈α⊃words␈α⊂or␈α⊂relocated␈α⊃cells.␈α⊂ We␈α⊂are␈α⊃now␈α⊂ready␈α⊂to␈α⊃enter␈α⊂the
␈↓ α←␈↓␈↓↓update␈↓ phase.
␈↓"β␈↓ α←␈↓␈↓ β'Here is the picture:
␈↓"⊃␈↓ α←␈↓
. . .
␈↓"␈↓ α←␈↓
⊂αααπααα⊃
␈↓"␈↓ α←␈↓
77 ~ 65~402~
␈↓"␈↓ α←␈↓
%ααα∀ααα$
␈↓"␈↓ α←␈↓
. . .
␈↓"␈↓ α←␈↓
⊂αααπααα⊃
␈↓"␈↓ α←␈↓
100 ~204~402~
␈↓"␈↓ α←␈↓
%ααα∀ααα$
␈↓"␈↓ α←␈↓
. . .
␈↓"␈↓ α←␈↓
⊂αααπααα⊃
␈↓"␈↓ α←␈↓
155 ~402~ 77~
␈↓"␈↓ α←␈↓
%ααα∀ααα$
␈↓"␈↓ α←␈↓
. . . ← ␈↓↓col␈↓
␈↓"␈↓ α←␈↓
⊂αααπααα⊃
␈↓"␈↓ α←␈↓
204 ~ ~155~
␈↓"␈↓ α←␈↓
%ααα∀ααα$
␈↓"␈↓ α←␈↓
. . .
␈↓"␈↓ α←␈↓
⊂αααπααα⊃
␈↓"␈↓ α←␈↓
402 ~ ~100~
␈↓"␈↓ α←␈↓
%ααα∀ααα$
␈↓"␈↓ α←␈↓
. . .
␈↓"∀␈↓ α←␈↓We␈αexamine␈αthe␈αinitial␈αsegment␈αof␈αour␈αspace␈αfrom␈αthe␈αbottom␈αto␈α␈↓↓col␈↓␈αlooking
␈↓ α←␈↓for␈α∞any␈α∞references␈α∞to␈α∞that␈α∞area␈α∞␈↓¬above␈↓␈α∞␈↓↓col␈↓.␈α∞A␈α∞reference␈α∞to␈α∞that␈α∞area␈α∂must␈α∞be
␈↓ α←␈↓changed.␈αWhat␈αis␈αfound␈αin␈αthe␈αreferenced␈αcell␈αis␈αnot␈αthe␈αdesired␈αinformation,
␈↓ α←␈↓but␈α⊂is␈α⊂the␈α⊂forwarding␈α∂address␈α⊂of␈α⊂the␈α⊂desired␈α∂information.␈α⊂ What␈α⊂to␈α⊂do␈α∂is
␈↓ α←␈↓obvious:␈αtell␈αthe␈αsender␈αwhat␈αthe␈αchange␈αof␈αaddress␈αis.␈α Thus␈αthe␈α␈↓αcdr␈↓-part␈αof
␈↓ α←␈↓cell␈α∩␈↓
77␈↓␈α∩becomes␈α⊃␈↓
100␈↓;␈α∩the␈α∩␈↓αcar␈↓-part␈α⊃doesn't␈α∩change.␈α∩Cell␈α⊃␈↓
100␈↓␈α∩refers␈α∩to␈α⊃two
␈↓ α←␈↓relocated cells; we find their forwarding addresses, and cell ␈↓
100␈↓ becomes
␈↓"⊃␈↓ α←␈↓
⊂αααπααα⊃
␈↓"␈↓ α←␈↓
100 ~155~100~
␈↓"␈↓ α←␈↓
%ααα∀ααα$
␈↓"∀␈↓ α←␈↓Similar␈αtreatment␈αis␈αgiven␈αto␈αcell␈α␈↓
155␈↓,␈αmodifying␈αthe␈α␈↓αcar␈↓-part.␈α When␈αall␈αcells
␈↓ α←␈↓below␈α
␈↓↓col␈↓␈α
are␈αupdated,␈α
the␈α
garbage␈α
collection␈αis␈α
finished.␈α
The␈α
cells␈αabove␈α
␈↓↓col␈↓
␈↓ α←␈↓are all available for the free-list.
␈↓ α←␈↓␈↓380 Storage Structures and Efficiency␈↓
(7.4␈↓
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. Is ␈↓↓col␈↓ in the free space list after the update phase?
␈↓" ␈↓ α←␈↓2. Write a LISP algorithm for compacting garbage collection in LISP.
␈↓"β␈↓ α←␈↓␈↓ ¬|␈↓↓7.5 Bit-tables␈↓
␈↓"β␈↓ α←␈↓In␈α⊂the␈α⊂marking␈α⊂phase␈α⊂of␈α⊂a␈α⊃garbage␈α⊂collector␈α⊂it␈α⊂is␈α⊂necessary␈α⊂to␈α⊃record␈α⊂the
␈↓ α←␈↓visitation␈αof␈αeach␈αword.␈α Frequently␈αit␈αis␈αnot␈αpossible␈αto␈αplace␈αa␈αmark␈αin␈αthe
␈↓ α←␈↓actual word. This might occur for several reasons:
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ For␈α∂a␈α∂word␈α∂in␈α⊂FS,␈α∂there␈α∂is␈α∂no␈α∂room␈α⊂if␈α∂each␈α∂word␈α∂contains␈α⊂exactly␈α∂two
␈↓ α←␈↓␈↓ β∂addresses.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ ␈αFor␈αa␈αword␈αin␈αFWS,␈αthe␈αinformation␈αwould␈αbe␈αchanged␈αif␈αwe␈αmodified␈αa
␈↓ α←␈↓␈↓ β∂bit.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ ␈αIn␈αstructures,␈αmore␈αcomplex␈αthan␈αdotted␈αpairs,␈αthere␈αmay␈αnot␈αbe␈αroom␈αfor
␈↓ α←␈↓␈↓ β∂marking bits.
␈↓" ␈↓ α←␈↓␈↓↓4.␈↓ ␈αIf␈αa␈αmark␈αbit␈αis␈αassigned␈αin␈αeach␈αword,␈αthen␈αthe␈αinitialize␈αphase␈αrequires
␈↓ α←␈↓␈↓ β∂that we visit each word. This violates "locality of reference".␈↓π 5␈↓
␈↓"β␈↓ α←␈↓An␈α∩alternative␈α∩solution␈α⊃designates␈α∩a␈α∩separate␈α⊃section␈α∩of␈α∩memory␈α∩called␈α⊃a
␈↓ α←␈↓␈↓↓bit-table␈↓.␈α⊂The␈α∂bit-table␈α⊂is␈α⊂a␈α∂sequence␈α⊂of␈α∂binary␈α⊂flags␈α⊂such␈α∂that␈α⊂there␈α⊂is␈α∂a
␈↓ α←␈↓one-to-one␈α
correspondence␈α∞between␈α
a␈α∞flag␈α
and␈α∞a␈α
markable␈α∞memory␈α
location.
␈↓ α←␈↓Whenever␈αwe␈αwish␈α
to␈αrecord␈αthe␈α
visiting␈αof␈αa␈α
word,␈αwe␈αset␈αthe␈α
corresponding
␈↓ α←␈↓flag␈α∂in␈α∞the␈α∂bit␈α∞table.␈α∂ A␈α∂bit␈α∞table␈α∂is␈α∞represented␈α∂as␈α∞a␈α∂sequence␈α∂of␈α∞machine
␈↓ α←␈↓locations␈α∪with␈α∪several␈α∪flags␈α∪represented␈α∪in␈α∪each␈α∪word.␈α∀The␈α∪initialization
␈↓ α←␈↓phase␈α∂is␈α∂improved␈α∂since␈α∂it␈α∂is␈α∂faster␈α∂to␈α∂initialize␈α∂a␈α∂whole␈α∂table␈α∂rather␈α∞than
␈↓ α←␈↓initialize␈αsingle␈αbits␈α
in␈αseparate␈αwords.␈α The␈α
mark␈αphase␈αis␈α
rapid␈αif␈αthere␈αis␈α
a
␈↓ α←␈↓simple␈α∃calculation␈α∃to␈α∀relate␈α∃each␈α∃bit␈α∃in␈α∀a␈α∃word␈α∃with␈α∃its␈α∀corresponding
␈↓ α←␈↓markable location.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 5␈↓Locality␈α∀refers␈α∀to␈α∃the␈α∀relative␈α∀distance␈α∀between␈α∃memory␈α∀locations
␈↓ α←␈↓assigned␈αin␈αa␈αparticular␈αstructure.␈αIn␈αsome␈αmachine␈αorganizations,␈αmemory␈αis
␈↓ α←␈↓divided␈αinto␈α
"pages"␈αof␈αa␈α
relatively␈αsmall␈αsize.␈α
There␈αis␈α
significant␈αoverhead
␈↓ α←␈↓involved␈α
in␈α
crossing␈α
page␈α
boundaries.␈α
Therefore␈α
memory␈α
referencing␈α
which
␈↓ α←␈↓entails many scattered references is said to violate "locality of reference."
␈↓ α←␈↓␈↓7.6␈↓ ¬aRepresentations of Complex Data Structures 381␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧
␈↓↓7.6 Representations of Complex Data Structures␈↓
␈↓"β␈↓ α←␈↓In␈α∂our␈α∂discussion␈α∂of␈α∂abstract␈α∞context-free␈α∂data␈α∂structures␈α∂in␈α∂Section 2.1,␈α∞we
␈↓ α←␈↓isolated three kinds of structures:
␈↓"β␈↓ α←␈↓␈↓ ¬v␈↓
D␈↓ ::= ␈↓
D␈↓β1␈↓ ... ␈↓
D␈↓β1␈↓
␈↓"β␈↓ α←␈↓e.g.,␈↓ ∧l<seq> ::= ␈↓↓(␈↓<seq elem>␈↓↓,␈↓ ..., <seq elem>␈↓↓)␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬a␈↓
D␈↓ ::= ␈↓
D␈↓β1␈↓ | ␈↓
D␈↓β2␈↓ | ␈↓
D␈↓β3␈↓
␈↓"β␈↓ α←␈↓e.g.,␈↓ ¬∀<seq elem> ::= <indiv> | <seq>
␈↓"β␈↓ α←␈↓␈↓ ¬M␈↓
D␈↓ ::= ␈↓
D␈↓β1␈↓ ␈↓
D␈↓β2␈↓ ␈↓
D␈↓β3␈↓ ... ␈↓
D␈↓βn␈↓
␈↓"β␈↓ α←␈↓e.g.,␈↓ ¬∞<sexpr> ::= ␈↓α(␈↓<sexpr> . <sexpr>␈↓α)␈↓
␈↓"∀␈↓ α←␈↓We␈α⊗have␈α⊗discussed␈α⊗the␈α⊗behaviorial␈α⊗characteristics␈α⊗of␈α⊗algorithms␈α⊗which
␈↓ α←␈↓operate␈α∂on␈α∂these␈α∞structures.␈α∂Now␈α∂we␈α∂wish␈α∞to␈α∂examine␈α∂the␈α∂storage␈α∞structure
␈↓ α←␈↓aspects of these data structures.
␈↓"β␈↓ α←␈↓␈↓ β'Corresponding␈αto␈αthese␈αthree␈αdata␈αstructures␈αare␈αthree␈α"natural"␈αstorage
␈↓ α←␈↓representations.␈α By␈α
"natural"␈αwe␈α
mean␈αthat␈αeven␈α
though␈αall␈α
these␈αstructures
␈↓ α←␈↓can␈α≥be␈α≥represented␈α≥as␈α≤LISP␈α≥S-expressions,␈α≥for␈α≥example,␈α≥there␈α≤are
␈↓ α←␈↓representations␈α∂which␈α∂might␈α⊂better␈α∂suit␈α∂the␈α⊂operations␈α∂which␈α∂we␈α⊂expect␈α∂to
␈↓ α←␈↓perform␈α∞on␈α∞those␈α∞structures.␈α∞ Since␈α∞"natural"␈α∞is␈α∞not␈α∞a␈α∞well-defined␈α∞term,␈α∞we
␈↓ α←␈↓will clarify its meaning using examples of context-free data structures.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊂first␈α⊂type␈α⊂of␈α⊂data␈α⊂structure␈α⊂given␈α⊂above,␈α⊂maps␈α⊂naturally␈α⊂onto␈α⊂a
␈↓ α←␈↓representation␈α∞which␈α
contains␈α∞information␈α∞that␈α
the␈α∞object␈α∞is␈α
of␈α∞type␈α∞␈↓
D␈↓␈α
and
␈↓ α←␈↓contains␈αspace␈αfor␈αthe␈αstorage␈αinstance␈αof␈αthis␈αdata␈αtype.␈α Elements␈αof␈αtype␈α␈↓
D␈↓
␈↓ α←␈↓are␈αhomogeneous,␈α
being␈αall␈αof␈α
type␈α␈↓
D␈↓β1␈↓;␈αhowever,␈α
the␈αsize␈αof␈α
a␈αtype␈α␈↓
D␈↓␈α
element
␈↓ α←␈↓is␈α
indefinite.␈α Depending␈α
on␈α
the␈αoperations␈α
which␈α
are␈αto␈α
be␈α
performed␈αon␈α
the
␈↓ α←␈↓representation,␈α∀either␈α∃a␈α∀list␈α∀representation␈α∃or␈α∀an␈α∀array␈α∃representation␈α∀is
␈↓ α←␈↓reasonable␈αfor␈αthe␈αstorage␈αstructure.␈αUnless␈αthe␈αoperations␈αare␈αquite␈αcomplex,
␈↓ α←␈↓a sequential allocation scheme suffices.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
second␈α
type␈αof␈α
data␈α
structure␈α
is␈αfrequently␈α
represented␈α
as␈αa␈α
pointer.
␈↓ α←␈↓There␈α⊂really␈α⊂isn't␈α⊂any␈α⊂storage␈α⊂allocated␈α⊂for␈α⊂objects␈α⊂of␈α⊂this␈α⊂type.␈α∂ Instances
␈↓ α←␈↓which␈αsatisfy␈αthis␈αequation␈αhave␈αtheir␈αstorage␈αrequirements␈αset␈αby␈αone␈αof␈αthe
␈↓ α←␈↓␈↓
D␈↓βi␈↓␈α∞alternatives.␈α∞ We␈α∂will␈α∞discuss␈α∞pointer␈α∞manipulation␈α∂in␈α∞LISP␈α∞in␈α∂the␈α∞next
␈↓ α←␈↓section.
␈↓"β␈↓ α←␈↓␈↓ β'This␈αsection␈α
will␈αdiscuss␈αthe␈α
third␈αabstract␈α
data␈αstructure.␈α The␈α
essential
␈↓ α←␈↓characteristic␈αhere␈αis␈αthat␈αinstances␈αof␈αthis␈αstructure␈αhave␈αa␈αfixed␈αnumber␈αof
␈↓ α←␈↓components,␈αand␈αthose␈αcomponents␈αneed␈α
not␈αbe␈αof␈αhomogeneous␈αtype.␈α
Those
␈↓ α←␈↓components␈α∞are␈α∂typically␈α∞referenced␈α∞by␈α∂name.␈α∞ These␈α∞characteristics␈α∂form␈α∞a
␈↓ α←␈↓natural␈α
distinction␈α
between␈α
this␈α
third␈α
class␈α
and␈α
the␈α
first␈α
class,␈α
even␈αthough␈α
an
␈↓ α←␈↓appropriate␈α
encoding␈αwould␈α
make␈α
it␈αpossible␈α
to␈α
represent␈αeither␈α
class␈α
in␈αthe
␈↓ α←␈↓other.
␈↓"β␈↓ α←␈↓␈↓ β'For example, in equations like
␈↓"∀␈↓ α←␈↓␈↓ ¬∞<sexpr> ::= ␈↓α(␈↓<sexpr> . <sexpr>␈↓α)␈↓
␈↓"β␈↓ α←␈↓or ␈↓ ¬∧<form> ::= <function>[<arg-list>]
␈↓"∀␈↓ α←␈↓we reference components by selectors like ␈↓αcar␈↓, ␈↓αcdr␈↓, ␈↓αfunc␈↓, and ␈↓αarglist␈↓.
␈↓ α←␈↓␈↓382 Storage Structures and Efficiency␈↓
(7.6␈↓
␈↓"β␈↓ α←␈↓␈↓ β'LISP␈αrepresents␈αinstances␈αof␈αthe␈αabove␈αequations␈αas␈αobjects␈αof␈αthe␈αfirst
␈↓ α←␈↓and␈α⊂second␈α∂types␈α⊂of␈α⊂data␈α∂structure:␈α⊂variable-length␈α⊂lists␈α∂of␈α⊂pointers.␈α⊂ As␈α∂a
␈↓ α←␈↓result,␈αwe␈αhave␈α
thought␈αof␈αthese␈αselectors␈α
as␈αoperations␈αwhich␈α
might␈αrequire
␈↓ α←␈↓some␈α∞nontrivial␈α∞amount␈α∞of␈α∂computation␈α∞to␈α∞discover␈α∞the␈α∂desired␈α∞component,
␈↓ α←␈↓but␈αas␈αwe␈αsaw␈αin␈αSection 1.8␈αwhat␈αis␈αalgorithm␈αand␈αwhat␈αis␈αdata␈αdepends␈αon
␈↓ α←␈↓your␈αpoint␈αof␈α
view.␈αFor␈αexample,␈αwe␈α
could␈αthink␈αof␈αa␈α
dotted␈αpair␈αas␈αan␈α
array
␈↓ α←␈↓which␈α
has␈αtwo␈α
components,␈α
one␈αreferenced␈α
by␈α
␈↓αcar␈↓,␈αone␈α
referenced␈α
by␈α␈↓αcdr␈↓.␈α
We
␈↓ α←␈↓say␈α∩"array,"␈α∪since␈α∩the␈α∩number␈α∪of␈α∩components␈α∩is␈α∪known;␈α∩but␈α∪the␈α∩element
␈↓ α←␈↓references are done by nonnumerical names.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊗natural␈α∃storage␈α⊗requirements␈α∃for␈α⊗such␈α∃objects␈α⊗imply␈α⊗a␈α∃fixed
␈↓ α←␈↓amount␈α
of␈α
storage.␈αThat␈α
storage␈α
can␈α
be␈αsequentially␈α
allocated␈α
since␈α
the␈αsize␈α
of
␈↓ α←␈↓the␈α∞element␈α∞will␈α∞not␈α∞vary.␈α∂The␈α∞representation␈α∞must␈α∞also␈α∞encode␈α∂the␈α∞scheme
␈↓ α←␈↓for associating external selector with internal representation.
␈↓"β␈↓ α←␈↓␈↓ β'For example,
␈↓"⊃␈↓ α←␈↓
⊂αααααπααααα⊃
␈↓"␈↓ α←␈↓
~ CAR ~ ~
␈↓"␈↓ α←␈↓
εαααααβαααααλ
␈↓"␈↓ α←␈↓
~ CDR ~ ~
␈↓"␈↓ α←␈↓
%ααααα∀ααααα$
␈↓"∀␈↓ α←␈↓Notice␈α↔that␈α↔the␈α↔array-referencing␈α↔mechanisms␈α↔have␈α↔to␈α↔solve␈α↔a␈α⊗similar
␈↓ α←␈↓problem.␈α⊃However,␈α⊃array␈α⊃representation␈α⊃is␈α⊃such␈α⊃that␈α⊃the␈α⊃dope␈α∩vector␈α⊃can
␈↓ α←␈↓perform a ␈↓¬calculation␈↓ to locate the element.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∃storage␈α∃element␈α∃which␈α⊗we␈α∃are␈α∃developing␈α∃is␈α∃called␈α⊗a␈α∃␈↓↓record␈↓
␈↓ α←␈↓([Pop 68]), or a ␈↓↓structure␈↓ ([Alg 75], [EL1 74]), or a ␈↓↓plex␈↓ ([Han 69]).␈↓π 6␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Besides␈αthe␈αusual␈αconstructors,␈αselectors␈αand␈αrecognizers,␈αrecords␈αmay␈α
be
␈↓ α←␈↓supplied␈αwith␈αa␈αfunction␈αto␈αmodify␈αcomponents␈αof␈αa␈αstructure.␈αThis␈αfunction
␈↓ α←␈↓is␈α
called␈αan␈α
␈↓↓updater␈↓.␈α Just␈α
as␈α
we␈αcan␈α
write␈α␈↓αA[43] ← 56␈↓␈α
where␈α␈↓αA␈↓␈α
is␈α
an␈αarray,
␈↓ α←␈↓an updater function would implement a statement like ␈↓αcar[x] ← (A . B)␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'Updating␈α∩of␈α∩simple␈α⊃variables␈α∩is␈α∩called␈α⊃assignment.␈α∩ A␈α∩discussion␈α⊃of
␈↓ α←␈↓"updating"␈α
of␈α
more␈α
general␈α
data␈α
structures␈α
requires␈α
a␈α∞deeper␈α
understanding
␈↓ α←␈↓of␈αthe␈αimplementation␈αand␈αstorage␈αstructures.␈αIn␈αthe␈αcase␈αof␈αLISP,␈αit␈αrequires
␈↓ α←␈↓a discussion of pointers. That is the topic of the next section.
␈↓"β␈↓ α←␈↓␈↓ ¬H␈↓↓7.7 ␈↓αrplaca␈↓↓ and ␈↓αrplacd␈↓↓␈↓α
␈↓"β␈↓ α←␈↓The␈α∪discussion␈α∪in␈α∪Chapter 5␈α∀developed␈α∪an␈α∪implementation␈α∪of␈α∀the␈α∪LISP
␈↓ α←␈↓operations␈α∂in␈α∂terms␈α∂of␈α∂the␈α∂manipulation␈α∂of␈α∂pointers.␈α∂ Those␈α∞manipulations
␈↓ α←␈↓allowed␈α∩the␈α∩creation␈α∪of␈α∩new␈α∩structure␈α∩or␈α∪allowed␈α∩sharing␈α∩of␈α∪an␈α∩existing
␈↓ α←␈↓structure.␈α
None␈αof␈α
these␈αoperations␈α
involved␈αthe␈α
modification␈αof␈α
an␈αexisting
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 6␈↓A␈α∞similar␈α
device,␈α∞called␈α
a␈α∞␈↓↓hunk␈↓,␈α
has␈α∞been␈α
implemented␈α∞in␈α
MacLISP
␈↓ α←␈↓[Ste pc].
␈↓ α←␈↓␈↓7.7␈↓ λ≤␈↓αrplaca␈↓ and ␈↓αrplacd␈↓ 383␈↓α
␈↓"β␈↓ α←␈↓structure.␈α
In␈α
this␈α
section␈α
we␈α
will␈α
discuss␈α
some␈α
LISP␈α
coding␈α
tricks␈α
which␈α
do
␈↓ α←␈↓involve modification operations.
␈↓"β␈↓ α←␈↓First, consider
␈↓"∀␈↓ α←␈↓␈↓α␈↓ β≥append <= λ[[x;y][null[x] → y; ␈↓
t␈↓α → concat[first[x];append[rest[x];y]]]]␈↓
␈↓"∀␈↓ α←␈↓This␈α
function␈α
copies␈α
␈↓αx␈↓␈α
onto␈α
the␈α
front␈α
of␈α
␈↓αy␈↓.␈↓π 7␈↓␈α
Or␈α
recall␈α
the␈α
␈↓αsubst␈↓␈α
function:␈α
it
␈↓ α←␈↓generates␈αa␈α
copy␈αwith␈αthe␈α
correct␈αsubstitutions␈αmade;␈α
the␈αsubstitutions␈αare␈α
not
␈↓ α←␈↓made␈αin␈αthe␈α
original␈αS-expr.␈αSince␈α
it␈αis␈αthe␈α
constructors␈αwhich␈αcarry␈α
out␈αthe
␈↓ α←␈↓copying␈α
operations,␈α
and␈α
since␈α
the␈α
application␈α
of␈α
a␈α
constructor␈α
may␈αinitiate␈α
the
␈↓ α←␈↓expensive␈α∂operation␈α⊂of␈α∂garbage␈α∂collection,␈α⊂we␈α∂should␈α∂examine␈α⊂the␈α∂possible
␈↓ α←␈↓ways of reducing copying.
␈↓"β␈↓ α←␈↓␈↓ β'Consider␈α∂the␈α∂expression␈α∂␈↓αappend[(A␈α⊂B␈α∂C);(D␈α∂E)]␈↓.␈α∂ It␈α∂appears␈α⊂that␈α∂we
␈↓ α←␈↓could␈α∂get␈α∂the␈α∂effect␈α∂of␈α∞␈↓αappend␈↓␈α∂by␈α∂␈↓αrest␈↓-ing␈α∂down␈α∂the␈α∞list␈α∂␈↓α(A␈α∂B␈α∂C)␈↓␈α∂until␈α∞we
␈↓ α←␈↓found␈αthe␈α
terminator;␈αthen␈α
we␈αcould␈α
replace␈αthat␈α
terminator␈αwith␈α
a␈αpointer␈α
to
␈↓ α←␈↓the list ␈↓α(D E)␈↓. Thus,
␈↓"⊃␈↓ α←␈↓
⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπαα⊃ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
~ A ~ #αβα→~ B ~ #αβα→~ C ~≤'. . .→~ D ~ #αβα→~ E ~≤'~
␈↓"␈↓ α←␈↓
%ααα∀ααα$ %ααα∀ααα$ %ααα∀αα$ %ααα∀ααα$ %ααα∀αα$
␈↓"∀␈↓ α←␈↓The␈α∞resulting␈α∞structure␈α∂does␈α∞indeed␈α∞look␈α∞like␈α∂one␈α∞we␈α∞would␈α∂have␈α∞obtained
␈↓ α←␈↓from␈α∀␈↓αappend␈↓.␈α∀The␈α∀operation␈α∀we␈α∀want␈α∀to␈α∀perform␈α∀␈↓¬modifies␈↓␈α∀the␈α∀existing
␈↓ α←␈↓structure,␈α~instead␈α≠of␈α~␈↓¬copying␈↓␈α≠it␈α~as␈α~␈↓αappend␈↓␈α≠would␈α~have␈α≠done.␈α~Such
␈↓ α←␈↓modifications can cause serious difficulties.
␈↓"β␈↓ α←␈↓␈↓ β'Let␈α∂us␈α∞call␈α∂the␈α∂modifying␈α∞version␈α∂of␈α∂␈↓αappend␈↓,␈α∞␈↓αnconc␈↓;␈α∂and␈α∂consider␈α∞the
␈↓ α←␈↓execution of the following sequence of statements:
␈↓"β␈↓ α←␈↓α␈↓first␈↓α␈↓ ¬;i ← (A B C)
␈↓"β␈↓ α←␈↓α␈↓then␈↓α␈↓ ¬;j ← (D E)
␈↓"β␈↓ α←␈↓α␈↓and finally,␈↓α␈↓ ¬;k ← nconc[i;j]
␈↓"∀␈↓ α←␈↓After␈α⊃execution␈α⊂of␈α⊃the␈α⊂third␈α⊃statement,␈α⊂␈↓αk␈↓␈α⊃would␈α⊂have␈α⊃the␈α⊃expected␈α⊂value
␈↓ α←␈↓␈↓α(A B C D E)␈↓.␈α
However␈α
␈↓αi␈↓␈α
would␈α
␈↓¬also␈↓␈α∞have␈α
this␈α
value␈α
since␈α
we␈α∞modified␈α
the
␈↓ α←␈↓structure␈α∩assigned␈α∩to␈α∩␈↓αi␈↓.␈α∩ Also,␈α∪any␈α∩value␈α∩which␈α∩was␈α∩sharing␈α∩part␈α∪of␈α∩the
␈↓ α←␈↓structure␈α
of␈α
␈↓αi␈↓␈α
will␈α
also␈αbe␈α
changed.␈α
␈↓αnconc␈↓␈α
is␈α
a␈α
pointer␈αmodification␈α
procedure;
␈↓ α←␈↓its␈α∪effect␈α∪can␈α∀be␈α∪quite␈α∪far-reaching␈α∀and␈α∪unexpected.␈α∪ Exclusion␈α∀of␈α∪such
␈↓ α←␈↓features␈α⊂is␈α⊂one␈α⊂solution.␈α⊂However␈α⊂a␈α⊂programming␈α⊂language␈α⊂␈↓¬is␈↓␈α⊂a␈α⊂tool,␈α⊂and
␈↓ α←␈↓used␈αcarefully,␈αsuch␈α
features␈αare␈αvaluable␈α
for␈αdecreasing␈αstorage␈α
requirements
␈↓ α←␈↓and␈α∩execution␈α∩time.␈α∩ Inclusion␈α∩of␈α⊃such␈α∩features␈α∩must␈α∩be␈α∩done␈α∩with␈α⊃care,
␈↓ α←␈↓however. The chance for inadvertent application must be minimized.
␈↓"β␈↓ α←␈↓␈↓ β'With␈α_the␈α_preceding␈α_adminitions,␈α_we␈α_introduce␈α_the␈α_LISP␈α↔pointer
␈↓ α←␈↓modification␈α⊗primitives.␈α⊗Their␈α∃appearance␈α⊗at␈α⊗this␈α∃position␈α⊗in␈α⊗this␈α∃text
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 7␈↓Note: ␈↓αy␈↓ is not copied.
␈↓ α←␈↓␈↓384 Storage Structures and Efficiency␈↓
(7.7␈↓
␈↓"β␈↓ α←␈↓indicates␈α⊗that␈α⊗such␈α⊗operations␈α⊗are␈α⊗not␈α⊗critical␈α⊗to␈α⊗an␈α⊗understanding␈α∃of
␈↓ α←␈↓programming␈α∩languages,␈α∩and␈α∩also␈α∩that␈α⊃such␈α∩features␈α∩should␈α∩not␈α∩be␈α⊃used
␈↓ α←␈↓without a reasonable understanding of that language.
␈↓"β␈↓ α←␈↓␈↓ β'Pointer␈α∂modification␈α∂functions␈α∂for␈α⊂LISP␈α∂are␈α∂defined␈α∂in␈α∂terms␈α⊂of␈α∂two
␈↓ α←␈↓primitive␈α
operations;␈α
␈↓αrplaca␈↓␈α
replaces␈α∞the␈α
␈↓αcar␈↓␈α
pointer;␈α
␈↓αrplacd␈↓␈α
replaces␈α∞the␈α
␈↓αcdr␈↓
␈↓ α←␈↓pointer.
␈↓"β␈↓ α←␈↓␈↓ β'The expression ␈↓αrplaca␈↓␈↓α[x;y]␈↓ replaces the ␈↓αcar␈↓-part of ␈↓αx␈↓ with ␈↓αy␈↓.
␈↓"⊃␈↓ α←␈↓
␈↓αdest␈↓
␈↓αenv␈↓
␈↓"␈↓ α←␈↓
~ ~
␈↓"␈↓ α←␈↓
~ ⊂ααααααα⊃ ~ ⊂ααααααα⊃
␈↓"␈↓ α←␈↓
%→ ~ #αβα⊃ %→ ~ #αβαα###→
␈↓"␈↓ α←␈↓
εαααπαααλ ↓ εαααπαααλ
␈↓"␈↓ α←␈↓
# # # ←$ ~ x ~ # βα→α⊃
␈↓"␈↓ α←␈↓
~ ~ β→ - - →⊃ εαααβαααλ ~
␈↓"␈↓ α←␈↓
εαααβαααλ ~ y ~ #αβ→⊃ ↓
␈↓"␈↓ α←␈↓
~ ~ ~ ↓ %ααα∀ααα$ ↓ ~
␈↓"␈↓ α←␈↓
# # # ⊂αααπααα⊃ ⊂ααααααα⊃ ~ ↓
␈↓"␈↓ α←␈↓
εαααβαααλ ~ # ~ ? ~ ⊂ - - →~ ? ~←$ ~
␈↓"␈↓ α←␈↓
~ ~ ~ ⊂→%α|α∀ααα$ | %ααααααα$ ~
␈↓"␈↓ α←␈↓
%ααα∀ααα$ ↑ % - → - →$ ↓
␈↓"␈↓ α←␈↓
%←ααα←ααααα←αααα←ααααα←ααααα←ααα$
␈↓"∀␈↓ α←␈↓␈↓ Algorithm for ␈↓αrplaca␈↓ ␈↓α
␈↓"λ␈↓ α←␈↓The AMBIT/G description of ␈↓αrplacd␈↓ was given on page 285.
␈↓"β␈↓ α←␈↓Now ␈↓αnconc␈↓ can be defined as:␈↓π 8␈↓
␈↓"∀␈↓ α←␈↓αnconc <= λ[[x;y] prog[[z]
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧7[null[x] → return[y]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧7z ← x;
␈↓"β␈↓ α←␈↓α␈↓ ∧∪a␈↓ ∧7[null[cdr[z]] → rplacd[z;y]; return [x]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧7z ←cdr [z];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧7go[a] ]]
␈↓"β␈↓ α←␈↓α␈↓Consider:␈↓α␈↓ ∧+prog[[x]␈↓ ¬#x ← (NOTHING CAN GO WRONG);
␈↓"β␈↓ α←␈↓α␈↓ ∧+␈↓ ¬#rplacd[cdddr[x];cddr[x]];
␈↓"β␈↓ α←␈↓α␈↓ ∧+␈↓ ¬#print[x]]
␈↓"∀␈↓ α←␈↓This␈α⊗expression␈α↔will␈α⊗generate␈α↔a␈α⊗circular␈α⊗list.␈α↔ Circular␈α⊗lists␈α↔cannot␈α⊗be
␈↓ α←␈↓generated␈α
in␈α
LISP␈αwithout␈α
functions␈α
like␈α
␈↓αrplaca␈↓␈αand␈α
␈↓αrplacd␈↓.␈α
See␈αthe␈α
problem
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 8␈↓Since␈αwe're␈αreally␈αinvolved␈αwith␈α
the␈αrepresentation␈αwe␈αuse␈α␈↓αcar␈↓␈αand␈α
␈↓αcdr␈↓
␈↓ α←␈↓rather than ␈↓αfirst␈↓ and ␈↓αrest␈↓.
␈↓ α←␈↓␈↓7.7␈↓ λ≤␈↓αrplaca␈↓ and ␈↓αrplacd␈↓ 385␈↓α
␈↓"β␈↓ α←␈↓on␈α∂page 287.␈α∂ In␈α∂general,␈α∂to␈α∂circularize␈α∂a␈α∂nonempty␈α∂list,␈α⊂␈↓αx␈↓,␈α∂␈↓αrplacd[last[x];x]␈↓
␈↓ α←␈↓suffices where
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ ∧0␈↓αlast <=λ[[x][null[cdr[x]] → x; ␈↓
t␈↓α → last[cdr[x]]]]␈↓
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ ε_␈↓↓Problems␈↓
␈↓" ␈↓ α←␈↓1. What is the effect of evaluating ␈↓αrplacd[x;cdr[x]]␈↓?
␈↓" ␈↓ α←␈↓2. Recall␈α⊗the␈α⊗problem␈α⊗on␈α⊗hash␈α∃consing␈α⊗on␈α⊗page 287.␈α⊗ There␈α⊗we␈α∃were
␈↓ α←␈↓␈↓ β∂contemplating␈α⊃unique␈α⊃storage␈α⊃for␈α⊃all␈α⊃S-exprs.␈α⊃ Can␈α⊃such␈α⊃a␈α∩scheme␈α⊃be
␈↓ α←␈↓␈↓ β∂reconciled (efficiently) with functions like ␈↓αrplaca␈↓ and ␈↓αrplacd␈↓?
␈↓" ␈↓ α←␈↓3. It␈α∩has␈α∩been␈α⊃pointed␈α∩out␈α∩that␈α⊃␈↓αrplaca␈↓␈α∩and␈α∩␈↓αrplacd␈↓␈α⊃are␈α∩closely␈α∩related␈α⊃to
␈↓ α←␈↓␈↓ β∂assignment␈α→statements␈α→[And 76].␈α→Extend␈α→one␈α→of␈α→our␈α→evaluators␈α→to
␈↓ α←␈↓␈↓ β∂recognize expressions like:
␈↓"∞␈↓ α←␈↓␈↓ ¬H␈↓αcar[␈↓<form>␈↓α] ← ␈↓<form>
␈↓"β␈↓ α←␈↓␈↓ β∂as abbreviations for:
␈↓"β␈↓ α←␈↓α␈↓ ¬?rplaca[␈↓<form>; <form>␈↓α]␈↓
␈↓"β␈↓ α←␈↓␈↓ β∂This␈α⊂extension␈α⊂of␈α⊃assignment␈α⊂is␈α⊂obviously␈α⊃not␈α⊂restricted␈α⊂to␈α⊃␈↓αrplaca␈↓␈α⊂but
␈↓ α←␈↓␈↓ β∂could allow arbitrary forms on the left-hand side of an assignment.
␈↓"β␈↓ α←␈↓␈↓ ∧Y␈↓↓7.8 Applications of ␈↓αrplaca␈↓↓ and ␈↓αrplacd␈↓↓␈↓α
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∃begin␈α∃with␈α∃rather␈α⊗simple␈α∃examples.␈α∃Consider␈α∃the␈α⊗problem␈α∃of
␈↓ α←␈↓inserting␈α
an␈α
element␈α
into␈α
the␈α
middle␈α
of␈α
a␈α
list.␈α
For␈α
example␈α
let␈α
␈↓αx␈↓␈α
be␈α
the␈α
list␈α
␈↓α(A
␈↓ α←␈↓αB␈α∂C)␈↓.␈α∞ If␈α∂we␈α∞wished␈α∂to␈α∞insert␈α∂an␈α∞atom,␈α∂say␈α∞␈↓αD␈↓,␈α∂between␈α∞␈↓αB␈↓␈α∂and␈α∞␈↓αC␈↓,␈α∂we␈α∞could
␈↓ α←␈↓perform
␈↓"∀␈↓ α←␈↓α␈↓ ∧9x ← cons[car[x];cons[cadr[x];cons[D;cddr[x]]]]
␈↓"∀␈↓ α←␈↓We recopy the initial segment of ␈↓αx␈↓, adding ␈↓αD␈↓ at the appropriate place.
␈↓"β␈↓ α←␈↓␈↓ β'In␈αappropriate␈α
circumstances,␈αwe␈α
can␈αuse␈α␈↓αrplacd␈↓␈α
to␈αinsert␈α
elements␈αinto
␈↓ α←␈↓lists,␈α
using␈α
fewer␈α
␈↓αcons␈↓␈α
operations.␈α
For␈α
example,␈α
given␈α
the␈α
list␈α
␈↓α(A␈α
B␈α∞C)␈↓␈α
with
␈↓ α←␈↓pointers ␈↓αx␈↓ and ␈↓αy␈↓ into it as follows:
␈↓"⊃␈↓ α←␈↓
␈↓ βox y
␈↓"␈↓ α←␈↓
␈↓ βo~ ~
␈↓"␈↓ α←␈↓
␈↓ βo↓ ⊂αααπααα⊃ ↓ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
␈↓ βo%αα→~ A ~ #αβαα∀αα→~ B ~ #αβαα→~ C ~≤'~
␈↓"␈↓ α←␈↓
␈↓ βo %ααα∀ααα$ %ααα∀ααα$ %ααα∀αα$
␈↓ α←␈↓␈↓386 Storage#Structures and#Efficiency␈↓
(7.8␈↓
␈↓"∀␈↓ α←␈↓we␈α≡could␈α≥insert␈α≡the␈α≥element␈α≡␈↓αD␈↓␈α≥␈↓¬after␈↓␈α≡the␈α≥first␈α≡element␈α≥in␈α≡␈↓αy␈↓␈α≥by
␈↓ α←␈↓ ␈↓αrplacd[y;cons[D;cdr[y]]]␈↓, giving:␈↓π 9␈↓
␈↓"⊃␈↓ α←␈↓
␈↓ β?x y
␈↓"␈↓ α←␈↓
␈↓ β?~ ~
␈↓"␈↓ α←␈↓
␈↓ β?↓ ⊂αααπααα⊃ ↓ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
␈↓ β?%αα→~ A ~ #αβαα∀αα→~ B ~ #αβα⊃ ⊂→~ C ~≤'~
␈↓"␈↓ α←␈↓
␈↓ β? %ααα∀ααα$ %ααα∀ααα$ ↓ ↑ %ααα∀αα$
␈↓"␈↓ α←␈↓
␈↓ β? ↓ ~
␈↓"␈↓ α←␈↓
␈↓ β? ⊂αααπααα⊃ ↑
␈↓"␈↓ α←␈↓
␈↓ β? ~ D ~ #αβα$
␈↓"␈↓ α←␈↓
␈↓ β? %ααα∀ααα$
␈↓"∀␈↓ α←␈↓But␈αnote␈αthat␈αthe␈αvalue␈αof␈α␈↓αx␈↓␈αhas␈αalso␈αbeen␈αchanged,␈αand␈αany␈αS-expr␈αsharing
␈↓ α←␈↓the list ␈↓αx␈↓ or ␈↓αy␈↓ as a sublist has also been affected.
␈↓"β␈↓ α←␈↓␈↓ β'We could delete the element ␈↓αD␈↓ by
␈↓"∀␈↓ α←␈↓␈↓ ∧r␈↓αx ← cons[car[x]; cons[car[y];cddr[y]]]␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'We␈α
can␈α
also␈αuse␈α
␈↓αrplacd␈↓␈α
to␈αdelete␈α
␈↓αD␈↓␈α
without␈α
using␈α␈↓αcons␈↓;␈α
we␈α
delete␈αnot␈α
the
␈↓ α←␈↓␈↓¬first␈↓ element of ␈↓αy␈↓, but the next element in ␈↓αy␈↓ by
␈↓"∀␈↓ α←␈↓␈↓ ¬m␈↓αrplacd[y;cddr[y]]␈↓
␈↓"∀␈↓ α←␈↓Similarly,␈α
we␈α
can␈α
use␈α
␈↓αrplaca␈↓␈α
to␈α
modify␈α
an␈α
element␈α
in␈α
a␈α
list␈α
(or␈α
S-expr).␈α
To
␈↓ α←␈↓change the first element in the list, ␈↓αy␈↓, to the S-expr ␈↓αz␈↓ use
␈↓"∀␈↓ α←␈↓␈↓ ε∪␈↓αrplaca[y;z]␈↓
␈↓"∀␈↓ α←␈↓␈↓ β'Notice␈αthat␈αthe␈αuses␈αof␈α␈↓αrplacd␈↓␈αfor␈αinsertion␈αand␈αdeletion␈αare␈αcouched␈αin
␈↓ α←␈↓terms␈α∞of␈α∞insert␈α∞␈↓¬after␈↓␈α∞and␈α∞delete␈α∞␈↓¬after␈↓,␈α∞rather␈α∞than␈α∞insert␈α∞␈↓¬at␈↓␈α∞or␈α∞delete␈α∞␈↓¬at␈↓.␈α
If
␈↓ α←␈↓you look at a diagram you will see why.
␈↓"⊃␈↓ α←␈↓
␈↓ βox
␈↓"␈↓ α←␈↓
␈↓ βo~
␈↓"␈↓ α←␈↓
␈↓ βo↓ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
␈↓ βo%αα→~ A ~ #αβααααα→~ B ~ #αβαα→~ C ~≤'~
␈↓"␈↓ α←␈↓
␈↓ βo %ααα∀ααα$ %ααα∀ααα$ %ααα∀αα$
␈↓"∀␈↓ α←␈↓To␈α∞delete␈α∞the␈α∞element␈α∞␈↓αB␈↓␈α
requires␈α∞modifying␈α∞the␈α∞␈↓αcdr␈↓-part␈α∞of␈α∞the␈α
predecessor
␈↓ α←␈↓cell;␈α∪a␈α∪similar␈α∪remark␈α∪applies␈α∀to␈α∪insertion␈α∪at␈α∪a␈α∪specified␈α∪cell.␈α∀A␈α∪simple,
␈↓ α←␈↓perhaps␈αinefficient␈αscheme,␈αto␈αsupport␈αsuch␈αmodification␈αwould␈αbe␈αto␈α
start␈αa
␈↓ α←␈↓second␈αpointer␈αfrom␈α
the␈αbeginning␈αof␈α
the␈αlist,␈αlooking␈α
for␈αthe␈αcell␈α
whose␈α␈↓αcdr␈↓
␈↓ α←␈↓pointed to the desired spot; then make the modification.
␈↓"β␈↓ α←␈↓␈↓ β'If␈α~these␈α≠"modification-␈↓¬at␈↓"␈α~functions␈α~were␈α≠to␈α~be␈α≠performed␈α~very
␈↓ α←␈↓frequently,␈αthen␈αit␈αmight␈αbe␈αworth␈αstarting␈α␈↓¬two␈↓␈αpointers␈αdown␈αthe␈αlist,␈αone␈αat
␈↓ α←␈↓␈↓αx␈↓,␈αone,␈α
say␈α␈↓αy␈↓,␈α
at␈α␈↓αcdr[x]␈↓,␈α
as␈αabove.␈α
Then␈αtesting␈α
could␈αbe␈α
done␈αusing␈α
␈↓αy␈↓␈αand␈α
the
␈↓ α←␈↓modification␈α
could␈αbe␈α
done␈αusing␈α
␈↓αx␈↓.␈α
When␈αwe␈α
move␈α␈↓αy␈↓␈α
to␈α
␈↓αcdr[y]␈↓,␈αwe␈α
move␈α␈↓αx␈↓␈α
to
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 9␈↓Notice that ␈↓¬one␈↓ application of ␈↓αcons␈↓ is unavoidable.
␈↓ α←␈↓␈↓7.8␈↓ ε[Applications of ␈↓αrplaca␈↓ and ␈↓αrplacd␈↓ 387␈↓α
␈↓"β␈↓ α←␈↓␈↓αcdr[x]␈↓.␈α If␈α
we␈αwanted␈αto␈α
modify␈α␈↓¬before␈↓␈α
rather␈αthan␈α␈↓¬at␈↓,␈α
we␈αcould␈αproliferate␈α
the
␈↓ α←␈↓"back␈α∀pointers,"␈α∀but␈α∪if␈α∀this␈α∀kind␈α∪of␈α∀generality␈α∀is␈α∪required␈α∀a␈α∀change␈α∪of
␈↓ α←␈↓representation␈α∂is␈α∞called␈α∂for.␈α∂ We␈α∞might␈α∂resort␈α∞to␈α∂the␈α∂double-linking␈α∞scheme
␈↓ α←␈↓introduced␈α
on␈α∞page 247;␈α
more␈α∞complex␈α
representations␈α∞are␈α
also␈α∞discussed␈α
in
␈↓ α←␈↓detail in [Knu 68] Chapter 2.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α
LISP␈α
implementation␈α
which␈α
stores␈αp-lists␈α
as␈α
list␈α
structure␈α
would␈αuse
␈↓ α←␈↓␈↓αrplaca␈↓␈α
and␈α
␈↓αrplacd␈↓␈α
heavily;␈α
for␈α
example,␈α
functions␈α
which␈α
modify␈αproperties␈α
on
␈↓ α←␈↓the␈α∂p-lists␈α∂would␈α∂use␈α∂these␈α∂functions.␈α∂Here␈α∂are␈α∂the␈α∂two␈α∂p-list␈α∞manipulating
␈↓ α←␈↓functions, ␈↓αputprop␈↓ and ␈↓αremprop␈↓.
␈↓"∀␈↓ α←␈↓␈↓αputprop␈↓␈αwas␈αintroduced␈αon␈αpage 261.␈α Recall␈αthat␈αthe␈αeffect␈αof␈α␈↓αputprop␈↓␈αis␈αto
␈↓ α←␈↓␈↓ β'attach␈α∂an␈α∂indicator-value␈α∂pair␈α∂to␈α∞an␈α∂atom.␈α∂If␈α∂the␈α∂indicator␈α∂is␈α∞already
␈↓ α←␈↓␈↓ β'present,␈α⊂then␈α∂we␈α⊂will␈α∂simply␈α⊂change␈α⊂its␈α∂value;␈α⊂if␈α∂the␈α⊂indicator␈α⊂is␈α∂not
␈↓ α←␈↓␈↓ β'present,␈α∞then␈α∞we␈α∂will␈α∞add␈α∞the␈α∞indicator-value␈α∂pair␈α∞to␈α∞the␈α∞front␈α∂of␈α∞the
␈↓ α←␈↓␈↓ β'p-list.␈α
In␈α
the␈α
definition␈α
␈↓αn␈↓␈α
is␈α
an␈α
atom,␈α
␈↓αi␈↓␈α
is␈α
an␈α
indicator,␈α
and␈α
␈↓αv␈↓␈α
is␈α
the␈α
value
␈↓ α←␈↓␈↓ β'to be stored.
␈↓"∀␈↓ α←␈↓αputprop <= λ[[n;v;i] prog[[m]
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧Cm ← cdr[n];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨a␈↓ ∧C[eq[car[m];i] → rplaca[cdr[m];v];return[v]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧Cm ← cddr[m];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧C[null[m] → rplacd[n;cons[i;cons[v;cdr[n]]]];return[v]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∨␈↓ ∧Cgo[a] ]]
␈↓"∀␈↓ α←␈↓Note that extended conditional expressions are used in the definition.
␈↓"∀␈↓ α←␈↓␈↓αremprop␈↓␈α∂was␈α∂also␈α∂introduced␈α∂on␈α∂page 261.␈α∂ ␈↓αremprop␈↓␈α∂is␈α∂a␈α∂predicate␈α∂used␈α∂to
␈↓ α←␈↓␈↓ β'remove␈α
attribute-value␈αpairs␈α
from␈α
the␈αproperty␈α
list␈α
of␈αan␈α
atom.␈α We␈α
will
␈↓ α←␈↓␈↓ β'capitalize␈α
on␈α
the␈α
LISP␈α"␈↓αNIL␈↓-non ␈↓αNIL␈↓"␈α
trick␈α
for␈α
predicates␈α
and␈αreturn
␈↓ α←␈↓␈↓ β'the␈α≤removed␈α≥property␈α≤value␈α≤if␈α≥one␈α≤is␈α≤found.␈α≥ The␈α≤following
␈↓ α←␈↓␈↓ β'implementation of ␈↓αremprop␈↓ does that.
␈↓"∀␈↓ α←␈↓αremprop <= λ[[n;i] prog[[m]
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ∧+m ← n;
␈↓"β␈↓ α←␈↓α␈↓ ∧πa␈↓ ∧+[eq[cadr[m];i] → return[prog1[␈↓ π+caddr[m];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ∧+␈↓ π+rplacd[m;cdddr[m]]];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ∧+m ← cddr[m];
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ∧+[null[cdr[m]] → return[␈↓
f␈↓α]]
␈↓"β␈↓ α←␈↓α␈↓ ∧π␈↓ ∧+go[a]]]
␈↓"∀␈↓ α←␈↓where␈α␈↓αprog1␈↓␈αevaluates␈αits␈αarguments␈αfrom␈αleft␈αto␈αright␈αand␈αreturns␈αthe␈αvalue
␈↓ α←␈↓of its first argument.
␈↓"β␈↓ α←␈↓␈↓ β'Applications␈α⊂of␈α⊂␈↓αrplacd␈↓␈α⊂occur␈α⊂inside␈α⊂␈↓αratom␈↓␈α⊂when␈α⊂p-lists␈α⊂are␈α⊂built␈α∂and
␈↓ α←␈↓added␈α∞to␈α
the␈α∞object␈α∞list.␈α
On␈α∞page 394␈α
we␈α∞will␈α∞develop␈α
a␈α∞version␈α∞of␈α
LISP's
␈↓ α←␈↓parser␈α
which␈αuses␈α
pointer␈αmodification␈α
to␈αgain␈α
efficiency␈αwhen␈α
building␈αthe
␈↓ α←␈↓internal␈αrepresentation.␈α Pointer␈αmodification␈αis␈αalso␈αused␈αinside␈αthe␈αgarbage
␈↓ α←␈↓collector; examine the ␈↓αsweep␈↓ phase of the collector on page 286.
␈↓ α←␈↓␈↓388 Storage Structures and Efficiency␈↓
(7.8␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Finally,␈α∂pointer␈α∂modification␈α∂allows␈α∂the␈α∂construction␈α∂of␈α∂self-modifying
␈↓ α←␈↓programs.␈α∀ This␈α∪technique␈α∀is␈α∪similar␈α∀to␈α∪the␈α∀machine␈α∪language␈α∀tricks␈α∪of
␈↓ α←␈↓self-modifying␈α∞code␈α
and␈α∞should␈α
be␈α∞used␈α
with␈α∞similar␈α
care.␈α∞ The␈α∞freedom␈α
to
␈↓ α←␈↓hang␈αyourself␈αshould␈αnot␈αbe␈αconstrued␈αas␈αan␈αinvitation␈αto␈αdo␈αso,␈αbut␈αit␈αagain
␈↓ α←␈↓points␈α∞out␈α
the␈α∞similarities␈α∞of␈α
LISP␈α∞to␈α∞machine␈α
language␈α∞and␈α∞highlights␈α
the
␈↓ α←␈↓differences between LISP and its contemporary high-level languages.
␈↓"β␈↓ α←␈↓␈↓ β'LISP's␈α
central␈α
processor␈α
␈↓αeval␈↓␈α
operates␈α
by␈α
traversing␈α
and␈α
interpreting␈α
the
␈↓ α←␈↓data␈αstructure␈αrepresentation␈αof␈αthe␈αprogram;␈αthat␈αdata␈αstructure␈αis␈αalso␈αopen
␈↓ α←␈↓for␈α⊃inspection␈α⊂by␈α⊃LISP's␈α⊂data␈α⊃structure␈α⊂manipulating␈α⊃functions.␈α⊃ Since␈α⊂we
␈↓ α←␈↓now␈αhave␈α
list-modifying␈αfunctions,␈α
we␈αcould␈αmodify␈α
a␈αprogram␈α
by␈αchanging
␈↓ α←␈↓its␈αinternal␈αstructure.␈αIndeed␈αwe␈αcan␈αwrite␈αa␈αprogram␈αwhich␈αmodifies␈αits␈α␈↓¬own␈↓
␈↓ α←␈↓structure.
␈↓"β␈↓ α←␈↓Here's one:
␈↓"∀␈↓ α←␈↓αfoo <= λ[[x] prog[[y;z]
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧∪z←1;
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧∪y←sixth[body[foo]];
␈↓"β␈↓ α←␈↓α␈↓ β{a␈↓ ∧∪print[x];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧∪rplaca[rest[y];z←add1[z]];
␈↓"β␈↓ α←␈↓α␈↓ β{␈↓ ∧∪go[a] ]]
␈↓"∀␈↓ α←␈↓The␈αmystery␈α
created␈αby␈α
␈↓αy␈↓␈αis␈α
a␈αpointer␈α
into␈αthe␈α
representation␈αof␈αthe␈α
statement
␈↓ α←␈↓␈↓αprint[x]␈↓;␈α
that␈αrepresentation␈α
is␈α␈↓α(PRINT X)␈↓.␈α
Therefore␈αthe␈α
effect␈αof␈α
the␈αfirst
␈↓ α←␈↓␈↓αrplaca␈↓␈αis␈αto␈αchange␈α
␈↓α(PRINT X)␈↓␈αto␈α␈↓α(PRINT 2)␈↓.␈α Subsequent␈α
passes␈αthrough
␈↓ α←␈↓the␈αloop␈αwill␈αchange␈αthe␈αstatement␈αto␈αprint␈α␈↓α3,␈α4,␈↓␈αand␈αso␈αon.␈αThere␈αreally␈αisn't
␈↓ α←␈↓much that can be said about such a program.
␈↓"β␈↓ α←␈↓␈↓ ε_␈↓↓Problems␈↓
␈↓"→␈↓ α←␈↓1. More␈α∃on␈α∃␈↓αratom␈↓.␈α∀ Recall␈α∃the␈α∃discussion␈α∀of␈α∃␈↓αratom␈↓␈α∃in␈α∃Section 5.11␈α∀and
␈↓ α←␈↓␈↓ β∂Section 5.12.␈α
Now␈αthat␈α
you␈α
know␈αabout␈α
␈↓αrplaca␈↓␈α
and␈α␈↓αrplacd␈↓␈α
write␈α
a␈αmore
␈↓ α←␈↓␈↓ β∂detailed version of ␈↓αratom␈↓.
␈↓" ␈↓ α←␈↓2. On␈α⊂page 48␈α⊂and␈α⊂page 49␈α⊂we␈α⊂wrote␈α⊂various␈α⊂styles␈α⊂of␈α⊂␈↓αreverse␈↓.␈α⊃ All␈α⊂these
␈↓ α←␈↓␈↓ β∂functions␈α∀used␈α∀␈↓αconcat␈↓;␈α∪however,␈α∀we␈α∀should␈α∀be␈α∪able␈α∀to␈α∀reverse␈α∀a␈α∪list
␈↓ α←␈↓␈↓ β∂without␈αusing␈α␈↓↓any␈↓␈αnew␈αcells.␈α Express␈αthis␈αalgorithm␈αas␈αa␈αLISP␈αfunction.
␈↓ α←␈↓␈↓ β∂If you use ␈↓αprog␈↓, don't use any ␈↓αprog␈↓-variables.
␈↓ α←␈↓␈↓7.9␈↓ ∂Numbers 389␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬␈␈↓↓7.9 Numbers␈↓
␈↓"β␈↓ α←␈↓In␈α
many␈αimplementations␈α
of␈αLISP,␈α
numbers␈αare␈α
stored␈αas␈α
very␈α
simple␈αkinds
␈↓ α←␈↓of␈α∞atoms:␈α
they␈α∞are␈α
not␈α∞stored␈α
uniquely,␈α∞and␈α
do␈α∞not␈α
need␈α∞print␈α∞names.␈α
Most
␈↓ α←␈↓implementations␈α∀allow␈α∀fixed-␈α∀and␈α∀floating-point␈α∃representation;␈α∀therefore,
␈↓ α←␈↓indicators for these properties are needed. Thus:
␈↓"⊃␈↓ α←␈↓
␈↓ β?fixed-point 1
␈↓"␈↓ α←␈↓
␈↓ β?~
␈↓"␈↓ α←␈↓
␈↓ β?↓
␈↓"␈↓ α←␈↓
␈↓ β?~ ⊂ααααααααπααα⊃ ⊂ααααα⊃
␈↓"␈↓ α←␈↓
␈↓ β?%→ ~ FIXNUM ~ #αβαα→~ 1 ~
␈↓"␈↓ α←␈↓
␈↓ β? %αααααααα∀ααα$ %ααααα$
␈↓"␈↓ α←␈↓
␈↓ β?floating-point 1
␈↓"␈↓ α←␈↓
␈↓ β?~
␈↓"␈↓ α←␈↓
␈↓ β?↓
␈↓"␈↓ α←␈↓
␈↓ β?~ ⊂ααααααααπααα⊃ ⊂ααααααααααααααααααααααα⊃
␈↓"␈↓ α←␈↓
␈↓ β?%→ ~ FLONUM ~ #αβαα→~ <machine rep of 1.0> ~
␈↓"␈↓ α←␈↓
␈↓ β? %αααααααα∀ααα$ %ααααααααααααααααααααααα$
␈↓"∀␈↓ α←␈↓The␈α∩number␈α∩is␈α∩stored␈α∩in␈α∩FWS␈α∩and␈α∩the␈α∩type␈α∩is␈α∩indicated␈α∩by␈α∩a␈α⊃minimal
␈↓ α←␈↓property␈αlist.␈α This␈αrepresentation␈αis␈αexpensive␈αin␈αspace␈αand␈αadds␈αsignificant
␈↓ α←␈↓overhead␈α∞to␈α∞the␈α∞execution␈α∞of␈α∞arithmetic␈α∞operators.␈α∞Several␈α∂techniques␈α∞have
␈↓ α←␈↓been used to improve LISP arithmetic.
␈↓"β␈↓ α←␈↓␈↓ β'Assume␈α∞that␈α∞the␈α∞addressing␈α
space␈α∞of␈α∞the␈α∞machine␈α
is␈α∞2␈↓π18␈↓␈α∞and␈α∞that␈α
the
␈↓ α←␈↓usual␈α∪size␈α∪of␈α∪a␈α∪LISP␈α∪memory␈α∪image␈α∪is␈α∪N;␈α∪within␈α∪the␈α∪LISP␈α∪system,␈α∪all
␈↓ α←␈↓references␈αto␈αmemory␈αlocations␈αgreater␈αthan␈αN␈αare␈αillegal.␈α We␈αwill␈α
use␈αthese
␈↓ α←␈↓illegal␈αaddresses␈αto␈αencode␈αsome␈α
of␈αthe␈αsmaller␈αpositive␈αand␈αnegative␈α
integers,
␈↓ α←␈↓mapping␈α
zero␈αon␈α
the␈αmiddle␈α
address,␈αthe␈α
positive␈αnumbers␈α
to␈αlower␈α
addresses
␈↓ α←␈↓and␈α∞the␈α
negatives␈α∞onto␈α∞the␈α
higher␈α∞addresses.␈α
These␈α∞smaller␈α∞integers,␈α
called
␈↓ α←␈↓␈↓↓INUMS␈↓,␈αare␈α
represented␈αby␈α
pointers␈αoutside␈αof␈α
the␈αnormal␈α
LISP␈αaddressing
␈↓ α←␈↓space.␈α∩ This␈α∩trick␈α∩can␈α⊃considerably␈α∩decrease␈α∩the␈α∩storage␈α∩requirements␈α⊃for
␈↓ α←␈↓applications which use small numbers extensively.
␈↓ α←␈↓␈↓390 Storage#Structures and#Efficiency␈↓
(7.9␈↓
␈↓"⊃␈↓ α←␈↓
␈↓ β' ⊂αααααααααα⊃ 2␈↓π18␈↓
␈↓"␈↓ α←␈↓
␈↓ β' ~␈↓ ¬S~
␈↓"␈↓ α←␈↓
␈↓ β' ~␈↓ ¬S~
␈↓"␈↓ α←␈↓
␈↓ β' ~␈↓ ¬S~ m ␈↓<␈↓
0
␈↓"␈↓ α←␈↓
␈↓ β' ~␈↓ ¬S~
␈↓"␈↓ α←␈↓
␈↓ β' ~␈↓ ¬S~ m = 0
␈↓"␈↓ α←␈↓
␈↓ β' ~␈↓ ¬S~
␈↓"␈↓ α←␈↓
␈↓ β' ~␈↓ ¬S~ m ␈↓>␈↓
0
␈↓"␈↓ α←␈↓
␈↓ β' ~␈↓ ¬S~
␈↓"␈↓ α←␈↓
␈↓ β' ~␈↓ ¬S~
␈↓"␈↓ α←␈↓
␈↓ β' εααααααααααλ N
␈↓"␈↓ α←␈↓
␈↓ β' ~␈↓ ¬S~
␈↓"␈↓ α←␈↓
␈↓ β' ~␈↓ ¬S~
␈↓"␈↓ α←␈↓
␈↓ β' ~␈↓ ¬S~
␈↓"␈↓ α←␈↓
␈↓ β' ~␈↓ ¬S~
␈↓"␈↓ α←␈↓
␈↓ β' ~␈↓ ¬S~
␈↓"␈↓ α←␈↓
␈↓ β' ~␈↓ ¬S~
␈↓"␈↓ α←␈↓
␈↓ β' ~␈↓ ¬S~
␈↓"␈↓ α←␈↓
␈↓ β' ~␈↓ ¬S~
␈↓"␈↓ α←␈↓
␈↓ β' %αααααααααα$ 0
␈↓"∀␈↓ α←␈↓␈↓ Picture of INUM Space ␈↓
␈↓"λ␈↓ α←␈↓The␈α_INUM␈α_representation␈α_adds␈α_some␈α_complexity␈α_since␈α_the␈α↔arithmetic
␈↓ α←␈↓operators now have to recognize these illegal pointers as encoded numbers.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α%MACLISP␈α&([Moo 74])␈α%implementation␈α%uses␈α&a␈α%different
␈↓ α←␈↓representation␈α_for␈α↔numbers.␈↓π 10␈↓␈α_In␈α_that␈α↔implementation,␈α_two␈α_spaces␈α↔are
␈↓ α←␈↓allocated␈α∂for␈α⊂number␈α∂storage:␈α∂FIXNUM␈α⊂space␈α∂and␈α∂FLONUM␈α⊂space.␈α∂This
␈↓ α←␈↓makes␈α
a␈α
more␈αcompact␈α
representation␈α
since␈α
the␈αtype␈α
information␈α
is␈αimplied␈α
in
␈↓ α←␈↓the␈α
address␈α
of␈α
the␈αobject␈α
rather␈α
than␈α
being␈αexplicitly␈α
stored.␈α
To␈α
those␈αbasic
␈↓ α←␈↓spaces␈α⊂we␈α⊂add␈α⊂two␈α⊂temporary␈α∂stack␈α⊂areas:␈α⊂FIXPDL␈α⊂and␈α⊂FLOPDL.␈α∂These
␈↓ α←␈↓areas are used for temporary arithmetic computation.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∀temporary␈α∀areas␈α∪work␈α∀in␈α∀conjunction␈α∪with␈α∀a␈α∀type␈α∪declaration
␈↓ α←␈↓option␈α∃used␈α∃to␈α∃aid␈α∃the␈α∃MACLISP␈α∃compiler.␈α∃ If␈α∃we␈α∃know␈α∃that␈α∃certain
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 10␈↓Much␈α≠care␈α~went␈α≠into␈α~the␈α≠the␈α~representation␈α≠of␈α≠numbers␈α~in
␈↓ α←␈↓MACLISP.␈α∂That␈α∂LISP␈α∂system␈α∂is␈α∞used␈α∂as␈α∂the␈α∂implementation␈α∂language␈α∞for
␈↓ α←␈↓MACSYMA␈α≠([MAC 74],␈α≠[Wan 75],␈α≠[Mos 74]),␈α≠a␈α≠large␈α≠algebraic␈α≠and
␈↓ α←␈↓symbolic␈αmathematics␈αsystem.␈αMACLISP's␈αefficient␈αnumber␈αfacilities,␈α
coupled
␈↓ α←␈↓with␈α⊂its␈α⊂optimizing␈α⊂compiler,␈α⊂have␈α⊂resulted␈α⊂in␈α⊂the␈α⊂production␈α⊂of␈α∂compiled
␈↓ α←␈↓code␈α∩which␈α∩is␈α∪more␈α∩efficient␈α∩than␈α∪that␈α∩produced␈α∩by␈α∪DEC's␈α∩FORTRAN
␈↓ α←␈↓compiler ([Fat 73]).
␈↓ α←␈↓␈↓7.9␈↓ ∂Numbers 391␈↓
␈↓"β␈↓ α←␈↓variables␈α∞are␈α∞␈↓¬always␈↓␈α∞going␈α∞to␈α∞be␈α∞used␈α∞as␈α∞numbers␈α∞in␈α∞a␈α∞particular␈α
function,
␈↓ α←␈↓then␈α⊂we␈α∂can␈α⊂compile␈α∂better␈α⊂code.␈α∂Assume␈α⊂␈↓αx␈↓␈α∂and␈α⊂␈↓αy␈↓␈α∂are␈α⊂to␈α∂be␈α⊂used␈α⊂␈↓¬only␈↓␈α∂as
␈↓ α←␈↓FIXNUMs␈α⊂within␈α⊂a␈α⊂function␈α⊂␈↓αf␈↓;␈α∂we␈α⊂would␈α⊂make␈α⊂such␈α⊂declarations␈α⊂for␈α∂the
␈↓ α←␈↓MACLISP␈α⊂compiler␈α⊂just␈α⊂as␈α⊂we␈α⊂can␈α⊂declare␈α⊂some␈α⊂variables␈α⊂as␈α⊂"special"␈α⊂to
␈↓ α←␈↓other␈αLISP␈αcompilers.␈α When␈αwe␈αallocate␈αspace␈αfor␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓,␈αwe␈αallocate␈α
space
␈↓ α←␈↓on␈αthe␈αtop␈αof␈αFIXPDL.␈αWithin␈α␈↓αf␈↓␈αthe␈αarithmetic␈αoperations␈αuse␈αthe␈αhardware
␈↓ α←␈↓arithmetic␈α
and␈α
reference␈αthe␈α
stack␈α
elements.␈α
The␈αstack␈α
elements␈α
can␈αbe␈α
passed
␈↓ α←␈↓to␈αother␈αarithmetic␈αfunctions␈αcalled␈αwithin␈α␈↓αf␈↓,␈αand␈αno␈αpermanent␈αstorage␈αneed
␈↓ α←␈↓be␈α∩allocated␈α∩in␈α⊃FIXNUM␈α∩space␈α∩until␈α⊃later.␈α∩ The␈α∩efficiency␈α∩of␈α⊃arithmetic
␈↓ α←␈↓operations␈αis␈αdependent␈αon␈αthe␈αexistence␈αof␈αspecial␈αhardware␈αinstructions␈αfor
␈↓ α←␈↓such␈α↔arithmetic.␈α↔ However,␈α↔special␈α↔hardware␈α↔also␈α↔places␈α↔limits␈α↔on␈α↔the
␈↓ α←␈↓arithmetic␈αcapabilities␈αof␈αmost␈αlanguages:␈αarithmetic␈αis␈αusually␈αlimited␈αby␈αthe
␈↓ α←␈↓word size of the machine.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α⊂are␈α⊂several␈α⊂versions␈α⊂of␈α⊂LISP␈α⊂which␈α⊂will␈α⊃automatically␈α⊂change
␈↓ α←␈↓representation␈α∂when␈α⊂faced␈α∂with␈α∂overflow.␈α⊂ This␈α∂scheme␈α∂is␈α⊂called␈α∂␈↓↓arbitrary
␈↓ α←␈↓↓precision␈α⊂arithmetic␈↓␈α∂and␈α⊂has␈α∂been␈α⊂implemented␈α∂for␈α⊂both␈α⊂fixed-point␈α∂and
␈↓ α←␈↓floating-point␈α∩numbers.␈α∩We␈α∩will␈α∩describe␈α∩a␈α∩representation␈α∩for␈α∩fixed-point
␈↓ α←␈↓numbers called ␈↓↓BIGNUMS␈↓; they could have the following structure:
␈↓"⊃␈↓ α←␈↓
␈↓ βWpositive big number
␈↓"␈↓ α←␈↓
␈↓ βW~
␈↓"␈↓ α←␈↓
␈↓ βW↓
␈↓"␈↓ α←␈↓
␈↓ βW~ ⊂ααααααααπααα⊃ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
␈↓ βW%→ ~ POSNUM ~ #αβαα→~ # ~ #αβα→ ... →~ # ~≤'~
␈↓"␈↓ α←␈↓
␈↓ βW %αααααααα∀ααα$ %αβα∀ααα$ %αβα∀αα$
␈↓"␈↓ α←␈↓
␈↓ βW ↓ ↓
␈↓"␈↓ α←␈↓
␈↓ βW N␈↓β0␈↓
N␈↓βn␈↓
␈↓"␈↓ α←␈↓
␈↓ βWnegative big number
␈↓"␈↓ α←␈↓
␈↓ βW~
␈↓"␈↓ α←␈↓
␈↓ βW↓
␈↓"␈↓ α←␈↓
␈↓ βW~ ⊂ααααααααπααα⊃ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
␈↓ βW%→ ~ NEGNUM ~ #αβαα→~ # ~ #αβα→ ... →~ # ~≤'~
␈↓"␈↓ α←␈↓
␈↓ βW %αααααααα∀ααα$ %αβα∀ααα$ %αβα∀αα$
␈↓"␈↓ α←␈↓
␈↓ βW ↓ ↓
␈↓"␈↓ α←␈↓
␈↓ βW N␈↓β0␈↓
N␈↓βn␈↓
␈↓"∀␈↓ α←␈↓␈↓ Structure of a BIGNUM ␈↓
␈↓ α←␈↓␈↓392 Storage Structures and Efficiency␈↓
(7.9␈↓
␈↓"λ␈↓ α←␈↓The value of a BIGNUM is given by:
␈↓"∀␈↓ α←␈↓␈↓ ¬@␈↓εb␈↓(N␈↓β0␈↓ + ␈↓εa␈↓N␈↓β1␈↓+ ... + ␈↓εa␈↓πn␈↓N␈↓βn␈↓)
␈↓"∀␈↓ α←␈↓where␈α
␈↓εb␈↓␈α
is␈α
either␈α
+␈α
or␈α
-␈α
and␈α
␈↓εa␈↓-1␈α
is␈α
the␈α
largest␈α
number␈α
representable␈α
in␈αone
␈↓ α←␈↓machine␈αword.␈α The␈αtranslations␈αbetween␈αBIGNUMS␈αand␈αthe␈αother␈αnumeric
␈↓ α←␈↓representations are done automatically.
␈↓"β␈↓ α←␈↓␈↓ β'On␈α
most␈α
implementations␈α
of␈α
LISP,␈αno␈α
attempt␈α
is␈α
made␈α
to␈αstore␈α
numbers
␈↓ α←␈↓uniquely.␈α
Thus␈α
␈↓αeq␈↓␈α
will␈α
not␈α
work␈α
on␈α
numbers␈α
other␈α
than␈α
INUMs;␈αeither␈α
␈↓αequal␈↓
␈↓ α←␈↓is␈α∪extended␈α∪for␈α∪numbers␈α∪or␈α∪a␈α∪special␈α∪equality␈α∪predicate␈α∪for␈α∪numbers␈α∩is
␈↓ α←␈↓provided.
␈↓"β␈↓ α←␈↓␈↓ ¬"␈↓↓7.10 Stacks and Threading␈↓
␈↓"β␈↓ α←␈↓Though␈α⊗recursive␈α↔algorithms␈α⊗are␈α⊗usually␈α↔more␈α⊗illuminating␈α↔than␈α⊗their
␈↓ α←␈↓machine-oriented␈αcounterparts,␈αit␈αis␈αfrequently␈αmore␈αefficient␈αto␈αencode␈αthose
␈↓ α←␈↓algorithms␈α⊃in␈α⊃manners␈α∩which␈α⊃can␈α⊃take␈α∩advantage␈α⊃of␈α⊃the␈α∩hardware.␈α⊃This
␈↓ α←␈↓section␈α⊗will␈α↔discuss␈α⊗two␈α↔techniques␈α⊗which␈α↔"unwind"␈α⊗the␈α↔recursion␈α⊗and
␈↓ α←␈↓typically lead to faster execution.
␈↓"β␈↓ α←␈↓␈↓ β'Recall␈α
the␈αmarking␈α
phase␈αof␈α
a␈αgarbage␈α
collector␈αin␈α
Section 5.14.␈αThere
␈↓ α←␈↓we␈α∞wrote␈α∞␈↓αmark␈↓␈α∞as␈α∞a␈α∞recursive␈α∞algorithm.␈α∞We␈α∞could␈α∞equally␈α∞well␈α∞write␈α
␈↓αmark␈↓
␈↓ α←␈↓using an explicit stack:
␈↓"∀␈↓ α←␈↓αmark <= λ[[tr]prog[[st]
␈↓"β␈↓ α←␈↓α␈↓ ∧∪loop␈↓ ∧g[is_marked[tr] → go[chk_st];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧g is_full_wd[tr] → markA[tr];go[chk_st];
␈↓" ␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧g is_free_wd[tr] →␈↓ εWst←push[cdr[tr];st];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧g␈↓ εWmarkA[tr];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧g␈↓ εWtr←car[tr];go[loop]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧g ␈↓
t␈↓α → go[chk_st]]; ␈↓π 11␈↓α
␈↓"β␈↓ α←␈↓α␈↓ ∧∪chk_st␈↓ ∧g[null[st] → return[␈↓
t␈↓α]];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧gtr←top[st];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧gst←pop[st];
␈↓"β␈↓ α←␈↓α␈↓ ∧∪␈↓ ∧ggo[loop] ]]
␈↓"∀␈↓ α←␈↓αpush <= λ[[i;st] concat[i;st]]
␈↓" ␈↓ α←␈↓αtop <= λ[[st] first[st]]
␈↓" ␈↓ α←␈↓αpop <= λ[[st] rest[st]]
␈↓"∀␈↓ α←␈↓Notice␈α∞that␈α∂we␈α∞save␈α∂only␈α∞the␈α∂␈↓αcdr␈↓-node␈α∞in␈α∞the␈α∂stack;␈α∞even␈α∂at␈α∞that,␈α∂the␈α∞stack
␈↓ α←␈↓grows␈α∀proportionally␈α∀to␈α∀the␈α∀depth␈α∀of␈α∀the␈α∀tree␈α∀being␈α∀traversed.␈α∃See␈α∀the
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 11␈↓This␈α
branch␈α
of␈α
the␈α
conditional␈α
could␈α
be␈α
omitted␈α
and␈α
the␈α
effect␈α
would
␈↓ α←␈↓be the same.
␈↓ α←␈↓␈↓7.10␈↓ πsStacks and Threading 393␈↓
␈↓"β␈↓ α←␈↓problem␈αon␈αpage 393.␈α The␈αtechnique␈αof␈αusing␈αan␈αexplicit␈αstack␈αsometimes␈αis
␈↓ α←␈↓more intuitive and sometimes will lead to faster execution.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
second␈α
technique␈αis␈α
more␈α
tricky␈α
but␈αwill␈α
lead␈α
to␈αsignificant␈α
pay-offs
␈↓ α←␈↓in␈α∪execution␈α∪time.␈↓π 12␈↓␈α∀The␈α∪technique␈α∪is␈α∀called␈α∪␈↓↓threading␈↓.␈α∪ The␈α∀basis␈α∪for
␈↓ α←␈↓threading␈α∞is␈α∂a␈α∞desire␈α∞to␈α∂traverse␈α∞tree␈α∞structures␈α∂in␈α∞a␈α∞more␈α∂efficient␈α∞fashion
␈↓ α←␈↓than␈α∞that␈α∞typically␈α∞available␈α∂implicitly␈α∞in␈α∞recursion,␈α∞or␈α∞explicitly␈α∂via␈α∞stacks.
␈↓ α←␈↓Recall␈α~that␈α~on␈α~page 247␈α~we␈α~surmised␈α~that␈α~␈↓↓double-linking␈↓␈α~might␈α→be
␈↓ α←␈↓advantageous␈α∀in␈α∀moving␈α∀up␈α∀and␈α∀down␈α∀the␈α∀"spine"␈α∀of␈α∀a␈α∃tree␈α∀structure.
␈↓ α←␈↓Double␈αlinks␈αwould␈αallow␈αus␈αto␈αfind␈αthe␈αsuccessors␈αand␈αpredecessors␈αof␈αnodes
␈↓ α←␈↓easily.␈α
However␈α
the␈α
extra␈α
link␈α
gives␈α
us␈αno␈α
help␈α
if␈α
we␈α
wish␈α
to␈α
descend␈αinto␈α
the
␈↓ α←␈↓substructure.␈α
It␈α∞is␈α
this␈α
area␈α∞to␈α
which␈α
threading␈α∞addresses␈α
itself:␈α∞descent␈α
into
␈↓ α←␈↓tree structure.
␈↓"β␈↓ α←␈↓␈↓ β'Examination␈α
of␈α
the␈αnew␈α
␈↓αmark␈↓␈α
algorithm␈α
will␈αreveal␈α
that␈α
for␈α
a␈α␈↓¬fixed␈↓␈α
tree
␈↓ α←␈↓and␈αa␈α␈↓¬fixed␈↓␈α
order␈αof␈αtraversal;␈α
any␈αtwo␈αapplications␈α
of␈αmarking␈αwill␈αhave␈α
the
␈↓ α←␈↓same␈α∞pattern␈α
of␈α∞behavior.␈α
The␈α∞order␈α
of␈α∞visitation␈α
to␈α∞each␈α
node␈α∞will␈α∞be␈α
the
␈↓ α←␈↓same,␈α
but␈α
more␈α
importantly,␈α
the␈α
dynamic␈α
changes␈α
in␈α
the␈α
state␈α
of␈α
the␈α
stack␈α
will
␈↓ α←␈↓␈↓¬also␈↓␈αbe␈α
the␈αsame.␈α
Instead␈αof␈α
replicating␈αthe␈αportion␈α
of␈αthe␈α
stack,␈αit␈α
might␈αbe
␈↓ α←␈↓possible␈α⊃to␈α⊃store␈α∩the␈α⊃stack␈α⊃information␈α∩in␈α⊃the␈α⊃structure␈α∩itself.␈α⊃ Threading
␈↓ α←␈↓hides␈α⊗the␈α⊗control␈α⊗structure␈α⊗in␈α⊗the␈α⊗data␈α⊗structure.␈α↔ Typically,␈α⊗threading
␈↓ α←␈↓requires␈αa␈αmore␈αcomplex␈αdata␈αstructure␈αsince␈αwe␈αmust␈αstore␈αboth␈αthreads␈α
and
␈↓ α←␈↓links.␈α⊃The␈α⊃traversal␈α⊃algorithms␈α⊃also␈α⊃become␈α⊃more␈α⊃complex␈α⊃since␈α⊃we␈α⊂must
␈↓ α←␈↓recognize␈α∞the␈α∞difference␈α∞between␈α∞control␈α∞threads␈α∞and␈α∞data␈α∞links.␈α∞Care␈α∞must
␈↓ α←␈↓also␈αbe␈αtaken␈αif␈αwe␈αwish␈αto␈αshare␈αthreaded␈αlist␈αstructure.␈αSee␈α[Knu 68]␈α
for␈αa
␈↓ α←␈↓complete discussion of the techniques and tricks.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αdo␈αnot␈αwish␈αto␈αcomplicate␈αthe␈αLISP␈αstructures,␈αbut␈αdispensing␈αwith
␈↓ α←␈↓a␈αstack,␈αbe␈αit␈αimplicit␈αor␈αexplict,␈αdoes␈αinfluence␈αstorage␈αrequirements.␈αWe␈αcan
␈↓ α←␈↓strike␈α∀a␈α∀compromise;␈α∀instead␈α∀of␈α∀permanently␈α∀storing␈α∀the␈α∀threads␈α∀in␈α∀the
␈↓ α←␈↓structure,␈α⊂we␈α⊃can␈α⊂␈↓¬temporarily␈↓␈α⊃store␈α⊂threads␈α⊃as␈α⊂we␈α⊃traverse␈α⊂trees.␈α⊃The␈α⊂first
␈↓ α←␈↓application␈α⊂is␈α∂in␈α⊂the␈α∂design␈α⊂of␈α∂a␈α⊂nonrecursive␈α∂␈↓αread␈↓␈α⊂program.␈α⊂ The␈α∂second
␈↓ α←␈↓application we will describe is in the mark phase of a garbage collector.
␈↓"β␈↓ α←␈↓␈↓ ε≡␈↓↓Problem␈↓
␈↓"→␈↓ α←␈↓1. With␈α∞a␈α∞little␈α∞more␈α∞testing␈α∞before␈α∞stacking␈α∞we␈α∞can␈α∞significantly␈α∂cut␈α∞down
␈↓ α←␈↓␈↓ β∂the␈α∞number␈α∞of␈α∞␈↓αpush␈↓es␈α∞we␈α∞have␈α∞to␈α∞do.␈α∞Namely,␈α∞if␈α∞some␈α∞of␈α∞the␈α
branches
␈↓ α←␈↓␈↓ β∂point␈αimmediately␈αto␈αatoms␈αwe␈αmight␈αas␈αwell␈αmark␈αthem␈αat␈αthat␈αtime␈αand
␈↓ α←␈↓␈↓ β∂proceed␈α∞without␈α
doing␈α∞a␈α∞stack␈α
operation.␈α∞Only␈α
when␈α∞both␈α∞branches␈α
are
␈↓ α←␈↓␈↓ β∂"nonatomic"␈αdo␈αwe␈αneed␈αstack␈αthe␈α␈↓αcdr␈↓.␈αWrite␈αsuch␈αan␈αalgorithm.␈α Further,
␈↓ α←␈↓␈↓ β∂is it better to stack the ␈↓αcdr␈↓ nodes or the ␈↓αcdr␈↓ nodes?
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 12␈↓But there will be a proportional loss in clarity in the code.
␈↓ α←␈↓␈↓394 Storage Structures and Efficiency␈↓
_7.11␈↓
␈↓"β␈↓ α←␈↓␈↓ ¬*␈↓↓7.11 A Non-recursive ␈↓αread␈↓↓␈↓α
␈↓"β␈↓ α←␈↓The␈α⊂original␈α⊂␈↓αread␈↓␈α⊂algorithm␈α⊂of␈α⊂Section 5.11␈α∂is␈α⊂a␈α⊂good␈α⊂example␈α⊂of␈α⊂a␈α∂clear
␈↓ α←␈↓recursive␈αalgorithm;␈αit␈αis␈αreasonably␈αstraightforward␈αto␈αfollow␈αthe␈αflow␈αof␈αthe
␈↓ α←␈↓algorithm.␈α However,␈αnow␈α
that␈αwe␈αunderstand␈α
what␈αthe␈αrun-time␈αbehavior␈α
of
␈↓ α←␈↓such␈αa␈α
recursive␈αprogram␈α
is,␈αwe␈α
see␈αthat␈α
␈↓αread␈↓␈αis␈α
a␈αdrain␈α
on␈α␈↓¬two␈↓␈α
resources:␈αit
␈↓ α←␈↓uses␈α∞free-space␈α∂to␈α∞construct␈α∂the␈α∞internal␈α∂representation␈α∞of␈α∂the␈α∞input;␈α∂it␈α∞uses
␈↓ α←␈↓the␈α⊂run-time␈α⊂stack␈α⊂in␈α⊂the␈α⊂implementation␈α⊂of␈α⊂the␈α⊂recursion␈α⊂and␈α⊂for␈α∂saving
␈↓ α←␈↓parameters␈α∞and␈α∂intermediate␈α∞computations.␈α∞A␈α∂deeply␈α∞nested␈α∂expression␈α∞will
␈↓ α←␈↓use␈αa␈α
lot␈αof␈αthe␈α
run-time␈αstack.␈α
Clearly,␈αthere␈αis␈α
nothing␈αwe␈α
can␈αdo␈αabout␈α
the
␈↓ α←␈↓drain␈αon␈αthe␈αfree␈αlists,␈↓π 13␈↓␈αbut␈αthreading␈α␈↓¬can␈↓␈αdispense␈αwith␈αthe␈αrun-time␈αstack.
␈↓ α←␈↓We␈α
can␈αin␈α
fact␈αdo␈α
so␈αwithout␈α
a␈αproportional␈α
increase␈αin␈α
the␈αuse␈α
of␈αfree␈α
space;
␈↓ α←␈↓indeed␈αwe␈α
need␈αonly␈α
␈↓¬one␈↓␈αadditional␈αfree␈α
cell,␈αregardless␈α
of␈αthe␈α
complexity␈αof
␈↓ α←␈↓the␈α⊃input!␈α⊃The␈α∩algorithm␈α⊃will␈α⊃be␈α⊃much␈α∩more␈α⊃complex␈α⊃that␈α∩the␈α⊃recursive
␈↓ α←␈↓parser,␈αbut␈α
that's␈αwhy␈α
this␈αsection␈αon␈α
storage␈αand␈α
efficiency␈αis␈α
where␈αit␈αis.␈α
We
␈↓ α←␈↓now␈α∃understand␈α∃the␈α∃purpose␈α∃and␈α⊗intent␈α∃of␈α∃␈↓αread␈↓.␈α∃ Now␈α∃that␈α⊗the␈α∃basic
␈↓ α←␈↓algorithm is well understood we can be clever and efficient.
␈↓"β␈↓ α←␈↓␈↓ β'First␈α⊂we␈α⊂describe␈α∂the␈α⊂basic␈α⊂ideas␈α∂of␈α⊂the␈α⊂algorithm,␈α∂then␈α⊂we␈α⊂give␈α∂the
␈↓ α←␈↓algorithm.␈α∂ The␈α∂main␈α∂idea␈α∂in␈α∂the␈α∂algorithm␈α∂is␈α∂the␈α∂realization␈α∂that␈α∂we␈α∞can
␈↓ α←␈↓determine␈α
the␈αstorage␈α
requirements␈αfor␈α
a␈αcomplex␈α
S-expr␈αor␈α
list␈α
structure␈αas
␈↓ α←␈↓we␈α
read␈α
it␈α
in.␈α
For␈α∞example,␈α
consider␈α
the␈α
input␈α
string␈α
"␈↓α(A (B C) D)␈↓".␈α∞As␈α
we
␈↓ α←␈↓start␈α
our␈αleft-to-right␈α
scan␈αof␈α
the␈αinput,␈α
we␈αsee␈α
"␈↓α(␈↓".␈αThis␈α
immediately␈α
tells␈αus
␈↓ α←␈↓that␈αwe␈αneed␈αat␈αleast␈αone␈α␈↓αcons␈↓.␈α We␈αread␈α"␈↓αA␈↓";␈αthat␈αtells␈αus␈αwhat␈αthe␈α␈↓αcar␈↓␈αof␈α
the
␈↓ α←␈↓expression␈α∪is.␈α∪Notice␈α∀that␈α∪we␈α∪don't␈α∪yet␈α∀know␈α∪whether␈α∪the␈α∀expression␈α∪is
␈↓ α←␈↓"dotted"␈α∀or␈α∪"listed,"␈α∀but␈α∀the␈α∪storage␈α∀requirements␈α∀will␈α∪be␈α∀the␈α∀same.␈α∪On
␈↓ α←␈↓reading␈α
the␈α
next␈α∞open␈α
parenthesis␈α
we␈α∞know␈α
we␈α
need␈α
to␈α∞add␈α
a␈α
new␈α∞level␈α
in
␈↓ α←␈↓the␈α∞developing␈α∞representation.␈α∞The␈α∞"␈↓αB␈↓"␈α∂and␈α∞"␈↓αC␈↓"␈α∞add␈α∞elements␈α∞to␈α∂that␈α∞level,
␈↓ α←␈↓and␈α∪the␈α∪closing␈α∪parenthesis␈α∀finishes␈α∪it␈α∪off.␈α∪The␈α∪closing␈α∀parenthesis␈α∪also
␈↓ α←␈↓should␈α
signal␈α∞our␈α
parser␈α
to␈α∞return␈α
to␈α
the␈α∞prior␈α
level␈α
and␈α∞continue␈α
scanning
␈↓ α←␈↓the␈α∩input.␈α∩The␈α∩"␈↓αD␈↓"␈α∩goes␈α∩on␈α∩that␈α∩level␈α∩and␈α∩the␈α∩final␈α∪closing␈α∩parenthesis
␈↓ α←␈↓completes␈α
the␈α
input.␈α
To␈αimplement␈α
this␈α
informal␈α
idea,␈αwe␈α
keep␈α
a␈α
thread␈αin
␈↓ α←␈↓the␈α∂␈↓αcdr␈↓-part␈α⊂of␈α∂the␈α∂last␈α⊂cell␈α∂on␈α∂every␈α⊂level.␈α∂When␈α∂we␈α⊂go␈α∂down␈α∂a␈α⊂level␈α∂we
␈↓ α←␈↓manufacture␈α
a␈αnew␈α
cell␈αwith␈α
the␈α
␈↓αcdr␈↓␈αpointing␈α
to␈αthe␈α
cell␈α
we␈αjust␈α
came␈αfrom␈α
in
␈↓ α←␈↓the␈αprevious␈αlevel;␈αthis␈αhappens␈αwhen␈αwe␈αsee␈αa␈αleft␈αparenthesis.␈αWe␈αgo␈αup␈αa
␈↓ α←␈↓level␈α⊃when␈α⊃we␈α⊃see␈α⊃a␈α⊃right␈α∩parenthesis;␈α⊃that␈α⊃is␈α⊃done␈α⊃by␈α⊃following␈α∩up␈α⊃the
␈↓ α←␈↓thread in the current level, after doing appropriate cleanup.
␈↓"β␈↓ α←␈↓␈↓ β'There are three basic states in the reader:
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ The␈αnext␈αinput␈αshould␈αgo␈αinto␈αthe␈α␈↓αcar␈↓-part␈αof␈αthe␈αcurrent␈αcell.␈α This␈αstate
␈↓ α←␈↓␈↓ β∂is␈α∞entered␈α
when␈α∞we␈α
go␈α∞down␈α
a␈α∞level.␈α
It␈α∞is␈α
labeled␈α∞␈↓αhead␈↓␈α
in␈α∞the␈α
following
␈↓ α←␈↓␈↓ β∂program.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ The␈αnext␈αinput␈αshould␈αgo␈αon␈αthe␈αcurrent␈αlevel.␈αThis␈αis␈αthe␈αtypical␈αstate␈αin
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 13␈↓We␈α
probably␈α
will␈α
be␈α
drawing␈α
on␈αthe␈α
full␈α
word␈α
area␈α
for␈α
print␈αname
␈↓ α←␈↓storage as well as on the free space area for list structure storage.
␈↓ α←␈↓␈↓7.11␈↓ πrA Non-recursive ␈↓αread␈↓ 395␈↓α
␈↓"β␈↓ α←␈↓␈↓ β∂the␈α
building␈α
of␈α
a␈αlist-input.␈α
Here␈α
we␈α
add␈α
a␈αnew␈α
cell␈α
in␈α
the␈α
current␈αlevel
␈↓ α←␈↓␈↓ β∂and␈α
put␈αthe␈α
input␈αin␈α
the␈α
␈↓αcar␈↓-part␈αof␈α
that␈αcell;␈α
then␈α
stay␈αin␈α
this␈αstate.␈α
This
␈↓ α←␈↓␈↓ β∂state corresponds to label ␈↓αtail␈↓.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ The␈α⊂other␈α∂main␈α⊂state␈α∂occurs␈α⊂on␈α∂reading␈α⊂a␈α∂dot␈α⊂when␈α∂in␈α⊂␈↓αtail␈↓ state.␈↓π 14␈↓␈α∂In
␈↓ α←␈↓␈↓ β∂dot state␈α⊂we␈α⊂check␈α⊂the␈α⊂next␈α⊂input;␈α⊂if␈α⊂it␈α⊂is␈α⊂an␈α⊂atom␈α⊂we␈α⊂store␈α⊂it␈α⊂on␈α∂the
␈↓ α←␈↓␈↓ β∂thread␈αand␈αfollow␈αthe␈αthread.␈α If␈αthe␈αinput␈αis␈αa␈αleft␈αparenthesis␈αwe␈αadd␈αa
␈↓ α←␈↓␈↓ β∂new cell and go down.
␈↓"β␈↓ α←␈↓␈↓ β'There␈αare␈α
some␈αanomalies␈αin␈α
the␈αalgorithm␈αsince␈α
it␈αmust␈αrecognize␈α
both
␈↓ α←␈↓S-expr␈α
notation␈α
and␈αlist␈α
notation.␈α
To␈αhandle␈α
both␈α
kinds␈αof␈α
input,␈α
we␈α
add␈αa
␈↓ α←␈↓parenthesis␈α⊃counter;␈α⊃it␈α⊃increments␈α⊂for␈α⊃left␈α⊃parentheses␈α⊃and␈α⊃decrements␈α⊂for
␈↓ α←␈↓right␈α
parentheses.␈α
A␈α
legal␈α
input␈α
has␈α
been␈α
recognized␈α
when␈α
we␈α
are␈α
back␈αat␈α
the
␈↓ α←␈↓top level and the count is zero.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
final␈αdifference␈α
between␈α
the␈αold␈α
parser␈α
and␈αthe␈α
new␈α
one␈αinvolves
␈↓ α←␈↓the␈αscanner␈α␈↓αratom␈↓.␈αWe␈αassume␈αa␈αnew␈α␈↓αratom␈↓␈αwhich␈αreads␈α␈↓α()␈↓␈αand␈αreturns␈α␈↓αNIL␈↓.
␈↓ α←␈↓If␈α↔the␈α↔scanner␈α↔sees␈α↔an␈α↔open␈α↔parenthesis,␈α↔it␈α↔looks␈α↔ahead␈α↔to␈α↔the␈α⊗next
␈↓ α←␈↓meaningful␈αcharacter.␈↓π 15␈↓␈αIf␈αthe␈αcharacter␈αis␈αa␈αclosing␈αparenthesis,␈αthe␈αscanner
␈↓ α←␈↓takes␈αit;␈αif␈αthe␈αcharacter␈αis␈αnot,␈αit␈αis␈αleft␈αfor␈αthe␈αnext␈αcall␈αon␈α␈↓αratom␈↓␈αand␈α␈↓αratom␈↓
␈↓ α←␈↓returns with an indication that it has seen a left parenthesis.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 14␈↓Dots seen in any other context are errors.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 15␈↓It ignores spaces and the like.
␈↓ α←␈↓␈↓396 Storage Structures and Efficiency␈↓
_7.11␈↓
␈↓"∀␈↓ α←␈↓With this introduction, here is the new ␈↓αread␈↓:
␈↓"∀␈↓ α←␈↓αread <= λ[[] prog[[j;cp;count;top;temp]
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧Ocount←init[]; cp←count; top←cp;
␈↓"β␈↓ α←␈↓α␈↓ βchead␈↓ ∧Oj←ratom[];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O[or[is_dot[j];is_rpar[j]] → err[];
␈↓" ␈↓ α←␈↓α␈↓ βc␈↓ ∧O is_lpar[j] →␈↓ ¬wincr[count];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O␈↓ ¬wcp←down[cp];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O␈↓ ¬wgo[head];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O atom[j] → stuff[cp;j]; go[ckend]];
␈↓"β␈↓ α←␈↓α␈↓ βctail␈↓ ∧Oj←ratom[];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O[atom[j] → cp←insert_move[cp;j]; go[ckend];
␈↓" ␈↓ α←␈↓α␈↓ βc␈↓ ∧O is_rpar[j] →␈↓ ¬wdecr[count];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O␈↓ ¬w[eq[top;cp] → go[ck1];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O␈↓ ¬w ␈↓
t␈↓α → cp←stuff_up[cp;NIL]; go[ckend]];
␈↓"β
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧Ois_lpar[j] →␈↓ ¬wincr[count];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O␈↓ ¬wcp←down[insert_move[cp;NIL]];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O␈↓ ¬wgo[head];
␈↓" ␈↓ α←␈↓α␈↓ βc␈↓ ∧Ois_dot[j] →␈↓ ¬wj←ratom[];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O␈↓ ¬w[or[is_dot[j];is_rpar[j]] → err[];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O␈↓ ¬w is_lpar[j] →␈↓ π∨incr[count];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O␈↓ ¬w␈↓ ππ␈↓ π∨cp←insert_move[cp;NIL];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O␈↓ ¬w␈↓ ππ␈↓ π∨go[head];
␈↓" ␈↓ α←␈↓α␈↓ βc␈↓ ∧O␈↓ ¬w atom[j] →␈↓ ππcp←stuff_up[cp;j];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O␈↓ ¬w␈↓ ππgo[ckend]]]; ␈↓π 16␈↓α
␈↓"β␈↓ α←␈↓α␈↓ βcckend␈↓ ∧O[eq[cp;top] → go[ck1];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O ␈↓
t␈↓α → go[tail]];
␈↓"β␈↓ α←␈↓α␈↓ βcck1␈↓ ∧Otemp← cnt[top];
␈↓"β␈↓ α←␈↓α␈↓ βcend2␈↓ ∧O[zerop[temp] → return[exp[top]];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧Oj←ratom[];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O[is_rpar[j] → temp←sub1[temp]; go[end2];
␈↓"β␈↓ α←␈↓α␈↓ βc␈↓ ∧O ␈↓
t␈↓α → err[] ]]]
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 16␈↓This ␈↓αgo␈↓ is superfluous, but makes the flow more apparent.
␈↓ α←␈↓␈↓7.11␈↓ πrA Non-recursive ␈↓αread␈↓ 397␈↓α
␈↓"β␈↓ α←␈↓αinit <= λ[[] cons[NIL;0]]
␈↓"β␈↓ α←␈↓αstuff <= λ[[x;y] rplaca[x;y]]
␈↓"β␈↓ α←␈↓αincr <= λ[[z] rplacd[z;add1[cdr[z]]]]
␈↓"β␈↓ α←␈↓αinsert_move <= λ[[cp;val] rplacd[cp;cons[val;cdr[cp]]]; cdr[cp]]
␈↓"β␈↓ α←␈↓αdown <= λ[[cp] rplaca[cp;cons[NIL;cp]];car[cp]]
␈↓"β␈↓ α←␈↓αstuff_up <= λ[[cp;j] prog[[temp]
␈↓"β␈↓ α←␈↓α␈↓ ¬/temp ← cdr[cp];
␈↓"β␈↓ α←␈↓α␈↓ ¬/rplacd[cp;j];
␈↓"β␈↓ α←␈↓α␈↓ ¬/return[temp]]]
␈↓"β␈↓ α←␈↓αcnt <= λ[[x] cdr[x]]
␈↓" ␈↓ α←␈↓αexp <= λ[[x] car[x]]
␈↓"∀␈↓ α←␈↓The␈αdevelopment␈α
and␈αunderstanding␈αof␈α
this␈αalgorithm␈α
requires␈αmost␈αof␈α
what
␈↓ α←␈↓we␈αhave␈α
covered␈αin␈αthe␈α
course.␈αWe␈αuse␈α
our␈αknowledge␈αof␈α
the␈αparser,␈α␈↓αread␈↓;␈α
we
␈↓ α←␈↓use␈αour␈αfamiliarity␈αwith␈αS-exprs␈αstored␈αas␈αlinked␈αlists;␈αwe␈αhave␈αto␈αunderstand
␈↓ α←␈↓the␈α∂run-time␈α∂control␈α∂of␈α∂recursive␈α∞calling␈α∂sequences;␈α∂we␈α∂have␈α∂to␈α∞understand
␈↓ α←␈↓pointer␈α∀manipulation;␈α∃we␈α∀have␈α∀to␈α∃understand␈α∀pointer␈α∃modification;␈α∀and
␈↓ α←␈↓finally␈α
we␈αhave␈α
to␈α
be␈αwickedly␈α
clever.␈α
With␈αthat␈α
understanding␈α
we␈αwere␈α
able
␈↓ α←␈↓to apply threading at a level higher than a "once only" trick.
␈↓"β␈↓ α←␈↓␈↓ ε≡␈↓↓Problem␈↓
␈↓"→␈↓ α←␈↓␈↓↓1.␈↓ Write␈αa␈αversion␈α
of␈α␈↓αread␈↓␈αwhich␈α
uses␈αan␈αexplicit␈α
stack␈αto␈αremember␈αwhere␈α
it
␈↓ α←␈↓␈↓ β∂is in the parse.
␈↓"β␈↓ α←␈↓␈↓ ∧Z␈↓↓7.12 More Applications of Threading␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧-␈↓↓A Link-Bending Garbage Collector for LISP␈↓
␈↓"∀␈↓ α←␈↓The␈α
use␈α
of␈α
a␈α
stack␈α
is␈αone␈α
of␈α
the␈α
difficulties␈α
associated␈α
with␈αgarbage␈α
collection.
␈↓ α←␈↓Garbage␈α∞collection␈α∞is␈α∞invoked␈α∞when␈α∞available␈α∞space␈α∞has␈α∂become␈α∞exhausted,
␈↓ α←␈↓but␈α
here␈α
we␈α
are␈α
asking␈α
for␈α␈↓¬more␈↓␈α
space␈α
to␈α
use␈α
for␈α
stacking.␈α
The␈αusual␈α
solution
␈↓ α←␈↓to␈αsuch␈αa␈α
problem␈αis␈αto␈α
allocate␈αa␈αseparate␈α
area␈αfor␈αstack␈α
storage.␈αThis␈αhas␈α
its
␈↓ α←␈↓drawbacks.␈α∞If␈α∞we␈α∞don't␈α∞allocate␈α∞enough␈α
stack␈α∞space␈α∞the␈α∞depth␈α∞of␈α∞a␈α∞piece␈α
of
␈↓ α←␈↓structure␈α∂may␈α∂become␈α∂too␈α∞great,␈α∂and␈α∂the␈α∂marker␈α∞will␈α∂fail.␈α∂ The␈α∂amount␈α∞of
␈↓ α←␈↓stack␈α
space␈α
can␈α∞become␈α
large␈α
-␈α∞proportional␈α
to␈α
the␈α
depth␈α∞of␈α
a␈α
list.␈α∞ We␈α
can
␈↓ α←␈↓apply␈αthreading␈αhere,␈α
modifying␈αthe␈αstructure␈αas␈α
we␈αtraverse␈αit;␈αas␈α
usual␈αthe
␈↓ α←␈↓threads␈αwill␈α
be␈αused␈αas␈α
control␈αinformation.␈αAs␈α
we␈αfinish␈αmarking␈α
a␈αbranch
␈↓ α←␈↓␈↓398 Storage Structures and Efficiency␈↓
_7.12␈↓
␈↓"β␈↓ α←␈↓we␈α⊂restore␈α⊂the␈α⊂structure␈α⊂to␈α⊂its␈α⊂original␈α⊂topology.␈α⊂ Several␈α⊂versions␈α⊂of␈α⊂such
␈↓ α←␈↓threaded␈α∀collectors␈α∃are␈α∀available;␈α∃see␈α∀[Chr 68]␈α∀for␈α∃a␈α∀version␈α∃written␈α∀in
␈↓ α←␈↓AMBIT/G;␈α∂a␈α∂more␈α∂traditional␈α∂description␈α∞is␈α∂found␈α∂in␈α∂[Sch 67];␈↓π 17␈↓␈α∂and␈α∞see
␈↓ α←␈↓[Knu 68] for several alternatives.
␈↓"β␈↓ α←␈↓␈↓ ¬)␈↓↓Binding Implementations␈↓
␈↓"∀␈↓ α←␈↓Threading␈α∂can␈α∂be␈α∂used␈α∂in␈α∂the␈α∂shallow␈α∂binder␈α∂described␈α∂in␈α⊂Section 5.20␈α∂to
␈↓ α←␈↓remember␈α∩the␈α∩path␈α∩through␈α∩the␈α∩environment␈α∩tree ([Urm 76]).␈α∪We␈α∩thread
␈↓ α←␈↓from␈αE␈↓βbind␈↓␈αto␈αE␈↓βinter␈↓␈αwhen␈αwe␈αare␈αlooking␈αfor␈αE␈↓βinter␈↓.␈αThis␈αconsists␈αof␈αreversing
␈↓ α←␈↓the␈α⊂access␈α⊂links␈α∂as␈α⊂we␈α⊂proceed␈α∂toward␈α⊂E␈↓βinter␈↓.␈α⊂ Then,␈α∂as␈α⊂we␈α⊂swap␈α⊂back␈α∂the
␈↓ α←␈↓value cells, we will unthread from E␈↓βinter␈↓ to E␈↓βbind␈↓.
␈↓"β␈↓ α←␈↓␈↓ ∧b␈↓↓7.13 Storage Management and LISP␈↓
␈↓"β␈↓ α←␈↓There␈α≥are␈α≥two␈α≥basic␈α≥areas␈α≥of␈α≥LISP␈α≥which␈α≥require␈α≥attention:␈α≥the
␈↓ α←␈↓implementation␈α_of␈α_data␈α↔stuctures,␈α_and␈α_the␈α↔implementation␈α_of␈α_a␈α↔LISP
␈↓ α←␈↓machine. We will discuss applications in that order.
␈↓"β␈↓ α←␈↓␈↓ β'LISP's␈α∂typical␈α∞data␈α∂object␈α∂is␈α∞a␈α∂dotted␈α∂pair;␈α∞however,␈α∂dotted␈α∂pairs␈α∞are
␈↓ α←␈↓frequently␈α⊃used␈α⊃to␈α⊃represent␈α⊃more␈α⊃structured␈α⊃objects.␈α⊃ For␈α⊃example,␈α⊃many
␈↓ α←␈↓common␈α∂LISP␈α∂programs␈α∂involve␈α∂list operations␈α∂on␈α∂list␈α∂representations.␈α∂But
␈↓ α←␈↓lists,␈α∂we␈α∂know,␈α∂are␈α⊂representations␈α∂of␈α∂sequences.␈α∂ From␈α∂Section 7.2␈α⊂we␈α∂now
␈↓ α←␈↓also␈α
know␈α
that␈α
arrays␈α
are␈α
efficient␈α
representations␈α
of␈α
sequences.␈α Indeed␈α
array
␈↓ α←␈↓representations␈αare␈αtypically␈αmore␈αefficient␈αthan␈αthe␈αgeneral␈α
LISP␈αlinked-list.
␈↓ α←␈↓We␈α⊃would␈α⊃like␈α⊃to␈α⊃capitalize␈α⊃on␈α⊃this␈α⊃more␈α⊃efficient␈α⊃representation␈α⊂without
␈↓ α←␈↓jeopardizing the LISP operations.
␈↓"β␈↓ α←␈↓␈↓ β'An␈α
analysis␈α
of␈αthe␈α
LISP␈α
operations␈αshows␈α
that␈α
we␈α
␈↓¬share␈↓␈αsubstructures,
␈↓ α←␈↓and␈α∞if␈α∞using␈α∂␈↓αrplaca␈↓␈α∞or␈α∞␈↓αrplacd␈↓,␈α∞we␈α∂␈↓¬modify␈↓␈α∞existing␈α∞structures.␈α∂Any␈α∞proposed
␈↓ α←␈↓economies␈α
in␈α
storage␈α
layout␈α∞must␈α
take␈α
cognizance␈α
of␈α
these␈α∞facts.␈α
Fortunately
␈↓ α←␈↓these requirements are compatible.
␈↓"β␈↓ α←␈↓Consider the typical representation of the sequence:
␈↓"∀␈↓ α←␈↓α␈↓ ¬4(LAMBDA (X) (F X Y))
␈↓"⊗␈↓ α←␈↓
␈↓ β'⊂ααααααααπααα⊃ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
␈↓ β'~ LAMBDA ~ #αβαα→~ # ~ #αβα→~ # ~≤'~
␈↓"␈↓ α←␈↓
␈↓ β'%αααααααα∀ααα$ %αβα∀ααα$ %αβα∀αα$
␈↓"␈↓ α←␈↓
␈↓ β' ~ ↓
␈↓"␈↓ α←␈↓
␈↓ β' ⊂αααααααααα$ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
␈↓ β' ↓ ~ F ~ #αβ→~ X ~ #αβ→~ Y ~≤'~
␈↓"␈↓ α←␈↓
␈↓ β' ⊂αααπαα⊃ %ααα∀ααα$ %ααα∀ααα$ %ααα∀αα$
␈↓"␈↓ α←␈↓
␈↓ β' ~ X ~≤'~
␈↓"␈↓ α←␈↓
␈↓ β' %ααα∀αα$
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 17␈↓The correctness of [Sch 67] has been proved by de Roever.
␈↓ α←␈↓␈↓7.13␈↓ π
Storage Management and LISP 399␈↓
␈↓"β␈↓ α←␈↓This␈αrepresentation␈αtakes␈αseven␈αwords.␈αThe␈α␈↓αcar␈↓-part␈αof␈αeach␈αcell␈αcontains␈αthe
␈↓ α←␈↓information;␈αthe␈α␈↓αcdr␈↓-part␈α
tells␈αwhere␈αthe␈α
rest␈αof␈αthe␈α
expression␈αis␈αto␈αbe␈α
found.
␈↓ α←␈↓That␈α⊂is,␈α⊂we␈α⊂have␈α⊂dedicated␈α⊃14␈α⊂half-words␈α⊂to␈α⊂represent␈α⊂the␈α⊃structure;␈α⊂only
␈↓ α←␈↓seven␈αof␈α
which␈αcontain␈αthe␈α
actual␈αinformation␈αwe␈α
wish␈αto␈αstore.␈α
Using␈αsome
␈↓ α←␈↓extra␈α∂encoding␈α∂we␈α∂can␈α∂carry␈α∂the␈α∂same␈α∂information␈α∂in␈α∂seven␈α∂slightly␈α∞larger
␈↓ α←␈↓cells.
␈↓"⊃␈↓ α←␈↓
␈↓ ∧∪⊂ααααααααα⊃
␈↓"␈↓ α←␈↓
␈↓ ∧∪~↓ LAMBDA ~
␈↓"␈↓ α←␈↓
␈↓ ∧∪εαααααααααλ ⊂αααααααα⊃
␈↓"␈↓ α←␈↓
␈↓ ∧∪~↓ #αβαααα→~/ X ~
␈↓"␈↓ α←␈↓
␈↓ ∧∪εαααααααααλ %αααααααα$
␈↓"␈↓ α←␈↓
␈↓ ∧∪~/ #αβα⊃
␈↓"␈↓ α←␈↓
␈↓ ∧∪%ααααααααα$ ↓ ⊂αααααααα⊃
␈↓"␈↓ α←␈↓
␈↓ ∧∪ %αα→~↓ F ~
␈↓"␈↓ α←␈↓
␈↓ ∧∪ εααααααααλ
␈↓"␈↓ α←␈↓
␈↓ ∧∪ ~↓ X ~
␈↓"␈↓ α←␈↓
␈↓ ∧∪ εααααααααλ
␈↓"␈↓ α←␈↓
␈↓ ∧∪ ~/ Y ~
␈↓"␈↓ α←␈↓
␈↓ ∧∪ %αααααααα$
␈↓"∀␈↓ α←␈↓The␈α
intent␈αof␈α
the␈αspecial␈α
characters␈αis␈α
to␈αencode␈α
type␈αinformation␈α
about␈αthe
␈↓ α←␈↓␈↓↓next␈↓␈αcell␈αin␈αthe␈α
representation.␈αIt␈αthus␈αis␈α
called␈α␈↓↓␈↓αcdr␈↓↓-coding␈↓.␈α The␈α␈↓
↓␈↓␈αmeans␈α
the
␈↓ α←␈↓next cell ␈↓↓is␈↓ the ␈↓αcdr␈↓; ␈↓
/␈↓ means the ␈↓αcdr␈↓ is ␈↓αNIL␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
typical␈α
LISP␈α
cell␈α
is␈α
a␈α∞third␈α
variety␈α
of␈α
␈↓αcdr␈↓-coding:␈α
the␈α
code␈α∞␈↓
→␈↓␈α
says
␈↓ α←␈↓the␈αnext␈αcell␈αcontains␈α
a␈αpointer␈αto␈αthe␈α␈↓αcdr␈↓.␈α
With␈αthat,␈αwe␈αintroduce␈α
the␈αfinal
␈↓ α←␈↓code:␈α␈↓
*␈↓␈αmeans␈αthis␈αcell␈αis␈αthe␈α␈↓αcdr␈↓-half␈αof␈αa␈αLISP␈αword.␈α Thus␈α␈↓α(A B)␈↓␈αcould␈αbe
␈↓ α←␈↓expressed in any of the following forms:
␈↓"⊃␈↓ α←␈↓
␈↓ β'⊂ααααα⊃ ⊂ααααα⊃
␈↓"␈↓ α←␈↓
␈↓ β'~↓ A ~ ~→ A ~
␈↓"␈↓ α←␈↓
␈↓ β'εαααααλ εαααααλ ⊂ααααα⊃
␈↓"␈↓ α←␈↓
␈↓ β'~/ B ~ ~* #αβααα→~/ B ~
␈↓"␈↓ α←␈↓
␈↓ β'%ααααα$ %ααααα$ %ααααα$
␈↓"␈↓ α←␈↓
␈↓ β'⊂ααααα⊃ ⊂αααααπααααα⊃ ⊂αααααπααααα⊃
␈↓"␈↓ α←␈↓
␈↓ β'~→ A ~ ~ A ~ #αβααα→~ B ~ ≤' ~
␈↓"␈↓ α←␈↓
␈↓ β'εαααααλ ⊂ααααα⊃ %ααααα∀ααααα$ %ααααα∀ααααα$
␈↓"␈↓ α←␈↓
␈↓ β'~* #αβαα→~→ B ~
␈↓"␈↓ α←␈↓
␈↓ β'%ααααα$ εαααααλ
␈↓"␈↓ α←␈↓
␈↓ β' ~* NIL~
␈↓"␈↓ α←␈↓
␈↓ β' %ααααα$
␈↓"∀␈↓ α←␈↓␈↓ β'However␈α∞this␈α∞encoding␈α∞scheme␈α∞is␈α∞not␈α∞sufficient␈α∞as␈α∞it␈α∂stands.␈α∞Consider
␈↓ α←␈↓the following example: Given internal pointers ␈↓αx␈↓ and ␈↓αz␈↓ into
␈↓ α←␈↓␈↓400 Storage#Structures and#Efficiency␈↓
_7.13␈↓
␈↓"⊃␈↓ α←␈↓
␈↓ β{x z
␈↓"␈↓ α←␈↓
␈↓ β{~ ~
␈↓"␈↓ α←␈↓
␈↓ β{↓ ⊂αααπααα⊃ ↓ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
␈↓ β{%αα→~ F ~ #αβαα∀αα→~ X ~ #αβαα→~ Y ~≤'~
␈↓"␈↓ α←␈↓
␈↓ β{ %ααα∀ααα$ %ααα∀ααα$ %ααα∀αα$
␈↓"∀␈↓ α←␈↓and␈α∪assume␈α∪we␈α∪wish␈α∪to␈α∪perform␈α∪␈↓αrplacd[x;(A B C)]␈↓.␈α∪ Using␈α∀our␈α∪standard
␈↓ α←␈↓implementation, we would have:
␈↓"⊃␈↓ α←␈↓
␈↓ β?x z
␈↓"␈↓ α←␈↓
␈↓ β?~ ~
␈↓"␈↓ α←␈↓
␈↓ β?↓ ⊂αααπααα⊃ ↓ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
␈↓ β?%αα→~ F ~ # ~ %αα→~ X ~ #αβαα→~ Y ~≤'~
␈↓"␈↓ α←␈↓
␈↓ β? %ααα∀αβα$ %ααα∀ααα$ %ααα∀αα$
␈↓"␈↓ α←␈↓
␈↓ β? ↓
␈↓"␈↓ α←␈↓
␈↓ β? ~ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ α←␈↓
␈↓ β? %→~ A ~ #αβααααα→~ B ~ #αβαα→~ C ~≤'~
␈↓"␈↓ α←␈↓
␈↓ β? %ααα∀ααα$ %ααα∀ααα$ %ααα∀αα$
␈↓"∀␈↓ α←␈↓However,␈α
a␈α
problem␈α
arises␈α
if␈α
␈↓α(F␈α
X␈α
Y)␈↓␈α
is␈α
represented␈α
in␈α
its␈α
compact␈α
form.␈α
We
␈↓ α←␈↓can't replace the cell
␈↓"⊃␈↓ α←␈↓
␈↓ β?⊂ααααα⊃ ⊂αααααα⊃
␈↓"␈↓ α←␈↓
␈↓ β?~↓ X ~ by ~* #αβ→ to ␈↓α(A B C)␈↓
␈↓"␈↓ α←␈↓
␈↓ β?%ααααα$ %αααααα$
␈↓"∀␈↓ α←␈↓since␈α⊂the␈α⊂value␈α⊂of␈α⊂␈↓αz␈↓␈α⊂would␈α⊂change.␈α⊂The␈α⊂solution␈α⊂is␈α⊂an␈α⊂application␈α⊂of␈α∂the
␈↓ α←␈↓forwarding␈α⊂address␈α⊃scheme␈α⊂we␈α⊂introduced␈α⊃on␈α⊂page 377␈α⊂in␈α⊃the␈α⊂compacting
␈↓ α←␈↓garbage␈α
collector.␈α
We␈αput␈α
a␈α
forwarding␈αaddress␈α
in␈α
the␈αcell␈α
referenced␈α
by␈α␈↓αx␈↓;
␈↓ α←␈↓then␈αallocate␈αa␈αnew␈αpair␈αof␈αhalf-cells,␈αputting␈α␈↓αF␈↓␈αin␈αthe␈αfirst␈αand␈αa␈αpointer␈αto
␈↓ α←␈↓␈↓α(A B C)␈↓ in the second.
␈↓"⊃␈↓ α←␈↓
␈↓ β' x ⊂ααααααααα⊃ ⊂αααααααα⊃ ⊂αααααααα⊃
␈↓"␈↓ α←␈↓
␈↓ β' %→αα→~i #αβαααα→~↓ F ~ ⊂→αα→~↓ A ~
␈↓"␈↓ α←␈↓
␈↓ β' εαααααααααλ εααααααααλ ↑ εααααααααλ
␈↓"␈↓ α←␈↓
␈↓ β' z →α→~↓ X ~ ~* #αβα$ ~↓ B ~
␈↓"␈↓ α←␈↓
␈↓ β' εαααααααααλ %αααααααα$ εααααααααλ
␈↓"␈↓ α←␈↓
␈↓ β' ~/ Y ~ ~/ C ~
␈↓"␈↓ α←␈↓
␈↓ β' %ααααααααα$ %αααααααα$
␈↓"∀␈↓ α←␈↓These␈αforwarding␈αaddresses␈αare␈αan␈αinstance␈αof␈α␈↓↓invisible␈αpointers␈↓␈αused␈αby␈αR.
␈↓ α←␈↓Greenblatt␈α_in␈α_his␈α_LISP␈α↔machine;␈α_he␈α_has␈α_also␈α_implemented␈α↔hardware
␈↓ α←␈↓␈↓αcdr␈↓-coding.␈α Between␈α
invisible␈αpointers␈α
and␈α␈↓αcdr␈↓-coding,␈α
we␈α␈↓¬can␈↓␈α
effect␈αall␈α
LISP
␈↓ α←␈↓operations using this potentially more compact representation.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α⊂must␈α⊂be␈α⊃able␈α⊂to␈α⊂maintain␈α⊃that␈α⊂compact␈α⊂representation␈α⊃while␈α⊂the
␈↓ α←␈↓program␈αis␈α
running.␈αThis␈α
requires␈αmore␈α
care␈αin␈α
the␈αmanagement␈α
of␈αstorage.
␈↓ α←␈↓We␈α⊂cannot␈α⊂simply␈α⊂garbage␈α⊂collect␈α⊃and␈α⊂fragment␈α⊂space;␈α⊂we␈α⊂cannot␈α⊃use␈α⊂the
␈↓ α←␈↓simple␈αcompacting␈αgarbage␈αcollector␈αdiscussed␈αin␈αSection 7.4␈αsince␈αit␈αdoes␈αnot
␈↓ α←␈↓␈↓7.13␈↓ π
Storage Management and LISP 401␈↓
␈↓"β␈↓ α←␈↓attempt␈α
to␈αmaintain␈α
the␈αcompact␈α
representation.␈αSeveral␈α
algorithms␈α
with␈αthe
␈↓ α←␈↓desired␈α⊗properties␈α⊗exist␈α⊗([Che 70],␈α∃[Cla 76]).␈α⊗ One␈α⊗feature␈α⊗of␈α⊗this␈α∃data
␈↓ α←␈↓representation␈αis␈αits␈αuse␈α
of␈αvariable-sized␈αlinked␈αblocks␈αof␈α
sequential␈αstorage.
␈↓ α←␈↓The␈α
management␈αof␈α
these␈αstorage␈α
blocks␈α
is␈αmore␈α
complex␈αthan␈α
that␈αof␈α
simple
␈↓ α←␈↓dotted pairs,␈αbut␈αthe␈αadditional␈αoverhead␈αmay␈αbe␈αacceptable␈αif␈αit␈αgives␈αbetter
␈↓ α←␈↓locality of reference and faster access to list elements.␈↓π 18␈↓
␈↓"β␈↓ α←␈↓␈↓ β'There␈α~is␈α~less␈α~conflict␈α~about␈α~the␈α~use␈α~of␈α~more␈α≠complex␈α~storage
␈↓ α←␈↓management␈αtechniques␈αin␈αthe␈αarea␈αof␈αLISP's␈αdynamic␈αimplementation.␈α The
␈↓ α←␈↓original␈α∩versions␈α∩of␈α∩LISP 1.5␈α∩used␈α∩dotted␈α∩pair␈α∩structure␈α∩to␈α∩represent␈α⊃the
␈↓ α←␈↓access␈α↔environments.␈↓π 19␈↓␈α↔This␈α↔generality␈α_gave␈α↔a␈α↔correct␈α↔solution␈α_to␈α↔the
␈↓ α←␈↓implementation␈α∞of␈α∞␈↓αfunction␈↓,␈α∂but␈α∞experience␈α∞with␈α∞LISP␈α∂implementations␈α∞has
␈↓ α←␈↓shown␈α∪that␈α∪it␈α∩is␈α∪quite␈α∪expensive␈α∪to␈α∩maintain␈α∪this␈α∪generality␈α∪when␈α∩most
␈↓ α←␈↓applications␈α_are␈α→of␈α_a␈α_less␈α→general␈α_nature.␈α→ Implementation␈α_techniques,
␈↓ α←␈↓patterned␈α⊃after␈α⊂our␈α⊃Weizenbaum␈α⊂diagrams,␈α⊃allow␈α⊂some␈α⊃economies␈α⊂without
␈↓ α←␈↓loss␈α⊂of␈α⊂generality.␈α⊂ Again,␈α⊂storage␈α⊂would␈α⊂be␈α⊂allocated␈α⊂in␈α⊂sequential␈α⊂blocks;
␈↓ α←␈↓each␈α∂block␈α∂would␈α∂be␈α⊂of␈α∂a␈α∂size␈α∂sufficient␈α⊂to␈α∂hold␈α∂the␈α∂representation␈α⊂of␈α∂the
␈↓ α←␈↓name-value␈α∞entries␈α∞along␈α∞with␈α∞the␈α∞additional␈α
areas␈α∞to␈α∞link␈α∞the␈α∞block␈α∞to␈α
the
␈↓ α←␈↓environment.␈α
The␈α
storage␈αblocks␈α
need␈α
not␈αbe␈α
allocated␈α
sequentially;␈αindeed,
␈↓ α←␈↓in␈αthe␈αgeneral␈αcase␈αblocks␈αcannot␈αbe␈αallocated␈αsequentially.␈αThe␈αde-allocation
␈↓ α←␈↓problems␈α∂are␈α∂somewhat␈α∂different␈α∞from␈α∂those␈α∂experienced␈α∂by␈α∂data␈α∞structure
␈↓ α←␈↓representations.␈α
The␈α
environment␈α
structures␈α
are␈α
much␈α∞more␈α
"well behaved"
␈↓ α←␈↓than␈α⊃general␈α⊃list-structures.␈α⊃ Therefore␈α⊃an␈α⊃"environment␈α⊃garbage␈α⊂collector"
␈↓ α←␈↓may not be needed.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊗most␈α⊗general␈α⊗techniques␈α⊗for␈α⊗management␈α⊗of␈α⊗LISP's␈α∃dynamic
␈↓ α←␈↓environment are based on [Bob 73a] and succeeding papers.␈↓π 20␈↓
␈↓"β␈↓ α←␈↓␈↓ β'At␈α∪a␈α∪lower␈α∪level␈α∪of␈α∪implementation,␈α∪LISP␈α∪has␈α∪much␈α∪to␈α∪say␈α∪about
␈↓ α←␈↓machine␈α?␈απorganization.␈α?␈απThe␈α?␈απimplementation␈α?␈απof␈α?␈απefficient
␈↓ α←␈↓environment-swapping␈α∞algorithms␈α
is␈α∞a␈α∞problem␈α
which␈α∞any␈α∞operating␈α
system
␈↓ α←␈↓must␈α≡face.␈α≡ The␈α≡traditional␈α≡solutions␈α≡impose␈α≡severe␈α≡restrictions␈α≡on
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 18␈↓Notice␈α∂that␈α⊂the␈α∂␈↓αcdr␈↓-coded␈α⊂representations␈α∂of␈α⊂␈↓α(A B)␈↓␈α∂and␈α⊂␈↓α(A . B)␈↓␈α∂are
␈↓ α←␈↓equally␈α∩expensive.␈α⊃In␈α∩the␈α∩typical␈α⊃linked-list␈α∩representation,␈α∩␈↓α(A B)␈↓␈α⊃requires
␈↓ α←␈↓more space than ␈↓α(A . B)␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 19␈↓The␈α⊂control␈α⊂information␈α⊂did␈α⊃use␈α⊂a␈α⊂stack␈α⊂implementation␈α⊃coded␈α⊂in
␈↓ α←␈↓machine language.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 20␈↓There␈αis␈αsomething␈αcontradictory␈αabout␈αLISP␈αimplementors'␈α
attitudes
␈↓ α←␈↓toward␈α∩storage␈α∩and␈α⊃dynamics.␈α∩Much␈α∩effort␈α⊃is␈α∩expended␈α∩in␈α∩attempting␈α⊃to
␈↓ α←␈↓minimize␈α⊃the␈α⊂overhead␈α⊃involved␈α⊃in␈α⊂the␈α⊃dynamic␈α⊃operation␈α⊂of␈α⊃LISP;␈α⊃it␈α⊂is
␈↓ α←␈↓frequently␈α⊗stated␈α⊗that␈α⊗users␈α⊗should␈α⊗not␈α⊗be␈α⊗penalized␈α↔for␈α⊗access/control
␈↓ α←␈↓constructs␈α
which␈α
they␈α∞do␈α
not␈α
use.␈α
However,␈α∞that␈α
attitude␈α
is␈α
not␈α∞extended␈α
to
␈↓ α←␈↓LISP's␈αdata␈αstructures.␈αThere␈αare␈αvery␈αgenerous␈αsubsets␈αof␈αLISP␈αapplications
␈↓ α←␈↓in␈αwhich␈αthe␈α
data␈αstructure␈αoperations␈α
are␈αsuitably␈αwell-behaved,␈αthat␈α
storage
␈↓ α←␈↓reclamation␈α⊂techniques␈α⊃less␈α⊂general␈α⊂than␈α⊃garbage␈α⊂collection␈α⊃are␈α⊂applicable.
␈↓ α←␈↓Analysis of this area of LISP should lead to profitable results.
␈↓ α←␈↓␈↓402 Storage Structures and Efficiency␈↓
_7.13␈↓
␈↓"β␈↓ α←␈↓interprocess␈α⊗communications.␈α⊗The␈α⊗algorithms␈α⊗developed␈α⊗for␈α⊗LISP␈α⊗show
␈↓ α←␈↓promise for giving efficient implementations of more general scope.
␈↓"β␈↓ α←␈↓␈↓ β'LISP's␈α≡organization␈α≥of␈α≡memory␈α≡also␈α≥has␈α≡lessons␈α≡for␈α≥machine
␈↓ α←␈↓architecture.␈α∩The␈α⊃management␈α∩of␈α∩large␈α⊃variable-sized␈α∩memory␈α∩spaces␈α⊃like
␈↓ α←␈↓[Ste 73]␈α⊂or␈α⊂[Wegb 70]␈α⊃can␈α⊂be␈α⊂supported␈α⊃in␈α⊂hardware.␈α⊂The␈α⊃allocation␈α⊂and
␈↓ α←␈↓de-allocation␈αof␈αsuch␈αlarge␈αspaces␈αalso␈αrequire␈αcare;␈αLISP␈αimplementors␈αhave
␈↓ α←␈↓begun to address these problems ([Ste 76a], [Bis 74a]).
␈↓"β␈↓ α←␈↓␈↓ ¬@␈↓↓7.14 Hash Techniques␈↓
␈↓"β␈↓ α←␈↓One␈αphenomenon␈αof␈αLISP␈αis␈αthe␈α
sheer␈αsize␈αof␈αdata␈αstructures␈αwhich␈α
a␈αlarge
␈↓ α←␈↓LISP␈αprogram␈αgenerates.␈α
Many␈αLISP␈αprojects␈α
approach␈α10␈↓π7␈↓␈αbits␈αof␈α
program
␈↓ α←␈↓and␈α∩data.␈α∪ Several␈α∩techniques␈α∪have␈α∩been␈α∩developed␈α∪to␈α∩help␈α∪shrink␈α∩data
␈↓ α←␈↓representation;␈α␈↓αcdr␈↓-coding␈α
(Section 7.13)␈αis␈α
one␈αtechnique.␈α
Another␈αtechnique
␈↓ α←␈↓stems␈α∂from␈α∂the␈α∂observation␈α∂that␈α∞LISP␈α∂tends␈α∂to␈α∂␈↓¬copy␈↓␈α∂structures␈α∂rather␈α∞than
␈↓ α←␈↓␈↓¬share␈↓␈αthem.␈αWe␈αknow␈αthat␈αthe␈αsharing␈αof␈αstructures␈αmust␈αbe␈αdone␈αwith␈αgreat
␈↓ α←␈↓care␈α∀if␈α∃modification␈α∀operations␈α∃like␈α∀␈↓αrplaca␈↓␈α∀and␈α∃␈↓αrplacd␈↓␈α∀are␈α∃present,␈α∀but
␈↓ α←␈↓sharing␈α⊂of␈α⊃structure␈α⊂can␈α⊂mean␈α⊃a␈α⊂significant␈α⊂saving␈α⊃in␈α⊂space.␈α⊂ In␈α⊃fact,␈α⊂the
␈↓ α←␈↓saving␈αcan␈αalso␈αimprove␈αthe␈αalgorithms␈αwhich␈αmanipulate␈αthe␈αstructures.␈αFor
␈↓ α←␈↓example␈α∪if␈α∪every␈α∪list structure␈α∪is␈α∪stored␈α∪uniquely,␈α∪then␈α∪the␈α∪time␈α∪for␈α∩the
␈↓ α←␈↓equality␈α
test␈α␈↓αequal␈↓␈α
is␈αa␈α
constant␈αrather␈α
than␈αbeing␈α
proportional␈αto␈α
the␈αdepth
␈↓ α←␈↓of the structure.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∩present␈α∩two␈α∩techniques␈α∩for␈α∩maintaining␈α∩unique␈α∩structure:␈α⊃either
␈↓ α←␈↓maintain␈α∂list␈α∂space␈α⊂such␈α∂that␈α∂unique␈α⊂representations␈α∂are␈α∂always␈α⊂present␈α∂or
␈↓ α←␈↓supply␈αan␈αalgorithm␈αwhich␈αwill␈α"uniquize"␈αstructures␈αupon␈αrequest.␈αThe␈αfirst
␈↓ α←␈↓alternative␈αis␈αusually␈αcalled␈α␈↓↓hash␈αconsing␈↓;␈αthe␈αsecond␈αtechnique␈αis␈αcalled␈α␈↓↓list
␈↓ α←␈↓↓condensation␈↓ ([Lin 73]).␈α≡ A␈α≡condensation␈α≡algorithm␈α≡must␈α∨remove␈α≡all
␈↓ α←␈↓duplicated␈αstructure␈αfrom␈αwithin␈αa␈αlist.␈αSince␈αcondensation␈αis␈αa␈αcomponent␈αof
␈↓ α←␈↓many␈α⊃hashed␈α⊂LISP␈α⊃implementations,␈α⊂we␈α⊃will␈α⊂concentrate␈α⊃our␈α⊃attention␈α⊂on
␈↓ α←␈↓hash consing.
␈↓"β␈↓ α←␈↓␈↓ β'Hash␈α⊃consing␈α⊂is␈α⊃an␈α⊃extension␈α⊂of␈α⊃the␈α⊂LISP␈α⊃technique␈α⊃for␈α⊂generating
␈↓ α←␈↓unique␈αatoms.␈α
Since␈αlist␈α
structure␈αis␈αcreated␈α
only␈αby␈α
the␈α␈↓αcons␈↓␈αoperation,␈↓π 21␈↓␈α
we
␈↓ α←␈↓place␈αthe␈αresponsibility␈α
for␈αmaintaining␈αunique␈α
structure␈αwithin␈α␈↓αcons␈↓.␈α
If␈αthe
␈↓ α←␈↓result␈α∩of␈α∩a␈α∩pending␈α∩␈↓αcons␈↓␈α∩is␈α⊃already␈α∩present,␈α∩we␈α∩return␈α∩a␈α∩pointer␈α∩to␈α⊃that
␈↓ α←␈↓structure,␈αotherwise␈αwe␈αperform␈αthe␈α␈↓αcons␈↓␈α
and␈αrecord␈αthe␈αresult␈αso␈αthat␈α
it␈αwill
␈↓ α←␈↓be␈αretrieved␈αif␈αthe␈αsame␈α␈↓αcons␈↓␈αhappens␈αagain.␈αThe␈αadjective␈α"hash"␈αis␈αapplied
␈↓ α←␈↓to␈α∪this␈α∪version␈α∪of␈α∪␈↓αcons␈↓␈α∪since␈α∪the␈α∪typical␈α∪implementation␈α∪uses␈α∀a␈α∪hashing
␈↓ α←␈↓algorithm␈α
to␈αmaintain␈α
the␈αuniqueness.␈α
Hash␈α␈↓αcons␈↓ing␈α
imposes␈α
restrictions␈αon
␈↓ α←␈↓the␈α⊂programmer's␈α⊂use␈α⊂of␈α⊂list␈α∂modification␈α⊂operations.␈α⊂If␈α⊂unique␈α⊂copies␈α∂are
␈↓ α←␈↓available,␈α⊃severe␈α⊃difficulties␈α⊃result␈α⊃if␈α⊃modifications␈α⊃are␈α⊃made.␈α⊃ One␈α⊂either
␈↓ α←␈↓may␈α∞disallow␈α∞list␈α∞modification␈α∂or␈α∞may␈α∞supply␈α∞additional␈α∞operations␈α∂to␈α∞copy
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 21␈↓However, list structure may be modified by ␈↓αrplaca␈↓ and ␈↓αrplacd␈↓.
␈↓ α←␈↓␈↓7.14␈↓ λ*Hash Techniques 403␈↓
␈↓"β␈↓ α←␈↓structure,␈α⊃modify␈α⊂it,␈α⊃and␈α⊃"uniquize"␈α⊂the␈α⊃result,␈α⊂or␈α⊃an␈α⊃implementation␈α⊂may
␈↓ α←␈↓supply␈α~different␈α~kinds␈α~of␈α~structures,␈α~some␈α~modifiable␈α~and␈α~some␈α→not
␈↓ α←␈↓modifiable.
␈↓"β␈↓ α←␈↓␈↓ β'A␈α∂hash␈α⊂␈↓αcons␈↓␈α∂was␈α⊂proposed␈α∂for␈α∂LISP␈α⊂1.75␈α∂on␈α⊂the␈α∂IBM M44,␈α⊂but␈α∂the
␈↓ α←␈↓implementation␈αwas␈αnever␈αcompleted.␈αA␈αlimited␈αversion␈αof␈αhash␈α
␈↓αcons␈↓ing␈αwas
␈↓ α←␈↓implemented as an extension of LISP 1.6 at Stanford.
␈↓"β␈↓ α←␈↓␈↓ β'Impressive␈α⊃and␈α∩extensive␈α⊃applications␈α⊃of␈α∩hashing␈α⊃appear␈α∩in␈α⊃HLISP
␈↓ α←␈↓([Got 74],␈α∩[Ter 75]).␈α∩That␈α∩implementation␈α∩of␈α∩LISP␈α∩supplies␈α∩two␈α∩different
␈↓ α←␈↓kinds␈α_of␈α_structures:␈α→ordinary␈α_list␈α_structure␈α_and␈α→"monocopy"␈α_structures.
␈↓ α←␈↓Operations␈α
are␈α
also␈α
supplied␈α
for␈α
conversion␈α
between␈α
types.␈αExtensive␈α
analysis
␈↓ α←␈↓of␈α
hashing␈α∞effects␈α
on␈α∞algorithm␈α
performance␈α
has␈α∞also␈α
been␈α∞done␈α
([Got 76]).
␈↓ α←␈↓HLISP␈α_also␈α_employs␈α_hashing␈α→in␈α_its␈α_implementation␈α_of␈α→property␈α_lists.
␈↓ α←␈↓Property␈α∂lists␈α⊂are␈α∂not␈α⊂stored␈α∂explicitly,␈α∂but␈α⊂rather␈α∂the␈α⊂atom␈α∂name␈α⊂and␈α∂the
␈↓ α←␈↓property␈α∃name␈α∃are␈α∃used␈α∃to␈α∃form␈α∃a␈α∃hash␈α∃index;␈α∃the␈α∃property␈α∃value␈α∀is
␈↓ α←␈↓associated␈α∞with␈α∞that␈α∞hash␈α∞index.␈α∂ For␈α∞example,␈α∞␈↓αget[x;i]␈↓␈α∞hashes␈α∞with␈α∂both␈α∞␈↓αx␈↓
␈↓ α←␈↓and ␈↓αi␈↓ to retrieve the property value.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α↔other␈α↔major␈α_implementations␈α↔of␈α↔LISP␈α↔also␈α_offer␈α↔specialized
␈↓ α←␈↓operations␈α⊂for␈α⊃dealing␈α⊂with␈α⊃hashed␈α⊂quantities;␈α⊃see␈α⊂[Moo 74],␈α⊃[Int 75],␈α⊂and
␈↓ α←␈↓[Bob 75].
␈↓ α←␈↓␈↓404 Implications of LISP␈↓
88.␈↓
␈↓"β␈↓ α←␈↓␈↓ ∨␈↓↓CHAPTER 8␈↓
␈↓"β␈↓ α←␈↓␈↓ Implications of LISP␈↓
␈↓"β␈↓ α←␈↓Any␈αtext␈αwhich␈αis␈α
of␈αthe␈αsize␈αand␈αextent␈α
of␈αthis␈αbook␈αcertainly␈αowes␈α
a␈αword
␈↓ α←␈↓of␈α
explanation␈α
to␈α
its␈α
readers;␈α
after␈α
404␈α∞pages␈α
it␈α
is␈α
not␈α
fair␈α
to␈α
turn␈α∞the␈α
page
␈↓ α←␈↓and␈α∪find␈α∪the␈α∪index.␈α∪ This␈α∪section␈α∩will␈α∪try␈α∪to␈α∪summarize␈α∪what␈α∪we␈α∩have
␈↓ α←␈↓accomplished␈α⊃in␈α⊂this␈α⊃book␈α⊃and␈α⊂will␈α⊃address␈α⊃some␈α⊂of␈α⊃the␈α⊃current␈α⊂research
␈↓ α←␈↓related to LISP-like languages.
␈↓"β␈↓ α←␈↓␈↓ β'It␈αis␈αthe␈αauthor's␈αbelief␈αthat␈αLISP␈αshould␈αbe␈αthe␈αfirst␈α
language␈αlearned
␈↓ α←␈↓by␈α∀persons␈α∀interested␈α∀in␈α∀computer␈α∪science.␈α∀ As␈α∀a␈α∀language␈α∀for␈α∪studying
␈↓ α←␈↓algorithms␈α
for␈αdata␈α
structures,␈α
it␈αis␈α
presently␈α
without␈αpeer.␈α
As␈α
you␈αhave␈α
seen,
␈↓ α←␈↓the␈αproblems␈αof␈αlanguage␈αimplementation␈αand␈αtheir␈αsolutions␈αare␈αdescribable
␈↓ α←␈↓quite␈αnaturally␈αin␈α
the␈αimplementation␈αof␈α
LISP.␈α As␈αa␈αprogramming␈α
language,
␈↓ α←␈↓LISP␈α⊂has␈α⊂powerful␈α⊂features␈α⊃possessed␈α⊂by␈α⊂few␈α⊂languages,␈α⊂in␈α⊃particular␈α⊂the
␈↓ α←␈↓uniform representation of program and data.
␈↓"β␈↓ α←␈↓␈↓ β'We␈αhave␈α
developed␈αseveral␈α
areas␈αof␈α
programming␈αlanguages␈α
and␈αdata
␈↓ α←␈↓structures␈α∞in␈α
this␈α∞book␈α
and␈α∞have␈α
hinted␈α∞at␈α
future␈α∞possibilities␈α
in␈α∞several␈α
of
␈↓ α←␈↓those areas:
␈↓ α←␈↓␈↓8.␈↓ λεImplications of LISP 405␈↓
␈↓" ␈↓ α←␈↓␈↓↓1.␈↓ Mathematical␈α∞models:␈α
This␈α∞refers␈α∞to␈α
some␈α∞of␈α
the␈α∞theoretical␈α∞areas␈α
which
␈↓ α←␈↓␈↓ β∂use␈αLISP␈αas␈α
the␈αbasis␈αfor␈α
mathematical␈αstudies␈αof␈αalgorithms,␈α
equivalence
␈↓ α←␈↓␈↓ β∂of␈α⊂programs,␈α∂and␈α⊂program␈α∂synthesis␈α⊂from␈α∂formal␈α⊂specifications.␈α∂ These
␈↓ α←␈↓␈↓ β∂are␈α
not␈α
of␈α
purely␈α∞theoretical␈α
interest:␈α
correctness␈α
of␈α∞non-trivial␈α
programs
␈↓ α←␈↓␈↓ β∂is␈α
an␈α
important␈α
practical␈α∞problem.␈α
The␈α
issue␈α
of␈α∞programming␈α
language
␈↓ α←␈↓␈↓ β∂semantics␈α∪also␈α∪needs␈α∪clarification.␈α∪ This␈α∪branch␈α∪of␈α∪semantics␈α∀seeks␈α∪a
␈↓ α←␈↓␈↓ β∂descriptive␈α
tool␈αfor␈α
the␈αmeaning␈α
of␈αconstructs␈α
of␈αa␈α
language;␈αin␈α
particular,
␈↓ α←␈↓␈↓ β∂a␈α⊂tool␈α⊂of␈α⊂the␈α⊂power␈α⊂and␈α⊂clarity␈α⊂of␈α⊂BNF␈α⊂descriptions␈α⊂of␈α⊂the␈α⊃syntax␈α⊂of
␈↓ α←␈↓␈↓ β∂languages.␈α∂We␈α∂have␈α⊂talked␈α∂a␈α∂bit␈α⊂about␈α∂semantic␈α∂issues␈α⊂in␈α∂Section 3.13.
␈↓ α←␈↓␈↓ β∂The␈αclose␈αrelationship␈αbetween␈αLISP␈αevaluators␈αand␈αdenotational␈αmodels
␈↓ α←␈↓␈↓ β∂is encouraging.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ Generalized␈α∂control␈α∂structures:␈α∞We␈α∂hinted␈α∂at␈α∞some␈α∂of␈α∂the␈α∂options␈α∞under
␈↓ α←␈↓␈↓ β∂consideration␈α∞for␈α∞control␈α∞of␈α∞algorithms.␈α∞The␈α∞work␈α∞on␈α∞generalized␈α∞access
␈↓ α←␈↓␈↓ β∂and␈α⊂control,␈α⊂also␈α⊂known␈α⊂as␈α⊂"spaghetti stacks"␈α⊂([Bob 73a]),␈α⊂is␈α⊂of␈α∂current
␈↓ α←␈↓␈↓ β∂interest␈α⊂and␈α⊂many␈α⊂of␈α⊂its␈α⊂motivations␈α⊂and␈α⊂implications␈α⊂should␈α⊂be␈α∂more
␈↓ α←␈↓␈↓ β∂understandable␈α⊂now.␈α∂The␈α⊂devices␈α∂which␈α⊂are␈α∂required␈α⊂for␈α⊂such␈α∂general
␈↓ α←␈↓␈↓ β∂control␈α⊗also␈α⊗come␈α⊗directly␈α⊗from␈α⊗the␈α⊗LISP␈α⊗experience.␈α↔ Devices␈α⊗like
␈↓ α←␈↓␈↓ β∂"spaghetti stacks"␈α∃serve␈α⊗for␈α∃implementation␈α⊗of␈α∃higher␈α⊗level␈α∃language
␈↓ α←␈↓␈↓ β∂constructs like pattern directed invocation.
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ Interpreter/compilers:␈α
This␈α
is␈α
an␈α
interesting␈α
area␈α
which␈α
begins␈α
to␈α
resolve
␈↓ α←␈↓␈↓ β∂the␈α
dichotomy␈αbetween␈α
compilation␈αand␈α
interpretation.␈αWork␈α
was␈αdone␈α
in
␈↓ α←␈↓␈↓ β∂[Mit 70],␈αbut␈αlittle␈αhas␈αbeen␈αdone␈αsince.␈αAgain,␈αLISP␈αis␈αa␈αnatural␈αvehicle
␈↓ α←␈↓␈↓ β∂for discussing this topic.
␈↓" ␈↓ α←␈↓␈↓↓4.␈↓ Implementation␈α∂tricks␈α⊂and␈α∂machine␈α∂organization:␈α⊂In␈α∂the␈α∂past␈α⊂LISP␈α∂has
␈↓ α←␈↓␈↓ β∂been␈α⊗the␈α⊗originator␈α↔of␈α⊗several,␈α⊗now␈α⊗every-day,␈α↔programming␈α⊗tricks.
␈↓ α←␈↓␈↓ β∂Current␈α∂production␈α∂versions␈α∂of␈α∂LISP␈α∂continue␈α∂to␈α⊂develop␈α∂sophisticated
␈↓ α←␈↓␈↓ β∂techniques␈α∞for␈α∂management␈α∞of␈α∂very␈α∞large␈α∂spaces,␈α∞representation␈α∂of␈α∞data
␈↓ α←␈↓␈↓ β∂structures,␈α⊃and␈α⊃execution␈α⊃of␈α⊃complex␈α⊃algorithms.␈α⊃Many␈α⊃of␈α∩those␈α⊃ideas
␈↓ α←␈↓␈↓ β∂have␈α
direct␈α
implications␈α
for␈α
the␈αdevelopment␈α
of␈α
hardware␈α
for␈α
LISP␈αlike
␈↓ α←␈↓␈↓ β∂languages.
␈↓" ␈↓ α←␈↓␈↓↓5.␈↓ Languages␈α
for␈αArtificial␈α
Intelligence␈αresearch:␈α
Though␈αLISP␈α
is␈αthought␈α
of
␈↓ α←␈↓␈↓ β∂primarily␈α∩as␈α∩the␈α∩language␈α∩for␈α∩Artificial␈α∩Intelligence␈α∩programming,␈α⊃we
␈↓ α←␈↓␈↓ β∂have␈αbarely␈αtouched␈αon␈αthose␈αapplications.␈α In␈αthe␈αlast␈αdecade,␈α
LISP␈αhas
␈↓ α←␈↓␈↓ β∂really␈α∂become␈α∂a␈α∂systems␈α∞programming␈α∂language,␈α∂rather␈α∂than␈α∂a␈α∞research
␈↓ α←␈↓␈↓ β∂tool.␈α∞It␈α
is␈α∞the␈α∞systems␈α
language␈α∞for␈α
developing␈α∞more␈α∞powerful␈α
languages
␈↓ α←␈↓␈↓ β∂for␈α⊃A.I.␈α⊃They␈α⊃are␈α⊃"more␈α⊃powerful"␈α⊃in␈α⊃the␈α⊃sense␈α⊃of␈α⊃descriptive␈α⊃power,
␈↓ α←␈↓␈↓ β∂rather␈α∂than␈α∞computational␈α∂power.␈α∂ LISP␈α∞is␈α∂also␈α∞used␈α∂exclusively␈α∂as␈α∞the
␈↓ α←␈↓␈↓ β∂systems programming language on the M.I.T. LISP machine.
␈↓ α←␈↓␈↓406 Implications of LISP␈↓
88.␈↓
␈↓" ␈↓ α←␈↓␈↓↓6.␈↓ Interaction␈α
and␈α
personal␈α
computation.␈α
Though␈α
LISP␈α
developed␈α
in␈αthe␈α
late
␈↓ α←␈↓␈↓ β∂1950's,␈α∃contemporary␈α⊗implementations␈α∃are␈α⊗finally␈α∃exploiting␈α⊗the␈α∃true
␈↓ α←␈↓␈↓ β∂interactive␈α⊂nature␈α∂of␈α⊂the␈α∂language.␈α⊂A␈α∂LISP␈α⊂machine␈α∂is␈α⊂a␈α∂sophisticated
␈↓ α←␈↓␈↓ β∂and␈α∩powerful␈α∩calculator.␈α∩The␈α∩language␈α∩is␈α∩the␈α∩most␈α∩interactive␈α∩of␈α⊃the
␈↓ α←␈↓␈↓ β∂major␈αprogramming␈αlanguages,␈αand␈αas␈αsuch,␈αshould␈αattract␈αthe␈αinterest␈αof
␈↓ α←␈↓␈↓ β∂the␈α_personal␈α_computer␈α_field.␈α_This␈α_should␈α_see␈α_the␈α_development␈α_of
␈↓ α←␈↓␈↓ β∂sophisticated␈α∪␈↓εm␈↓-computer␈α∀LISP␈α∪implementations.␈α∪ The␈α∀combination␈α∪of
␈↓ α←␈↓␈↓ β∂LISP,␈αinexpensive␈αhardware,␈αand␈αcreative␈αminds␈αshould␈αbe␈αinteresting␈αto
␈↓ α←␈↓␈↓ β∂watch.
␈↓"∀␈↓ α←␈↓␈↓ β'The␈αmain␈αpurpose␈αof␈αthis␈αepilog␈αis␈αto␈αtie␈αtogether␈αmost␈αof␈αthe␈αmaterial
␈↓ α←␈↓we␈α
have␈α
studied.␈α
The␈αunderlying␈α
thread␈α
in␈α
our␈αstudy␈α
has␈α
been␈α
the␈αinternal
␈↓ α←␈↓and␈α
external␈αbehavior␈α
of␈α
LISP.␈αA␈α
rather␈α
natural␈αvehicle␈α
to␈α
unify␈αthese␈α
topics
␈↓ α←␈↓is␈αthe␈α
design␈αof␈α
a␈αnew␈α
LISP-like␈αlanguage.␈α
Language␈αdesign␈α
is␈αnot␈αa␈α
pastime
␈↓ α←␈↓to␈αbe␈α
entered␈αinto␈α
lightly;␈αwe␈αwill␈α
therefore␈αsketch␈α
an␈αexisting␈αLISP␈α
extension
␈↓ α←␈↓named EL1. The name EL1 is derived from Extensible Language/1.
␈↓"β␈↓ α←␈↓␈↓ β'There␈α∃are␈α∃two␈α∃basic␈α∃views␈α∃in␈α∃programming␈α∃language␈α∃design:␈α∀one
␈↓ α←␈↓approach␈α∞is␈α∂to␈α∞design␈α∞a␈α∂small␈α∞language,␈α∞called␈α∂a␈α∞base␈α∞language,␈α∂which␈α∞has
␈↓ α←␈↓sufficent␈αexpressive␈α
power␈αto␈α
allow␈αits␈αuser␈α
to␈αmold␈α
a␈αspecial␈α
language␈αfrom
␈↓ α←␈↓that␈α∂base.␈α∂This␈α∂is␈α∞called␈α∂the␈α∂"core"␈α∂approach␈α∞and␈α∂such␈α∂base␈α∂languages␈α∞are
␈↓ α←␈↓called␈αextensible␈α
languages.␈α The␈α
alternative,␈αcalled␈α
the␈α"shell"␈α
approach,␈αis␈α
to
␈↓ α←␈↓design␈α∞a␈α∞full␈α∞language,␈α∞capable␈α∂of␈α∞covering␈α∞a␈α∞specific␈α∞area.␈α∞That␈α∂area␈α∞may
␈↓ α←␈↓only␈α
cover␈α
a␈α
special␈α
domain␈α
of␈α
algorithms␈α
or␈α
might␈α
encompass␈α
␈↓¬all␈↓␈α
algorithmic
␈↓ α←␈↓processes.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
"shell"␈α
approach␈α
to␈αgeneral␈α
purpose␈α
languages␈α
is␈α
best␈αexemplified
␈↓ α←␈↓by␈αPL/1.␈α
This␈αapproach␈αattempts␈α
to␈αbuild␈αa␈α
language␈αwhich␈αencompasses␈α
all
␈↓ α←␈↓the␈α
Pl/1␈α
is␈α
an␈α
The␈α
approach␈α
gives␈α
rise␈α
to␈α
many␈α
problems.␈α
Of␈α
necessity,␈α
the
␈↓ α←␈↓language␈αis␈αlarge;␈αunless␈αcare␈αis␈αtaken␈αa␈αprogrammer␈αwill␈αhave␈αdifficulties␈αin
␈↓ α←␈↓learning␈αthe␈αlanguage.␈αEven␈αif␈αa␈αsmall␈αsubset␈αis␈αpresented␈αto␈αa␈αbeginner,␈αthe
␈↓ α←␈↓occurrence␈α
of␈α∞bugs␈α
in␈α
a␈α∞user␈α
program␈α
may␈α∞cause␈α
mysterious␈α
results␈α∞if␈α
those
␈↓ α←␈↓bugs␈α∀happen␈α∀to␈α∀invoke␈α∀features␈α∀outside␈α∀the␈α∀subset.␈α∀ Also␈α∃the␈α∀language
␈↓ α←␈↓implementor␈α∩is␈α∩in␈α∩for␈α∩a␈α∪hard␈α∩time;␈α∩language␈α∩processors␈α∩will␈α∩have␈α∪to␈α∩be
␈↓ α←␈↓cognizant␈αof␈α␈↓¬all␈↓␈αthe␈αlanguage␈αfeatures␈αeven␈αif␈αthe␈αuser␈αwishes␈αto␈αwork␈αwithin
␈↓ α←␈↓a␈α⊂small␈α⊂subset.␈α⊂The␈α∂problems␈α⊂of␈α⊂optimization␈α⊂are␈α⊂compounded␈α∂immensely,
␈↓ α←␈↓since␈α∂the␈α∂interaction␈α∂of␈α∂language␈α⊂features␈α∂may␈α∂well␈α∂lead␈α∂to␈α⊂torturous␈α∂code.
␈↓ α←␈↓Though␈α
the␈α∞"shell"␈α
approach␈α∞presents␈α
severe␈α∞problems,␈α
the␈α∞"core"␈α
approach
␈↓ α←␈↓of␈α∩extensibility␈α∩is␈α∩not␈α∩without␈α∩flaw.␈α∩There␈α∩are␈α∩non-trivial␈α∩research␈α⊃areas
␈↓ α←␈↓involved␈αin␈αdeveloping␈αsmooth␈αand␈αefficient␈αmeans␈αof␈αdescribing␈αthe␈αsyntax,
␈↓ α←␈↓pragmatics␈α∞and␈α∞semantics␈α∞of␈α∞user␈α∞defined␈α∞data␈α∞structures,␈α∂control␈α∞structures
␈↓ α←␈↓and operations.
␈↓"β␈↓ α←␈↓␈↓ β'An␈α∞extensible␈α∞language␈α∂is␈α∞designed␈α∞to␈α∂supply␈α∞a␈α∞base␈α∂language,␈α∞which
␈↓ α←␈↓has␈αsufficient␈αhandles␈αsuch␈αthat␈αa␈αuser␈αcan␈αdescribe␈αnew␈αdata␈αstructures,␈αnew
␈↓ α←␈↓operations,␈α∂and␈α∂new␈α∂control␈α∂structures.␈α∞These␈α∂new␈α∂objects␈α∂are␈α∂described␈α∞in
␈↓ α←␈↓terms␈α⊃of␈α⊃combinations␈α⊂of␈α⊃constructs␈α⊃in␈α⊂the␈α⊃base␈α⊃language.␈α⊃Extensibility␈α⊂is
␈↓ α←␈↓␈↓8.␈↓ λεImplications of LISP 407␈↓
␈↓"β␈↓ α←␈↓implicitly␈αcommitted␈αto␈αthe␈αpremiss␈αthat␈αthe␈αpower␈αof␈αhigh␈αlevel␈αlanguages␈αis
␈↓ α←␈↓primarily␈α∞notational␈α∞rather␈α∞than␈α∞computational.␈α∞That␈α∞is␈α∞apparent␈α∂from␈α∞our
␈↓ α←␈↓experience␈αwith␈αhigh␈αlevel␈αnumerical␈αlanguages.␈α Their␈αnotations␈αallow␈αus␈αto
␈↓ α←␈↓express␈α∞our␈α∞problems␈α∞in␈α∞mathematics-like␈α∞statements,␈α∞rather␈α∞than␈α∂coding␈α∞in
␈↓ α←␈↓machine language.
␈↓"β␈↓ α←␈↓␈↓ β'Like␈α∪LISP,␈α∩the␈α∪extensible␈α∩language␈α∪EL1␈α∩maps␈α∪programs␈α∪onto␈α∩data
␈↓ α←␈↓structures.␈α
EL1␈α
has␈αricher␈α
data␈α
types␈αincluding␈α
integers,␈α
characters,␈αpointers,
␈↓ α←␈↓and␈α⊗structures.␈α⊗Its␈α↔syntax␈α⊗is␈α⊗described␈α⊗in␈α↔BNF␈α⊗and␈α⊗a␈α↔mapping␈α⊗from
␈↓ α←␈↓well-formed␈αsyntactic␈α
units␈αto␈αdata␈α
structures␈αis␈αgiven.␈α
The␈αEL1␈α
evaluator␈αis
␈↓ α←␈↓written␈α⊃in␈α⊃EL1␈α⊃and␈α⊃manipulates␈α⊃the␈α⊃data-structure␈α⊃representation␈α⊃of␈α⊃EL1
␈↓ α←␈↓programs in a manner totally analogous to the LISP ␈↓αeval␈↓ function.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∩syntax␈α∩of␈α∩EL1␈α∩is␈α∩similar␈α∩to␈α∩that␈α∩of␈α∩M-expression␈α∩LISP.␈α∩ The
␈↓ α←␈↓details␈α⊃are␈α⊃not␈α⊃relevant␈α⊃and␈α⊃are␈α⊃best␈α⊃left␈α⊃to␈α⊃the␈α⊃user's␈α⊃manual,␈α⊃[EL1 74].
␈↓ α←␈↓What␈α∩␈↓¬is␈↓␈α∩important␈α∩is␈α∩the␈α∩interrelationships␈α∩between␈α∩the␈α∩constucts␈α∪of␈α∩the
␈↓ α←␈↓language␈α∀and␈α∀their␈α∀data␈α∀structure␈α∪representations.␈α∀ That␈α∀is,␈α∀we␈α∀wish␈α∪to
␈↓ α←␈↓develop␈α∀a␈α∪representation␈α∀of␈α∀the␈α∪abstract␈α∀syntax␈α∀of␈α∪EL1␈α∀using␈α∀the␈α∪data
␈↓ α←␈↓structures␈α
available␈α
in␈α
EL1.␈α
Our␈α
approach␈αhere␈α
is␈α
the␈α
other␈α
way␈α
round:␈αto
␈↓ α←␈↓motivate␈α
the␈α
data␈α
structures␈α
of␈α
a␈α
language␈α
by␈α
the␈α
requirements␈αfor␈α
expressing
␈↓ α←␈↓a realistic evaluator.␈↓π 1␈↓
␈↓"∀␈↓ α←␈↓Consider this fragment of the LISP syntax from page 17:
␈↓" ␈↓ α←␈↓<form>␈↓ ∧+::= <constant> | <application> | <variable>
␈↓" ␈↓ α←␈↓<application>␈↓ ∧+::= <function-part>[<arg-list>]
␈↓" ␈↓ α←␈↓<arg-list>␈↓ ∧+::= <arg> | <arg-list>;<arg>
␈↓"∀␈↓ α←␈↓These␈α⊂equations␈α⊂demonstrate␈α⊂the␈α⊂three␈α∂kinds␈α⊂of␈α⊂BNF␈α⊂equations.␈α⊂ We␈α∂will
␈↓ α←␈↓concentrate our attention on the last two equations.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∞LISP␈α∞M-to-S-expression␈α∞mapping␈α∞will␈α∞map␈α∞an␈α∂<application>␈α∞like
␈↓ α←␈↓␈↓αf[x;y;z]␈↓␈αonto␈α␈↓α(F␈αX␈αY␈αZ)␈↓.␈αFor␈αall␈αintents␈αand␈αpurposes,␈αLISP␈αhas␈αlittle␈αchoice;
␈↓ α←␈↓LISP␈αhas␈αfew␈α
representations␈αavailable␈αand␈αsince␈α
we␈αwish␈αto␈αuse␈α
the␈αS-expr
␈↓ α←␈↓representation␈α∪as␈α∪the␈α∪programming␈α∪language,␈α∪the␈α∪representation␈α∪must␈α∩be
␈↓ α←␈↓readable.␈α∪ In␈α∪the␈α∪typical␈α∪implementations␈α∪of␈α∪LISP,␈α∪the␈α∀representation␈α∪of
␈↓ α←␈↓␈↓αf[x;y;z]␈↓␈α⊗is␈α∃␈↓α(F . (X . (Y . (X NIL))))␈↓.␈α⊗ That␈α∃requires␈α⊗a␈α∃lot␈α⊗of␈α⊗space,␈α∃and
␈↓ α←␈↓requires␈α
some␈α
decoding␈α
by␈α
any␈αprogram␈α
which␈α
is␈α
to␈α
use␈α
this␈αrepresentation.
␈↓ α←␈↓If␈α⊗we␈α⊗look␈α∃closely␈α⊗at␈α⊗the␈α∃storage␈α⊗requirements␈α⊗for␈α⊗<application>'s␈α∃and
␈↓ α←␈↓<arg-list>'s, we see that there are differences.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αrepresentation␈αof␈αan␈α<application>␈αhas␈α␈↓¬fixed␈↓␈αstorage␈αrequirements;
␈↓ α←␈↓it␈α∞demands␈α∂space␈α∞for␈α∂two␈α∞components:␈α∞a␈α∂<function-part>␈α∞component,␈α∂and␈α∞a
␈↓ α←␈↓<arg-list>␈α↔component.␈α↔We␈α⊗have␈α↔seen␈α↔such␈α⊗storage␈α↔structures␈α↔before␈α⊗in
␈↓ α←␈↓Section 7.6;␈α∞they␈α∞are␈α∞called␈α∞record␈α
structures.␈α∞ The␈α∞name␈α∞components␈α∞of␈α
the
␈↓ α←␈↓record␈α⊗structure␈α⊗can␈α↔be␈α⊗␈↓αfun␈↓␈α⊗and␈α⊗␈↓αargs␈↓,␈α↔and␈α⊗the␈α⊗selector␈α↔functions␈α⊗are
␈↓ α←␈↓implemented␈αby␈αmatching␈αthe␈αname␈α
components.␈α Note␈αthat␈αthe␈αuse␈αof␈α
record
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 1␈↓Compare the following discussion with Section 7.6.
␈↓ α←␈↓␈↓408 Implications of LISP␈↓
88.␈↓
␈↓"β␈↓ α←␈↓structure␈α∞is␈α∞a␈α
bit␈α∞freer␈α∞than␈α
LISP's␈α∞list␈α∞representation;␈α
we␈α∞need␈α∞not␈α∞make␈α
a
␈↓ α←␈↓commitment to the position of the ␈↓αfun␈↓ component in the representation.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∂requirements␈α⊂for␈α∂<arg-list>␈α∂are␈α⊂different.␈α∂An␈α⊂arbitrary␈α∂<arg-list>
␈↓ α←␈↓has␈α∃a␈α∃variable␈α∃number␈α∃of␈α∃components.␈α∃Each␈α∃component␈α∃has␈α∃the␈α∃same
␈↓ α←␈↓characteristic:␈α⊃it's␈α⊃an␈α⊃<arg>.␈α⊃We␈α⊃can␈α⊃represent␈α⊃a␈α⊃homogeneous␈α∩object␈α⊃like
␈↓ α←␈↓<arg-list>␈α∂as␈α∂a␈α∂sequence␈α∂whose␈α∂length␈α∂is␈α∂fixed.␈α∂A␈α∂natural␈α∂storage␈α⊂class␈α∂for
␈↓ α←␈↓such␈αsequences␈αis␈αa␈αlinear␈αarray,␈αeach␈αcomponent␈αof␈αwhich␈αis␈αan␈αitem␈α
of␈αthe
␈↓ α←␈↓sequence.␈α⊂ Information␈α⊂about␈α⊂the␈α⊂length␈α⊂of␈α⊂the␈α⊂sequence,␈α⊂and␈α⊂the␈α⊂class␈α⊂to
␈↓ α←␈↓which␈αelements␈αof␈αthe␈αsequence␈αbelong,␈αcan␈αbe␈αstored␈αin␈αthe␈α"dope vector"␈αof
␈↓ α←␈↓the representation.
␈↓"β␈↓ α←␈↓␈↓ β'What␈α↔we␈α_are␈α↔developing␈α_is␈α↔a␈α_description␈α↔of␈α_a␈α↔class␈α_of␈α↔storage
␈↓ α←␈↓representations␈α⊂for␈α⊂language␈α⊂constructs.␈α⊂This␈α⊂class␈α⊂of␈α⊂structures␈α⊂covers␈α⊂the
␈↓ α←␈↓space␈α∞which␈α∞LISP␈α∞covers,␈α∂but␈α∞partitions␈α∞it␈α∞differently.␈α∞More␈α∂information␈α∞is
␈↓ α←␈↓stored␈α⊃explicitly,␈α⊃and␈α⊃the␈α⊂representations␈α⊃are␈α⊃more␈α⊃discriminating␈α⊃in␈α⊂their
␈↓ α←␈↓storage␈α∞requirements.␈α∞Assuming␈α∞that␈α∞the␈α∞resulting␈α∞structures␈α∞are␈α∂made␈α∞data
␈↓ α←␈↓structures␈αof␈αthe␈αlanguage,␈αwe␈αcan␈αthen␈αwrite␈αa␈αLISP-like␈α␈↓αeval␈↓␈αwhich␈αruns␈αon
␈↓ α←␈↓these data structures.
␈↓"β␈↓ α←␈↓␈↓ β'Our␈αrefinement␈αis␈αnot␈αwithout␈αpenalty.␈αWe␈αare␈αin␈αfact␈αimposing␈αa␈αtype
␈↓ α←␈↓structure␈α⊂on␈α⊂the␈α⊂language.␈α⊂We␈α⊂know␈α⊂that␈α⊂such␈α⊂restrictions␈α⊂are␈α⊃not␈α⊂always
␈↓ α←␈↓desireable.␈αThe␈αtype␈αstructure␈α
becomes␈αmore␈αapparent␈αwhen␈αwe␈α
consider␈αthe
␈↓ α←␈↓remaining syntax equation:
␈↓"∀␈↓ α←␈↓␈↓ ∧⊂<form> ::= <constant> | <application> | <variable>
␈↓"∀␈↓ α←␈↓Consistent␈αwith␈α
our␈αtreatment␈α
of␈αrecord␈αstructures␈α
and␈αsequences,␈α
we␈αshould
␈↓ α←␈↓develop␈α
some␈α
representation␈α
for␈α∞<form>.␈α
In␈α
LISP,␈α
no␈α
storage␈α∞was␈α
allocated
␈↓ α←␈↓for␈α
the␈αrepresentation␈α
of␈αsuch␈α
alternative␈α
BNF␈αequations;␈α
the␈αrecognition␈α
was
␈↓ α←␈↓done by recognizers embedded in a conditional expression:
␈↓"∀␈↓ α←␈↓α␈↓ ∧C[is-const[x] → ...
␈↓"β␈↓ α←␈↓α␈↓ ∧C is-app[x] → ...
␈↓"β␈↓ α←␈↓α␈↓ ∧C is-var[x] →
␈↓"β␈↓ α←␈↓α␈↓ ∧C ␈↓
t␈↓ → ... what to do if ␈↓αx␈↓ is ␈↓¬not␈↓ a form ...
␈↓"∀␈↓ α←␈↓␈↓ β'In␈α_EL1,␈α_every␈α_data␈α_item␈α_has␈α_an␈α_associated␈α_type.␈α_Since␈α_we␈α↔are
␈↓ α←␈↓representing␈α∃language␈α⊗constructs␈α∃as␈α⊗data␈α∃structures␈α⊗they␈α∃will␈α⊗also␈α∃have
␈↓ α←␈↓associated␈α
types.␈α
To␈α
determine␈α
if␈α
something␈α
is␈α
an␈α
<application>␈α
requires␈α
only
␈↓ α←␈↓that␈αwe␈αexamine␈αthe␈αassociated␈αtype.␈α The␈αquestion␈αthen␈αarises:␈αwhat␈αkind␈αof
␈↓ α←␈↓object␈α∞is␈α∂to␈α∞be␈α∂associated␈α∞with␈α∂an␈α∞object␈α∞like␈α∂<form>?␈α∞At␈α∂any␈α∞one␈α∂time␈α∞an
␈↓ α←␈↓object␈αof␈αtype␈α<form>␈αis␈αone␈αof␈αthe␈αthree␈αalternatives.␈αThe␈αEL1␈αsolution␈αis␈αto
␈↓ α←␈↓assign␈α
a␈α
pointer-like␈α
data␈α
object␈α∞as␈α
the␈α
representation␈α
of␈α
such␈α∞objects.␈α
The
␈↓ α←␈↓type of the pointer is constrained to point at one of the alternatives.
␈↓"β␈↓ α←␈↓␈↓ β'Let's␈αcompare␈αLISP:␈αGiven␈αan␈αapplication␈α␈↓αf[x;A;1]␈↓␈αwe␈αrepresent␈αit␈α
as␈αa
␈↓ α←␈↓constant ␈↓α(F X (QUOTE A) 1)␈↓. That is:
␈↓"∀␈↓ α←␈↓␈↓ ∧W␈↓λr␈↓βLISP␈↓∞(␈↓αf[x;A;1]␈↓∞)␈↓ = ␈↓α(F X (QUOTE A) 1)␈↓
␈↓"∀␈↓ α←␈↓We␈αwish␈αto␈αrepresent␈αthis␈αapplication␈αas␈αa␈αconstant␈αin␈αEL1␈αas␈αwell.␈αWe␈αneed
␈↓ α←␈↓␈↓8.␈↓ λεImplications of LISP 409␈↓
␈↓"β␈↓ α←␈↓some␈α⊗notation␈α⊗for␈α⊗record␈α⊗structures␈α⊗and␈α⊗sequence␈α⊗constants.␈α⊗ A␈α⊗record
␈↓ α←␈↓constant␈α
of␈αtype␈α
␈↓αt␈↓␈α
will␈αbe␈α
represented␈αas␈α
<␈↓βt␈↓ c␈↓β1␈↓; ... ;c␈↓βn␈↓>.␈↓π 2␈↓␈α
A␈αsequence␈α
of␈α
type␈α␈↓αt␈↓
␈↓ α←␈↓will be represented as (␈↓βt␈↓s␈↓β1␈↓; ... ;s␈↓βn␈↓).
␈↓"∀␈↓ α←␈↓Then ␈↓λr␈↓βEL1␈↓∞(␈↓αf[x;A;1]␈↓∞)␈↓ = <␈↓βapp␈↓ ¬k␈↓λr␈↓βEL1␈↓∞(␈↓αf␈↓∞)␈↓;
␈↓"β␈↓ α←␈↓␈↓ ¬k(␈↓βarg-list␈↓ εW␈↓λr␈↓βEL1␈↓∞(␈↓αx␈↓∞)␈↓;
␈↓"β␈↓ α←␈↓␈↓ ¬k␈↓ εW␈↓λr␈↓βEL1␈↓∞(␈↓αA␈↓∞)␈↓;
␈↓"β␈↓ α←␈↓␈↓ ¬k␈↓ εW␈↓λr␈↓βEL1␈↓∞(␈↓α1␈↓∞)␈↓ ) >
␈↓"∀␈↓ α←␈↓We␈α
have␈α
suppressed␈α
much␈α
of␈α
the␈α
detail␈α
because␈α
each␈α
of␈α
the␈α
components␈α
of
␈↓ α←␈↓the representation must also have type information.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
structured␈α
items␈α
in␈α
LISP␈αare␈α
built␈α
from␈α
dotted␈α
pairs;␈αthe␈α
structured
␈↓ α←␈↓items␈αin␈α
EL1␈αare␈αbuilt␈α
from␈αsequences␈α(or␈α
lists),␈αfrom␈αrecords␈α
(or␈αstructures),
␈↓ α←␈↓and␈α∞from␈α∞pointers␈α∂(or␈α∞references).␈α∞The␈α∞difference␈α∂is␈α∞that␈α∞the␈α∞EL1␈α∂user␈α∞has
␈↓ α←␈↓more␈α∀choice␈α∪over␈α∀the␈α∪underlying␈α∀representation.␈α∪This␈α∀can␈α∪lead␈α∀to␈α∪more
␈↓ α←␈↓efficient␈α∂utilization␈α∂of␈α∂storage␈α∞and␈α∂perhaps␈α∂more␈α∂efficient␈α∂programs.␈α∞ Since
␈↓ α←␈↓the␈αevaluator␈αis␈αjust␈αan␈αEL1␈αprogram,␈αthese␈αconsiderations␈αcarry␈αover␈αto␈αthe
␈↓ α←␈↓evaluation␈α⊂process.␈α⊂ EL1␈α⊃allows␈α⊂us␈α⊂to␈α⊂␈↓¬represent␈↓␈α⊃the␈α⊂data␈α⊂structures␈α⊃of␈α⊂the
␈↓ α←␈↓evaluation␈α
process␈α
in␈α
terms␈α
closer␈α
to␈α
actual␈α
implementation.␈α
Both␈α
LISP␈α
and
␈↓ α←␈↓EL1␈α⊗allow␈α⊗us␈α⊗to␈α⊗␈↓¬express␈↓␈α⊗realistic␈α⊗implementations,␈α⊗however␈α⊗LISP␈α∃may
␈↓ α←␈↓ultimately represent its structures as dotted pairs.
␈↓"β␈↓ α←␈↓␈↓ β'This␈α⊂realism␈α⊂of␈α⊂representation␈α⊂carries␈α⊂over␈α⊂to␈α⊂the␈α⊂evaluation␈α⊂process
␈↓ α←␈↓which␈α⊃runs␈α∩on␈α⊃the␈α∩representation.␈α⊃The␈α⊃language␈α∩is␈α⊃capable␈α∩of␈α⊃accurately
␈↓ α←␈↓representing␈α∨more␈α of␈α∨the␈α∨techniques␈α which␈α∨occur␈α∨in␈α a␈α∨language
␈↓ α←␈↓implementation.␈α
The␈α
language␈α
supplies␈α
storage␈α
management␈αprimitives␈α
which
␈↓ α←␈↓allow␈α∂the␈α⊂creation␈α∂of␈α∂stack-like␈α⊂objects␈α∂as␈α∂well␈α⊂as␈α∂the␈α∂heap-stored␈α⊂items␈α∂of
␈↓ α←␈↓LISP.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α⊗language␈α⊗offers␈α⊗the␈α↔user␈α⊗the␈α⊗ability␈α⊗to␈α⊗␈↓¬define␈↓␈α↔abstract␈α⊗data
␈↓ α←␈↓structures␈α
in␈α
a␈α∞manner␈α
similar␈α
to␈α
that␈α∞we␈α
have␈α
been␈α∞advocating␈α
informally.
␈↓ α←␈↓Given␈α∪the␈α∪finer␈α∪partition␈α∪of␈α∪storage␈α∪structures,␈α∪the␈α∪user␈α∪can␈α∀map␈α∪those
␈↓ α←␈↓structures␈α↔onto␈α_more␈α↔frugal␈α↔representations␈α_than␈α↔LISP,␈α↔and␈α_since␈α↔the
␈↓ α←␈↓type-checking␈α∞is␈α∂built␈α∞into␈α∂the␈α∞language,␈α∞the␈α∂language␈α∞processors␈α∂can␈α∞check
␈↓ α←␈↓the␈α∩consistency␈α⊃of␈α∩the␈α∩parameter␈α⊃passing.␈α∩ Relating␈α⊃the␈α∩abstract␈α∩with␈α⊃the
␈↓ α←␈↓representation␈αrequires␈αsome␈αcare.␈αSupplying␈αa␈αcomfortable␈αinterface␈αbetween
␈↓ α←␈↓these␈α∀two␈α∀domains␈α∀is␈α∀a␈α∀non-trivial␈α∀problem.␈α∀EL1␈α∀supplies␈α∀"lifting"␈α∀and
␈↓ α←␈↓"lowering"␈α∂mechanisms␈α∂to␈α⊂aid␈α∂in␈α∂this␈α∂problem;␈α⊂the␈α∂result␈α∂is␈α⊂not␈α∂completely
␈↓ α←␈↓satisfactory.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α⊃have␈α⊃frequently␈α⊃seen␈α⊃how␈α∩easy␈α⊃it␈α⊃has␈α⊃been␈α⊃to␈α⊃extend␈α∩LISP␈α⊃by
␈↓ α←␈↓modifying␈α≥␈↓αeval␈↓.␈α≥ This␈α≥is␈α≥particularly␈α≥easy␈α≥because␈α≥we␈α≥are␈α≥making
␈↓ α←␈↓modifications␈α⊂at␈α⊂the␈α⊂level␈α⊃of␈α⊂data-structure␈α⊂representation␈α⊂of␈α⊃programs.␈α⊂In
␈↓ α←␈↓EL1␈α∞we␈α
wish␈α∞to␈α
make␈α∞the␈α
extensions␈α∞at␈α
the␈α∞level␈α
of␈α∞concrete␈α∞syntax,␈α
rather
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 2␈↓We␈α∂have␈α∂suppressed␈α∂the␈α∂explicit␈α∂naming␈α∂of␈α∂each␈α∂component␈α∂of␈α∞the
␈↓ α←␈↓record.␈α∀We␈α∪assume␈α∀that␈α∪a␈α∀"template"␈α∀of␈α∪each␈α∀type␈α∪␈↓αt␈↓␈α∀is␈α∀available.␈α∪That
␈↓ α←␈↓template can be consulted to determine which component is referenced.
␈↓ α←␈↓␈↓410 Implications of LISP␈↓
88.␈↓
␈↓"β␈↓ α←␈↓than␈α⊃abstract␈α⊂syntax␈α⊃as␈α⊃LISP␈α⊂does.␈↓π 3␈↓␈α⊃EL1␈α⊂can␈α⊃do␈α⊃this␈α⊂by␈α⊃using␈α⊃a␈α⊂parser
␈↓ α←␈↓which␈α∞is␈α∞table-driven,␈α∞and␈α∞operates␈α∞on␈α∞a␈α∞modifiable␈α∞set␈α∞of␈α∂productions.␈α∞To
␈↓ α←␈↓introduce␈α
a␈α
new␈α
construct␈α
into␈α
the␈α
language␈α
we␈α
need␈α
to␈α
supply␈α∞the␈α
concrete
␈↓ α←␈↓syntax␈α
for␈αthe␈α
item,␈αits␈α
abstract␈αsystax,␈α
a␈αmapping␈α
from␈αconcrete␈α
to␈αabstract,
␈↓ α←␈↓and␈α∞its␈α∞pragmatics␈α∞expressed␈α∞as␈α∞additions␈α∞to␈α∞the␈α∞evaluator.␈α∞ However␈α∞there
␈↓ α←␈↓may␈α∂be␈α⊂wider␈α∂implications␈α⊂in␈α∂the␈α⊂language␈α∂and␈α⊂more␈α∂general␈α⊂features␈α∂are
␈↓ α←␈↓required.␈↓π 4␈↓ The field of language design is still quite young and tentative.
␈↓"β␈↓ α←␈↓␈↓ β'Though␈αLISP␈α
is␈αa␈αfull␈α
twenty␈αyears␈αold,␈α
it␈αis␈αstill␈α
a␈αfertile␈αresearch␈α
area.
␈↓ α←␈↓Projects␈α
are␈α
extending␈α
LISP␈α
in␈α
several␈α
directions.␈α
[Car 76]␈α∞investigates␈α
the
␈↓ α←␈↓possibilities␈α⊗of␈α↔adding␈α⊗a␈α↔type-structure␈α⊗to␈α↔LISP,␈α⊗giving␈α↔a␈α⊗strong-typed
␈↓ α←␈↓programming␈α∀language␈α∀and␈α∀a␈α∀precise␈α∀formalism␈α∀in␈α∀which␈α∀properties␈α∀of
␈↓ α←␈↓Typed␈αLISP␈αprograms␈αcan␈αbe␈αdiscussed.␈α This␈αproject␈αsupplies␈αan␈αAlgol-like
␈↓ α←␈↓user language as well as develops interesting theoretical results.
␈↓"β␈↓ α←␈↓␈↓ β'Several␈αpeople␈αhave␈αsupplied␈αparsers␈αwhich␈αgive␈αthe␈αuser␈αan␈αAlgol-like
␈↓ α←␈↓syntax␈α∪([Mli 73],␈α∪[Pra 76]).␈α∪Extensions␈α∀to␈α∪the␈α∪ideas␈α∪of␈α∀SDIO␈α∪Section 9.4
␈↓ α←␈↓are being pursued.
␈↓"β␈↓ α←␈↓␈↓ β'Programming␈αlanguage␈αsemantics␈αis␈α
being␈αcoupled␈αwith␈αthe␈α
realities␈αof
␈↓ α←␈↓programming␈α⊃language␈α⊃design␈α⊂in␈α⊃a␈α⊃successor␈α⊂of␈α⊃[LCF 72].␈α⊃ This␈α⊃is␈α⊂being
␈↓ α←␈↓built on the LISP experience.
␈↓"β␈↓ α←␈↓␈↓ β'LISP␈α
is␈α
the␈α∞direct␈α
source␈α
of␈α
inspiration␈α∞for␈α
two␈α
current␈α∞MIT␈α
projects.
␈↓ α←␈↓For␈αseveral␈α
years␈αC. Hewitt␈αhas␈α
been␈αdeveloping␈α
an␈αinteresting␈αphilosophy␈α
of
␈↓ α←␈↓computation.␈α≥Building␈α≥on␈α≥his␈α≥experience␈α≥with␈α≥LISP,␈α≥he␈α≤developed
␈↓ α←␈↓PLANNER␈α[Hew 72],␈α
and␈αmore␈αrecently␈α
refined␈αthse␈α
ideas␈αin␈αa␈α
methodology
␈↓ α←␈↓called␈α
Actors␈α
([Hew 74],␈α[Hew 76]).␈α
One␈α
of␈αthe␈α
aims␈α
of␈α
these␈αinvestigations
␈↓ α←␈↓is␈αto␈α
develop␈αa␈αself-sufficient␈α
description␈αof␈αmodern␈α
computation␈αmuch␈αin␈α
the
␈↓ α←␈↓way␈α∪that␈α∩the␈α∪␈↓λλ␈↓-calculus␈α∪gave␈α∩a␈α∪foundation␈α∪to␈α∩the␈α∪notion␈α∪of␈α∩computable
␈↓ α←␈↓function.␈α∂The␈α∂goal␈α∂therefore␈α∂is␈α∂much␈α∂higher␈α∂than␈α∂to␈α∂develop␈α⊂just␈α∂another
␈↓ α←␈↓programming␈α_language.␈α→Along␈α_the␈α_way,␈α→however,␈α_these␈α→projects␈α_have
␈↓ α←␈↓developed␈α↔some␈α↔of␈α↔the␈α↔most␈α↔notable␈α↔ideas␈α↔of␈α↔advanced␈α↔programming
␈↓ α←␈↓languages.␈α↔ From␈α_the␈α↔PLANNER␈α_experience,␈α↔partially␈α_implemented␈α↔in
␈↓ α←␈↓[Mic 71],␈α⊃evolved␈α⊃the␈α⊃ideas␈α⊃of␈α⊃pattern-directed␈α⊃invocation;␈α⊃see␈α⊃Section 2.5.
␈↓ α←␈↓This␈αgave␈αPLANNER␈αa␈αnew␈αway␈αto␈αcall␈αprocedures,␈αand␈αits␈αimplementation
␈↓ α←␈↓in␈αMicro-PLANNER␈αgave␈αresearchers␈αnew␈αpower␈αof␈αexpression␈αmuch␈αin␈αthe
␈↓ α←␈↓way␈αthat␈α
LISP␈αimproved␈αover␈α
machine␈αlanguage␈α
several␈αyears␈αbefore.␈α
These
␈↓ α←␈↓investigations␈α∞generated␈α∞much␈α∞controversy␈α∞and␈α∞stimulated␈α∞more␈α∂research␈α∞in
␈↓ α←␈↓language␈α∞design␈α
for␈α∞artificial␈α
intelligence;␈α∞see␈α
[Bob 74],␈α∞[Con 73],␈α
[QA4 72].
␈↓ α←␈↓The␈α
lessons␈α
of␈α
PLANNER␈αled␈α
to␈α
Actors␈α
and␈α
a␈αstudy␈α
of␈α
the␈α
control␈αaspects␈α
of
␈↓ α←␈↓programming␈α∂languages.␈α∞From␈α∂these␈α∂investigations␈α∞Hewitt␈α∂has␈α∂developed␈α∞a
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 3␈↓To␈αprogram␈α
in␈αthe␈α
abstract␈αsyntax␈α
would␈αbe␈α
possible,␈αbut␈α
messy.␈αWe
␈↓ α←␈↓would␈α⊃be␈α⊃writing␈α⊃constants␈α∩consisting␈α⊃of␈α⊃pointers,␈α⊃records,␈α∩and␈α⊃sequences,
␈↓ α←␈↓rather than the list notation of LISP.
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 4␈↓For␈α
example,␈α
if␈α
new␈α
control␈α
structures␈α
are␈α
desired,␈α
major␈α∞revision␈α
of
␈↓ α←␈↓the inner structure of the language may be necessary ([Pre 76b]).
␈↓ α←␈↓␈↓8.␈↓ λεImplications of LISP 411␈↓
␈↓"β␈↓ α←␈↓model␈α⊃which␈α⊃looks␈α∩on␈α⊃control␈α⊃as␈α⊃an␈α∩activity␈α⊃of␈α⊃passing␈α∩message␈α⊃between
␈↓ α←␈↓program␈αmodules.␈α Recently,␈αHewitt's␈α
efforts␈αhave␈αagain␈αstimulated␈α
others␈αto
␈↓ α←␈↓examine programming languages more closely.
␈↓"β␈↓ α←␈↓␈↓ β'G.␈α⊂Sussman␈α⊂and␈α⊂G.␈α⊃Steele␈α⊂have␈α⊂been␈α⊂developing␈α⊂a␈α⊃language␈α⊂named
␈↓ α←␈↓SCHEME␈α⊗([Sus 75],␈α↔[Ste 76b],␈α⊗and␈α↔[Ste 76c])␈α⊗which␈α↔was␈α⊗begun␈α↔as␈α⊗an
␈↓ α←␈↓experiment␈α
to␈α
understand␈α
and␈α
implement␈α
several␈α
of␈α
the␈α
ideas␈α
of␈α
Hewitt.␈α
The
␈↓ α←␈↓result␈α∂is␈α⊂a␈α∂dialect␈α∂of␈α⊂LISP␈α∂which␈α∂uses␈α⊂static␈α∂binding␈α∂rather␈α⊂than␈α∂dynamic
␈↓ α←␈↓binding.␈α∞The␈α
interpreter␈α∞is␈α∞built␈α
in␈α∞the␈α
spirit␈α∞of␈α∞[Con 73]␈α
and␈α∞is␈α∞similar␈α
to
␈↓ α←␈↓the␈α∞evaluator␈α∂of␈α∞Section 4.8.␈α∞ The␈α∂result␈α∞is␈α∞an␈α∂interpreter␈α∞which␈α∂makes␈α∞the
␈↓ α←␈↓control␈αaspects␈αof␈αthe␈αlanguage␈αmuch␈αmore␈αexplicit.␈α Using␈αSCHEME,␈αSteele
␈↓ α←␈↓and␈α∞Sussman␈α∞have␈α∂been␈α∞able␈α∞to␈α∞illuminate␈α∂many␈α∞of␈α∞the␈α∞control␈α∂and␈α∞access
␈↓ α←␈↓problems of programming languages.
␈↓"β␈↓ α←␈↓␈↓ β'In␈α≤these␈α≤two␈α≤projects␈α≤we␈α≤see␈α≤two␈α≤views␈α≤of␈α≥computation:␈α≤the
␈↓ α←␈↓philosophical␈α⊃and␈α⊂the␈α⊃tool␈α⊃builder.␈α⊂Both␈α⊃are␈α⊂important;␈α⊃together␈α⊃they␈α⊂are
␈↓ α←␈↓developing an impressive array of knowledge.
␈↓"β␈↓ α←␈↓␈↓ β'Finally,␈α∪LISP␈α∪is␈α∪being␈α∩used␈α∪as␈α∪an␈α∪effective␈α∩tool␈α∪for␈α∪the␈α∪design␈α∩of
␈↓ α←␈↓interactive␈α'programming␈α'systems.␈α'The␈α'successful␈α'development␈α&of
␈↓ α←␈↓programming␈α_systems␈α_which␈α_integrate␈α↔all␈α_phases␈α_of␈α_program␈α↔creation,
␈↓ α←␈↓debugging and optimization, will be based on LISP user's experience.
␈↓"β␈↓ α←␈↓␈↓ β'Many␈α∂people␈α∂find␈α∂it␈α∂curious␈α∂that␈α∂LISP␈α∂has␈α∂survived␈α∂so␈α∂long␈α⊂and␈α∂so
␈↓ α←␈↓well.␈α
It␈α
is␈α
not␈α
supported␈α
by␈α
any␈α
organization␈α
or␈α
computer␈α
manufacturer␈αyet␈α
it
␈↓ α←␈↓flourishes␈α∂and␈α∂continues␈α∂to␈α∂attract␈α∂many␈α∂of␈α∂the␈α∂most␈α∂exceptional␈α∂computer
␈↓ α←␈↓science␈αtalents.␈α LISP␈αdoes␈αa␈αlot␈αof␈αthings␈αwell.␈α As␈αa␈αprogramming␈αlanguage,
␈↓ α←␈↓it␈α∃is␈α∃an␈α∃exceptional␈α∃tool␈α∃for␈α∃developing␈α∃sophisticated␈α∃applications.␈α∀The
␈↓ α←␈↓artificial␈αintelligence␈αcommunity␈αhas␈αalways␈αbeen␈αone␈αof␈αthe␈αmost␈αdemanding
␈↓ α←␈↓and␈α∂creative␈α⊂builders␈α∂of␈α∂programming␈α⊂tools.␈α∂ LISP's␈α∂treatment␈α⊂of␈α∂program
␈↓ α←␈↓and data supports this kind of behavior.
␈↓"β␈↓ α←␈↓␈↓ β'Until␈αwe␈α
can␈αdevelop␈α
a␈αtool␈αwhich␈α
handles␈αany␈α
of␈αthese␈α
areas␈αas␈αwell␈α
as
␈↓ α←␈↓LISP, LISP will survive.
␈↓ α←␈↓␈↓412 Projects␈↓
89.␈↓
␈↓"β␈↓ α←␈↓␈↓ ␈↓↓CHAPTER 9␈↓
␈↓"β␈↓ α←␈↓␈↓ Projects␈↓
␈↓"β␈↓ α←␈↓This␈αchapter␈αconsists␈α
of␈αa␈αset␈α
of␈αnon-trivial␈αprojects␈α
which␈αeither␈αapply␈α
LISP
␈↓ α←␈↓or extend LISP by adding new language features.
␈↓"β␈↓ α←␈↓␈↓ ¬C␈↓↓9.1 Extensions to ␈↓αeval␈↓↓␈↓α
␈↓"β␈↓ α←␈↓␈↓ β'This␈αfirst␈αproject␈αwas␈αderived␈αfrom␈αthe␈αsyntax␈αof␈αMUDDLE␈α[Mud 75],
␈↓ α←␈↓CONNIVER [Con 73], and MICRO-PLANNER [Mic 71].
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∞have␈α
seen␈α∞that␈α
LISP␈α∞calling␈α
sequences␈α∞are␈α
of␈α∞two␈α∞varieties:␈α
either
␈↓ α←␈↓evaluate␈α∀␈↓¬all␈↓␈α∀of␈α∀the␈α∀arguments;␈α∪or␈α∀evaluate␈α∀␈↓¬none␈↓␈α∀of␈α∀the␈α∀arguments.␈α∪ To
␈↓ α←␈↓generalize␈αthis␈αregime␈αwe␈αmight␈αallow␈αthe␈αevaluation␈αof␈αsome␈αselection␈αof␈αthe
␈↓ α←␈↓actual␈α
parameters;␈α
the␈α
formal␈α
parameter␈α
list␈α
could␈α
specify␈α∞which␈α
parameters
␈↓ α←␈↓are␈α⊃to␈α⊃be␈α⊃evaluated.␈α⊃ We␈α⊃have␈α⊃also␈α⊃specified␈α⊃that␈α⊃the␈α⊃number␈α⊃of␈α⊃formal
␈↓ α←␈↓parameters␈α∀must␈α∀agree␈α∀with␈α∀the␈α∪number␈α∀of␈α∀actual␈α∀parameters;␈α∀yet␈α∀it␈α∪is
␈↓ α←␈↓sometimes␈α useful␈α in␈α practice␈α to␈α allow␈α such␈α a␈α!discrepancy.␈α Some
␈↓ α←␈↓implementations␈α∞allow␈α∂a␈α∞mis-match,␈α∂supplying␈α∞default␈α∂values␈α∞when␈α∂too␈α∞few
␈↓ α←␈↓are␈αgiven,␈αand␈αdiscarding␈αthe␈αexcess␈αactual␈αparameters␈αafter␈αtheir␈αevaluation.
␈↓ α←␈↓We␈αmight␈αpartition␈αthe␈αformal␈αparameters␈αinto␈αrequired␈αparameters,␈αoptional
␈↓ α←␈↓parameters,␈αand␈αan␈αexcess␈αcollector␈αto␈αhandle␈αany␈αactual␈αparameters␈αleft␈α
over.
␈↓ α←␈↓Required␈α⊂parameters␈α⊂␈↓¬must␈↓␈α⊂have␈α⊂corresponding␈α⊂actual␈α⊃parameters;␈α⊂optional
␈↓ α←␈↓actual␈α
parameters␈α∞are␈α
used␈α
if␈α∞present,␈α
otherwise␈α
default␈α∞values␈α
are␈α∞used.␈α
If
␈↓ α←␈↓there␈α
are␈αmore␈α
actual␈α
parameters␈αthan␈α
the␈α
formals␈αencompassed␈α
by␈α
the␈αfirst
␈↓ α←␈↓two classes, then they are associated with the excess collector.
␈↓ α←␈↓␈↓9.1␈↓ λ→Extensions to ␈↓αeval␈↓ 413␈↓α
␈↓"β␈↓ α←␈↓␈↓ β'To be more precise consider the following possible BNF equations:
␈↓"∀␈↓ α←␈↓<varlist>␈↓ ∧π::=[<required> <optional> <excess>]
␈↓" ␈↓ α←␈↓<required>␈↓ ∧π::= <par>; ...;<par> | ␈↓λε␈↓ ␈↓π 1␈↓
␈↓" ␈↓ α←␈↓<optional>␈↓ ∧π::= "optional" <opn>; ...; <opn> | ␈↓λε␈↓
␈↓" ␈↓ α←␈↓<excess>␈↓ ∧π::= "excess" <par> | ␈↓λε␈↓
␈↓" ␈↓ α←␈↓<par>␈↓ ∧π::= <variable> | ␈↓λ`␈↓<variable>
␈↓" ␈↓ α←␈↓<opn>␈↓ ∧π::= <par> | <par> ← <form>
␈↓"→␈↓ α←␈↓␈↓↓1.␈↓ The␈α∞formal␈α∂parameters␈α∞are␈α∞bound␈α∂to␈α∞the␈α∞actual␈α∂parameters␈α∞from␈α∂left␈α∞to
␈↓ α←␈↓␈↓ β∂right as usual.
␈↓" ␈↓ α←␈↓␈↓↓2.␈↓ There␈α
must␈α∞be␈α
an␈α∞actual␈α
parameter␈α∞for␈α
each␈α∞required␈α
parameter,␈α∞and␈α
if
␈↓ α←␈↓␈↓ β∂there␈αis␈αno␈αexcess␈αcollector␈αthere␈αmay␈αnot␈αbe␈αmore␈αactual␈αparameters␈αthan
␈↓ α←␈↓␈↓ β∂formals. (There may be fewer if we have optionals.)
␈↓" ␈↓ α←␈↓␈↓↓3.␈↓ If␈α⊃a␈α⊃<variable>␈α⊃in␈α⊃a␈α⊃formal␈α⊃parameter␈α⊃is␈α⊃preceded␈α⊃by␈α⊃a␈α⊃"␈↓λ`␈↓",␈α∩then␈α⊃the
␈↓ α←␈↓␈↓ β∂corresponding␈α
actual␈α
parameter␈α
is␈α
␈↓¬not␈↓␈α
evaluated.␈α
This␈α
is␈α
implements␈α
the
␈↓ α←␈↓␈↓ β∂␈↓αquote␈↓-ing ␈↓αread␈↓ macro.
␈↓" ␈↓ α←␈↓␈↓↓4.␈↓ If␈αwe␈αexhaust␈αthe␈αactual␈αparameters␈αwhile␈αbinding␈αthe␈αoptionals,␈αwe␈αlook
␈↓ α←␈↓␈↓ β∂at␈α⊂the␈α⊂remaining␈α⊂formal␈α⊂optionals.␈α⊃ If␈α⊂a␈α⊂formal␈α⊂parameter␈α⊂is␈α⊃simply␈α⊂a
␈↓ α←␈↓␈↓ β∂<par>␈αthen␈αwe␈αbind␈αit␈αto␈α␈↓α( )␈↓;␈αif␈αa␈αformal␈αis␈α␈↓λ`␈↓<variable> ← <form>␈αthen␈αwe
␈↓ α←␈↓␈↓ β∂bind the <variable> to the <form>.
␈↓"β␈↓ α←␈↓␈↓ β∂If␈αthe␈αformal␈αis␈α<variable> ← <form>,␈αwe␈αbind␈α<variable>␈αto␈αthe␈αvalue␈αof
␈↓ α←␈↓␈↓ β∂<form>,␈α_where␈α↔the␈α_evaluation␈α↔is␈α_to␈α↔take␈α_place␈α↔␈↓¬after␈↓␈α_the␈α↔required
␈↓ α←␈↓␈↓ β∂parameters have been bound.
␈↓" ␈↓ α←␈↓␈↓↓5.␈↓ Finally,␈α⊂the␈α⊂excess␈α⊂collector␈α⊂is␈α⊂bound␈α⊂to␈α⊂a␈α⊂list␈α⊂of␈α⊂any␈α⊂remaining␈α⊂actual
␈↓ α←␈↓␈↓ β∂parameters:␈α⊂if␈α∂<par>␈α⊂is␈α∂<variable>␈α⊂then␈α∂using␈α⊂the␈α⊂calling␈α∂environment,
␈↓ α←␈↓␈↓ β∂form␈α∃a␈α∀list␈α∃of␈α∀the␈α∃values␈α∃of␈α∀the␈α∃remaining␈α∀arguments;␈α∃if␈α∃<par>␈α∀is
␈↓ α←␈↓␈↓ β∂␈↓λ`␈↓<variable>,␈αbind␈α<variable>␈αto␈αthe␈αactual␈αlist.␈αIf␈αthere␈αis␈αno␈αexcess,␈αbind
␈↓ α←␈↓␈↓ β∂to ␈↓αNIL␈↓.
␈↓"∀␈↓ α←␈↓␈↓ β'We␈α↔will␈α⊗also␈α↔extend␈α⊗␈↓αprog␈↓-variables␈α↔slightly,␈α⊗allowing␈α↔them␈α↔to␈α⊗be
␈↓ α←␈↓initialized␈α
explicitly.␈α
If␈αa␈α
␈↓αprog␈↓-variable␈α
is␈αatomic,␈α
intialize␈α
it␈αto␈α
␈↓α( )␈↓,␈α
as␈αusual.␈α
If
␈↓ α←␈↓it␈α∞is␈α∞of␈α∂the␈α∞form␈α∞<variable> ← <form>␈α∂then␈α∞initialize␈α∞it␈α∂to␈α∞the␈α∞value␈α∂of␈α∞the
␈↓ α←␈↓<form>.
␈↓"β␈↓ α←␈↓␈↓ β'Here are some examples:
␈↓"∞␈↓ α←␈↓1. In the initialization of ␈↓αlength␈↓ on page 186, we could write:
␈↓"∞␈↓ α←␈↓␈↓ ¬D␈↓α... prog[[l ← x; c ← 0] ...␈↓
␈↓"∀␈↓ α←␈↓2. ␈↓αlist␈↓ could now be defined as: ␈↓αλ[[␈↓"excess"␈↓α x]x]␈↓.
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 1␈↓The symbol "␈↓λε␈↓" stands for the empty string.
␈↓ α←␈↓␈↓414 Projects␈↓
(9.1␈↓
␈↓"∀␈↓ α←␈↓3. Consider the following definition:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ β{␈↓αbaz <= λ[␈↓ ∧s[x; ␈↓λ`␈↓αy;␈↓"optional"␈↓α z; u ← 0; ␈↓"excess"␈↓α v]
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧sprint[x];
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧sprint[y];
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧sprint[z];
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧sprint[u];
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧sprint[v] ]
␈↓"∀␈↓ α←␈↓α␈↓Then a call of:
␈↓" ␈↓ α←␈↓αeval[␈↓ β'(BAZ 2 (CAR (QUOTE (X Y))) 4 5 6 7 (CAR (QUOTE (A . B))));
␈↓"β␈↓ α←␈↓α␈↓ β'NIL]␈↓
␈↓" ␈↓ α←␈↓would print:␈↓α␈↓ ∧s␈↓ ∧␈2
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧s(CAR(QUOTE (X Y)))
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧s4
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧s5
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧s(6 7 A)␈↓
␈↓"β␈↓ α←␈↓and return value: ␈↓α(6 7 A)␈↓.
␈↓" ␈↓ α←␈↓Similarly, defining:
␈↓"∀␈↓ α←␈↓␈↓ β'␈↓ β{␈↓αfii <= λ[␈↓ ∧s[x;y;␈↓"optional"␈↓α z; u ← 0; ␈↓"excess"␈↓α v]
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧sprint[x];
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧sprint[y];
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧sprint[z];
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧sprint[u];
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧sprint[v]]
␈↓"∀␈↓ α←␈↓α␈↓and calling: ␈↓ ∧K␈↓αeval[(FII 2 (CAR (QUOTE (X Y)));NIL]␈↓
␈↓" ␈↓ α←␈↓prints:␈↓α␈↓ β{␈↓ ∧s2
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧sX
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧sNIL
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧s0
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ β{␈↓ ∧sNIL␈↓
␈↓"λ␈↓ α←␈↓␈↓ ε≡␈↓↓Problem␈↓
␈↓"∀␈↓ α←␈↓Design␈αsimple␈αS-expr␈αrepresentations␈αof␈αthese␈αproposed␈αconstructs.␈α Make␈αthe
␈↓ α←␈↓necessary extensions to ␈↓αeval␈↓.
␈↓"β␈↓ α←␈↓␈↓ ¬T␈↓↓9.2 Pretty-printing␈↓
␈↓"β␈↓ α←␈↓This␈α⊃project␈α⊃expands␈α⊃on␈α∩the␈α⊃basic␈α⊃notion␈α⊃of␈α⊃"pretty-printing"␈α∩which␈α⊃was
␈↓ α←␈↓introduced␈α→on␈α→page 274.␈↓π 2␈↓␈α→This␈α→section␈α→presents␈α→several␈α→considerations
␈↓ α←␈↓involved␈α∩in␈α∩designing␈α∩such␈α∩a␈α∩pretty␈α∩printer.␈α∩Take␈α∩these␈α∪suggestions␈α∩and
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 2␈↓Pretty printers are called "grinders" at MIT.
␈↓ α←␈↓␈↓9.2␈↓ λ;Pretty-printing 415␈↓
␈↓"β␈↓ α←␈↓develop␈α∂a␈α∂suitable␈α⊂program␈α∂based␈α∂on␈α⊂the␈α∂suggestions␈α∂and␈α⊂your␈α∂experience
␈↓ α←␈↓with␈αyour␈αlocally␈αavailable␈αpretty␈αprinter.␈α In␈α[Gol 73]␈αseveral␈αpretty␈αprinting
␈↓ α←␈↓formats are discussed.
␈↓"∀␈↓ α←␈↓I.␈α␈↓↓Linear␈αformat␈↓:␈αThe␈αminimal␈αacceptable␈αoutput␈αformat␈αis␈αthat␈αproduced␈αby
␈↓ α←␈↓␈↓αprint␈↓.␈α Acceptability␈αis␈αjudged␈αby␈αwhether␈αthat␈αoutput␈αcan␈αbe␈αread␈αback␈αinto
␈↓ α←␈↓the machine and have a structure ␈↓αequal␈↓ the the structure printed out.␈↓π 3␈↓
␈↓"∀␈↓ α←␈↓II.␈α⊃␈↓↓Standard␈α⊂format␈↓:␈α⊃Given␈α⊃a␈α⊂list␈α⊃␈↓α(␈↓εa␈α⊃b␈α⊂c␈α⊃␈↓...␈↓ε␈α⊃d␈↓α)␈↓␈α⊂the␈α⊃standard␈α⊃format␈α⊂will
␈↓ α←␈↓assume that we are trying to print a function application and will produce:
␈↓" ␈↓ α←␈↓␈↓ ∧C␈↓α(␈↓εa␈↓ ∧sb
␈↓"β␈↓ α←␈↓ε␈↓ ∧C␈↓ ∧sc
␈↓"β␈↓ α←␈↓ε␈↓ ∧C␈↓ ∧s ␈↓. . .␈↓ε
␈↓"β␈↓ α←␈↓ε␈↓ ∧C␈↓ ∧sd␈↓α)
␈↓"∀␈↓ α←␈↓Thus ␈↓α(FOO (CAR (CONS (QUOTE A) B)) (G (H A) 4))␈↓ would produce:
␈↓"∞␈↓ α←␈↓␈↓ β'␈↓ ∧C␈↓α(FOO␈↓ ¬↔(CAR (CONS␈↓ εW(QUOTE A)
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧C␈↓ ¬↔␈↓ ¬;␈↓ εWB))
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧C␈↓ ¬↔(G␈↓ ¬;(H A)
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧C␈↓ ¬↔␈↓ ¬;4))
␈↓" ␈↓ α←␈↓Note␈αthat␈αthe␈α"standard␈αformat"␈αis␈αrecursively␈αapplied,␈αand␈αthus␈αmay␈αbecome
␈↓ α←␈↓too␈α∂wide␈α∞for␈α∂the␈α∂output␈α∞device.␈α∂It␈α∂that␈α∞case␈α∂we␈α∂can␈α∞resort␈α∂to␈α∂the␈α∞following
␈↓ α←␈↓format.
␈↓"∀␈↓ α←␈↓III. ␈↓↓Miser format␈↓: Write a list ␈↓α(␈↓εa b c ␈↓...␈↓ε d␈↓α)␈↓ as:
␈↓" ␈↓ α←␈↓␈↓ ∧C␈↓α(␈↓εa
␈↓"β␈↓ α←␈↓ε␈↓ ∧C b
␈↓"β␈↓ α←␈↓ε␈↓ ∧C c
␈↓"β␈↓ α←␈↓ε␈↓ ∧C ␈↓. . .␈↓ε
␈↓"β␈↓ α←␈↓ε␈↓ ∧C d␈↓α)
␈↓" ␈↓ α←␈↓Again,␈α∩the␈α∪recursive␈α∩application␈α∪of␈α∩this␈α∩format␈α∪can␈α∩overflow␈α∪the␈α∩output
␈↓ α←␈↓width. In that case we may have to resort to "linear format".
␈↓"β␈↓ α←␈↓________________
␈↓"β␈↓ α←␈↓␈↓ β'␈↓π 3␈↓We␈α
must␈αhedge␈α
that␈α
a␈αbit,␈α
since␈α␈↓αgensym␈↓␈α
atoms␈α
are␈αnot␈α
placed␈α
on␈αthe
␈↓ α←␈↓object list. Also structure made by ␈↓αrplaca␈↓ or ␈↓αrplacd␈↓ may not be re-readable.
␈↓ α←␈↓␈↓416 Projects␈↓
(9.2␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Typical␈α∃pretty␈α∃printers␈α∃also␈α∃recognize␈α∃certain␈α∃LISP␈α∃constructs␈α∀and
␈↓ α←␈↓"grind"␈αthem␈αdifferently.␈αThat␈αis,␈αwe␈αbuild␈αsome␈αsemantic␈αknowledge␈αinto␈αthe
␈↓ α←␈↓grinder. Block format is useful in many of these contexts.
␈↓"∀␈↓ α←␈↓IV. ␈↓↓Block format␈↓: A list ␈↓α(␈↓εa␈↓β1␈↓ ... ␈↓εa␈↓βi-1␈↓, ... ␈↓εa␈↓βn␈↓α)␈↓ has the block format:
␈↓" ␈↓ α←␈↓␈↓ ∧C␈↓α(␈↓ ∧O␈↓εa␈↓β1␈↓ ... ␈↓εa␈↓βi-1␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧C␈↓ ∧O␈↓εa␈↓βi␈↓ ... ␈↓εa␈↓βn␈↓α)␈↓
␈↓" ␈↓ α←␈↓For␈α⊂example,␈α∂the␈α⊂list␈α∂representation␈α⊂of␈α∂a␈α⊂␈↓αprog␈↓␈α∂might␈α⊂be␈α∂"ground"␈α⊂with␈α∂its
␈↓ α←␈↓␈↓αprog␈↓␈α∞variables␈α∞in␈α
block␈α∞format␈α∞and␈α
special␈α∞indenting␈α∞would␈α
be␈α∞used␈α∞in␈α
the
␈↓ α←␈↓␈↓αprog␈↓ body␈α"to␈α!emphasize␈α"the␈α!label␈α"and␈α!statement␈α"structure.␈α! The
␈↓ α←␈↓representation␈α
of␈α
␈↓αlength␈↓␈α
given␈α∞on␈α
page 192␈α
illustrates␈α
the␈α
special␈α∞format␈α
for
␈↓ α←␈↓␈↓αprog␈↓s,␈α∞though␈α∞the␈α∂␈↓αprog␈↓ variable␈α∞list␈α∞is␈α∞not␈α∂sufficently␈α∞long␈α∞to␈α∂require␈α∞block
␈↓ α←␈↓format.
␈↓"β␈↓ α←␈↓␈↓ β'Another␈αlist␈αformat␈αwhich␈αis␈αtreated␈αspecially␈αis␈αthe␈αrepresentation␈αof␈αa
␈↓ α←␈↓λ-expression.
␈↓"∀␈↓ α←␈↓␈↓ ∧π␈↓α(LAMBDA␈↓ ¬#␈↓<λ-list ground in block format>
␈↓" ␈↓ α←␈↓␈↓ ∧π␈↓ ¬#<body ground as a block>␈↓α)
␈↓"∀␈↓ α←␈↓␈↓ β'The␈α∞example␈α∞on␈α∞page 192␈α∂also␈α∞illistrates␈α∞this.␈α∞ This␈α∞format␈α∂will␈α∞allow
␈↓ α←␈↓multiple-bodied λ-expressions. For example:
␈↓" ␈↓ α←␈↓α␈↓ β'␈↓ ∧π(LAMBDA␈↓ ¬#(X Y Z)
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧π␈↓ ¬#(CONS␈↓ ε∂X
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧π␈↓ ¬#␈↓ ε∂(QUOTE A))
␈↓"β␈↓ α←␈↓α␈↓ β'␈↓ ∧π␈↓ ¬#(H 1 2))
␈↓" ␈↓ α←␈↓Notice␈α
that␈α
we␈α
decided␈α
to␈α
write␈α
␈↓α(H␈α
1␈α
2)␈↓␈α
in␈α
linear␈α
format␈α
rather␈α
than␈α
standard
␈↓ α←␈↓format;␈αsomehow␈αit␈α"looks␈αbetter".␈αPersonal␈αtaste␈αplays␈αa␈αstrong␈αrole␈αin␈αpretty
␈↓ α←␈↓printing,␈α
so␈α
several␈α
grinders␈α
give␈α
the␈α
users␈α
the␈α
ability␈α
to␈α
describe␈α
their␈αown
␈↓ α←␈↓formats.␈α∀We␈α∀will␈α∀see␈α∀a␈α∪similar,␈α∀but␈α∀more␈α∀general␈α∀device␈α∀in␈α∪Section 9.4.
␈↓ α←␈↓Another␈αpossibility␈αfor␈αuser␈αextension␈αlies␈αwith␈αour␈αproperty␈αlist␈αevaluator␈αof
␈↓ α←␈↓Section 5.8.␈α⊃ Part␈α⊃of␈α∩the␈α⊃definition␈α⊃of␈α∩the␈α⊃various␈α⊃LISP␈α∩constructs␈α⊃would
␈↓ α←␈↓allow␈αthe␈α
specification␈αof␈α
output␈αconventions␈α
for␈αinstances␈α
of␈αthose␈α
constructs;
␈↓ α←␈↓thus␈α∪besides␈α∪specifying␈α∪evaluation␈α∪properties␈α∪for␈α∪␈↓αLAMBDA␈↓,␈α∀␈↓αPROG␈↓,␈α∪and
␈↓ α←␈↓␈↓αCOND␈↓,␈αwe␈αwould␈αalso␈αthe␈αgrinding␈αroutines␈αfor␈αoutputting␈αinstances␈αof␈α
those
␈↓ α←␈↓constructs.
␈↓"β␈↓ α←␈↓␈↓ β'Since␈α∩lists␈α∩beginning␈α∩with␈α∩␈↓αCOND␈↓␈α∩are␈α∩representations␈α∩of␈α⊃conditional
␈↓ α←␈↓expressions they too are handled specially by the grinding routines.
␈↓"∀␈↓ α←␈↓␈↓ ∧π␈↓α(COND␈↓␈↓ ∧s<grind clause␈↓β1␈↓>
␈↓" ␈↓ α←␈↓␈↓ ∧π␈↓ ∧s . . .
␈↓" ␈↓ α←␈↓␈↓ ∧π␈↓ ∧s<grind clause␈↓βn␈↓>␈↓α)
␈↓"∀␈↓ α←␈↓These␈α∪selected␈α∪formats␈α∪should␈α∪give␈α∪a␈α∪reasonable␈α∪idea␈α∪of␈α∪the␈α∪techniques
␈↓ α←␈↓available␈α⊂for␈α⊂pretty␈α∂printers.␈α⊂More␈α⊂techniques␈α∂can␈α⊂be␈α⊂extracted␈α⊂from␈α∂your
␈↓ α←␈↓local grinder.
␈↓ α←␈↓␈↓9.2␈↓ λ;Pretty-printing 417␈↓
␈↓"β␈↓ α←␈↓␈↓ β'Your␈α∩pretty␈α∩printer␈α∩may␈α∩assume␈α∩the␈α∩existence␈α∩of␈α∩␈↓αpatom␈↓,␈α∩␈↓αprint␈↓,␈α⊃and
␈↓ α←␈↓␈↓αterpri␈↓␈αof␈αSection 5.11;␈αand␈α
you␈αmay␈αassume␈αthe␈α
existence␈αof␈αthe␈αusual␈αclass␈α
of
␈↓ α←␈↓arithmetic operations. In addition, the following primitives may be used:
␈↓"β␈↓ α←␈↓␈↓↓1.␈↓ ␈↓αlinelength␈↓:␈α
If␈α
␈↓αlinelength[ ]␈↓␈α
is␈α
evaluated,␈α
the␈α
value␈α
returned␈α
is␈α
the␈αnumber
␈↓ α←␈↓␈↓ β'of␈α⊗characters␈α⊗allowed␈α∃on␈α⊗a␈α⊗line␈α⊗of␈α∃the␈α⊗current␈α⊗output␈α⊗device.␈α∃If
␈↓ α←␈↓␈↓ β'␈↓αlinelength␈↓␈α
is␈α
called␈αwith␈α
a␈α
numeric␈α
argument,␈αthen␈α
the␈α
line␈α
length␈αof␈α
the
␈↓ α←␈↓␈↓ β'current output device is set to that number.
␈↓"β␈↓ α←␈↓␈↓↓2.␈↓ ␈↓αchrct␈↓:␈α∞This␈α
is␈α∞a␈α
nullary␈α∞function,␈α
and␈α∞returns␈α
a␈α∞number␈α∞representing␈α
the
␈↓ α←␈↓␈↓ β'number␈α∃of␈α∃character␈α∀positions␈α∃remaining␈α∃on␈α∀the␈α∃current␈α∃line.␈α∀For
␈↓ α←␈↓␈↓ β'example, just after ␈↓αterpri[]␈↓ has been executed
␈↓"β␈↓ α←␈↓␈↓ β'␈↓αchrct[] = linelength[]␈↓
␈↓"β␈↓ α←␈↓␈↓ β'and ␈↓αprog␈↓β2␈↓α[patom[ABC]; difference[linelength[];chrct[]]] = 3␈↓
␈↓"β␈↓ α←␈↓␈↓↓3.␈↓ ␈↓αflatsize␈↓:␈α∃A␈α∃simple␈α∃count␈α∃of␈α∃the␈α∃atoms␈α∃and␈α∃special␈α∃characters␈α⊗in␈α∃an
␈↓ α←␈↓␈↓ β'expression␈α∀won't␈α∀give␈α∀an␈α∃accurate␈α∀picture␈α∀of␈α∀the␈α∃requirements␈α∀for
␈↓ α←␈↓␈↓ β'printing␈α
an␈α
expression.␈α
Special␈α
characters␈α
take␈α
one␈α
character␈α
position,
␈↓ α←␈↓␈↓ β'but␈α
literal␈α
atoms␈α
and␈α
numbers␈α
may␈α
require␈α
more.␈α
To␈α
determine␈α
whether
␈↓ α←␈↓␈↓ β'or␈α⊗not␈α↔a␈α⊗special␈α↔format␈α⊗can␈α↔be␈α⊗used␈α↔on␈α⊗an␈α↔expression␈α⊗requires
␈↓ α←␈↓␈↓ β'knowledge␈α
of␈αits␈α
character␈α
count.␈αThe␈α
number␈α
of␈αcharacters␈α
in␈αthe␈α
atom
␈↓ α←␈↓␈↓ β'␈↓αx␈↓␈α⊃is␈α⊃given␈α∩by␈α⊃␈↓αflatsize[x]␈↓;␈α⊃for␈α∩example,␈α⊃␈↓αflatsize[ABCD]␈↓␈α⊃is␈α∩␈↓α4␈↓.␈α⊃Actually
␈↓ α←␈↓␈↓ β'␈↓αflatsize␈↓␈α∩is␈α∩defined␈α∩for␈α⊃non-atomic␈α∩arguments,␈α∩giving␈α∩the␈α∩number␈α⊃of
␈↓ α←␈↓␈↓ β'character␈αpositions␈αrequired␈αto␈α␈↓αprint␈↓␈αits␈αargument.␈αThus,␈α␈↓αflatsize[(A.B)]␈↓
␈↓ α←␈↓␈↓ β'is ␈↓α7␈↓ rather than ␈↓α5␈↓ since ␈↓αprint␈↓ will surround the dot with spaces.
␈↓"β␈↓ α←␈↓␈↓ ¬∂␈↓↓9.3 Syntax-directed Processes␈↓
␈↓"β␈↓ α←␈↓This␈α_project␈α_is␈α_only␈α_an␈α→introduction␈α_to␈α_the␈α_very␈α_important␈α→area␈α_of
␈↓ α←␈↓␈↓↓syntax-directed␈αprocesses␈↓.␈α As␈αthe␈αname␈αimplies,␈αthere␈αis␈αa␈αclose␈αrelationship
␈↓ α←␈↓between␈α∂the␈α∂syntax␈α∂specification␈α∂of␈α∂an␈α∂object,␈α∂and␈α∂the␈α∂computational␈α∂rules
␈↓ α←␈↓which␈α
we␈αwish␈α
to␈αapply.␈α
Syntax-directed␈αtechniques␈α
are␈αused␈α
extensively␈αin
␈↓ α←␈↓compiler␈α∂construction,␈α∞relating␈α∂the␈α∞syntax␈α∂equations␈α∞to␈α∂the␈α∂code␈α∞generation.
␈↓ α←␈↓We shall begin by applying syntax-directed techniques to evaluation.
␈↓"β␈↓ α←␈↓␈↓ β'We␈α∩know␈α⊃that␈α∩there␈α⊃are␈α∩alternatives␈α⊃to␈α∩the␈α∩call-by-value␈α⊃evaluation
␈↓ α←␈↓scheme;␈αand␈αwe␈αknow␈αthere␈αare␈αalternatives␈αto␈αthe␈αprefix␈αnotation␈α
which␈αwe
␈↓ α←␈↓chose to represent function application.
␈↓"β␈↓ α←␈↓␈↓ β'For␈α∩example,␈α⊃in␈α∩grade␈α∩school␈α⊃we␈α∩all␈α∩learned␈α⊃infix␈α∩notation␈α∩and␈α⊃its
␈↓ α←␈↓implied␈α
precedence␈α
relations,␈αsay␈α
for␈α
+␈α
and␈α*.␈α
Simply␈α
because␈α
infix␈αnotation␈α
is
␈↓ α←␈↓the␈αfirst␈αrepresentation␈αwe␈αsee␈αdoesn't␈α
mean␈αthat␈αit␈αis␈αthe␈αmost␈αconvenient␈α
for
␈↓ α←␈↓evaluation either by us or by machine.
␈↓"β␈↓ α←␈↓␈↓ β'Let's␈α_take␈α→as␈α_example␈α_the␈α→expression:␈α_2+3*5.␈α_ The␈α→grade␈α_school
␈↓ α←␈↓precedence␈α⊗relations␈α∃say␈α⊗that␈α∃*␈α⊗takes␈α∃precedence␈α⊗over␈α∃+.␈α⊗ That␈α⊗is,␈α∃the
␈↓ α←␈↓␈↓418 Projects␈↓
(9.3␈↓
␈↓"β␈↓ α←␈↓expression␈α⊗represents␈α⊗2+(3*5)␈α⊗rather␈α⊗than␈α⊗(2+3)*5.␈α⊗ When␈α⊗we␈α⊗write␈α⊗the
␈↓ α←␈↓expression␈α
in␈α
prefix␈α
notation:␈α
+[2;*[3;5]],␈α
the␈α
precedence␈α
of␈α
operations␈αis␈α
made
␈↓ α←␈↓explicit.␈α∞Similarly,␈α∂postfix␈α∞notation␈α∂(where␈α∞the␈α∂operators␈α∞follow␈α∂rather␈α∞than
␈↓ α←␈↓precede the operands) is easy: [2;[3;5]*]+.
␈↓"β␈↓ α←␈↓␈↓ β'Some␈α∀notational␈α∀schemes␈α∃lend␈α∀themselves␈α∀to␈α∃mechanical␈α∀evaluation
␈↓ α←␈↓better␈α⊗than␈α⊗others.␈α⊗ There␈α⊗is␈α∃a␈α⊗certain␈α⊗amount␈α⊗of␈α⊗implied␈α∃intelligence
␈↓ α←␈↓required␈α⊗in␈α∃the␈α⊗usual␈α⊗infix␈α∃scheme.␈α⊗ We␈α⊗have␈α∃already␈α⊗seen␈α⊗one␈α∃very
␈↓ α←␈↓mechanical␈α_method␈α→for␈α_evaluating␈α_some␈α→prefix␈α_expressions:␈α→the␈α_␈↓αvalue␈↓
␈↓ α←␈↓function in Section 2.7.
␈↓"β␈↓ α←␈↓␈↓ β'A␈αstrong␈αpoint␈αof␈α
postfix␈αstring␈αnotation␈αis␈α
its␈αease␈αof␈αevaluation.␈α
First,
␈↓ α←␈↓since␈α_we␈α_know␈α_that␈α_plus␈α↔and␈α_times␈α_are␈α_both␈α_binary␈α_operations,␈α↔the
␈↓ α←␈↓punctuation,␈α ], [,␈αand␈α
; , ␈αis␈αredundant␈α(this␈α
is␈αalso␈αtrue␈αfor␈α
prefix␈αnotation).
␈↓ α←␈↓Thus the string, 2 3 5 * +, contains the same information as [2;[3;5]*]+.
␈↓"β␈↓ α←␈↓␈↓ β'Using␈α∞"↓"␈α∞to␈α
point␈α∞to␈α∞the␈α
current␈α∞position␈α∞in␈α
the␈α∞string␈α∞and␈α∞using␈α
the
␈↓ α←␈↓"| ... |"-notation␈αof␈α
page 124␈αto␈α
represent␈αthe␈αstack,␈α
the␈αfollowing␈α
is␈αa␈α
trace␈αof
␈↓ α←␈↓the evaluation of the above string, 2 3 5 * +.
␈↓"∀␈↓ α←␈↓↓ ↓ ↓
␈↓"β␈↓ α←␈↓2 3 5 * + ;| | => 3 5 * + ;| 2 | => 5 * + ;␈↓ ε{| 3 | =>
␈↓"β␈↓ α←␈↓␈↓ ∧7␈↓ ε{| 2 |
␈↓"∀␈↓ α←␈↓↓ ↓
␈↓"β␈↓ α←␈↓* + ; | 5 | => + ;␈↓ ∧7| 15 | => | 17 |
␈↓"β␈↓ α←␈↓ | 3 |␈↓ ∧7| 2 |
␈↓"β␈↓ α←␈↓ | 2 |
␈↓"∀␈↓ α←␈↓It␈α⊂is␈α⊂a␈α⊂very␈α⊂simple␈α⊂task␈α⊂to␈α⊂program␈α⊂this␈α⊂scheme␈α⊂in␈α⊂LISP,␈α⊂and␈α⊂it␈α⊂is␈α⊂quite
␈↓ α←␈↓simple to extend this evaluation scheme to n-ary operators.
␈↓"β␈↓ α←␈↓␈↓ β'Given␈α∞an␈α
arbitrary␈α∞arithmetic␈α∞expression␈α
involving␈α∞constants,␈α∞and␈α
the
␈↓ α←␈↓binary␈α∞operations␈α
of␈α∞plus␈α
and␈α∞times,␈α
we␈α∞have␈α
a␈α∞straightforward␈α
mechanical
␈↓ α←␈↓evaluation␈α∂scheme.␈α∂It␈α∂is␈α∂intuitively␈α∂clear␈α∂how␈α∂to␈α∂translate␈α∂infix␈α∞expressions
␈↓ α←␈↓into␈α⊂postfix␈α⊃notation.␈α⊂ If␈α⊃we␈α⊂could␈α⊃mechanize␈α⊂this␈α⊃process␈α⊂then␈α⊃we␈α⊂would
␈↓ α←␈↓have␈αan␈αalgorithm␈αfor␈αthe␈αevaluation␈αof␈αinfix␈αexpressions.␈α First␈αlet's␈αattempt
␈↓ α←␈↓to␈αdescribe␈αprecisely␈αthe␈αclass␈αof␈αinfix␈αexpressions␈αwhich␈αwe␈αwish␈αto␈α
evaluate.
␈↓ α←␈↓The BNF notation is a good vehicle. Perhaps the following:
␈↓"∀␈↓ α←␈↓␈↓ ∧C<exp>␈↓ ¬;::= <exp><binop><exp>
␈↓"β␈↓ α←␈↓␈↓ ∧C␈↓ ¬;::= <integer>
␈↓" ␈↓ α←␈↓␈↓ ∧C<binop>␈↓ ¬;::= + | *
␈↓"∀␈↓ α←␈↓There␈α
are␈α
many␈α
difficulties␈αwith␈α
this␈α
grammar.␈α
First,␈α
many␈αexpressions␈α
have
␈↓ α←␈↓more␈α
than␈α
one␈α
possible␈α
description␈α
or␈αparse␈α
tree;␈α
the␈α
grammar␈α
is␈α
said␈α
to␈αbe
␈↓ α←␈↓␈↓↓ambiguous␈↓.␈α∀Second,␈α∀this␈α∀grammar␈α∀doesn't␈α∀express␈α∀our␈α∃usual␈α∀precedence
␈↓ α←␈↓relations. The next attempt is successful:
␈↓ α←␈↓␈↓9.3␈↓ π9Syntax-directed Processes 419␈↓
␈↓"∀␈↓ α←␈↓␈↓ ∧C<exp>␈↓ ¬G::= <exp> + <term>␈↓ π␈(1)
␈↓"β␈↓ α←␈↓␈↓ ∧C␈↓ ¬G::= <term>␈↓ π␈(2)
␈↓" ␈↓ α←␈↓␈↓ ∧C<term>␈↓ ¬G::= <term> * <factor>␈↓ π␈(3)
␈↓"β␈↓ α←␈↓␈↓ ∧C␈↓ ¬G::= <factor>␈↓ π␈(4)
␈↓" ␈↓ α←␈↓␈↓ ∧C<factor>␈↓ ¬G::= ( <exp> )␈↓ π␈(5)
␈↓"β␈↓ α←␈↓␈↓ ∧C␈↓ ¬G::= <integer>␈↓ π␈(6)
␈↓" ␈↓ α←␈↓␈↓ ∧C<integer>␈↓ ¬G::= 0 | 1 | 2 . . .␈↓ π␈(7)
␈↓"∀␈↓ α←␈↓For example the (only) parsing of 2+3*5 is:
␈↓"∀␈↓ α←␈↓ <exp>
␈↓"β␈↓ α←␈↓ |
␈↓"β␈↓ α←␈↓ <exp> + <term>
␈↓"β␈↓ α←␈↓ | |
␈↓"β␈↓ α←␈↓ <term> <term>*<factor>
␈↓"β␈↓ α←␈↓ | | |
␈↓"β␈↓ α←␈↓ <factor> <factor> <integer>
␈↓"β␈↓ α←␈↓ | | |
␈↓"β␈↓ α←␈↓ <integer> <integer> 5
␈↓"β␈↓ α←␈↓ | |
␈↓"β␈↓ α←␈↓ 2 3
␈↓"β
␈↓"β
␈↓"∀␈↓ α←␈↓Our next step is based on the following:
␈↓"β␈↓ α←␈↓Assumption:␈αGiven␈αan␈αarbitrary,␈αwell-formed,␈αarithmetic␈αexpression,␈αe,␈αof␈α
our
␈↓ α←␈↓␈↓ βWabove␈α
class,␈αwe␈α
can␈αfind␈α
the␈αleft-most␈α
well-formed␈α
subexpression,␈αs,
␈↓ α←␈↓␈↓ βWsuch␈α
that␈αs␈α
is␈αan␈α
instance␈α
of␈αthe␈α
RHS␈αof␈α
one␈α
of␈αthe␈α
rules,␈α(1)-(7).␈α
Let
␈↓ α←␈↓␈↓ βWe␈↓λ'␈↓␈αbe␈α
the␈αexpression␈α
obtained␈αfrom␈α
e,␈αby␈α
replacing␈αthe␈αoccurrence␈α
of
␈↓ α←␈↓␈↓ βWthe RHS by the LHS; then our assumption is also applicable to e␈↓λ'␈↓.
␈↓"∀␈↓ α←␈↓For example,
␈↓" ␈↓ α←␈↓e␈↓ ¬/s␈↓ ππe␈↓λ'␈↓␈↓ ?rule
␈↓" ␈↓ α←␈↓ 2+3*5␈↓ ¬/ 2␈↓ ππ<integer>+3*5␈↓ ?(7)
␈↓"β␈↓ α←␈↓<integer>+3*5␈↓ ¬/<integer>␈↓ ππ<factor>+3*5␈↓ ?(6)
␈↓"β␈↓ α←␈↓<factor>+3*5␈↓ ¬/<factor>␈↓ ππ<term>+3*5␈↓ ?(4)
␈↓"β␈↓ α←␈↓<term>+3*5␈↓ ¬/<term>␈↓ ππ<exp>+3*5␈↓ ?(2)
␈↓"β␈↓ α←␈↓<exp>+3*5␈↓ ¬/3␈↓ ππ<exp>+<integer>*5␈↓ ?(7)
␈↓"β␈↓ α←␈↓<exp>+<integer>*5␈↓ ¬/<integer>␈↓ ππ<exp>+<factor>*5␈↓ ?(6)
␈↓"β␈↓ α←␈↓<exp>+<factor>*5␈↓ ¬/<factor>␈↓ ππ<exp>+<term>*5␈↓ ?(4)
␈↓"β␈↓ α←␈↓<exp>+<term>*5␈↓ ¬/5␈↓ ππ<exp>+<term>*<integer>␈↓ ?(7)
␈↓"β␈↓ α←␈↓<exp>+<term>*<integer>␈↓ ¬/<integer>␈↓ ππ<exp>+<term>*<factor>␈↓ ?(6)
␈↓"β␈↓ α←␈↓<exp>+<term>*<factor>␈↓ ¬/<term>*<factor>␈↓ ππ<exp>+<term>␈↓ ?(3)
␈↓"β␈↓ α←␈↓<exp>+<term>␈↓ ¬/<exp>+<term>␈↓ ππ<exp>␈↓ ?(1)
␈↓"∀␈↓ α←␈↓Now␈αwe␈αassociate␈αan␈αaction␈αwith␈αeach␈αof␈αthe␈αrules␈α(1)-(7)␈αsuch␈αthat␈αwhenever
␈↓ α←␈↓we␈α⊂apply␈α⊂one␈α⊂of␈α⊂the␈α⊂rules␈α⊂in␈α⊂the␈α⊂above␈α⊂reduction␈α⊂technique,␈α⊂we␈α⊂will␈α∂also
␈↓ α←␈↓␈↓420 Projects␈↓
(9.3␈↓
␈↓"β␈↓ α←␈↓execute␈α⊃the␈α∩corresponding␈α⊃action.␈α⊃We␈α∩will␈α⊃also␈α⊃designate␈α∩an␈α⊃initialization
␈↓ α←␈↓routine which will be executed at the beginning of the reduction.
␈↓"∀␈↓ α←␈↓Initialization:␈α∂Let␈α∞V[0:N]␈α∂be␈α∞a␈α∂vector␈α∞indexed␈α∂from␈α∞0␈α∂to␈α∞N,␈α∂where␈α∞N␈α∂is␈α∞at
␈↓ α←␈↓least␈α⊃as␈α⊂long␈α⊃as␈α⊃the␈α⊂input␈α⊃character␈α⊂string.␈α⊃ Let␈α⊃i␈α⊂be␈α⊃an␈α⊃integer␈α⊂variable,
␈↓ α←␈↓initialized to 0.
␈↓"∀␈↓ α←␈↓␈↓ β∂␈↓ ∧∪rule␈↓ ε3 action
␈↓" ␈↓ α←␈↓␈↓ β∂<exp>␈↓ ∧∪::= <exp> + <term>␈↓ ε3V(i) ← `+'; i ← i+1
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ ∧∪::= <term>␈↓ ε3 do nothing
␈↓" ␈↓ α←␈↓␈↓ β∂<term>␈↓ ∧∪::= <term>*<factor>␈↓ ε3V(i) ← `*'; i ← i+1
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ ∧∪::= <factor>␈↓ ε3 do nothing
␈↓" ␈↓ α←␈↓␈↓ β∂<factor>␈↓ ∧∪::= (<exp>)␈↓ ε3 do nothing
␈↓"β␈↓ α←␈↓␈↓ β∂␈↓ ∧∪::= <integer>␈↓ ε3 do nothing
␈↓" ␈↓ α←␈↓␈↓ β∂<integer>␈↓ ∧∪::= 0 | 1 | ...␈↓ ε3V(i) ← 0 | V(i) ← 1 | ... ; i ← i+1
␈↓"∀␈↓ α←␈↓Again␈αperforming␈αthe␈αreduction␈αof␈αthe␈αexpression,␈α2+3*5,␈αbut␈α
now␈αexecuting
␈↓ α←␈↓the␈α∪action␈α∪routines␈α∪as␈α∀well␈α∪we␈α∪find␈α∪the␈α∀contents␈α∪of␈α∪V␈α∪will␈α∀contain␈α∪the
␈↓ α←␈↓following:
␈↓"∀␈↓ α←␈↓
␈↓"β␈↓ α←␈↓ V: 0 1 2 3 4
␈↓"β␈↓ α←␈↓ 2
␈↓"β␈↓ α←␈↓ 2 3
␈↓"β␈↓ α←␈↓ 2 3 5
␈↓"β␈↓ α←␈↓ 2 3 5 *
␈↓"β␈↓ α←␈↓ 2 3 5 * +
␈↓"β
␈↓"∀␈↓ α←␈↓That is, the postfix form of the arithmetic expression is formed in V.
␈↓"β␈↓ α←␈↓␈↓ β'So␈α↔combining␈α↔the␈α↔algorithms␈α↔for␈α↔infix-to-postfix␈α↔translation,␈α↔with
␈↓ α←␈↓postfix␈α
evaluation,␈αwe␈α
could␈α
obtain␈αan␈α
infix␈α
evaluator.␈αHowever,␈α
we␈α
can␈αdo
␈↓ α←␈↓better.␈α∩By␈α∩a␈α∩simple␈α∩change␈α∩to␈α∩the␈α∩action␈α∩routines␈α∩we␈α∩can␈α∪perform␈α∩infix
␈↓ α←␈↓evaluation as we reduce the expression.
␈↓"∀␈↓ α←␈↓Initialization:␈αLet␈αV[0:N]␈αbe␈αa␈αvector␈αand␈αlet␈αi␈αbe␈αan␈αinteger-valued␈αvariable,
␈↓ α←␈↓initialized to 0.
␈↓"∀␈↓ α←␈↓␈↓ α←␈↓ βc rule␈↓ ε' action
␈↓" ␈↓ α←␈↓␈↓ α←<exp>␈↓ βc::= <exp>+<term>␈↓ ε'V(i-2) ← V(i-1)+V(i-2); i ← i-1
␈↓"β␈↓ α←␈↓␈↓ α←␈↓ βc::= <term>␈↓ ε' do nothing
␈↓" ␈↓ α←␈↓␈↓ α←<term>␈↓ βc::= <term>*<factor>␈↓ ε'V(i-2) ← V(i-1)*V(i-2); i ← i-1
␈↓"β␈↓ α←␈↓␈↓ α←␈↓ βc::= <factor>␈↓ ε' do nothing
␈↓" ␈↓ α←␈↓␈↓ α←<factor>␈↓ βc::= (<exp>)␈↓ ε' do nothing
␈↓"β␈↓ α←␈↓␈↓ α←␈↓ βc::= <integer>␈↓ ε' do nothing
␈↓" ␈↓ α←␈↓␈↓ α←<integer>␈↓ βc::= 0 | 1 | ...␈↓ ε'V(i) ← 0 | V(i) ← 1| ... ; i ← i+1
␈↓"∀␈↓ α←␈↓When␈α∞the␈α∞arithmetic␈α∞expression␈α∞has␈α∞been␈α∞recognized,␈α∞V(0)␈α∞will␈α∞contain␈α∞the
␈↓ α←␈↓␈↓9.3␈↓ π9Syntax-directed Processes 421␈↓
␈↓"β␈↓ α←␈↓value␈α
of␈α
that␈α
expression.␈α
Notice␈α
that␈α
the␈α
combination␈α
of␈α
V␈α
and␈α
its␈α
index␈αi,␈α
is
␈↓ α←␈↓performing␈αas␈αa␈αstack␈αin␈αthis␈αtranslator.␈αThat␈αis:␈αwhen␈αwe␈αsee␈αan␈αinteger,␈αwe
␈↓ α←␈↓push␈α∩it␈α∩into␈α∩the␈α∩stack;␈α∩when␈α∩we␈α∩see␈α∩a␈α∩binary␈α∩operator␈α∩we␈α∩pop␈α∪the␈α∩two
␈↓ α←␈↓operands, perform the operation and push the result back on the stack.
␈↓"β␈↓ α←␈↓␈↓ β'This␈α∃technique␈α∀of␈α∃associating␈α∃action␈α∀routines␈α∃(also␈α∃called␈α∀semantic
␈↓ α←␈↓routines)␈α∂with␈α∂the␈α∞BNF␈α∂(or␈α∂syntax)␈α∞equations␈α∂is␈α∂extremely␈α∂powerful.␈α∞ Such
␈↓ α←␈↓processes are called syntax-directed.
␈↓"∪␈↓ α←␈↓␈↓ ε&␈↓↓Project␈↓
␈↓"→␈↓ α←␈↓Write␈α⊃a␈α⊃LISP␈α⊃program␈α⊃to␈α⊃perform␈α⊃infix␈α⊃to␈α⊃postfix␈α⊃translation;␈α∩and␈α⊃then
␈↓ α←␈↓modify␈α∞it␈α∞to␈α
perform␈α∞infix␈α∞evaluation.␈α∞Write␈α
your␈α∞programs␈α∞two␈α∞ways:␈α
first
␈↓ α←␈↓use an explicit stack; then use recursion to operate with an implicit stack.
␈↓"∪␈↓ α←␈↓␈↓ ε&␈↓↓Project␈↓
␈↓"→␈↓ α←␈↓As␈α
a␈α
further␈α
example␈α
of␈α
syntax-directed␈α
processes␈α
recall␈α
the␈α
set␈αof␈α
expressions
␈↓ α←␈↓evaluated␈α
by␈α
␈↓αtgmoaf␈↓:␈α
the␈α
five␈α
primitives␈α
under␈α
composition␈α
of␈α
functions,␈α
all
␈↓ α←␈↓with␈α∩constant␈α∩arguments.␈α∩ Write␈α∪syntax␈α∩equations␈α∩and␈α∩action␈α∪routines␈α∩to
␈↓ α←␈↓effect the evaluation of such expressions.
␈↓"β␈↓ α←␈↓␈↓ ¬4␈↓↓9.4 Syntax-directed I/O␈↓
␈↓"β␈↓ α←␈↓It␈α∃is␈α∃frequently␈α∀quite␈α∃convenient␈α∃to␈α∃enter␈α∀input␈α∃and␈α∃receive␈α∃output␈α∀in
␈↓ α←␈↓something␈α
other␈α
than␈α
list␈α
notation.␈α Recall␈α
our␈α
diagram␈α
on␈α
page 56.␈α We␈α
wish
␈↓ α←␈↓to mechanize the encoding of the input and the decoding of the output.
␈↓"β␈↓ α←␈↓␈↓ β'Consider␈α↔for␈α⊗example,␈α↔the␈α⊗problem␈α↔of␈α⊗simplification␈α↔of␈α⊗algebraic
␈↓ α←␈↓expressions.␈α~ Many␈α~rather␈α→sophisticated␈α~simplifiers␈α~have␈α~been␈α→written
␈↓ α←␈↓([Hea 68],␈α∪[MAC 74]).␈α∪Assume␈α∪that␈α∩we␈α∪have␈α∪one␈α∪named␈α∪␈↓αsimplify␈↓␈α∩which
␈↓ α←␈↓expects list input and gives list output. Thus for example:
␈↓"∀␈↓ α←␈↓α(3+4)*x + x =␈↓βI ␈↓α=> (PLUS (TIMES (PLUS 3 4) X) X)
␈↓"β␈↓ α←␈↓α␈↓ εβ=␈↓βsimplify␈↓α=> (TIMES 8 X) =␈↓βO ␈↓α=> 8*x
␈↓"∀␈↓ α←␈↓We␈αwould␈αlike␈αtransformations␈αI␈αand␈αO␈αdone␈αautomatically.␈αM-expr␈αnotation
␈↓ α←␈↓is␈α∞a␈α∞candidate␈α∞for␈α∞such␈α∞a␈α∂task.␈α∞Then␈α∞we␈α∞could␈α∞write␈α∞algorithms␈α∂in␈α∞M-expr
␈↓ α←␈↓notation and have them executed by ␈↓αeval␈↓.
␈↓"∀␈↓ α←␈↓αcons[A;B] =␈↓βI ␈↓α=> (CONS (QUOTE A) (QUOTE B))
␈↓"β␈↓ α←␈↓α␈↓ εβ=␈↓βeval␈↓α=> (A . B) =␈↓βO ␈↓α=> (A . B)
␈↓"∀␈↓ α←␈↓Transformation O is the identity in this case.
␈↓"β␈↓ α←␈↓␈↓ β'Frequently,␈α∪the␈α∪input␈α∪and␈α∩output␈α∪transformations␈α∪can␈α∪be␈α∩generated
␈↓ α←␈↓automatically.␈α! We␈α"describe␈α!one␈α!such␈α"program,␈α!called␈α"SDIO␈α!for
␈↓ α←␈↓Syntax-Directed␈α⊗Input-Output␈α⊗([Qua 68].)␈α⊗It␈α∃was␈α⊗the␈α⊗forerunner␈α⊗of␈α∃the
␈↓ α←␈↓␈↓422 Projects␈↓
(9.4␈↓
␈↓"β␈↓ α←␈↓MLISP2␈α∂project;␈α∂see␈α⊂[Mli 73].␈α∂ We␈α∂will␈α∂assume␈α⊂that␈α∂the␈α∂input␈α⊂and␈α∂output
␈↓ α←␈↓syntax␈α∪is␈α∪specified␈α∩in␈α∪BNF.␈α∪With␈α∪each␈α∩BNF␈α∪equation␈α∪we␈α∪will␈α∩associate
␈↓ α←␈↓semantics␈α∩describing␈α∩the␈α∪S-expr␈α∩representation.␈α∩The␈α∪input␈α∩transformation
␈↓ α←␈↓(parser)␈αwill␈αuse␈αthis␈αinformation␈αto␈αbuild␈αthe␈αrepresentation;␈αand␈αthe␈αoutput
␈↓ α←␈↓transformation (unparser) will map the internal representation back.
␈↓"β␈↓ α←␈↓␈↓ β'Syntax␈α∩directed␈α∪I/O␈α∩is␈α∩more␈α∪than␈α∩a␈α∩cosmetic.␈α∪ Assume␈α∩we␈α∪wish␈α∩to
␈↓ α←␈↓represent␈α⊂a␈α⊂structure␈α⊂as␈α⊂a␈α∂particularly␈α⊂horrible␈α⊂list␈α⊂structure.␈α⊂We␈α⊂can␈α∂give
␈↓ α←␈↓augmented␈α⊃BNF␈α⊃equations␈α∩specifying␈α⊃the␈α⊃external␈α⊃representation␈α∩and␈α⊃the
␈↓ α←␈↓translation␈α∂to␈α∂the␈α∂underlying␈α∂representation.␈α∂Clearly␈α∂when␈α⊂outputting␈α∂these
␈↓ α←␈↓structures␈α⊂we␈α⊂do␈α⊂not␈α⊂want␈α⊂to␈α⊂see␈α⊂the␈α⊂internal␈α⊂representation.␈α⊂This␈α⊂can␈α⊂be
␈↓ α←␈↓particularly␈αannoying␈αwhen␈αwe␈αare␈αdebugging;␈αwe␈αwish␈αto␈αconcentrate␈αon␈αthe
␈↓ α←␈↓misbehavior␈α→of␈α→the␈α→algorithm;␈α→we␈α_do␈α→not␈α→want␈α→to␈α→be␈α→distracted␈α_by
␈↓ α←␈↓incomprehensible␈α∪output.␈α∪ Syntax␈α∀directed␈α∪output,␈α∪or␈α∪unparsing,␈α∀can␈α∪aid
␈↓ α←␈↓significantly.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α
easiest␈α
introduction␈α
to␈α
SDIO␈α
is␈α
to␈α
examine␈α
an␈α
example.␈αConsider
␈↓ α←␈↓the␈α∂proposed␈α∂simplification␈α∞task␈α∂above.␈α∂The␈α∞"natural"␈α∂input␈α∂syntax␈α∂can␈α∞be
␈↓ α←␈↓described␈α∀in␈α∀BNF.␈α∪ We␈α∀have␈α∀given␈α∪closely␈α∀related␈α∀syntax␈α∀equations␈α∪on
␈↓ α←␈↓page 419. We will display a few equations augmented by SDIO semantics.
␈↓"β␈↓ α←␈↓For example:
␈↓"∀␈↓ α←␈↓ <EXP>␈↓ ∧∪::= <EXP> + <TERM>␈↓ εW=>(PLUS EXP TERM)
␈↓"β␈↓ α←␈↓␈↓ ∧∪::= <TERM>␈↓ εW=>*
␈↓" ␈↓ α←␈↓ <TERM>␈↓ ∧∪::= <NUMBER>␈↓ εW=>*
␈↓"∀␈↓ α←␈↓To␈αthe␈αinput␈αparser␈αthe␈αfirst␈αBNF␈αequation␈αmeans:␈αwhenever␈αthe␈αright␈αhand
␈↓ α←␈↓side␈αis␈αrecognized,␈αreduce␈αthat␈αoccurrence␈αto␈αthe␈αleft␈αhand␈αside␈α
and␈αassociate
␈↓ α←␈↓with␈α
it␈α
the␈α
list␈αconsisting␈α
of␈α
the␈α
atom␈αPLUS,␈α
the␈α
S-expr␈α
associated␈α
with␈αthe
␈↓ α←␈↓occurrence␈α⊃of␈α⊃<EXP>,␈α∩and␈α⊃the␈α⊃S-expr␈α∩associated␈α⊃with␈α⊃the␈α∩occurrrence␈α⊃of
␈↓ α←␈↓<TERM>.␈α~ The␈α→second␈α~equation␈α→means␈α~reduce␈α→<TERM>␈α~to␈α→<EXP>
␈↓ α←␈↓associating␈αwhatever␈αS-expr␈αis␈αattached␈αto␈α<TERM>␈αwith␈αthat␈αoccurrence␈αof
␈↓ α←␈↓<EXP>.␈α∂In␈α∂the␈α⊂third␈α∂equation␈α∂we␈α∂assume␈α⊂that␈α∂<NUMBER>␈α∂is␈α⊂a␈α∂syntactic
␈↓ α←␈↓type␈α⊃recognized␈α⊃by␈α⊃the␈α⊃scanner,␈α∩and␈α⊃return␈α⊃that␈α⊃number␈α⊃as␈α∩the␈α⊃semantic
␈↓ α←␈↓value.␈α For␈αexample,␈αif␈αsuch␈αa␈αparser␈αwere␈αgiven␈α2+3+44␈αit␈αshould␈αreturn␈αthe
␈↓ α←␈↓list ␈↓α(PLUS (PLUS 2 3) 44)␈↓.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αunparser␈α
uses␈αthese␈α
equations␈αin␈αthe␈α
inverse␈αmanner.␈α
It␈αwill␈α
see␈αa
␈↓ α←␈↓S-expr␈α∂and␈α∂will␈α∂attempt␈α∂to␈α∂match␈α∂that␈α∂to␈α∂the␈α∂description␈α∂of␈α⊂the␈α∂semantics,
␈↓ α←␈↓outputting an instance of the BNF if successful.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αSDIO␈αprogram␈αwill␈αtake␈αsuch␈αan␈αaugmented␈αset␈αof␈αBNF␈α
equations
␈↓ α←␈↓and␈α∩generate␈α∪a␈α∩parser␈α∪and␈α∩an␈α∪unparser␈α∩for␈α∪the␈α∩language.␈α∪ This␈α∩project
␈↓ α←␈↓involves␈αwriting␈αsuch␈αa␈αSDIO␈αprogram.␈αWe␈αdescribe␈αa␈αbasic␈αSDIO␈αprogram
␈↓ α←␈↓and suggest extensions and improvements.
␈↓"β␈↓ α←␈↓␈↓ β'The␈αbest␈α
way␈αto␈αdescribe␈α
the␈αformat␈αof␈α
SDIO␈αinput␈αis␈α
to␈αgive␈αan␈α
SDIO
␈↓ α←␈↓description.
␈↓ α←␈↓␈↓9.4␈↓ λπSyntax-directed I/O 423␈↓
␈↓"∀␈↓ α←␈↓<RULES>␈↓ ∧7::= END␈↓ ε{=>NIL
␈↓"β␈↓ α←␈↓␈↓ ∧7::=<RULE><RULES>␈↓ ε{=>(RULE . RULES)
␈↓" ␈↓ α←␈↓<RULE>␈↓ ∧7::= <LFPT><RTLST>␈↓ ε{=>(LFPT RTLST)
␈↓" ␈↓ α←␈↓<RTLST>␈↓ ∧7::= ::=<RTPT><SEXPR><RTLST>
␈↓"β␈↓ α←␈↓␈↓ ∧7␈↓ ε{=>((RTPT SEXPR) . RTLST)
␈↓"β␈↓ α←␈↓␈↓ ∧7::= ␈↓λε␈↓␈↓ ε{=>NIL
␈↓" ␈↓ α←␈↓<LFPT>␈↓ ∧7::= <<ID>>␈↓ ε{=>*
␈↓" ␈↓ α←␈↓<RTPT>␈↓ ∧7::= "=>␈↓ ε{=>NIL
␈↓"β␈↓ α←␈↓␈↓ ∧7::= <RPELEM><RTPT>
␈↓"β␈↓ α←␈↓␈↓ ∧7␈↓ ε{=>(RPELEM . RTPT)
␈↓" ␈↓ α←␈↓<RPELEM>␈↓ ∧7::= <<ID>>␈↓ ε{=>*
␈↓"β␈↓ α←␈↓␈↓ ∧7::= <ID>␈↓ ε{=>(SPWD ID)
␈↓"β␈↓ α←␈↓␈↓ ∧7::= ""<CHAR>␈↓ ε{=>(QCH CHAR)
␈↓"β␈↓ α←␈↓␈↓ ∧7::= <CHAR>␈↓ ε{=>(CH CHAR)
␈↓" ␈↓ α←␈↓<SEXPR>␈↓ ∧7::= <ATOM>␈↓ ε{=>*
␈↓"β␈↓ α←␈↓␈↓ ∧7::= (<SEXPRLIST>)␈↓ ε{=>*
␈↓" ␈↓ α←␈↓<SEXPRLIST>␈↓ ∧7::= <ATOM>␈↓ ε{=>*
␈↓"β␈↓ α←␈↓␈↓ ∧7::= <SEXPR> <SEXPRLIST>
␈↓"β␈↓ α←␈↓␈↓ ∧7␈↓ ε{=>(SEXPR . SEXPRLIST)
␈↓"β␈↓ α←␈↓␈↓ ∧7::= ␈↓λε␈↓␈↓ ε{=>NIL
␈↓"∀␈↓ α←␈↓END
␈↓"β␈↓ α←␈↓The␈α∞expressions␈α∞␈↓α(SPWD␈α∞ID),␈α∞(QCH␈α∞CHAR),␈↓␈α∞and␈α∞␈↓α(CH␈α∞CHAR)␈↓␈α∞are␈α
S-expr
␈↓ α←␈↓representations␈α⊃of␈α⊂calls␈α⊃on␈α⊃rountines␈α⊂to␈α⊃process␈α⊂special␈α⊃or␈α⊃reserved␈α⊂words,
␈↓ α←␈↓quoted characters or special characters, respectively.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∃input␈α∃to␈α∃SDIO␈α∃is␈α∃a␈α∃sequence␈α∃of␈α∃augmented␈α⊗BNF␈α∃equations
␈↓ α←␈↓terminated␈α~with␈α~END.␈α~ What␈α≠the␈α~SDIO␈α~program␈α~sees␈α~is␈α≠a␈α~S-expr
␈↓ α←␈↓representation␈α∞of␈α∂these␈α∞equations.␈α∂The␈α∞sample␈α∂equations␈α∞for␈α∂<EXP>␈α∞above
␈↓ α←␈↓would pass the following to the SDIO program:
␈↓"β␈↓ α←␈↓␈↓ βK(␈↓ ∧π(EXP (␈↓ ∧␈((EXP (CH +) TERM) (PLUS EXP TERM))
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ ∧π␈↓ ∧␈((TERM) NIL)))
␈↓"β␈↓ α←␈↓␈↓ βK␈↓ ∧π(TERM (␈↓ ∧␈((NUMBER) NIL))) )
␈↓"∀␈↓ α←␈↓The SDIO program generates the parser and unparser.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∩elements␈α∩of␈α∩the␈α∩BNF␈α∩equations␈α∩in␈α∩SDIO␈α∩are␈α∩rather␈α∩standard:
␈↓ α←␈↓syntactic␈α∪variables,␈α∀which␈α∪are␈α∀identifiers␈α∪bracketed␈α∪by␈α∀"<"␈α∪and␈α∀">";␈α∪and
␈↓ α←␈↓special␈α⊗words,␈α⊗which␈α⊗are␈α↔identifiers;␈α⊗and␈α⊗special␈α⊗characters,␈α↔which␈α⊗are
␈↓ α←␈↓preceeded by " if they conflict with the special characters of the BNF.
␈↓"β␈↓ α←␈↓␈↓ β'The␈α∀elements␈α∀of␈α∀the␈α∪semantics␈α∀are:␈α∀unbracketed␈α∀syntactic␈α∪variables
␈↓ α←␈↓occurring␈α∩in␈α∩the␈α∩RHS␈α∩of␈α∩the␈α∩associated␈α∩BNF␈α∩equation;␈α∩other␈α⊃identifiers,
␈↓ α←␈↓taken␈α∃as␈α∀constants;␈α∃NIL,␈α∃the␈α∀LISP␈α∃atom;␈α∀notation␈α∃for␈α∃␈↓αcons␈↓-ing,␈α∀␈↓α( . )␈↓;
␈↓ α←␈↓notation for making a list, ␈↓α(e␈↓β1␈↓α ... e␈↓βn␈↓α)␈↓; the character *, described above.
␈↓ α←␈↓␈↓424 Projects␈↓
(9.4␈↓
␈↓"β␈↓ α←␈↓␈↓ ε&␈↓↓Project␈↓
␈↓"→␈↓ α←␈↓Write␈αsuch␈α
a␈αSDIO␈α
program.␈αYou␈αshould␈α
consult␈αlocal␈α
LISP␈αdocumentation
␈↓ α←␈↓when building the basic I/O routines like <NUMBER>, <CHAR>, or <ID>.
␈↓"∪␈↓ α←␈↓␈↓ ¬o␈↓↓First Extension␈↓
␈↓"→␈↓ α←␈↓You␈α∃may␈α∃have␈α∃noticed␈α∃already␈α∃that␈α∃the␈α∃basic␈α∃SDIO␈α∃program␈α⊗fails␈α∃to
␈↓ α←␈↓distinguish␈αtwo␈αoccurrences␈αof␈αthe␈αsame␈αsyntactic␈αvariable␈αon␈αthe␈αRHS␈αof␈αan
␈↓ α←␈↓equation. Thus an equation like:
␈↓"∀␈↓ α←␈↓<ZIP>␈↓ βW::= <ZAP> <FOO> <ZAP> must be replaced by the pair:
␈↓" ␈↓ α←␈↓<ZIP>␈↓ βW::= <ZAP> <FOO> <ZAP1>
␈↓" ␈↓ α←␈↓<ZAP1>␈↓ βW::=<ZAP>
␈↓"∀␈↓ α←␈↓This␈αtrick␈αis␈αcalled␈αstratification.␈αIt␈αis␈αa␈αsyntactic␈αtrick,␈αadding␈αnothing␈αto␈αthe
␈↓ α←␈↓semantics.
␈↓"β␈↓ α←␈↓␈↓ β'Add␈α
notation␈α
to␈αthe␈α
semantics␈α
of␈α
your␈αSDIO␈α
program␈α
to␈α
handle␈αRHS
␈↓ α←␈↓with␈α~multiple␈α~occurrences␈α≠of␈α~syntactic␈α~variables.␈α~Modify␈α≠your␈α~parser
␈↓ α←␈↓generators accordingly.
␈↓"∪␈↓ α←␈↓␈↓ ¬`␈↓↓Second Extension␈↓
␈↓"→␈↓ α←␈↓Besides␈α∃building␈α∃a␈α∃S-expr␈α∃representation␈α∃of␈α∃the␈α∃input,␈α∃it␈α⊗is␈α∃frequently
␈↓ α←␈↓desirable␈α∪to␈α∩generate␈α∪other␈α∪information␈α∩during␈α∪the␈α∩input␈α∪parse.␈α∪Lists␈α∩of
␈↓ α←␈↓occurrences␈α
of␈α
operators␈α
or␈α
other␈α
tables␈α
are␈α
commonly␈α
needed.␈α
The␈α
additional
␈↓ α←␈↓information␈αcould␈αbe␈αdiscovered␈αby␈αexamination␈αof␈αthe␈αcompleted␈αparse␈αtree,
␈↓ α←␈↓but␈α
that␈αrequires␈α
reexamination␈α
of␈αthe␈α
tree.␈α
It␈αis␈α
much␈α
more␈αefficient␈α
to␈αdo␈α
as
␈↓ α←␈↓much as possible on a single pass.
␈↓"β␈↓ α←␈↓␈↓ β'Introduce␈α
notation␈α
which␈α
will␈αallow␈α
execution␈α
of␈α
arbitrary␈α
LISP␈αcode␈α
as
␈↓ α←␈↓the␈α⊂parse␈α⊂progresses.␈α⊂That␈α⊂code␈α⊂should␈α⊂be␈α⊂able␈α⊂to␈α⊂manipulate␈α⊂any␈α⊃of␈α⊂the
␈↓ α←␈↓semantic␈α∞properties␈α∞associated␈α∞with␈α∞the␈α∞syntactic␈α∞variables␈α∞appearing␈α∞in␈α
the
␈↓ α←␈↓RHS of the associated syntax equation.
␈↓"∪␈↓ α←␈↓␈↓ ¬k␈↓↓Third extension␈↓
␈↓"→␈↓ α←␈↓While␈α∀it␈α∀is␈α∀obviously␈α∀advantageous␈α∀to␈α∀produce␈α∀output␈α∀in␈α∀the␈α∀language
␈↓ α←␈↓described␈αby␈α
the␈αBNF␈αequations␈α
rather␈αthan␈αthe␈α
S-expr␈αform,␈α
formatting␈αof
␈↓ α←␈↓the␈α∞output␈α∞can␈α∞be␈α∞equally␈α∞beneficial.␈α∂ We␈α∞should␈α∞like␈α∞to␈α∞be␈α∞able␈α∂to␈α∞specify
␈↓ α←␈↓formatting␈α∃information␈α∃in␈α∀SDIO␈α∃such␈α∃that␈α∀spacing␈α∃and␈α∃line-length␈α∀are
␈↓ α←␈↓controlled.
␈↓"β␈↓ α←␈↓␈↓ β'One␈α∞proposal␈α∂is␈α∞to␈α∂embed␈α∞spacing␈α∞and␈α∂line-feed␈α∞control␈α∂characters␈α∞in
␈↓ α←␈↓the␈α⊂BNF␈α∂equations.␈α⊂The␈α∂spacing␈α⊂character␈α∂is␈α⊂"→"␈α∂and␈α⊂the␈α∂line-feed␈α⊂is␈α∂"↓".
␈↓ α←␈↓The␈α∂"↑"␈α∂sets␈α∂the␈α∂indentation␈α∂point␈α∂for␈α∂the␈α∂string␈α∂on␈α∂its␈α∂right;␈α∂and␈α∂the␈α∂"→"
␈↓ α←␈↓followed␈α⊂by␈α⊂a␈α⊂digit␈α⊂says␈α⊂space␈α⊃over␈α⊂than␈α⊂number␈α⊂of␈α⊂spaces␈α⊂from␈α⊃the␈α⊂last
␈↓ α←␈↓␈↓9.4␈↓ λπSyntax-directed I/O 425␈↓
␈↓"β␈↓ α←␈↓indentation␈α∩point␈α∩if␈α⊃the␈α∩remaining␈α∩space␈α⊃on␈α∩the␈α∩line␈α⊃is␈α∩not␈α∩sufficient␈α⊃to
␈↓ α←␈↓contain␈α∩all␈α∪text␈α∩specified␈α∩by␈α∪the␈α∩remaining␈α∩RHS␈α∪of␈α∩the␈α∪equation.␈α∩ "0→",
␈↓ α←␈↓meaning go to the indentation point can be written "→".
␈↓"β␈↓ α←␈↓For example consider the following:
␈↓"∀␈↓ α←␈↓␈↓ βo<EXPR>␈↓ ¬S::= <ID>( ↓ <EXPR_LIST>)
␈↓"β␈↓ α←␈↓␈↓ βo␈↓ ¬S::= <ID>
␈↓" ␈↓ α←␈↓␈↓ βo<EXPR_LIST>␈↓ ¬S::= ↑<EXPR> , →<EXPR_LIST>
␈↓"β␈↓ α←␈↓␈↓ βo␈↓ ¬S::= ↑<EXPR>
␈↓"∀␈↓ α←␈↓These equations, when used to drive an unparser, could give:
␈↓"∀␈↓ α←␈↓α␈↓ βKmumf(␈↓ ∧+a,
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧+foobaz(garp(b)),
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧+bletch(a,b,c),
␈↓"β␈↓ α←␈↓α␈↓ βK␈↓ ∧+d)
␈↓"β␈↓ α←␈↓as the formatted version of:
␈↓"∀␈↓ α←␈↓α␈↓ ∧hmumf(a,foobaz(garp(b)),bletch(a,b,c),d)
␈↓"∀␈↓ α←␈↓␈↓ β'Extend SDIO to handle formatting of output.
␈↓ α←␈↓␈↓426 BIBLIOGRPAHY␈↓
O␈↓
␈↓"β␈↓ α←␈↓␈↓ β'␈↓ Bibliography␈↓
␈↓"β␈↓ α←␈↓The basic form of an entry consists of three items:
␈↓"β␈↓ α←␈↓␈↓ β≠␈↓↓1.␈↓ A short name which is how the document is referenced in the text.
␈↓" ␈↓ α←␈↓␈↓ β≠␈↓↓2.␈↓ The full bibliographical reference.
␈↓" ␈↓ α←␈↓␈↓ β≠␈↓↓3.␈↓␈α
A␈αsequence␈α
of␈α
pages␈αin␈α
the␈α
text␈αwhich␈α
refer␈α
to␈αthis␈α
document.␈α
If␈αthe
␈↓ α←␈↓␈↓ β≠document␈α∪is␈α∩not␈α∪referenced␈α∪the␈α∩statement␈α∪␈↓↓[ norefs ]␈↓␈α∪appears␈α∩instead;
␈↓ α←␈↓␈↓ β≠these␈α∀documents,␈α∀while␈α∀not␈α∀referenced,␈α∀are␈α∀relevant␈α∀to␈α∃the␈α∀material
␈↓ α←␈↓␈↓ β≠covered in the text.
␈↓"∀␈↓ α←␈↓[Aho 72]␈↓ ∧πAho,␈αA.,␈αand␈αUllman,␈α
J.,␈α␈↓αThe␈αTheory␈αof␈αParsing,␈α
Translation␈α&
␈↓ α←␈↓α␈↓ ∧∪Compiling␈↓,␈α"Prentice␈α"Hall␈α!Inc.,␈α"Englewood␈α"Cliffs,␈α!N.J.,
␈↓ α←␈↓␈↓ ∧∪1972. ␈↓↓[␈↓␈↓↓ 4␈↓␈↓↓ 165␈↓␈↓↓ 307␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Alg 63]␈↓ ∧πNaur,␈α∞P.␈α∞et.␈α∞al.,␈α∂`Revised␈α∞Report␈α∞on␈α∞the␈α∂Algorithmic␈α∞Language
␈↓ α←␈↓␈↓ ∧∪Algol 60', ␈↓αComm. ACM 6␈↓, ␈↓↓1␈↓, Jan., 1963. ␈↓↓[␈↓␈↓↓ 164␈↓␈↓↓ 357␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Alg 75]␈↓ ∧πvan␈α⊗Wijngaarden,␈α∃A.,␈α⊗et.␈α∃al.,␈α⊗eds.,␈α∃`Revised␈α⊗Report␈α⊗on␈α∃the
␈↓ α←␈↓␈↓ ∧∪Algorithmic␈α~Language␈α~Algol 68',␈α~␈↓αActa␈α~Informatica␈↓,␈α~Vol. 5,
␈↓ α←␈↓␈↓ ∧∪Fac. 1-3, p. 1-236, 1975. ␈↓↓[␈↓␈↓↓ 163␈↓␈↓↓ 281␈↓␈↓↓ 357␈↓␈↓↓ 382␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Ama 72]␈↓ ∧πAmarel,␈α∞S.,␈α∂`A␈α∞Set␈α∂of␈α∞Goals␈α∞and␈α∂Approaches␈α∞for␈α∂Education␈α∞in
␈↓ α←␈↓␈↓ ∧∪Computer␈α∃Science',␈α∃AFIPS␈α∃Conference␈α∃Proceedings,␈α∃Vol 40,
␈↓ α←␈↓␈↓ ∧∪p. 841-846, 1972. ␈↓↓[␈↓␈↓↓ 157␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[And 76a]␈↓ ∧πAnderson,␈α~D.␈α~Bruce,␈α~`The␈α~Samefringe␈α~Problem',␈α~␈↓αSIGART
␈↓ α←␈↓α␈↓ ∧∪Newsletter␈↓, No. 60, p. 4, Nov. 1976. ␈↓↓[␈↓␈↓↓ 223␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[And 76]␈↓ ∧πAnderson,␈α⊂D.␈α⊃Bruce,␈α⊂`A␈α⊃Brief␈α⊂Critique␈α⊃of␈α⊂LISP',␈α⊃Proc.␈α⊂AISB
␈↓ α←␈↓␈↓ ∧∪Summer Conference, Edinburgh, p. 14-25, 1976. ␈↓↓[␈↓␈↓↓ 229␈↓␈↓↓ 385␈↓␈↓↓ ]␈↓
␈↓ α←␈↓␈↓␈↓ λHBIBLIOGRPAHY 427␈↓
␈↓"β␈↓ α←␈↓[Bac 73]␈↓ ∧πBackus,␈α_J.,␈α_`Programming␈α↔Language␈α_Semantics␈α_and␈α↔Closed
␈↓ α←␈↓␈↓ ∧∪Applicative␈α
Languages',␈α
IBM␈α
Research␈α
Lab,␈α
RJ-1245,␈αSan␈α
Jose,
␈↓ α←␈↓␈↓ ∧∪Cal., July 1973. ␈↓↓[␈↓␈↓↓ 156␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Bar 66]␈↓ ∧πBarron,␈α∩D.,␈α∩and␈α∩Strachey,␈α∩C.,␈α∩`Programming',␈α∩in␈α∩␈↓αAdvances␈α⊃in
␈↓ α←␈↓α␈↓ ∧∪Computer␈α Programming␈α and␈α Non-numerical␈α∨Computation␈↓,
␈↓ α←␈↓␈↓ ∧∪ L. Fox, ed., Academic Press, New York, p. 49-82, 1966. ␈↓↓[␈↓␈↓↓ 196␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Bar 71]␈↓ ∧πBarbacci,␈α∂M.,␈α∂et␈α∂al.,␈α⊂`C.ai (P.LISP),␈α∂A␈α∂LISP␈α∂processor␈α⊂of␈α∂C.ai',
␈↓ α←␈↓␈↓ ∧∪Carnegie-Mellon U., 1971. ␈↓↓[␈↓␈↓↓ 241␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Bau 72]␈↓ ∧πBaumgart,␈α≡B.,␈α≡`MICRO-PLANNER␈α∨Alternative␈α≡Reference
␈↓ α←␈↓␈↓ ∧∪Manual',␈α≥Stanford␈α≥Univ.␈α≥Operating␈α≥Note␈α≥No. 67,␈α≥April
␈↓ α←␈↓␈↓ ∧∪1972. ␈↓↓[␈↓␈↓↓ 73␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Ber 64]␈↓ ∧πBerkeley,␈αE.,␈αand␈αBobrow,␈αD.,␈αeds.,␈α␈↓αThe␈αProgramming␈αLanguage
␈↓ α←␈↓α␈↓ ∧∪LISP: Its␈α5Operation␈α5and␈α5Applications␈↓,␈α4Information
␈↓ α←␈↓␈↓ ∧∪International, Cambridge, Mass., 1964. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Ber 71]␈↓ ∧πBerkling,␈αH.,␈α`A␈αComputing␈α
Machine␈αBased␈αon␈αTree␈α
Structures',
␈↓ α←␈↓␈↓ ∧∪␈↓αIEEE␈α)Trans␈α(on␈α)Comptr.␈↓␈α(20,␈α)C-20,␈α)␈↓↓4␈↓,␈α(p. 404-418,
␈↓ α←␈↓␈↓ ∧∪April 1971. ␈↓↓[␈↓␈↓↓ 248␈↓␈↓↓ 374␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Ber 75]␈↓ ∧πBerkling,H.,␈α_`A␈α↔Recursive␈α_Control␈α↔Structure␈α_for␈α↔Processing
␈↓ α←␈↓␈↓ ∧∪Reduction␈α"Machine␈α"Languages',␈α"Micro␈α#Architecture␈α"of
␈↓ α←␈↓␈↓ ∧∪Computer␈α⊗Systems,␈α⊗Hartenstein,␈α∃R.,␈α⊗&␈α⊗Zaks,R.,␈α⊗eds.,␈α∃North
␈↓ α←␈↓␈↓ ∧∪Holland, p. 191-200, 1975. ␈↓↓[␈↓␈↓↓ 81␈↓␈↓↓ 108␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Berr 71]␈↓ ∧πBerry,␈α
D.,␈α∞`Block␈α
Structure: Retention␈α
or␈α∞Deletion?',␈α
Proc.␈α∞of␈α
3␈↓πrd␈↓
␈↓ α←␈↓␈↓ ∧∪Annual␈αACM␈αSymposium␈αon␈αTheory␈αof␈αComputing,␈αp. 86-100,
␈↓ α←␈↓␈↓ ∧∪1971. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Bis 74a]␈↓ ∧πBishop,␈αP.,␈α
`Garbage␈αCollection␈α
in␈αa␈α
Very␈αLarge␈αAddress␈α
Space',
␈↓ α←␈↓␈↓ ∧∪M.I.T. A.I. Lab, Working paper 111, Sep. 1975. ␈↓↓[␈↓␈↓↓ 402␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Bis 74]␈↓ ∧πBishop,␈α≠P.,␈α≠`Spaghetti␈α≠Stacks',␈α≠unpublished␈α≤paper,␈α≠M.I.T.,
␈↓ α←␈↓␈↓ ∧∪Dec 19, 1974. ␈↓↓[␈↓␈↓↓ 291␈↓␈↓↓ 314␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Bla 71]␈↓ ∧πBlair,␈α∩F.,␈α∩`The␈α∩Structure␈α∩of␈α∩the␈α∩Lisp␈α∪Compiler',␈α∩unpublished
␈↓ α←␈↓␈↓ ∧∪paper, 1971. ␈↓↓[␈↓␈↓↓ 219␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Bob 67]␈↓ ∧πBobrow,␈α
D.,␈αand␈α
Murphy,␈α
D.,␈α`The␈α
Structure␈αof␈α
a␈α
LISP␈αSystem
␈↓ α←␈↓␈↓ ∧∪Using␈α∨Two-level␈α∨Storage',␈α∨␈↓αComm. ACM 10␈↓,␈α∨␈↓↓3␈↓,␈α≡p. 155-159,
␈↓ α←␈↓␈↓ ∧∪Mar. 1967. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Bob 69]␈↓ ∧πBobrow,␈α∪D.,␈α∀`(LISP␈α∪Bulletin)',␈α∀␈↓αACM␈α∪Sigplan␈α∀Notices␈↓,␈α∪Vol. 1,
␈↓ α←␈↓␈↓ ∧∪No. 9, p. 17-45, Sept. 1969. ␈↓↓[␈↓␈↓↓ 194␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Bob 73a]␈↓ ∧πBobrow,␈α!D.,␈α and␈α!Wegbreit,␈α B.,␈α!`A␈α Model␈α!and␈α Stack
␈↓ α←␈↓␈↓ ∧∪Implementation␈αof␈αMultiple␈αEnvironments',␈α
␈↓αComm. ACM 16␈↓,␈α␈↓↓10␈↓,
␈↓ α←␈↓␈↓ ∧∪p. 591-603, Oct. 1973. ␈↓↓[␈↓␈↓↓ 229␈↓␈↓↓ 301␈↓␈↓↓ 354␈↓␈↓↓ 401␈↓␈↓↓ 405␈↓␈↓↓ ]␈↓
␈↓ α←␈↓␈↓428 BIBLIOGRAPHY␈↓
O␈↓
␈↓"β␈↓ α←␈↓[Bob 74]␈↓ ∧πBobrow,␈α
D.,␈α
and␈αRaphael,␈α
D.,␈α
`New␈α
Programming␈αLanguages␈α
for
␈↓ α←␈↓␈↓ ∧∪A.I.␈α&Research',␈α&␈↓αComputing␈α&Surveys,␈α&6␈↓,␈α&␈↓↓3␈↓,␈α&p. 154-174,
␈↓ α←␈↓␈↓ ∧∪Sep 1974. ␈↓↓[␈↓␈↓↓ 410␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Bob 75]␈↓ ∧πBobrow,␈α∃D.,␈α∃`A␈α∃Note␈α∀on␈α∃Hash␈α∃Linking',␈α∃␈↓αComm. ACM 18␈↓,␈α∀␈↓↓7␈↓,
␈↓ α←␈↓␈↓ ∧∪p. 413-414, July, 1975. ␈↓↓[␈↓␈↓↓ 403␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Boy 75]␈↓ ∧πBoyer,␈α→R.,␈α→and␈α→Moore,␈α_J,␈α→`Proving␈α→theorems␈α→about␈α_LISP
␈↓ α←␈↓␈↓ ∧∪functions', ␈↓αJour. ACM ␈↓, ␈↓↓1␈↓, p. 129-144, Jan. 1975. ␈↓↓[␈↓␈↓↓ 96␈↓␈↓↓ 160␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Bur 76]␈↓ ∧πBurge,␈αW.,␈α␈↓αRecursive␈α
Programming␈αTechniques␈↓,␈αAddison␈α
Wesley,
␈↓ α←␈↓␈↓ ∧∪Reading, Mass., 1976. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Car 75]␈↓ ∧πCarpenter,␈αR.,␈α&␈αDoran,␈α
R.,␈α␈↓αThe␈αOther␈αTuring␈α
Machine␈↓,␈αMassey
␈↓ α←␈↓␈↓ ∧∪University Computing Unit, Rep. No. 23 Aug 1975. ␈↓↓[␈↓␈↓↓ 239␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Car 76]␈↓ ∧πCartwright,␈α≥R.,␈α≤`A␈α≥Practical␈α≤and␈α≥Formal␈α≥Semantics␈α≤and
␈↓ α←␈↓␈↓ ∧∪Verification␈α%System␈α%for␈α%TYPED␈α%LISP',␈α$Ph.D. Thesis,
␈↓ α←␈↓␈↓ ∧∪Computer␈α?␈α⊗Science␈α?␈α⊗Dept.,␈α?␈α⊗Stanford␈α?␈α⊗Univ.,
␈↓ α←␈↓␈↓ ∧∪1976. ␈↓↓[␈↓␈↓↓ 96␈↓␈↓↓ 160␈↓␈↓↓ 242␈↓␈↓↓ 410␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Che 67]␈↓ ∧πCheatham,␈α≤T.,␈α≠␈↓αThe␈α≤Theory␈α≠&␈α≤Construction␈α≤of␈α≠Compilers␈↓,
␈↓ α←␈↓␈↓ ∧∪Computer Associates, Wakefield, Mass., 1967. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Che 70]␈↓ ∧πCheney,␈α→C.,␈α~`A␈α→Nonrecursive␈α→List␈α~Compacting␈α→Algorithm',
␈↓ α←␈↓␈↓ ∧∪␈↓αComm. ACM 13␈↓, ␈↓↓11␈↓, p. 677-678, Nov. 1970. ␈↓↓[␈↓␈↓↓ 401␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Che 76]␈↓ ∧πCheatham,␈α∞T.,␈α∞and␈α∞Townley,␈α∞J.,␈α∞`A␈α∞Look␈α∞at␈α∞Programming␈α∞and
␈↓ α←␈↓␈↓ ∧∪Programming␈α
Systems',␈α
in␈α
␈↓αAdvances␈α
in␈α
Computers␈↓,␈α
Vol.␈α∞14,␈α
M.
␈↓ α←␈↓␈↓ ∧∪Rubinoff,␈α∪and␈α∩M.␈α∪Yovits,␈α∪eds.,␈α∩Academic␈α∪Press,␈α∪New␈α∩York,
␈↓ α←␈↓␈↓ ∧∪p. 45-78, 1976. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Chr 68]␈↓ ∧πChristensen,␈α∞C.,␈α∞`An␈α∞Example␈α∞of␈α∞the␈α∞Manipulation␈α∞of␈α
Directed
␈↓ α←␈↓␈↓ ∧∪Graphs␈αin␈α
the␈αAMBIT/G␈α
Programming␈αLanguage',␈α423-435,␈α
in
␈↓ α←␈↓␈↓ ∧∪␈↓αInteractive␈α_Systems␈α↔for␈α_Experimental␈α_Applied␈α↔Mathematics␈↓,
␈↓ α←␈↓␈↓ ∧∪Klerer, M., & Reinfelds, J. eds., Academic Press, 1968. ␈↓↓[␈↓␈↓↓ 398␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Chu 41]␈↓ ∧πChurch,␈α→A.,␈α→␈↓αThe␈α~Calculi␈α→of␈α→Lambda-conversion␈↓,␈α~Annals␈α→of
␈↓ α←␈↓␈↓ ∧∪Mathematics␈α∂Studies,␈α∂Princeton␈α∂University␈α∂Press,␈α⊂New␈α∂Jersey,
␈↓ α←␈↓␈↓ ∧∪1941. ␈↓↓[␈↓␈↓↓ 110␈↓␈↓↓ 167␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Cl 76]␈↓ ∧πClark,␈α≠D.,␈α≤`List␈α≠Structure:␈α≠Measurements,␈α≤Algorithms␈α≠and
␈↓ α←␈↓␈↓ ∧∪Encodings',␈α
Ph.D.␈α
Thesis,␈α
Carnegie␈α
Mellon␈α∞University,␈α
August
␈↓ α←␈↓␈↓ ∧∪1976. ␈↓↓[␈↓␈↓↓ 377␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Cla 76]␈↓ ∧π401 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Con 73]␈↓ ∧πMcDermott,␈α
D.,␈α
and␈α
Sussman,␈α
G.,␈α
`The␈α
CONNIVER␈α
Reference
␈↓ α←␈↓␈↓ ∧∪Manual',␈α?␈α∧M.I.T.␈α?␈α¬A.I. Lab. Memo 259a,␈α?␈α∧Cambridge,
␈↓ α←␈↓␈↓ ∧∪Mass., 1973. ␈↓↓[␈↓␈↓↓ 74␈↓␈↓↓ 75␈↓␈↓↓ 219␈↓␈↓↓ 229␈↓␈↓↓ 301␈↓␈↓↓ 410␈↓␈↓↓ 411␈↓␈↓↓ 412␈↓␈↓↓ ]␈↓
␈↓ α←␈↓␈↓␈↓ λHBIBLIOGRPAHY 429␈↓
␈↓"β␈↓ α←␈↓[Con 74]␈↓ ∧πConrad,␈α∪W.,␈α∪`A␈α∪Compactifying␈α∪Garbage␈α∪Collector␈α∪for␈α∪ECL's
␈↓ α←␈↓␈↓ ∧∪Non-homogeneous␈α⊂Heap',␈α⊂Center␈α⊂for␈α⊂Research␈α⊃in␈α⊂Computing
␈↓ α←␈↓␈↓ ∧∪Technology, TR. 2-74, Harvard, Feb. 1974. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Cur 58]␈↓ ∧πCurry,␈α∀H,␈α∪and␈α∀Feys,␈α∪R.,␈α∀Combinatory␈α∀Logic,␈α∪North-Holland,
␈↓ α←␈↓␈↓ ∧∪1958. ␈↓↓[␈↓␈↓↓ 173␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Dar 73]␈↓ ∧πDarlington,␈α
J.,␈α
and␈α
Burstall,␈αR.,␈α
`A␈α
System␈α
which␈αAutomatically
␈↓ α←␈↓␈↓ ∧∪Improves␈α∂Programs',␈α∂Proc.␈α∂3␈↓πrd␈↓␈α∞Int.␈α∂J.␈α∂Conf.␈α∂on␈α∂A.I.,␈α∞Stanford,
␈↓ α←␈↓␈↓ ∧∪1973. ␈↓↓[␈↓␈↓↓ 160␈↓␈↓↓ 353␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Dav 76]␈↓ ∧πDavis,␈α∩R.,␈α∩`Deduction,␈α∩Truth,␈α∩and␈α∩Computation',␈α∩M.S. Thesis,
␈↓ α←␈↓␈↓ ∧∪Math. Dept., San Jose State University, 1976. ␈↓↓[␈↓␈↓↓ 162␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[DEC 69]␈↓ ∧π`PDP-10␈α!Systems␈α!Reference␈α!Manual',␈α!Digital␈α Equipment
␈↓ α←␈↓␈↓ ∧∪Corporation, Maynard Mass., 1969. ␈↓↓[␈↓␈↓↓ 312␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Deu 73]␈↓ ∧πDeutsch,␈α∞P.,␈α
`A␈α∞LISP␈α
Machine␈α∞With␈α
Very␈α∞Compact␈α
Programs',
␈↓ α←␈↓␈↓ ∧∪Proc. 3␈↓πrd␈↓ Int. J. Conf. on A.I., Stanford, 1973. ␈↓↓[␈↓␈↓↓ 241␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Dif 71]␈↓ ∧πDiffie,␈αW.,␈α`Documentation␈αof␈αthe␈αCompiler',␈αunpublished␈α
paper,
␈↓ α←␈↓␈↓ ∧∪Stanford A.I. Lab., 1971. ␈↓↓[␈↓␈↓↓ 265␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Dij 72a]␈↓ ∧πDijkstra,␈α∀E.,␈α∪`The␈α∀Humble␈α∪Programmer:␈α∀1972␈α∀ACM␈α∪Turing
␈↓ α←␈↓␈↓ ∧∪Lecture', ␈↓αComm. ACM 15␈↓, ␈↓↓10␈↓, p. 859-866, Oct 1972. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Dij 72]␈↓ ∧πDijkstra,␈α∞E.,␈α∂`Notes␈α∞on␈α∂Structured␈α∞Programming',␈α∂in␈α∞␈↓αStructured
␈↓ α←␈↓α␈↓ ∧∪Programming␈↓,␈α∂Dahl,␈α∂O.,␈α∂and␈α∞Hoare,␈α∂C.,␈α∂eds.,␈α∂Academic␈α∞Press,
␈↓ α←␈↓␈↓ ∧∪New York, 1972. ␈↓↓[␈↓␈↓↓ 53␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Dor 76]␈↓ ∧πDoran,␈α∩R.,␈α∩␈↓αArchitecture␈α∪of␈α∩General␈α∩Purpose␈α∩Computers␈↓,␈α∪to␈α∩be
␈↓ α←␈↓␈↓ ∧∪published by Academic Press, New York. ␈↓↓[␈↓␈↓↓ 236␈↓␈↓↓ 317␈↓␈↓↓ 372␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[DSIPL]␈↓ ∧πProceedings␈α#of␈α$a␈α#Symposium␈α$on␈α#Data␈α$Structures␈α#in
␈↓ α←␈↓␈↓ ∧∪Programming␈α.Languages,␈α.␈↓αSIGPLAN␈α.Notices␈α.6␈↓,␈α.␈↓↓2␈↓,
␈↓ α←␈↓␈↓ ∧∪Feb. 1971. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[EL1 74]␈↓ ∧πWegbreit,␈αB.,␈α`ECL␈αProgrammer's␈αManual',␈αCenter␈αfor␈αResearch
␈↓ α←␈↓␈↓ ∧∪in␈α(Computing␈α'Technology,␈α(TR 23-74,␈α(Harvard,␈α'Dec.
␈↓ α←␈↓␈↓ ∧∪1974. ␈↓↓[␈↓␈↓↓ 265␈↓␈↓↓ 382␈↓␈↓↓ 407␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Els 73]␈↓ ∧πElson,␈αM.,␈α␈↓αConcepts␈αof␈αProgranguages␈↓,␈αSRA␈αInc,␈αPalo␈αAlto,␈αCal,
␈↓ α←␈↓␈↓ ∧∪1973. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Fat 73]␈↓ ∧πFateman,␈α∞R.,␈α
`Reply␈α∞to␈α∞an␈α
Editorial',␈α∞␈↓αSIGSAM␈α∞Bulletin␈↓,␈α
No. 25,
␈↓ α←␈↓␈↓ ∧∪p. 9-11, Mar. 1973. ␈↓↓[␈↓␈↓↓ 303␈↓␈↓↓ 390␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Fel 68]␈↓ ∧πFeldman,␈α~J.,␈α~and␈α~Gries,␈α~D.,␈α~`Translator␈α~Writing␈α→Systems',
␈↓ α←␈↓␈↓ ∧∪␈↓αComm. ACM 11␈↓, ␈↓↓2␈↓, p. 77-113, Feb. 1968. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Fen 69]␈↓ ∧πFenichel,␈α
R.,␈α
and␈α
Yochelson,␈α∞J.,␈α
`A␈α
LISP␈α
Garbage␈α∞Collector␈α
for
␈↓ α←␈↓␈↓ ∧∪Virtual-memory␈α%Computer␈α%Systems',␈α%␈↓αComm. ACM 12␈↓,␈α$␈↓↓11␈↓,
␈↓ α←␈↓␈↓430 BIBLIOGRAPHY␈↓
O␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧∪p. 611-612, Nov. 1969. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Fin 76]␈↓ ∧πFinin,␈α∞T.,␈α∞and␈α∞Rutter,␈α∂P.,␈α∞`Different␈α∞Fringe␈α∞for␈α∂Different␈α∞Folk',
␈↓ α←␈↓␈↓ ∧∪␈↓αSIGART Newsletter␈↓, No. 60, p. 4-5, Nov. 1976. ␈↓↓[␈↓␈↓↓ 223␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Fis 70]␈↓ ∧πFisher,␈α∀D.,␈α∀`Control␈α∀Structures␈α∀for␈α∀Programming␈α∀Languages',
␈↓ α←␈↓␈↓ ∧∪Ph.D. Thesis,␈α↔Dept.␈α_of␈α↔Computer␈α_Science,␈α↔Carnegie-Mellon
␈↓ α←␈↓␈↓ ∧∪University, 1970. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Fis 72]␈↓ ∧πFischer,␈α↔M.,␈α_`Lambda␈α↔Calculus␈α_Schemata',␈α↔ACM␈α_Conf.␈α↔on
␈↓ α←␈↓␈↓ ∧∪Proving␈α assertions␈α about␈α programs,␈α!␈↓αSIGPLAN␈α Notices␈↓,
␈↓ α←␈↓␈↓ ∧∪p. 104-109, Jan. 1972. ␈↓↓[␈↓␈↓↓ 207␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Fri 74]␈↓ ∧πFriedman,␈α!D.,␈α"␈↓αThe␈α!Little␈α"LISPer␈↓,␈α!SRA,␈α"Menlo␈α!Park,
␈↓ α←␈↓␈↓ ∧∪1974. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Fri 76a]␈↓ ∧πFriedman,␈α⊂D.,␈α⊂and␈α⊂Wise,␈α⊂D.,␈α⊂`CONS␈α⊂Should␈α⊂Not␈α⊂Evaluate␈α∂its
␈↓ α←␈↓␈↓ ∧∪Arguments',␈αProc.␈α3␈↓πrd␈↓␈αInt.␈αColloq.␈αon␈αAutomata,␈αLanguages␈α
and
␈↓ α←␈↓␈↓ ∧∪Programming,␈α9Edinburgh␈α9Univ.␈α9Press,␈α9p.257-284,
␈↓ α←␈↓␈↓ ∧∪July 1976. ␈↓↓[␈↓␈↓↓ 12␈↓␈↓↓ 221␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Fri 76b]␈↓ ∧πFriedman,␈α&D.,␈α&and␈α&Wise,␈α&D.,␈α&`An␈α&Environment␈α&for
␈↓ α←␈↓␈↓ ∧∪Multiple-valued␈α∃Recursive␈α∀Procedures',␈α∃2␈↓πme␈↓␈α∀Colloque␈α∃sur␈α∀la
␈↓ α←␈↓␈↓ ∧∪Programation, Paris, Springer Verlag, Berlin, 1976. ␈↓↓[␈↓␈↓↓ 222␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Gol 73]␈↓ ∧πGoldstein,␈α≡I.,␈α≥`Pretty␈α≡Printing: Converting␈α≥List␈α≡to␈α≥Linear
␈↓ α←␈↓␈↓ ∧∪Structure', M.I.T. A.I. Lab, Memo 279, Feb. 1973. ␈↓↓[␈↓␈↓↓ 415␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Goo 57]␈↓ ∧πGoodstein,␈α⊃R.,␈α⊃␈↓αRecursive␈α⊃Number␈α⊃Theory␈↓,␈α∩North-Holland␈α⊃Pub.
␈↓ α←␈↓␈↓ ∧∪Co., Amsterdam, 1957. ␈↓↓[␈↓␈↓↓ 2␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Gor 73]␈↓ ∧πGordon,␈α≤M.,␈α≠`Models␈α≤of␈α≠Pure␈α≤LISP',␈α≠Dept.␈α≤of␈α≠Machine
␈↓ α←␈↓␈↓ ∧∪Intelligence,␈α/Experimental␈α0programming␈α/reports: No.30,
␈↓ α←␈↓␈↓ ∧∪University of Edinburgh, 1973. ␈↓↓[␈↓␈↓↓ 175␈↓␈↓↓ 177␈↓␈↓↓ 180␈↓␈↓↓ 235␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Gor 75]␈↓ ∧πGordon,␈αM.,␈α`Towards␈αa␈αSemantic␈αTheory␈αof␈αDynamic␈αBinding',
␈↓ α←␈↓␈↓ ∧∪Stanford␈α+A.I.␈α+Lab.␈α+Memo 265,␈α+Stanford␈α*University,
␈↓ α←␈↓␈↓ ∧∪1975. ␈↓↓[␈↓␈↓↓ 175␈↓␈↓↓ 180␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Got 74]␈↓ ∧πGoto,␈α
E.,␈α
`Monocopy␈α
and␈α
Associative␈α
Algorithms␈α
in␈α
an␈α
Extended
␈↓ α←␈↓␈↓ ∧∪Lisp',␈α?␈α
University␈α?␈α
of␈α?␈α
Tokyo,␈α?␈αJapan,␈α?␈α
May
␈↓ α←␈↓␈↓ ∧∪1974. ␈↓↓[␈↓␈↓↓ 46␈↓␈↓↓ 241␈↓␈↓↓ 287␈↓␈↓↓ 403␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Got 76]␈↓ ∧πGoto,␈α⊂E.,␈α∂and␈α⊂Kanada,␈α⊂Y.,␈α∂`Recursive␈α⊂Hashed␈α⊂Data␈α∂Structures
␈↓ α←␈↓␈↓ ∧∪with␈α∂Applications␈α∂to␈α∂Polynomial␈α∂Manipulations',␈α∂submitted␈α∞to
␈↓ α←␈↓␈↓ ∧∪SYMSAC 76. ␈↓↓[␈↓␈↓↓ 79␈↓␈↓↓ 403␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Gre 74]␈↓ ∧πGreenblatt,␈α∪R.,␈α∪`The␈α∀LISP␈α∪Machine',␈α∪M.I.T.,␈α∀Working␈α∪paper
␈↓ α←␈↓␈↓ ∧∪No. 79, Nov. 1974. ␈↓↓[␈↓␈↓↓ 241␈↓␈↓↓ 301␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Gre 75]␈↓ ∧πGreussay,␈α∪P.,␈α∩`Manuel␈α∪de␈α∪Reference␈α∩Provisoire: LISP T 1600',
␈↓ α←␈↓␈↓ ∧∪Universite Paris-Vincennes, Feb. 1975. ␈↓↓[␈↓␈↓↓ 219␈↓␈↓↓ ]␈↓
␈↓ α←␈↓␈↓␈↓ λHBIBLIOGRPAHY 431␈↓
␈↓"β␈↓ α←␈↓[Gre 76a]␈↓ ∧πGreussay,␈α∩P.,␈α∪`Iterative␈α∩Interpretation␈α∩of␈α∪Tail-Recursive␈α∩LISP
␈↓ α←␈↓␈↓ ∧∪Procedures',␈α"University␈α"of␈α"Vincennes,␈α#TR-20-76,␈α"Paris,
␈↓ α←␈↓␈↓ ∧∪Sep. 1976. ␈↓↓[␈↓␈↓↓ 230␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Gre 76]␈↓ ∧πGreussay,␈α∩P.,␈α∩`An␈α∪Iterative␈α∩LISP␈α∩Solution␈α∩to␈α∪the␈α∩Samefringe
␈↓ α←␈↓␈↓ ∧∪Problem', ␈↓αSIGART Newsletter␈↓, No. 59, p. 14, Aug. 1976. ␈↓↓[␈↓␈↓↓ 223␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Gri 71]␈↓ ∧πGries,␈α⊂D.,␈α⊂␈↓αCompiler␈α⊂Construction␈α⊂for␈α⊂Digital␈α⊂Computers␈↓,␈α∂Wiley,
␈↓ α←␈↓␈↓ ∧∪New York, 1971. ␈↓↓[␈↓␈↓↓ 278␈↓␈↓↓ 307␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Gua 69]␈↓ ∧πGuard,␈α≥J.,␈α≥Bennet,␈α≥J.,␈α≥and␈α≥Settle,␈α≥L.,␈α≥`Semi␈α≤Automated
␈↓ α←␈↓␈↓ ∧∪Mathematics', ␈↓αJACM 16␈↓, ␈↓↓1␈↓, p. 49-62, Jan. 1969. ␈↓↓[␈↓␈↓↓ 247␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Gun 76]␈↓ ∧πGunji,␈α
T.,␈α
`Analysis␈α
of␈α
Hash␈α
Addressing␈α
Methods',␈αDepartment
␈↓ α←␈↓␈↓ ∧∪of␈α
Information␈α
Sciences,␈α
TR 76-03,␈α
Univ.␈α
of␈α
Tokyo,␈αJapan,␈α
Jan.
␈↓ α←␈↓␈↓ ∧∪1976. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Ham 69]␈↓ ∧πHammer,␈α⊂M.,␈α∂`Formal␈α⊂Definition␈α∂of␈α⊂BASEL',␈α⊂Mass.␈α∂Computer
␈↓ α←␈↓␈↓ ∧∪Associates,␈α:Inc.,␈α:CA-6908-1511,␈α;Wakefield,␈α:Mass.,
␈↓ α←␈↓␈↓ ∧∪1969. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Han 69]␈↓ ∧πHansen,␈α∃W.,␈α∃`The␈α∃Impact␈α∃of␈α∃Storage␈α∃Management␈α∃on␈α∀Plex
␈↓ α←␈↓␈↓ ∧∪Processing␈α∨Language␈α≡Implementation',␈α∨Stanford␈α≡Graphics
␈↓ α←␈↓␈↓ ∧∪Project, July 1969. ␈↓↓[␈↓␈↓↓ 382␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Han 71]␈↓ ∧πHansen,␈α∩W.,␈α⊃`Creation␈α∩of␈α⊃Hierarchic␈α∩Text␈α⊃With␈α∩a␈α⊃Computer
␈↓ α←␈↓␈↓ ∧∪Display',␈α_Ph.D.␈α→Thesis,␈α_Computer␈α_Science␈α→Dept.,␈α_Stanford
␈↓ α←␈↓␈↓ ∧∪Univ., June 1971. ␈↓↓[␈↓␈↓↓ 367␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Har 64]␈↓ ∧πHart,␈α∞T.,␈α
and␈α∞Evans,␈α∞T.,␈α
`Notes␈α∞on␈α
Implementing␈α∞LISP␈α∞for␈α
the
␈↓ α←␈↓␈↓ ∧∪M-460 Computer', p. 191-203 in [Ber 64]. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Har 75]␈↓ ∧πHoward,␈α≡F.,␈α≡`Documentation␈α≥of␈α≡Harvard␈α≡PDP-11␈α≥LISP',
␈↓ α←␈↓␈↓ ∧∪unpublished documentation, 1975. ␈↓↓[␈↓␈↓↓ 245␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Hea 68]␈↓ ∧πHearn,␈αA.,␈α
`REDUCE␈αUser's␈α
Manual',␈αStanford␈α
A.I. Lab␈αMemo
␈↓ α←␈↓␈↓ ∧∪50, Stanford University, 1968. ␈↓↓[␈↓␈↓↓ 62␈↓␈↓↓ 421␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Hen 75]␈↓ ∧πvon␈α⊂Henke,␈α⊃F.,␈α⊂`On␈α⊃the␈α⊂Representation␈α⊃of␈α⊂Data␈α⊃Structures␈α⊂in
␈↓ α←␈↓␈↓ ∧∪LCF␈α⊗With␈α⊗Applications␈α⊗to␈α⊗Program␈α⊗Generation',␈α⊗Stanford
␈↓ α←␈↓␈↓ ∧∪A.I. Lab. Memo 267, Sep. 1975. ␈↓↓[␈↓␈↓↓ 54␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Hen 76]␈↓ ∧πHenderson,␈α%P.,␈α%and␈α&Morris,␈α%J.,␈α%`A␈α&Lazy␈α%Evaluator',
␈↓ α←␈↓␈↓ ∧∪SIGPLAN-SIGACT␈α
Symposium␈αon␈α
principles␈αof␈α
programming
␈↓ α←␈↓␈↓ ∧∪languages, Altanta, p.95-103, Jan. 1976. ␈↓↓[␈↓␈↓↓ 221␈↓␈↓↓ 223␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Hew 72]␈↓ ∧πHewitt,␈α≡C.,␈α≡`Description␈α≡and␈α≡Theoretical␈α≡Analysis␈α≥(using
␈↓ α←␈↓␈↓ ∧∪Schemata)␈α∀of␈α∪PLANNER',␈α∀M.I.T.␈α∪A.I.␈α∀Lab.,␈α∀TR-258,␈α∪April
␈↓ α←␈↓␈↓ ∧∪1972. ␈↓↓[␈↓␈↓↓ 73␈↓␈↓↓ 229␈↓␈↓↓ 410␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Hew 74]␈↓ ∧πHewitt,␈α_C.,␈α_et.␈α_al.,␈α_`Behavioral␈α_Semantics␈α_of␈α_Non-recursive
␈↓ α←␈↓␈↓432 BIBLIOGRAPHY␈↓
O␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧∪Control␈α↔Structures',␈α↔Proc.␈α↔Colloque␈α↔sur␈α↔la␈α↔Programmation,
␈↓ α←␈↓␈↓ ∧∪B. Robinet␈α∂ed.,␈α∂in␈α∂␈↓αLecture␈α∂Notes␈α∂in␈α∂Computer␈α∂Science,␈α∂No. 19␈↓,
␈↓ α←␈↓␈↓ ∧∪p. 385-407, Springer Verlag, Berlin, 1974. ␈↓↓[␈↓␈↓↓ 137␈↓␈↓↓ 223␈↓␈↓↓ 410␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Hew 75]␈↓ ∧πHewitt,␈α
C.,␈α
and␈α
Smith,␈α
B.,␈α
`Towards␈α
a␈αProgramming␈α
Apprentice',
␈↓ α←␈↓␈↓ ∧∪␈↓αIEEE␈α Trans.␈α on␈α Software␈α Engineering␈↓,␈α SE-1,␈α∨p. 26-45,
␈↓ α←␈↓␈↓ ∧∪Mar 1975. ␈↓↓[␈↓␈↓↓ 364␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Hew 76]␈↓ ∧πHewitt,␈α∂C.,␈α∞`Viewing␈α∂Control␈α∂Structures␈α∞as␈α∂Patterns␈α∂of␈α∞Passing
␈↓ α←␈↓␈↓ ∧∪Messages',␈α?␈α↔M.I.T. A.I. Lab,␈α?␈α↔Working␈α?␈α⊗paper 92,
␈↓ α←␈↓␈↓ ∧∪April 1976. ␈↓↓[␈↓␈↓↓ 172␈↓␈↓↓ 207␈↓␈↓↓ 229␈↓␈↓↓ 230␈↓␈↓↓ 358␈↓␈↓↓ 410␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Hoa 69]␈↓ ∧πHoare,␈α)C.A.R.,␈α)`An␈α)Axiomatic␈α)Basis␈α*for␈α)Computer
␈↓ α←␈↓␈↓ ∧∪Programming', ␈↓αComm. ACM 12␈↓, ␈↓↓10␈↓, p. 576-580, Oct. 1969. ␈↓↓[␈↓␈↓↓ 162␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Hoa 73a]␈↓ ∧πHoare,␈α→C.A.R.,␈α→`Hints␈α→on␈α→Programming␈α→Language␈α_Design',
␈↓ α←␈↓␈↓ ∧∪Stanford A.I. Lab Memo 224, Dec. 1973. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Hoa 73]␈↓ ∧πHoare,␈α∩C.A.R.,␈α∩`Recursive␈α⊃Data␈α∩Structures',␈α∩Stanford␈α⊃A.I. Lab
␈↓ α←␈↓␈↓ ∧∪Memo 223, Oct. 1973. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Hop 69]␈↓ ∧πHopcroft␈α↔J.,␈α⊗and␈α↔Ullman,␈α↔J.,␈α⊗␈↓αFormal␈α↔Languages␈α↔and␈α⊗their
␈↓ α←␈↓α␈↓ ∧∪Relation␈α!to␈α!Automata␈↓,␈α!Addison-Wesley,␈α!Reading,␈α!Mass.,
␈↓ α←␈↓␈↓ ∧∪1969. ␈↓↓[␈↓␈↓↓ 25␈↓␈↓↓ 55␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Int 75]␈↓ ∧πTeitelman,␈αW.,␈α`INTERLISP␈αReference␈αManual',␈αXerox␈αPARC,
␈↓ α←␈↓␈↓ ∧∪Palo Alto, 1975. ␈↓↓[␈↓␈↓↓ 128␈↓␈↓↓ 194␈↓␈↓↓ 242␈↓␈↓↓ 273␈↓␈↓↓ 364␈↓␈↓↓ 367␈↓␈↓↓ 403␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Ive 62]␈↓ ∧πIverson,␈α↔K.,␈α↔␈↓αA␈α↔Programming␈α↔Language␈↓,␈α↔Wiley,␈α_New␈α↔York,
␈↓ α←␈↓␈↓ ∧∪1962. ␈↓↓[␈↓␈↓↓ 357␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Joh 71]␈↓ ∧πJohnston,␈α≡J.,␈α≡`The␈α≥Contour␈α≡Model␈α≡of␈α≡Block␈α≥Structured
␈↓ α←␈↓␈↓ ∧∪Processes', p. 55-82 in [DSIPL]. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Kan 75]␈↓ ∧πKanada,␈α!Y.,␈α!`Implementation␈α!of␈α!HLISP␈α!and␈α Algebraic
␈↓ α←␈↓␈↓ ∧∪Manipulation␈α→Language␈α_REDUCE-2',␈α→Information␈α_Sciences
␈↓ α←␈↓␈↓ ∧∪Lab., TR 75-01, Univ. of Tokyo, Japan, Jan. 1975. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Kni 74]␈↓ ∧πKnight,␈α_T.,␈α→`The␈α_CONS␈α_Microprocessor',␈α→M.I.T.,␈α_Artificial
␈↓ α←␈↓␈↓ ∧∪Intelligence␈α1Working␈α2paper␈α1No. 80,␈α2.␈α1Cambrigde,
␈↓ α←␈↓␈↓ ∧∪Nov. 1974. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Knu 68]␈↓ ∧πKnuth,␈α∂D.,␈α∂␈↓αThe␈α⊂Art␈α∂of␈α∂Computer␈α⊂Programming,␈α∂Non-numerical
␈↓ α←␈↓α␈↓ ∧∪Algorithms␈↓,␈α0Vol. 1,␈α0Addison-Wesley,␈α1Reading,␈α0Mass.,
␈↓ α←␈↓␈↓ ∧∪1968. ␈↓↓[␈↓␈↓↓ 387␈↓␈↓↓ 393␈↓␈↓↓ 398␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Knu 72]␈↓ ∧πKnuth,␈α∂D.,␈α⊂␈↓αThe␈α∂Art␈α∂of␈α⊂Computer␈α∂Programming,␈α⊂Searching␈α∂and
␈↓ α←␈↓α␈↓ ∧∪Sorting␈↓, Vol. 3, Addison-Wesley, Reading, Mass., 1972. ␈↓↓[␈↓␈↓↓ 278␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Knu 74]␈↓ ∧πKnuth,␈αD.,␈α`Structured␈αProgramming␈αWith␈αGO␈αTO␈αStatements',
␈↓ α←␈↓␈↓ ∧∪␈↓αComputer Surveys 6␈↓, 4, p. 261-301, Dec. 1974. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓ α←␈↓␈↓␈↓ λHBIBLIOGRPAHY 433␈↓
␈↓"β␈↓ α←␈↓[Lan 64]␈↓ ∧πLandin,␈α≤P.,␈α≤`The␈α≤Mechanical␈α≤Evaluation␈α≤of␈α≠Expressions',
␈↓ α←␈↓␈↓ ∧∪␈↓αComputer Journal 6␈↓, ␈↓↓4␈↓, p. 308-320, Apr. 1964. ␈↓↓[␈↓␈↓↓ 173␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Lan 66]␈↓ ∧πLandin,␈α#P.,␈α$`The␈α#Next␈α#700␈α$Programming␈α#Languages',
␈↓ α←␈↓␈↓ ∧∪␈↓αComm. ACM 9␈↓, ␈↓↓3␈↓, p. 157-166, Mar. 1966. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[LCF 72]␈↓ ∧πMilner,␈α∂R.,␈α∂`Logic␈α⊂for␈α∂Computable␈α∂Functions,␈α∂Description␈α⊂of␈α∂a
␈↓ α←␈↓␈↓ ∧∪Machine␈α≤Implementation',␈α≥Stanford␈α≤A.I. Lab.␈α≥Memo␈α≤169,
␈↓ α←␈↓␈↓ ∧∪1972. ␈↓↓[␈↓␈↓↓ 96␈↓␈↓↓ 410␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Leh 73]␈↓ ∧πLehmann,␈α
D.,␈α∞`A␈α
Direct␈α
Proof␈α∞of␈α
the␈α∞Church-Rosser␈α
Theorem',
␈↓ α←␈↓␈↓ ∧∪Center␈α⊗for␈α↔Computer␈α⊗and␈α⊗Information␈α↔Sciences,␈α⊗Technical
␈↓ α←␈↓␈↓ ∧∪Report␈α∂No. 73-70,␈α∞Brown␈α∂University,␈α∞Providence,␈α∂R.I.,␈α∞August
␈↓ α←␈↓␈↓ ∧∪1973. ␈↓↓[␈↓␈↓↓ 173␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Lev un]␈↓ ∧πLevin,␈α↔M.,␈α⊗`Course␈α↔notes:␈α⊗6.542',␈α↔M.I.T.,␈α↔Cambridge,␈α⊗Mass.,
␈↓ α←␈↓␈↓ ∧∪undated ␈↓↓[␈↓␈↓↓ 181␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Lin 73]␈↓ ∧πLindstrom,␈α∪G.,␈α∪`Algorithms␈α∪For␈α∪List␈α∪Structure␈α∩Condensation',
␈↓ α←␈↓␈↓ ∧∪Dept.␈αof␈αComputer␈αScience,␈αTechnical␈αReport 73-14,␈αUniversity
␈↓ α←␈↓␈↓ ∧∪of Pittsburgh, 1973. ␈↓↓[␈↓␈↓↓ 402␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Lis 74]␈↓ ∧πLiskov,␈α∩B.,␈α⊃and␈α∩Zilles,␈α⊃S.,␈α∩`Programming␈α⊃With␈α∩Abstract␈α⊃Data
␈↓ α←␈↓␈↓ ∧∪Structures',␈α
Proc.␈αof␈α
Symposium␈α
on␈αVery␈α
high␈α
level␈αlanguages,
␈↓ α←␈↓␈↓ ∧∪␈↓αSIGPLAN Notices␈↓, Apr. 1974. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Lon 71]␈↓ ∧πLondon,␈α
R.,␈α
`Correctness␈αof␈α
Two␈α
Compilers␈αfor␈α
a␈α
LISP␈αSubset',
␈↓ α←␈↓␈↓ ∧∪Stanford A.I. Lab. Memo 151, Oct., 1971. ␈↓↓[␈↓␈↓↓ 337␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Lug 73]␈↓ ∧πLugger,␈α↔J.,␈α_and␈α↔Melenk,␈α_H.,␈α↔`Darstellung␈α_und␈α↔Bearbeitung
␈↓ α←␈↓␈↓ ∧∪Umfangreicher␈α!LISP-programme',␈α!␈↓αAngewandte␈α!Informatik␈↓,
␈↓ α←␈↓␈↓ ∧∪p. 257-263, June 1973. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[MAC 74]␈↓ ∧πBogen,␈α⊗R.,␈α↔`MACSYMA␈α⊗Reference␈α⊗Manual',␈α↔Project␈α⊗MAC,
␈↓ α←␈↓␈↓ ∧∪Mathlab Group, M.I.T., Cambridge, 1974. ␈↓↓[␈↓␈↓↓ 62␈↓␈↓↓ 390␈↓␈↓↓ 421␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Man 74]␈↓ ∧πManna,␈α∞Z.,␈α∞␈↓αMathematical␈α∞Theory␈α∞of␈α∞Computation␈↓,␈α∞McGraw-Hill,
␈↓ α←␈↓␈↓ ∧∪New York, 1974. ␈↓↓[␈↓␈↓↓ 160␈↓␈↓↓ 232␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[McB 63]␈↓ ∧πMcBeth,␈αH.␈α `On␈αthe␈αReference␈αCounter␈αMethod',␈α(letter),␈α␈↓αComm.
␈↓ α←␈↓α␈↓ ∧∪ACM 6␈↓, ␈↓↓9␈↓, p. 575, Sep 1963. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[McC 60a]␈↓ ∧πMcCarthy,␈α"J,␈α"et.␈α"al.,␈α"`LISP␈α"1␈α#Programmer's␈α"Manual',
␈↓ α←␈↓␈↓ ∧∪Computation␈α⊂Center␈α⊂and␈α∂Research␈α⊂Laboratory␈α⊂of␈α∂Electronics,
␈↓ α←␈↓␈↓ ∧∪M.I.T., Cambridge, 1960. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[McC 60]␈↓ ∧πMcCarthy,␈αJ.,␈α`Recursive␈αFunctions␈αof␈αSymbolic␈αExpressions␈α
and
␈↓ α←␈↓␈↓ ∧∪Their␈α→Computation␈α→by␈α→Machine',␈α→␈↓αComm. ACM␈↓,␈α_p. 184-195,
␈↓ α←␈↓␈↓ ∧∪April 1960. ␈↓↓[␈↓␈↓↓ 110␈↓␈↓↓ 185␈↓␈↓↓ 231␈↓␈↓↓ 287␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[McC 62]␈↓ ∧πMcCarthy,␈α)J.,␈α)`Towards␈α)a␈α)Mathematical␈α*Science␈α)of
␈↓ α←␈↓␈↓434 BIBLIOGRAPHY␈↓
O␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧∪Computation',␈α
IFIPS␈α
Proceedings␈α
of␈α
Munich␈α
Conference␈α1962,
␈↓ α←␈↓␈↓ ∧∪North Holland, Amsterdam, 1963. ␈↓↓[␈↓␈↓↓ 164␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[McC 63]␈↓ ∧πMcCarthy,␈α J.,␈α ␈↓αA␈α Basis␈α for␈α a␈α Mathematical␈α Theory␈α∨of
␈↓ α←␈↓α␈↓ ∧∪Computation,␈α⊂in␈α∂Computer␈α⊂Programming␈α∂and␈α⊂Formal␈α∂Systems␈↓,
␈↓ α←␈↓␈↓ ∧∪North Holland, Amsterdam, 1963. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[McC 65]␈↓ ∧πMcCarthy,␈α⊃J,␈α⊃et.␈α⊃al.,␈α⊃`LISP␈α⊃1.5␈α⊃Programmer's␈α∩Manual',␈α⊃M.I.T.
␈↓ α←␈↓␈↓ ∧∪Press, Cambridge, 1965. ␈↓↓[␈↓␈↓↓ 118␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[McC 66]␈↓ ∧πMcCarthy,␈αJ.,␈α
`A␈αFormal␈α
Description␈αof␈α
a␈αSubset␈α
of␈αALGOL',␈α
in
␈↓ α←␈↓␈↓ ∧∪␈↓αFormal␈α!Language␈α!Description␈α!Languages␈α!for␈α!Computer
␈↓ α←␈↓α␈↓ ∧∪Programming␈↓, North Holland, Amsterdam, 1966. ␈↓↓[␈↓␈↓↓ 164␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[McC 76]␈↓ ∧πMcCarthy,␈α⊃J.,␈α⊃␈↓αRecursive␈α⊃Programming␈α⊃in␈α⊃LISP␈↓,␈α∩CS206␈α⊃notes,
␈↓ α←␈↓␈↓ ∧∪Stanford University, 1976. ␈↓↓[␈↓␈↓↓ 337␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[McD 75]␈↓ ∧πMcDermott,␈α→D.,␈α_`Very␈α→Large␈α_PLANNER-type␈α→Data␈α_Bases',
␈↓ α←␈↓␈↓ ∧∪M.I.T. A.I. Lab␈α?␈α↔Memo 339,␈α?␈α↔Cambridge,␈α?␈α↔Mass.,
␈↓ α←␈↓␈↓ ∧∪Sep. 1975. ␈↓↓[␈↓␈↓↓ 75␈↓␈↓↓ 256␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Men 64]␈↓ ∧πMendelson,␈α≡E.,␈α≡␈↓αIntroduction␈α≡to␈α≡Mathematical␈α∨Logic␈↓,␈α≡Van
␈↓ α←␈↓␈↓ ∧∪Nostrand, Princeton, New Jersey, 1964. ␈↓↓[␈↓␈↓↓ 107␈↓␈↓↓ 162␈↓␈↓↓ 174␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Mic 71]␈↓ ∧πSussman,␈α_G.,␈α_et␈α_al.,␈α_`Micro-PLANNER␈α→Reference␈α_Manual',
␈↓ α←␈↓␈↓ ∧∪M.I.T.,␈α<A.I. Lab.␈α<Memo 203a,␈α=Cambridge,␈α<Mass.,
␈↓ α←␈↓␈↓ ∧∪Dec 1971. ␈↓↓[␈↓␈↓↓ 410␈↓␈↓↓ 412␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Mil 73]␈↓ ∧πMilner,␈α⊗R.,␈α⊗`λ-Calculus␈α∃and␈α⊗the␈α⊗Semantics␈α⊗of␈α∃Programming
␈↓ α←␈↓␈↓ ∧∪Languages',␈αLecture␈αNotes,␈αComputer␈αScience␈α
Dept.,␈αUniversity
␈↓ α←␈↓␈↓ ∧∪of Edinburgh, 1973-74. ␈↓↓[␈↓␈↓↓ 173␈↓␈↓↓ 181␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Min 70]␈↓ ∧πMinsky,␈α∀M.,␈α∀`Form␈α∀and␈α∪Content␈α∀in␈α∀Computer␈α∀Science:␈α∪1970
␈↓ α←␈↓␈↓ ∧∪ACM␈α4Turing␈α5Lecture',␈α4␈↓αJACM 17␈↓,␈α5␈↓↓2␈↓,␈α4p. 197-215,
␈↓ α←␈↓␈↓ ∧∪Apr. 1970. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Mit 70]␈↓ ∧πMitchell,␈αJ.,␈α`The␈αDesign␈α&␈αConstruction␈αof␈αFlexible␈α&␈αEfficient
␈↓ α←␈↓␈↓ ∧∪Interactive␈α?␈α
Programming␈α?␈α
Systems',␈α?␈α
Ph.D. Thesis,
␈↓ α←␈↓␈↓ ∧∪Carnegie-Mellon Unversity, June 1970. ␈↓↓[␈↓␈↓↓ 152␈↓␈↓↓ 358␈↓␈↓↓ 405␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Mli 73]␈↓ ∧πSmith,␈α⊃D.,␈α⊂and␈α⊃Enea,␈α⊃H.,␈α⊂`MLISP2',␈α⊃Stanford␈α⊃A.I. Lab␈α⊂Memo
␈↓ α←␈↓␈↓ ∧∪195, Stanford Univ., 1973. ␈↓↓[␈↓␈↓↓ 410␈↓␈↓↓ 422␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Mon 73]␈↓ ∧πMontangero,␈αC.,␈αet␈αal.,␈α`An␈αExtended␈αLISP␈αSystem␈αfor␈αComplex
␈↓ α←␈↓␈↓ ∧∪Control-structure␈α1Programming',␈α1University␈α2of␈α1Pisa,
␈↓ α←␈↓␈↓ ∧∪1973. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Mon 75a]␈↓ ∧πMontangero,␈α∂C.,␈α∂et␈α∂al.,␈α∂`MAGMA-Lisp:␈α∂A␈α∂`Machine␈α∞Language'
␈↓ α←␈↓␈↓ ∧∪for␈αArtifical␈αIntelligence',␈αProc.␈α4␈↓πth␈↓␈αInt.␈αJ.␈αConf.␈αon␈αA.I.,␈αTbilisi,
␈↓ α←␈↓␈↓ ∧∪p. 556-561, Sep. 1975. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓ α←␈↓␈↓␈↓ λHBIBLIOGRPAHY 435␈↓
␈↓"β␈↓ α←␈↓[Mon 75]␈↓ ∧πMontangero,␈α∀C.␈α∪et␈α∀al.,␈α∪`Information␈α∀Management␈α∀in␈α∪Context
␈↓ α←␈↓␈↓ ∧∪Trees', University of Pisa, N.I. B75-21, Oct. 1975. ␈↓↓[␈↓␈↓↓ 301␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Moo 74]␈↓ ∧πMoon,␈α≥D.,␈α≥`MacLISP␈α≥Reference␈α≥Manual',␈α≥Laboratory␈α≤for
␈↓ α←␈↓␈↓ ∧∪Computer␈α?␈α
Science,␈α?␈α
M.I.T.,␈α?␈α
Cambridge,␈α?␈α Mass,
␈↓ α←␈↓␈↓ ∧∪1974. ␈↓↓[␈↓␈↓↓ 128␈↓␈↓↓ 148␈↓␈↓↓ 194␈↓␈↓↓ 195␈↓␈↓↓ 198␈↓␈↓↓ 199␈↓␈↓↓ 280␈↓␈↓↓ 367␈↓␈↓↓ 390␈↓␈↓↓ 403␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Moor 74]␈↓ ∧πMoore,␈α∃J,␈α∃`Introducing␈α∃PROG␈α∃to␈α∃the␈α∃Pure␈α⊗LISP␈α∃Theorem
␈↓ α←␈↓␈↓ ∧∪Prover',␈α<CSL 74-3,␈α=Xerox␈α<PARC,␈α=Palo␈α<Alto,
␈↓ α←␈↓␈↓ ∧∪Dec 1974. ␈↓↓[␈↓␈↓↓ 47␈↓␈↓↓ 185␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Moor 75a]␈↓ ∧πMoore,␈αJ,␈α`The␈αINTERLISP␈αVirtual␈αMachine␈αSpecification',␈α(in
␈↓ α←␈↓␈↓ ∧∪preparation), Xerox PARC, Palo Alto, 1975. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Moor 75b]␈↓ ∧πMoore,␈αJ,␈α`Computational␈αLogic:␈α
Structure␈αSharing␈αand␈αProof␈α
of
␈↓ α←␈↓␈↓ ∧∪Program␈α∪Properties␈α∪Part␈α∩II',␈α∪CSL 75-2,␈α∪Xerox␈α∪PARC,␈α∩Palo
␈↓ α←␈↓␈↓ ∧∪Alto, Apr 1975. ␈↓↓[␈↓␈↓↓ 96␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Moor 76]␈↓ ∧π164 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Mor 55]␈↓ ∧πMorris,␈α∞C.,␈α∞`Foundations␈α∞of␈α∞the␈α∞Theory␈α∞of␈α∞Signs',␈α∞␈↓αInternational
␈↓ α←␈↓α␈↓ ∧∪Encyclopedia␈α∀of␈α∪Unified␈α∀Science␈↓,␈α∪Vol 1,␈α∀No 2,␈α∀University␈α∪of
␈↓ α←␈↓␈↓ ∧∪Chicago press, 1955. ␈↓↓[␈↓␈↓↓ 162␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Mor 68]␈↓ ∧πMorris,␈α(J.,␈α(`Lambda-calculus␈α(Models␈α(of␈α(Programming
␈↓ α←␈↓␈↓ ∧∪Languages',␈α?Project␈α?MAC,␈α?␈α↓M.I.T.,␈α?MAC-TR57,
␈↓ α←␈↓␈↓ ∧∪Dec 1968. ␈↓↓[␈↓␈↓↓ 22␈↓␈↓↓ 181␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Mor 73]␈↓ ∧πMorris,␈α⊗L.,␈α⊗`Advice␈α⊗on␈α⊗Structuring␈α⊗Compilers␈α⊗and␈α⊗Proving
␈↓ α←␈↓␈↓ ∧∪Them␈α'Correct',␈α'ACM␈α'Symposium␈α'on␈α'Principles␈α'of
␈↓ α←␈↓␈↓ ∧∪Programming Languages, Boston, p. 144-152, 1973. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Mor 74]␈↓ ∧πMorris,␈α~J.,␈α≠`Towards␈α~More␈α≠Flexible␈α~Type␈α≠Systems',␈α~Proc.
␈↓ α←␈↓␈↓ ∧∪Colloque␈α∞sur␈α
la␈α∞Programmation,␈α
Paris,␈α∞April␈α∞1974,␈α
p. 377-383,
␈↓ α←␈↓␈↓ ∧∪Springer Verlag, Berlin, 1974. ␈↓↓[␈↓␈↓↓ 36␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Mos 70]␈↓ ∧πMoses,␈α⊃J.,␈α⊂`The␈α⊃Function␈α⊂of␈α⊃FUNCTION␈α⊂in␈α⊃LISP',␈α⊂␈↓αSIGSAM
␈↓ α←␈↓α␈↓ ∧∪Bulletin␈↓, p. 13-27, July 1970. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Mos 74]␈↓ ∧πMoses,␈αJ.,␈α
`MACSYMA - ␈αThe␈αFifth␈α
Year',␈α␈↓αSIGSAM␈αBulletin␈↓,␈α
␈↓↓8␈↓,
␈↓ α←␈↓␈↓ ∧∪␈↓α3␈↓, p. 105-110, Aug 1974. ␈↓↓[␈↓␈↓↓ 62␈↓␈↓↓ 390␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Mot 76]␈↓ ∧πMotoyoshi,␈α⊃F.,␈α⊂`A␈α⊃Portable␈α⊂LISP␈α⊃Compiler␈α⊂on␈α⊃a␈α⊂Hypothetical
␈↓ α←␈↓␈↓ ∧∪LISP␈α
Machine',␈α
Dept.␈α∞of␈α
Info.␈α
Science,␈α
TR 76-5,␈α∞University␈α
of
␈↓ α←␈↓␈↓ ∧∪Tokyo, Japan, Jan. 1976. ␈↓↓[␈↓␈↓↓ 249␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Mud 75]␈↓ ∧πGalley,␈α"S.W.,␈α"and␈α#Pfister,␈α"G.,␈α"`The␈α#MDL␈α"Language',
␈↓ α←␈↓␈↓ ∧∪Programming␈α→Technology␈α_Division␈α→Doc. SYS.11.01,␈α_Project
␈↓ α←␈↓␈↓ ∧∪Mac, M.I.T., Cambridge, Mass., Nov. 1975. ␈↓↓[␈↓␈↓↓ 301␈↓␈↓↓ 412␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Mul 76]␈↓ ∧πMuller,␈α≡K.,␈α≥`On␈α≡the␈α≥Feasibility␈α≡of␈α≡Concurrent␈α≥Garbage
␈↓ α←␈↓␈↓ ∧∪Collection', Ph.D. Thesis, University of Delft, 1976. ␈↓↓[␈↓␈↓↓ 287␈↓␈↓↓ ]␈↓
␈↓ α←␈↓␈↓436 BIBLIOGRAPHY␈↓
O␈↓
␈↓"β␈↓ α←␈↓[New 61]␈↓ ∧πNewell,␈α≥A.,␈α≤␈↓αInformation␈α≥Processing␈α≤Language␈α≥V␈α≤Manual␈↓,
␈↓ α←␈↓␈↓ ∧∪Prentice␈α<Hall,␈α<Englewood␈α<Cliffs,␈α<New␈α<Jersey,
␈↓ α←␈↓␈↓ ∧∪1961. ␈↓↓[␈↓␈↓↓ 227␈↓␈↓↓ 236␈↓␈↓↓ 247␈↓␈↓↓ 256␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[New 75]␈↓ ∧πNewey,␈α∩M.,␈α∩`Formal␈α∪Semantics␈α∩of␈α∩LISP␈α∩with␈α∪Applications␈α∩to
␈↓ α←␈↓␈↓ ∧∪Program␈α
Correctness',␈α
Stanford␈α
A.I. ␈α
Lab␈α∞Memo 257,␈α
Stanford
␈↓ α←␈↓␈↓ ∧∪Univ., Jan 1975. ␈↓↓[␈↓␈↓↓ 96␈↓␈↓↓ 337␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Nor 70]␈↓ ∧πNordstrom,␈α
M.,␈α
et␈α∞al.,␈α
`LISP␈α
F1␈α∞-␈α
A␈α
Fortran␈α∞Implementation␈α
of
␈↓ α←␈↓␈↓ ∧∪LISP␈α
1.5',␈α
Computer␈α
Science␈α
Dept,␈α
Uppsala␈α
University,␈α
Sweden,
␈↓ α←␈↓␈↓ ∧∪1970. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Nor 76]␈↓ ∧πNorman,␈α∪E.,␈α∀`Documentation␈α∪for␈α∪1100␈α∀LISP␈α∪Implementation',
␈↓ α←␈↓␈↓ ∧∪unpublished␈α*paper,␈α)University␈α*of␈α)Wisconsin-Madison,
␈↓ α←␈↓␈↓ ∧∪1976. ␈↓↓[␈↓␈↓↓ 266␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Org 71]␈↓ ∧πOrganick,␈α⊂E.,␈α⊂and␈α∂Cleary,␈α⊂J.,␈α⊂`A␈α∂Data␈α⊂Structure␈α⊂Model␈α⊂of␈α∂the
␈↓ α←␈↓␈↓ ∧∪B6700 Computer System', p. 83-145 in [DSIPL]. ␈↓↓[␈↓␈↓↓ 372␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Org 73]␈↓ ∧πOrganick,␈α⊃E.,␈α⊃␈↓αComputer␈α⊃System␈α⊃Organization:␈α⊃the␈α⊃B5700/6700
␈↓ α←␈↓α␈↓ ∧∪Series␈↓,␈α⊂ACM␈α⊂Monograph␈α∂Series,␈α⊂Academic␈α⊂Press,␈α⊂New␈α∂York,
␈↓ α←␈↓␈↓ ∧∪1973. ␈↓↓[␈↓␈↓↓ 236␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Pac 73]␈↓ ∧πPacini,␈α∀G.,␈α∀`An␈α∀Optimal␈α∀Fix-point␈α∀Computation␈α∀Rule␈α∀for␈α∀a
␈↓ α←␈↓␈↓ ∧∪Simple␈α⊃Recursive␈α⊂Language',␈α⊃University␈α⊂of␈α⊃Pisa,␈α⊂N.I. B75-10,
␈↓ α←␈↓␈↓ ∧∪Oct. 1973. ␈↓↓[␈↓␈↓↓ 221␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Pag 76]␈↓ ∧πPage,␈α→R.,␈α~LISP␈α→for␈α~Fairchild␈α→F8,␈α~Private␈α→communication,
␈↓ α←␈↓␈↓ ∧∪1976. ␈↓↓[␈↓␈↓↓ 245␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Per 67]␈↓ ∧πPerlis,␈α⊂A.,␈α⊂`The␈α⊂Synthesis␈α∂of␈α⊂Algorithmic␈α⊂Systems:␈α⊂1966␈α∂ACM
␈↓ α←␈↓␈↓ ∧∪Turing Lecture', ␈↓αJACM 14␈↓, ␈↓↓1␈↓, p. 1-9, Jan. 1967. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Plo 74]␈↓ ∧πPlotkin,␈α∪G.,␈α∪`Call-by-name,␈α∪Call-by-value␈α∪and␈α∪the␈α∪λ-Calculus',
␈↓ α←␈↓␈↓ ∧∪␈↓αTheoretical Computer Science 1␈↓, p. 125-159, 1975. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Pop 68a]␈↓ ∧πPopplestone,␈αR.,␈α`The␈αDesign␈αPhilosophy␈αof␈αPOP-2',␈αin␈α␈↓αMachine
␈↓ α←␈↓α␈↓ ∧∪Intelligence 3␈↓, American Elsevier, New York, 1968. ␈↓↓[␈↓␈↓↓ 160␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Pop 68]␈↓ ∧πBurstall,␈α⊂R.,␈α⊃et␈α⊂al.,␈α⊃␈↓αPOP2␈α⊂Papers␈↓,␈α⊃Oliver & Boyd,␈α⊂Edinburgh,
␈↓ α←␈↓␈↓ ∧∪1968. ␈↓↓[␈↓␈↓↓ 160␈↓␈↓↓ 382␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Pra 73]␈↓ ∧πPratt,␈α∞V.,␈α∞`Top-down␈α∂Operator␈α∞Precedence',␈α∞Proceedings␈α∂of␈α∞the
␈↓ α←␈↓␈↓ ∧∪ACM␈α∃Symposium␈α∃on␈α∃Principles␈α∃of␈α⊗Programming,␈α∃p. 41-51,
␈↓ α←␈↓␈↓ ∧∪1973. ␈↓↓[␈↓␈↓↓ 162␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Pra 76]␈↓ ∧πPratt,␈α
V.,␈α
`CGOL - An␈α
Alternative␈α
External␈α
Representation␈α
for
␈↓ α←␈↓␈↓ ∧∪LISP␈α≡Users',␈α≡M.I.T.␈α≥A.I.␈α≡Lab,␈α≡Working␈α≡Paper␈α≥No. 89,
␈↓ α←␈↓␈↓ ∧∪1976. ␈↓↓[␈↓␈↓↓ 410␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Pre 72]␈↓ ∧πPrenner,␈α∩C.,␈α∩`Multi-path␈α∩Control␈α∩Structures␈α∩for␈α∩Programming
␈↓ α←␈↓␈↓␈↓ λHBIBLIOGRPAHY 437␈↓
␈↓"β␈↓ α←␈↓␈↓ ∧∪Languages',␈α∂Ph.D.␈α∂Thesis,␈α∞Center␈α∂for␈α∂Research␈α∂in␈α∞Computing
␈↓ α←␈↓␈↓ ∧∪Technology, Harvard Univ., 1972. ␈↓↓[␈↓␈↓↓ 229␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Pre 76a]␈↓ ∧πPrenner,␈α∂C.,␈α∂`Data␈α∂structures␈α∂for␈α∂Spaghetti␈α∂LISP',␈α∂unpublished
␈↓ α←␈↓␈↓ ∧∪notes,␈α6University␈α6of␈α6California,␈α6Berkeley,␈α5Calif,
␈↓ α←␈↓␈↓ ∧∪1976. ␈↓↓[␈↓␈↓↓ 229␈↓␈↓↓ 266␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Pre 76b]␈↓ ∧πPrenner,␈α⊃C.,␈α⊃`Implementation␈α⊃for␈α⊃Spaghetti␈α⊃EL1',␈α⊃unpublished
␈↓ α←␈↓␈↓ ∧∪notes, University of California, Berkeley, Calif, 1976. ␈↓↓[␈↓␈↓↓ 410␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[QA4 72]␈↓ ∧πRulifson,␈α⊂J.,␈α⊃et. al.,␈α⊂`QA4: A␈α⊃Procedural␈α⊂Calculus␈α⊃for␈α⊂Intuitive
␈↓ α←␈↓␈↓ ∧∪Reasoning',␈α⊃Stanford␈α⊂Research␈α⊃Institute,␈α⊂TN-73,␈α⊃Menlo␈α⊂Park,
␈↓ α←␈↓␈↓ ∧∪Cal., Nov. 1972. ␈↓↓[␈↓␈↓↓ 74␈↓␈↓↓ 410␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Qua 68]␈↓ ∧πQuam,␈α≤L.,␈α≤`SDIO␈α≠Manual',␈α≤unpublished␈α≤paper,␈α≠Stanford,
␈↓ α←␈↓␈↓ ∧∪1968. ␈↓↓[␈↓␈↓↓ 421␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Qua 72]␈↓ ∧πQuam,␈αL.,␈αand␈αDiffie,␈αW.,␈α`Stanford␈αLISP 1.6␈αManual',␈αStanford
␈↓ α←␈↓␈↓ ∧∪A.I. Lab.,␈α?␈α↓Operating␈α?Note 28.7,␈α?␈α↓Stanford␈α?Univ.,
␈↓ α←␈↓␈↓ ∧∪1972. ␈↓↓[␈↓␈↓↓ 128␈↓␈↓↓ 194␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Rey 72]␈↓ ∧πReynolds,␈α(J.,␈α'`Definitional␈α(Interpreters␈α(for␈α'High-order
␈↓ α←␈↓␈↓ ∧∪Programming␈α⊂Languages',␈α∂Proceedings␈α⊂of␈α∂the␈α⊂ACM␈α∂National
␈↓ α←␈↓␈↓ ∧∪convention, 1972, p. 717-740, ACM, 1972. ␈↓↓[␈↓␈↓↓ 137␈↓␈↓↓ 207␈↓␈↓↓ 219␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Ric 74]␈↓ ∧πRich,␈α0C.,␈α0and␈α0Shrobe,␈α0H.,␈α1`Understanding␈α0LISP
␈↓ α←␈↓␈↓ ∧∪Programs: Towards␈α?␈αa␈α?␈α
Programming␈α?␈αApprentice',
␈↓ α←␈↓␈↓ ∧∪M.I.T. A.I. Lab, Working paper 82, Dec. 1974. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Ris 73]␈↓ ∧πRisch,␈α∀T.,␈α∀`REMREC - A␈α∀Program␈α∀for␈α∃Automatic␈α∀Recursion
␈↓ α←␈↓␈↓ ∧∪Removal␈α⊃in␈α⊃LISP',␈α⊃Datalogilaboratoriet,␈α⊃DLU 73/24,␈α⊂Uppsala
␈↓ α←␈↓␈↓ ∧∪Univ., Nov. 1973. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Riv 76]␈↓ ∧πRivest,␈α∪R.,␈α∪`On␈α∪Self-organizing␈α∪Sequential␈α∪Search␈α∪Heuristics',
␈↓ α←␈↓␈↓ ∧∪␈↓αComm. ACM 19␈↓, ␈↓↓2␈↓, p. 63-67, Feb. 1976. ␈↓↓[␈↓␈↓↓ 261␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Rob 65]␈↓ ∧πRobinson,␈α≡J.,␈α≥`A␈α≡Machine-oriented␈α≥Logic␈α≡Based␈α≡on␈α≥the
␈↓ α←␈↓␈↓ ∧∪Resolution␈α&Principle',␈α'␈↓αJournal␈α&ACM␈α&12␈↓,␈α'␈↓↓1␈↓,␈α&p. 23-41,
␈↓ α←␈↓␈↓ ∧∪Jan 1965. ␈↓↓[␈↓␈↓↓ 75␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Roc 71]␈↓ ∧πRochfeld,␈α%A.,␈α%`New␈α$LISP␈α%Techniques␈α%for␈α%a␈α$Paging
␈↓ α←␈↓␈↓ ∧∪Environment',␈α)␈↓αComm.␈α)ACM␈α)14␈↓,␈α)␈↓↓12␈↓,␈α)p. 791-795,␈α(Dec
␈↓ α←␈↓␈↓ ∧∪1971. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Rog 67]␈↓ ∧πRogers,␈α≡H.,␈α≡␈↓αTheory␈α∨of␈α≡Recursive␈α≡Functions␈α∨&␈α≡Effective
␈↓ α←␈↓α␈↓ ∧∪Computability␈↓, McGraw-Hill, New York, 1967. ␈↓↓[␈↓␈↓↓ 22␈↓␈↓↓ 159␈↓␈↓↓ 160␈↓␈↓↓ 229␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Ros 71]␈↓ ∧πRosen,␈αB.␈α
`Subtree␈αReplacement␈α
Systems',␈αPh.D.␈αThesis,␈α
Harvard
␈↓ α←␈↓␈↓ ∧∪University, Cambridge, Mass.,1971. ␈↓↓[␈↓␈↓↓ 173␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Rus 64]␈↓ ∧πRussell␈α∞R.,␈α∞`KALAH␈α∞-␈α∞The␈α∞Game␈α∞and␈α∞the␈α∂Program',␈α∞Stanford
␈↓ α←␈↓␈↓ ∧∪A.I. Lab. Memo 22, Stanford University, 1964. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓ α←␈↓␈↓438 BIBLIOGRAPHY␈↓
O␈↓
␈↓"β␈↓ α←␈↓[Sam 75]␈↓ ∧πSamet,␈α$H.,␈α$`Automatically␈α$Proving␈α$the␈α%Correctness␈α$of
␈↓ α←␈↓␈↓ ∧∪Translations␈α∪Involving␈α∪Optimized␈α∪Code',␈α∀Stanford␈α∪A.I. Lab.
␈↓ α←␈↓␈↓ ∧∪Memo 259, May 1975. ␈↓↓[␈↓␈↓↓ 185␈↓␈↓↓ 262␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[San 75a]␈↓ ∧πSandewall,␈α∞E.,␈α∞`Ideas␈α∞About␈α
Management␈α∞of␈α∞LISP␈α∞Data␈α
Bases',
␈↓ α←␈↓␈↓ ∧∪M.I.T. A.I. Lab., Memo 332, May 1975. ␈↓↓[␈↓␈↓↓ 265␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[San 75]␈↓ ∧πSandewall,␈α2E.,␈α1`Some␈α2Observations␈α2on␈α1Conceptual
␈↓ α←␈↓␈↓ ∧∪Programming',␈α∪Computer␈α∪Science␈α∪Dept.,␈α∪Uppsala␈α∩University,
␈↓ α←␈↓␈↓ ∧∪Sweden, 1975. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[San 76]␈↓ ∧πSandewall,␈α∀E.,␈α∪`Programming␈α∀in␈α∪an␈α∀Interactive␈α∪Environment:
␈↓ α←␈↓␈↓ ∧∪The␈α"LISP␈α"Experience',␈α"Linkoping␈α#University,␈α"Sweden,
␈↓ α←␈↓␈↓ ∧∪1976. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Sau 64]␈↓ ∧πSaunders,␈α↔R.,␈α↔`The␈α↔LISP␈α↔System␈α↔for␈α↔the␈α↔Q-32␈α⊗Computer',
␈↓ α←␈↓␈↓ ∧∪p. 220-231 in [Ber 64]. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Sch 67]␈↓ ∧πSchorr,␈α∞H.,␈α∞and␈α∞Waite,␈α∞W.,␈α∞`An␈α∞Efficient␈α∞Machine-independent
␈↓ α←␈↓␈↓ ∧∪Procedure␈α∞for␈α∞Garbage␈α∂Collection␈α∞in␈α∞Various␈α∂List␈α∞Structures',
␈↓ α←␈↓␈↓ ∧∪␈↓αComm. ACM 10␈↓, ␈↓↓8␈↓, p. 501-506, Aug. 1967. ␈↓↓[␈↓␈↓↓ 398␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Sco 70]␈↓ ∧πScott,␈α∞D.,␈α∞`Outline␈α∂of␈α∞a␈α∞Mathematical␈α∞Theory␈α∂of␈α∞Computation',
␈↓ α←␈↓␈↓ ∧∪Oxford␈α%University␈α&Computing␈α%Labs,␈α&PRG-2,␈α%Oxford
␈↓ α←␈↓␈↓ ∧∪University, 1970. ␈↓↓[␈↓␈↓↓ 175␈↓␈↓↓ 177␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Sco 72]␈↓ ∧πScott,␈α∞D.,␈α∂`Mathematical␈α∞Concepts␈α∞in␈α∂Programming␈α∞Languages',
␈↓ α←␈↓␈↓ ∧∪AFIPS␈α2Conference␈α2Proceedings,␈α3Vol 40,␈α2p. 225-234,
␈↓ α←␈↓␈↓ ∧∪1972. ␈↓↓[␈↓␈↓↓ 161␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Sco 73]␈↓ ∧πScott,␈α→D.,␈α→`Models␈α→for␈α→various␈α→type-free␈α→calculi',␈α~in␈α→␈↓αLogic,
␈↓ α←␈↓α␈↓ ∧∪Methodology,␈α⊗and␈α⊗Philosophy␈α⊗of␈α⊗Science IV␈↓,␈α⊗North␈α∃Holland,
␈↓ α←␈↓␈↓ ∧∪p. 157-187, 1973. ␈↓↓[␈↓␈↓↓ 175␈↓␈↓↓ 177␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Sik 76]␈↓ ∧πSiklossy,␈α∞L.,␈α∂␈↓αLet's␈α∞Talk␈α∞LISP␈↓,␈α∂Prentice␈α∞Hall,␈α∂Englewood␈α∞Cliffs,
␈↓ α←␈↓␈↓ ∧∪New Jersey, 1976. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Sta 74]␈↓ ∧πvon␈α⊃Staa,␈α⊃A.,␈α⊃`Data␈α⊃Transmission␈α⊃and␈α⊃Modularity␈α⊃Aspects␈α⊂of
␈↓ α←␈↓␈↓ ∧∪Programming␈α
Languages',␈αDept.␈α
of␈α
Computer␈αScience,␈α
Research
␈↓ α←␈↓␈↓ ∧∪Report␈α∀CS-74-17,␈α∀University␈α∀of␈α∀Waterloo,␈α∀Ontario,␈α∪October
␈↓ α←␈↓␈↓ ∧∪1974. ␈↓↓[␈↓␈↓↓ 160␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Ste 73]␈↓ ∧πSteele,␈α⊗G.,␈α⊗`BIBOP␈α⊗LISP␈α⊗Memo',␈α⊗unpublished␈α⊗MIT␈α⊗paper,
␈↓ α←␈↓␈↓ ∧∪1973. ␈↓↓[␈↓␈↓↓ 266␈↓␈↓↓ 402␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Ste 76a]␈↓ ∧πSteele,␈α∂G.,␈α∂`Multiprocessing␈α∂Compactifying␈α⊂Garbage␈α∂Collection',
␈↓ α←␈↓␈↓ ∧∪␈↓αComm. ACM 18␈↓, ␈↓↓9␈↓, p. 495-508, Sep. 1967. ␈↓↓[␈↓␈↓↓ 402␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Ste 76b]␈↓ ∧πSteele,␈α G.,␈α!and␈α Sussman,␈α G.,␈α!`LAMBDA: The␈α Ultimate
␈↓ α←␈↓␈↓ ∧∪Imperative',␈α?␈α?␈α?␈α?␈α?␈α?␈αλM.I.T. A.I. Memo 353,
␈↓ α←␈↓␈↓ ∧∪Mar. 1976. ␈↓↓[␈↓␈↓↓ 150␈↓␈↓↓ 172␈↓␈↓↓ 219␈↓␈↓↓ 358␈↓␈↓↓ 411␈↓␈↓↓ ]␈↓
␈↓ α←␈↓␈↓␈↓ λHBIBLIOGRPAHY 439␈↓
␈↓"β␈↓ α←␈↓[Ste 76c]␈↓ ∧πSteele,␈α<G.,␈α<`LAMBDA: The␈α<Ultimate␈α;Declarative',
␈↓ α←␈↓␈↓ ∧∪M.I.T. A.I. Memo 379, Oct. 1976. ␈↓↓[␈↓␈↓↓ 172␈↓␈↓↓ 411␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Ste pc]␈↓ ∧πSteele, G., private communications. ␈↓↓[␈↓␈↓↓ 155␈↓␈↓↓ 368␈↓␈↓↓ 382␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Stei 74]␈↓ ∧πSteiger,␈α⊃R.,␈α∩`Actor␈α⊃Machine␈α⊃Architecture',␈α∩M.S. Thesis,␈α⊃M.I.T.,
␈↓ α←␈↓␈↓ ∧∪1974. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Sto 75]␈↓ ∧πStoyan,␈α↔H.,␈α⊗`Comparison␈α↔of␈α⊗Two␈α↔LISP␈α⊗Compilers: Stanford
␈↓ α←␈↓␈↓ ∧∪Versus␈α⊂DOS/ES-LISP',␈α⊃␈↓αElektronische␈α⊂Informationsverarbeitung
␈↓ α←␈↓α␈↓ ∧∪und Kybernetik 11␈↓, p. 371-375, 1975. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Str 67]␈↓ ∧πStrachey,␈α'C.,␈α'`Fundamental␈α'Concepts␈α'in␈α'Programming
␈↓ α←␈↓␈↓ ∧∪Languages', NATO Conference, Copenhagen, 1967. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Str 73]␈↓ ∧πStrachey,␈α∀C.,␈α∪`Varieties␈α∀of␈α∪Programming␈α∀Languages',␈α∪Oxford
␈↓ α←␈↓␈↓ ∧∪University␈α~Computing␈α~Labs,␈α~PRG-10,␈α~Oxford␈α→University,
␈↓ α←␈↓␈↓ ∧∪1973. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Str 74a]␈↓ ∧πStrachey,␈α'C.,␈α'and␈α'Wadsworth,␈α'C.,␈α(`Continuations:␈α'A
␈↓ α←␈↓␈↓ ∧∪Mathematical␈α∀Semantics␈α∀for␈α∀Handling␈α∀Full␈α∀Jumps',␈α∪Oxford
␈↓ α←␈↓␈↓ ∧∪University␈α≡Computing␈α≥Laboratory,␈α≡Technical␈α≥Monograph
␈↓ α←␈↓␈↓ ∧∪PRG-11, Jan 1974. ␈↓↓[␈↓␈↓↓ 207␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Str 74]␈↓ ∧πFoy,␈α
N.,␈α
`The␈α∞Words␈α
Games␈α
of␈α∞the␈α
Night␈α
Bird:␈α∞Interview␈α
with
␈↓ α←␈↓␈↓ ∧∪C. Strachey', ␈↓αComputing Europe␈↓, p. 10-11, Aug 15, 1974. ␈↓↓[␈↓␈↓↓ 97␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Sug 77]␈↓ ∧πSugarman,␈α⊗R.,␈α⊗`Chess␈α⊗Computers␈α⊗Start␈α⊗to␈α⊗Give␈α⊗Humans␈α∃a
␈↓ α←␈↓␈↓ ∧∪Tough␈α%Game',␈α%␈↓αElectronic Engineerging News␈↓,␈α%p. 4, 44, 92,
␈↓ α←␈↓␈↓ ∧∪Apr 18, 1977. ␈↓↓[␈↓␈↓↓ 66␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Sus 75]␈↓ ∧πSussman,␈α∃G.,␈α∃and␈α⊗Steele,␈α∃G.,␈α∃`SCHEME:␈α∃an␈α⊗Interpreter␈α∃for
␈↓ α←␈↓␈↓ ∧∪Extended␈α>Lambda␈α>Calculus',␈α=M.I.T. A.I. Memo 349,
␈↓ α←␈↓␈↓ ∧∪Dec. 1975. ␈↓↓[␈↓␈↓↓ 137␈↓␈↓↓ 172␈↓␈↓↓ 219␈↓␈↓↓ 358␈↓␈↓↓ 411␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Sus 76]␈↓ ∧πSussman,␈α(G.,␈α'and␈α(Steele,␈α'G.,␈α(`SCHEME␈α(Flash␈α' 1',
␈↓ α←␈↓␈↓ ∧∪M.I.T. A.I. Lab., Jan. 1976. ␈↓↓[␈↓␈↓↓ 230␈↓␈↓↓ 255␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Tei 72]␈↓ ∧πTeitelman,␈αW.,␈α`Automated␈αProgrammering - The␈αProgrammer's
␈↓ α←␈↓␈↓ ∧∪Assistant',␈αProceedings␈αof␈α
the␈αFall␈αJoint␈α
Computer␈αConference,
␈↓ α←␈↓␈↓ ∧∪Dec. 1972. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Ten 76]␈↓ ∧πTennent,␈α↔R.,␈α↔`The␈α⊗Denotational␈α↔Semantics␈α↔of␈α⊗Programming
␈↓ α←␈↓␈↓ ∧∪Languages', ␈↓αComm. ACM 19␈↓, ␈↓↓8␈↓, p. 437-453, Aug. 1976. ␈↓↓[␈↓␈↓↓ 164␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Ter 75]␈↓ ∧πTerashima,␈α⊗M.,␈α⊗`Algorithms␈α⊗Used␈α⊗in␈α⊗an␈α⊗Implementation␈α∃of
␈↓ α←␈↓␈↓ ∧∪HLISP',␈αInformation␈αSciences␈αLab.,␈αTR 75-03,␈αUniv.␈αof␈αTokyo,
␈↓ α←␈↓␈↓ ∧∪Japan, Jan. 1975. ␈↓↓[␈↓␈↓↓ 273␈↓␈↓↓ 403␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Urm 76]␈↓ ∧πUrmi,␈α∩J.,␈α∩`A␈α∩Shallow␈α∩Binding␈α∩Scheme␈α∩for␈α∩Fast␈α⊃Environment
␈↓ α←␈↓␈↓ ∧∪Changing␈α⊗in␈α⊗a␈α⊗`Spaghetti␈α⊗Stack'␈α⊗LISP␈α⊗System',␈α⊗Linkoping
␈↓ α←␈↓␈↓ ∧∪University, Sweden, 1976. ␈↓↓[␈↓␈↓↓ 398␈↓␈↓↓ ]␈↓
␈↓ α←␈↓␈↓440 BIBLIOGRAPHY␈↓
O␈↓
␈↓"β␈↓ α←␈↓[Vui 74]␈↓ ∧πVuillemin,␈α≡J.,␈α∨`Correct␈α≡and␈α≡Optimal␈α∨Implementations␈α≡of
␈↓ α←␈↓␈↓ ∧∪Recursion␈α∩in␈α∩a␈α∩Simple␈α∩Programming␈α∩Language',␈α∪␈↓αJournal␈α∩of
␈↓ α←␈↓α␈↓ ∧∪Computer and System Science␈↓, Vol 9, No 3, Dec 1974. ␈↓↓[␈↓␈↓↓ 221␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Wad 71]␈↓ ∧πWadsworth,␈α,C.,␈α,`Semantics␈α+and␈α,Pragmatics␈α,of␈α+the
␈↓ α←␈↓␈↓ ∧∪Lambda-calculus',␈α1Ph.D.␈α1Thesis,␈α1Oxford␈α1University,
␈↓ α←␈↓␈↓ ∧∪1971. ␈↓↓[␈↓␈↓↓ 173␈↓␈↓↓ 174␈↓␈↓↓ 175␈↓␈↓↓ 221␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Wad 74a]␈↓ ∧πWadsworth,␈αC.,␈αThe␈αRelation␈αBetween␈αLambda-expressions␈αand
␈↓ α←␈↓␈↓ ∧∪Their␈α↔Denotations',␈α↔unpublished␈α↔paper,␈α↔Systems␈α_and␈α↔Info.
␈↓ α←␈↓␈↓ ∧∪Science Dept, Syracuse Univ., 1974. ␈↓↓[␈↓␈↓↓ 173␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Wan 75]␈↓ ∧πWang,␈α∃P.,␈α∃`MACSYMA - A␈α∃Symbolic␈α⊗Manipulation␈α∃System',
␈↓ α←␈↓␈↓ ∧∪Proceedings␈α↔of␈α⊗International␈α↔Computer␈α↔Symposium,␈α⊗Vol. 1,
␈↓ α←␈↓␈↓ ∧∪p. 103-109, 1975. ␈↓↓[␈↓␈↓↓ 390␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[War 74]␈↓ ∧πWard,␈α→S.,␈α→`Functional␈α_Domains␈α→of␈α→Applicative␈α_Languages',
␈↓ α←␈↓␈↓ ∧∪Ph.D. Thesis,␈α,M.I.T.,␈α+MAC TR-136,␈α,Cambridge,␈α+Sep.
␈↓ α←␈↓␈↓ ∧∪1974. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Weg 68]␈↓ ∧πWegner,␈α∀P.,␈α∀␈↓αProgramming␈α∀Languages,␈α∀Information␈α∪Structures
␈↓ α←␈↓α␈↓ ∧∪and␈α'Machine␈α&Organization␈↓,␈α'McGraw-Hill,␈α'New␈α&York,
␈↓ α←␈↓␈↓ ∧∪1968. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Weg 70]␈↓ ∧πWegner,␈αP.,␈α`Three␈αComputer␈αCultures␈α-␈α
Computer␈αTechnology,
␈↓ α←␈↓␈↓ ∧∪Computer␈α⊂Mathematics␈α⊂&␈α⊂Computer␈α⊂Science',␈α⊂in␈α⊂␈↓αAdvances␈α∂in
␈↓ α←␈↓α␈↓ ∧∪Computers␈↓, ␈↓↓10␈↓, Academic Press, New York, 1970. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Weg 71]␈↓ ∧πWegner,␈α#P.,␈α"`Data␈α#Structure␈α"Models␈α#for␈α"Programming
␈↓ α←␈↓␈↓ ∧∪Languages', p. 1-54 in [DSIPL]. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Weg 72]␈↓ ∧πWegner,␈α↔P.,␈α⊗`The␈α↔Vienna␈α⊗Definition␈α↔Language',␈α⊗␈↓αComputing
␈↓ α←␈↓α␈↓ ∧∪Surveys␈↓, Vol 4, No 1, p. 5-63, Mar 1972. ␈↓↓[␈↓␈↓↓ 42␈↓␈↓↓ 163␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Wegb 70]␈↓ ∧πWegbreit,␈α⊂B.,␈α⊂`Studies␈α⊂in␈α⊂Extensible␈α⊂Programming␈α∂Languages',
␈↓ α←␈↓␈↓ ∧∪Ph.D.␈α?␈ααThesis,␈α?␈ααHarvard␈α?␈ααUniversity,␈α?␈ααCambridge,
␈↓ α←␈↓␈↓ ∧∪Mass.,1970. ␈↓↓[␈↓␈↓↓ 402␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Wegb 71]␈↓ ∧πWegbreit,␈α∃B.,␈α⊗`The␈α∃ECL␈α∃Programming␈α⊗System',␈α∃Proceedings
␈↓ α←␈↓␈↓ ∧∪AFIPS␈α↔1971␈α↔FJCC,␈α↔Vol 39,␈α↔AFIPS␈α↔Press,␈α↔Mondale,␈α⊗New
␈↓ α←␈↓␈↓ ∧∪Jersey, p. 253-262, 1971. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Wegb 72]␈↓ ∧πWegbreit,␈αB.,␈α`A␈αGeneralized␈αCompactifying␈αGarbage␈αCollector',
␈↓ α←␈↓␈↓ ∧∪␈↓αComputer Journal 15␈↓, ␈↓↓3␈↓, p. 204-208, Aug 1972. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Wegb 74]␈↓ ∧πWegbreit,␈α⊂B.,␈α⊃`The␈α⊂Treatment␈α⊂of␈α⊃Data␈α⊂Types␈α⊂in␈α⊃EL1',␈α⊂␈↓αComm.
␈↓ α←␈↓α␈↓ ∧∪ACM 17␈↓, ␈↓↓5␈↓, p. 251-264, May 1974. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Wegb 75]␈↓ ∧πWegbreit,␈α~B.,␈α→`Retrieval␈α~From␈α→Context␈α~Trees',␈α→␈↓αInformation
␈↓ α←␈↓α␈↓ ∧∪Processing Letters, 3␈↓, ␈↓↓4␈↓, p. 119-120, March 1975. ␈↓↓[␈↓␈↓↓ 150␈↓␈↓↓ 301␈↓␈↓↓ ]␈↓
␈↓ α←␈↓␈↓␈↓ λHBIBLIOGRPAHY 441␈↓
␈↓"β␈↓ α←␈↓[Wei 62]␈↓ ∧πWeizenbaum,␈α∩J.,␈α⊃`Knotted␈α∩List␈α⊃Structures',␈α∩␈↓αComm.␈α∩ACM 5␈↓,␈α⊃␈↓↓13␈↓,
␈↓ α←␈↓␈↓ ∧∪p. 161-165, Mar 1962. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Wei 63]␈↓ ∧πWeizenbaum,␈α⊂J.,␈α⊂`Symmetric␈α⊃List␈α⊂Processor',␈α⊂␈↓αComm.␈α⊃ACM 6␈↓,␈α⊂␈↓↓9␈↓,
␈↓ α←␈↓␈↓ ∧∪p. 524-544, Sep 1963. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Wei 68]␈↓ ∧πWeizenbaum,␈α&J.,␈α&`The␈α&FUNARG␈α&Problem␈α%Explained',
␈↓ α←␈↓␈↓ ∧∪unpublished␈α*memorandum,␈α*M.I.T.,␈α*Cambridge,␈α*Mass.,
␈↓ α←␈↓␈↓ ∧∪1968. ␈↓↓[␈↓␈↓↓ 131␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Weis 67]␈↓ ∧πWeismann,␈α∪C.,␈α∪`LISP␈α∪1.5␈α∪Primer',␈α∪Dickenson␈α∪Press,␈α∩Belmont,
␈↓ α←␈↓␈↓ ∧∪1967. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Win 75]␈↓ ∧πWinograd,␈α∞T.,␈α
`Breaking␈α∞the␈α
Complexity␈α∞Barrier (again)',␈α
␈↓αACM
␈↓ α←␈↓α␈↓ ∧∪SIGPLAN Notes 10␈↓, ␈↓↓1␈↓, p. 13-30, Jan. 1975. ␈↓↓[␈↓␈↓↓ 364␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Wis 75]␈↓ ∧πWise,␈α∀D.,␈α∀et.␈α∀al.,␈α∪`Boolean-valued␈α∀Loops',␈α∀␈↓αBIT␈↓␈α∀␈↓↓15␈↓␈α∪p. 431-451,
␈↓ α←␈↓␈↓ ∧∪1975. ␈↓↓[␈↓␈↓↓ 195␈↓␈↓↓ ]␈↓
␈↓"∀␈↓ α←␈↓[Zil 70]␈↓ ∧πZilles,␈αS.,␈α`An␈αExpansion␈αof␈αthe␈αData␈αStructuring␈αCapabilities␈αin
␈↓ α←␈↓␈↓ ∧∪PAL', M.S. Thesis, MIT, Jun 1970. ␈↓↓[␈↓
␈↓ α←␈↓␈↓␈↓ -INDEX 443␈↓
␈↓"β ␈↓ εo␈↓␈↓αequal␈↓ 23
␈↓ εo␈↓␈↓αerr␈↓ 198
␈↓ εo␈↓␈↓αerrset␈↓ 198
␈↓ εo␈↓␈↓αeval␈↓ 115, 241, 323, 412
␈↓ εo␈↓␈↓αevcond␈↓ 115
␈↓ εo␈↓␈↓αexamine␈↓ 331
␈↓ εo␈↓␈↓αfirst␈↓ 375
␈↓ α←␈↓␈↓ Index␈↓ ␈↓ εo␈↓␈↓αFUNARG␈↓ 146
␈↓ εo␈↓␈↓αfunction␈↓ 141
␈↓ εo␈↓␈↓αgensym␈↓ 327
␈↓ εo␈↓␈↓αgo␈↓ 189
␈↓ εo␈↓␈↓αisseq␈↓ 28
␈↓ εo␈↓␈↓αlabel␈↓ 136
␈↓ εo␈↓␈↓αlist 35
␈↓ εo␈↓αLPAR 271
␈↓ εo␈↓αmaplist 148
␈↓ εo␈↓αmkent 109
␈↓ εo␈↓αNIL 33, 270
␈↓ εo␈↓αnull 28, 375
␈↓ εo␈↓αOBLIST 276
␈↓ α←␈↓␈↓↓SM␈↓ 312 ␈↓ εo␈↓αor 154
␈↓ α←␈↓␈↓αλ␈↓-notation 110 ␈↓ εo␈↓αpatom 271
␈↓ α←␈↓␈↓α=␈↓ 376 ␈↓ εo␈↓αPERIOD 271
␈↓ α←␈↓␈↓αand␈↓ 154 ␈↓ εo␈↓αPNAME 267
␈↓ α←␈↓␈↓αappend␈↓ 48 ␈↓ εo␈↓αprin0 273
␈↓ α←␈↓␈↓αapply␈↓ 116 ␈↓ εo␈↓αprint 273
␈↓ α←␈↓␈↓αarray␈↓ 373 ␈↓ εo␈↓αprog 186
␈↓ α←␈↓␈↓αatom␈↓ 18 ␈↓ εo␈↓αputprop 387
␈↓ α←␈↓␈↓αBLANK␈↓ 271 ␈↓ εo␈↓αratom 271, 276, 278, 387
␈↓ α←␈↓␈↓αcar␈↓ 13 ␈↓ εo␈↓αread_head 272
␈↓ α←␈↓␈↓αcar-cdr-␈↓chains 14 ␈↓ εo␈↓αread_tail 272
␈↓ α←␈↓␈↓αcatch␈↓ 198 ␈↓ εo␈↓αread 272
␈↓ α←␈↓␈↓αcdr␈↓ 13 ␈↓ εo␈↓αread␈↓ macro 280
␈↓ α←␈↓␈↓αcdr␈↓-coding 399 ␈↓ εo␈↓␈↓αremprop␈↓ 262, 387
␈↓ α←␈↓␈↓αchar␈↓ 375 ␈↓ εo␈↓␈↓αrest␈↓ 29, 375
␈↓ α←␈↓␈↓αcompile␈↓ 307 ␈↓ εo␈↓␈↓αRPAR␈↓ 271
␈↓ α←␈↓␈↓αconcat␈↓ 29 ␈↓ εo␈↓␈↓αrplaca␈↓ 384
␈↓ α←␈↓␈↓αCOND␈↓ 106, 115 ␈↓ εo␈↓␈↓αseq␈↓ 29
␈↓ α←␈↓␈↓αcons␈↓ 12, 281 ␈↓ εo␈↓␈↓αstore␈↓ 373
␈↓ α←␈↓␈↓αdeposit␈↓ 331 ␈↓ εo␈↓␈↓αtgmoaf␈↓ 91, 323
␈↓ α←␈↓␈↓αeq␈↓ 19 ␈↓ εo␈↓␈↓αtgmoaf␈↓ 421
␈↓ α←␈↓␈↓444 INDEX␈↓
O␈↓
␈↓"β␈↓ α←␈↓␈↓αtgmoafr␈↓ 91, 323 ␈↓ εo␈↓concrete syntax 165
␈↓ α←␈↓␈↓αthrow␈↓ 198 ␈↓ εo␈↓conditional expression 18, 19, 115
␈↓ α←␈↓␈↓εa␈↓-rule 171 ␈↓ εo␈↓CONNIVER 412
␈↓ α←␈↓␈↓εb␈↓-rule 171 ␈↓ εo␈↓constructor 12
␈↓ α←␈↓a-lists 108 ␈↓ εo␈↓continuation 207
␈↓ α←␈↓access chain 132 ␈↓ εo␈↓control environment 142
␈↓ α←␈↓access link 132 ␈↓ εo␈↓control structures 39
␈↓ α←␈↓accumulators 47 ␈↓ εo␈↓deep binding 152
␈↓ α←␈↓activation environment 146 ␈↓ εo␈↓definition by recursion 43
␈↓ α←␈↓actual parameters 15 ␈↓ εo␈↓denotational 167
␈↓ α←␈↓AMBIT/G 253 ␈↓ εo␈↓differentiation 57
␈↓ α←␈↓analytic syntax 164 ␈↓ εo␈↓discriminator 18
␈↓ α←␈↓applicative language 15 ␈↓ εo␈↓dope vector 372
␈↓ α←␈↓Assemblers 320 ␈↓ εo␈↓dotted-pairs 6
␈↓ α←␈↓association lists 108 ␈↓ εo␈↓double-linking 393
␈↓ α←␈↓atom header 266 ␈↓ εo␈↓doubly-linked list structure 247
␈↓ α←␈↓atom space 246 ␈↓ εo␈↓dynamic binding 131
␈↓ α←␈↓auxiliary function 47 ␈↓ εo␈↓EL1 406
␈↓ α←␈↓base language 406 ␈↓ εo␈↓evaluation 13, 97
␈↓ α←␈↓Binary Program Space 320 ␈↓ εo␈↓examples of ␈↓αeval␈↓ 118
␈↓ α←␈↓binding 15 ␈↓ εo␈↓expr 224
␈↓ α←␈↓binding environment 146 ␈↓ εo␈↓extensible language 406
␈↓ α←␈↓binding strategy 149 ␈↓ εo␈↓fexpr 224
␈↓ α←␈↓bootstrapping 307 ␈↓ εo␈↓Fibonacci sequence 46
␈↓ α←␈↓bound occurrence 170 ␈↓ εo␈↓fix-up 330
␈↓ α←␈↓bound variable 130 ␈↓ εo␈↓fixed points 232
␈↓ α←␈↓box-notation 9 ␈↓ εo␈↓fixed-point operator 234
␈↓ α←␈↓BPS 320 ␈↓ εo␈↓fluid variable 131
␈↓ α←␈↓bucket hashing 275 ␈↓ εo␈↓form 12, 17, 111
␈↓ α←␈↓cache value cells 301 ␈↓ εo␈↓formal parameter 14
␈↓ α←␈↓call-by-name 100, 221 ␈↓ εo␈↓forward reference 329
␈↓ α←␈↓call-by-need 221 ␈↓ εo␈↓free space list 281
␈↓ α←␈↓call-by-value 100 ␈↓ εo␈↓free variable 130, 170
␈↓ α←␈↓case statement 193 ␈↓ εo␈↓Full Word Space 269
␈↓ α←␈↓case statement 157 ␈↓ εo␈↓funarg 141
␈↓ α←␈↓closure 141, 192 ␈↓ εo␈↓function 111
␈↓ α←␈↓code generators 308 ␈↓ εo␈↓function application 15
␈↓ α←␈↓coercion 242 ␈↓ εo␈↓functional argument 137
␈↓ α←␈↓collision 275 ␈↓ εo␈↓functional composition 14
␈↓ α←␈↓compiler 332, 335 ␈↓ εo␈↓functional value 143
␈↓ α←␈↓computed function 158 ␈↓ εo␈↓FWS 269
␈↓ α←␈↓␈↓␈↓ -INDEX 445␈↓
␈↓"β␈↓ α←␈↓garbage collection 282 ␈↓ εo␈↓numbers 389
␈↓ α←␈↓garbage collector 282, 373, 387 ␈↓ εo␈↓object list 276
␈↓ α←␈↓generalized control structures 197 ␈↓ εo␈↓offset 336
␈↓ α←␈↓generative definition 3 ␈↓ εo␈↓open addressing 275
␈↓ α←␈↓global variable 129, 131 ␈↓ εo␈↓open function 232
␈↓ α←␈↓halting problem 181 ␈↓ εo␈↓operation code 321
␈↓ α←␈↓hash consing 287, 385, 402 ␈↓ εo␈↓operational 167
␈↓ α←␈↓hashing 275 ␈↓ εo␈↓p-list 267
␈↓ α←␈↓hashing algorithm 275 ␈↓ εo␈↓parser 272
␈↓ α←␈↓inductive definition 3 ␈↓ εo␈↓partial application 160
␈↓ α←␈↓internal lambdas 113 ␈↓ εo␈↓partial function 11, 13
␈↓ α←␈↓invisible pointers 400 ␈↓ εo␈↓pattern directed invocation 73
␈↓ α←␈↓lambda list 111 ␈↓ εo␈↓PL/1 406
␈↓ α←␈↓length 186 ␈↓ εo␈↓pointer 245
␈↓ α←␈↓lexical binding 150 ␈↓ εo␈↓pointer space 246
␈↓ α←␈↓linear search 108 ␈↓ εo␈↓polymorphic functions 29
␈↓ α←␈↓linked allocation 291 ␈↓ εo␈↓precedence 417
␈↓ α←␈↓linked list structure 246 ␈↓ εo␈↓predicates 18
␈↓ α←␈↓LISP machine 289 ␈↓ εo␈↓prefix notation 58
␈↓ α←␈↓list terminator 33 ␈↓ εo␈↓pretty printing 20
␈↓ α←␈↓list-notation 33 ␈↓ εo␈↓print-name 267
␈↓ α←␈↓lists 31 ␈↓ εo␈↓property-list 256
␈↓ α←␈↓literal atoms 5 ␈↓ εo␈↓recognizer 18, 28
␈↓ α←␈↓local binding 129 ␈↓ εo␈↓record 382
␈↓ α←␈↓local symbol table 129 ␈↓ εo␈↓reduction rule 173
␈↓ α←␈↓local variable 187 ␈↓ εo␈↓reference counter 286, 301
␈↓ α←␈↓M-expr LISP 107 ␈↓ εo␈↓S-expr LISP 107
␈↓ α←␈↓M-expressions 236 ␈↓ εo␈↓S-expressions 5
␈↓ α←␈↓macros 355 ␈↓ εo␈↓S-exprs 5
␈↓ α←␈↓mapping functions 148 ␈↓ εo␈↓scanner 271
␈↓ α←␈↓marking phase 283 ␈↓ εo␈↓SDIO 421
␈↓ α←␈↓match-variable 6 ␈↓ εo␈↓selector 13
␈↓ α←␈↓meta-language 107, 236 ␈↓ εo␈↓self-applicative 175
␈↓ α←␈↓meta-variables 6 ␈↓ εo␈↓self-applicative functions 149
␈↓ α←␈↓MICRO-PLANNER 412 ␈↓ εo␈↓shallow binding 152
␈↓ α←␈↓mother-vector 373 ␈↓ εo␈↓singly linked 247
␈↓ α←␈↓MUDDLE 412 ␈↓ εo␈↓Special Form 154
␈↓ α←␈↓name stack 292 ␈↓ εo␈↓special forms 115
␈↓ α←␈↓non-local 129 ␈↓ εo␈↓special variable 356
␈↓ α←␈↓non-local variables 356 ␈↓ εo␈↓stack 291
␈↓ α←␈↓non-strict 21 ␈↓ εo␈↓stack synchronization 316
␈↓ α←␈↓␈↓446 INDEX␈↓
O␈↓
␈↓"β␈↓ α←␈↓static binding 150
␈↓ α←␈↓storage reclaimer 282
␈↓ α←␈↓stratification 424
␈↓ α←␈↓strict functions 12
␈↓ α←␈↓string processor 374
␈↓ α←␈↓sweep phase 284
␈↓ α←␈↓symbol tables 107
␈↓ α←␈↓Symbolic expressions 5
␈↓ α←␈↓syntax-directed 417
␈↓ α←␈↓table-driven 191
␈↓ α←␈↓termination conditions 45
␈↓ α←␈↓threading 393
␈↓ α←␈↓total function 11
␈↓ α←␈↓type fault 23
␈↓ α←␈↓unbound variable 130
␈↓ α←␈↓universal function 181
␈↓ α←␈↓value stack 292, 333
␈↓ α←␈↓value cell 152
␈↓ α←␈↓variables 128